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 6353ad76 2019-02-08 stsp
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
747 234035bc 2019-06-01 stsp
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
751 af54ae4a 2019-02-19 stsp
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
754 af54ae4a 2019-02-19 stsp
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 6353ad76 2019-02-08 stsp if (err)
757 6353ad76 2019-02-08 stsp goto done;
758 6353ad76 2019-02-08 stsp
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
763 af54ae4a 2019-02-19 stsp goto done;
764 af54ae4a 2019-02-19 stsp }
765 af54ae4a 2019-02-19 stsp
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 6353ad76 2019-02-08 stsp if (err)
768 6353ad76 2019-02-08 stsp goto done;
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
772 1430b4e0 2019-03-27 stsp if (err)
773 1430b4e0 2019-03-27 stsp goto done;
774 1430b4e0 2019-03-27 stsp } else {
775 1430b4e0 2019-03-27 stsp /*
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
779 1430b4e0 2019-03-27 stsp */
780 1430b4e0 2019-03-27 stsp }
781 6353ad76 2019-02-08 stsp
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 6353ad76 2019-02-08 stsp if (err)
785 6353ad76 2019-02-08 stsp goto done;
786 6353ad76 2019-02-08 stsp
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 1ee397ad 2019-07-12 stsp if (err)
790 1ee397ad 2019-07-12 stsp goto done;
791 6353ad76 2019-02-08 stsp
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
794 816dc654 2019-02-16 stsp goto done;
795 68c76935 2019-02-19 stsp }
796 68c76935 2019-02-19 stsp
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
801 68c76935 2019-02-19 stsp if (err)
802 68c76935 2019-02-19 stsp goto done;
803 816dc654 2019-02-16 stsp }
804 6353ad76 2019-02-08 stsp
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
807 70a0c8ec 2019-02-20 stsp goto done;
808 70a0c8ec 2019-02-20 stsp }
809 70a0c8ec 2019-02-20 stsp
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp }
815 6353ad76 2019-02-08 stsp done:
816 fdcb7daf 2019-12-15 stsp if (err) {
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
819 fdcb7daf 2019-12-15 stsp }
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
829 af57b12a 2020-07-23 stsp }
830 af57b12a 2020-07-23 stsp return err;
831 af57b12a 2020-07-23 stsp }
832 af57b12a 2020-07-23 stsp
833 af57b12a 2020-07-23 stsp static const struct got_error *
834 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
835 af57b12a 2020-07-23 stsp size_t target_len)
836 af57b12a 2020-07-23 stsp {
837 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
838 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
839 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
840 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
842 af57b12a 2020-07-23 stsp ondisk_path);
843 af57b12a 2020-07-23 stsp return NULL;
844 af57b12a 2020-07-23 stsp }
845 af57b12a 2020-07-23 stsp
846 af57b12a 2020-07-23 stsp /*
847 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
849 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
850 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
851 993e2a1b 2020-07-23 stsp *
852 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
853 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
854 993e2a1b 2020-07-23 stsp *
855 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
856 993e2a1b 2020-07-23 stsp * has deleted this symlink.
857 11cc08c1 2020-07-23 stsp */
858 11cc08c1 2020-07-23 stsp static const struct got_error *
859 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
860 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
862 11cc08c1 2020-07-23 stsp {
863 11cc08c1 2020-07-23 stsp const struct got_error *err;
864 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 11cc08c1 2020-07-23 stsp FILE *f = NULL;
866 11cc08c1 2020-07-23 stsp
867 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
868 11cc08c1 2020-07-23 stsp if (err)
869 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
872 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
874 11cc08c1 2020-07-23 stsp goto done;
875 11cc08c1 2020-07-23 stsp }
876 11cc08c1 2020-07-23 stsp
877 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
878 11cc08c1 2020-07-23 stsp if (err)
879 11cc08c1 2020-07-23 stsp goto done;
880 11cc08c1 2020-07-23 stsp
881 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
883 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
884 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
885 11cc08c1 2020-07-23 stsp "target paths were found:\n"
886 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
889 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
890 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
891 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
892 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
893 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
894 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
896 11cc08c1 2020-07-23 stsp goto done;
897 11cc08c1 2020-07-23 stsp }
898 11cc08c1 2020-07-23 stsp
899 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
900 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
901 11cc08c1 2020-07-23 stsp goto done;
902 11cc08c1 2020-07-23 stsp }
903 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
904 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
905 11cc08c1 2020-07-23 stsp goto done;
906 11cc08c1 2020-07-23 stsp }
907 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
909 11cc08c1 2020-07-23 stsp goto done;
910 11cc08c1 2020-07-23 stsp }
911 11cc08c1 2020-07-23 stsp done:
912 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
913 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
914 11cc08c1 2020-07-23 stsp free(path);
915 11cc08c1 2020-07-23 stsp free(id_str);
916 11cc08c1 2020-07-23 stsp free(label_deriv);
917 11cc08c1 2020-07-23 stsp return err;
918 11cc08c1 2020-07-23 stsp }
919 d219f183 2020-07-23 stsp
920 d219f183 2020-07-23 stsp /* forward declaration */
921 d219f183 2020-07-23 stsp static const struct got_error *
922 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
924 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
925 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
926 11cc08c1 2020-07-23 stsp
927 11cc08c1 2020-07-23 stsp /*
928 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
929 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
930 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
931 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
932 af57b12a 2020-07-23 stsp */
933 af57b12a 2020-07-23 stsp static const struct got_error *
934 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
935 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
936 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
937 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
938 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
940 af57b12a 2020-07-23 stsp {
941 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
942 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
943 af57b12a 2020-07-23 stsp struct stat sb;
944 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
945 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
946 11cc08c1 2020-07-23 stsp int have_local_change = 0;
947 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
948 af57b12a 2020-07-23 stsp
949 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
950 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
951 af57b12a 2020-07-23 stsp
952 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
953 d219f183 2020-07-23 stsp /*
954 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
955 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
956 d219f183 2020-07-23 stsp * a merge conflict.
957 d219f183 2020-07-23 stsp */
958 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
959 d219f183 2020-07-23 stsp int local_changes_subsumed;
960 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
961 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
962 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
963 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
964 d219f183 2020-07-23 stsp }
965 d219f183 2020-07-23 stsp
966 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
967 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
968 af57b12a 2020-07-23 stsp }
969 af57b12a 2020-07-23 stsp
970 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
971 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
972 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
973 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
974 af57b12a 2020-07-23 stsp ondisk_path);
975 af57b12a 2020-07-23 stsp goto done;
976 af57b12a 2020-07-23 stsp }
977 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
978 af57b12a 2020-07-23 stsp
979 526a746f 2020-07-23 stsp if (blob_orig) {
980 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
981 526a746f 2020-07-23 stsp if (err)
982 526a746f 2020-07-23 stsp goto done;
983 526a746f 2020-07-23 stsp }
984 af57b12a 2020-07-23 stsp
985 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
986 af57b12a 2020-07-23 stsp if (err)
987 af57b12a 2020-07-23 stsp goto done;
988 af57b12a 2020-07-23 stsp
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 11cc08c1 2020-07-23 stsp have_local_change = 1;
993 11cc08c1 2020-07-23 stsp
994 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
995 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
996 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
997 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
999 11cc08c1 2020-07-23 stsp
1000 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1001 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1002 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1003 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1004 11cc08c1 2020-07-23 stsp path);
1005 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1006 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1008 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1009 11cc08c1 2020-07-23 stsp path);
1010 11cc08c1 2020-07-23 stsp } else {
1011 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1012 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1013 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1014 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1015 11cc08c1 2020-07-23 stsp if (err)
1016 11cc08c1 2020-07-23 stsp goto done;
1017 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1018 11cc08c1 2020-07-23 stsp path);
1019 11cc08c1 2020-07-23 stsp }
1020 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1021 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1022 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1023 af57b12a 2020-07-23 stsp strlen(deriv_target));
1024 af57b12a 2020-07-23 stsp if (err)
1025 af57b12a 2020-07-23 stsp goto done;
1026 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1028 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1031 11cc08c1 2020-07-23 stsp if (err)
1032 11cc08c1 2020-07-23 stsp goto done;
1033 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 11cc08c1 2020-07-23 stsp path);
1035 6353ad76 2019-02-08 stsp }
1036 11cc08c1 2020-07-23 stsp
1037 af57b12a 2020-07-23 stsp done:
1038 af57b12a 2020-07-23 stsp free(ancestor_target);
1039 af57b12a 2020-07-23 stsp free(deriv_target);
1040 14c901f1 2019-08-08 stsp return err;
1041 14c901f1 2019-08-08 stsp }
1042 14c901f1 2019-08-08 stsp
1043 14c901f1 2019-08-08 stsp /*
1044 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1046 14c901f1 2019-08-08 stsp * acts as the second derived version.
1047 14c901f1 2019-08-08 stsp */
1048 14c901f1 2019-08-08 stsp static const struct got_error *
1049 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1051 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1052 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1053 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1055 14c901f1 2019-08-08 stsp {
1056 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1057 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1058 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1060 14c901f1 2019-08-08 stsp
1061 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1062 14c901f1 2019-08-08 stsp
1063 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1064 14c901f1 2019-08-08 stsp if (parent == NULL)
1065 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1066 14c901f1 2019-08-08 stsp
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1070 14c901f1 2019-08-08 stsp base_path = NULL;
1071 14c901f1 2019-08-08 stsp goto done;
1072 14c901f1 2019-08-08 stsp }
1073 14c901f1 2019-08-08 stsp
1074 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1075 14c901f1 2019-08-08 stsp if (err)
1076 14c901f1 2019-08-08 stsp goto done;
1077 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 14c901f1 2019-08-08 stsp blob_deriv);
1079 14c901f1 2019-08-08 stsp if (err)
1080 14c901f1 2019-08-08 stsp goto done;
1081 14c901f1 2019-08-08 stsp
1082 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1083 14c901f1 2019-08-08 stsp if (err)
1084 14c901f1 2019-08-08 stsp goto done;
1085 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1086 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1088 14c901f1 2019-08-08 stsp goto done;
1089 14c901f1 2019-08-08 stsp }
1090 14c901f1 2019-08-08 stsp
1091 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1094 14c901f1 2019-08-08 stsp done:
1095 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1097 14c901f1 2019-08-08 stsp free(base_path);
1098 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1099 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1100 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1101 14c901f1 2019-08-08 stsp }
1102 6353ad76 2019-02-08 stsp free(id_str);
1103 818c7501 2019-07-11 stsp free(label_deriv);
1104 4a1ddfc2 2019-01-12 stsp return err;
1105 4a1ddfc2 2019-01-12 stsp }
1106 4a1ddfc2 2019-01-12 stsp
1107 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1108 054041d0 2020-06-24 stsp create_fileindex_entry(struct got_fileindex *fileindex,
1109 054041d0 2020-06-24 stsp struct got_object_id *base_commit_id, const char *ondisk_path,
1110 054041d0 2020-06-24 stsp const char *path, struct got_object_id *blob_id)
1111 13d9040b 2019-03-27 stsp {
1112 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1113 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1114 13d9040b 2019-03-27 stsp
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1116 054041d0 2020-06-24 stsp if (err)
1117 054041d0 2020-06-24 stsp return err;
1118 054041d0 2020-06-24 stsp
1119 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1121 054041d0 2020-06-24 stsp if (err)
1122 054041d0 2020-06-24 stsp goto done;
1123 054041d0 2020-06-24 stsp
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1125 054041d0 2020-06-24 stsp done:
1126 054041d0 2020-06-24 stsp if (err)
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 13d9040b 2019-03-27 stsp return err;
1129 1ebedb77 2019-10-19 stsp }
1130 1ebedb77 2019-10-19 stsp
1131 1ebedb77 2019-10-19 stsp static mode_t
1132 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1133 1ebedb77 2019-10-19 stsp {
1134 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1135 1ebedb77 2019-10-19 stsp
1136 1ebedb77 2019-10-19 stsp if (executable) {
1137 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1142 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1143 1ebedb77 2019-10-19 stsp }
1144 1ebedb77 2019-10-19 stsp
1145 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1146 13d9040b 2019-03-27 stsp }
1147 13d9040b 2019-03-27 stsp
1148 8ba819a3 2020-07-23 stsp /* forward declaration */
1149 13d9040b 2019-03-27 stsp static const struct got_error *
1150 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1152 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1153 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1154 bd6aa359 2020-07-23 stsp struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 8ba819a3 2020-07-23 stsp
1157 5a1fbc73 2020-07-23 stsp /*
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1160 5a1fbc73 2020-07-23 stsp */
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1163 5a1fbc73 2020-07-23 stsp size_t target_len)
1164 5a1fbc73 2020-07-23 stsp {
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1168 5a1fbc73 2020-07-23 stsp int fd;
1169 5a1fbc73 2020-07-23 stsp
1170 5a1fbc73 2020-07-23 stsp /*
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1177 5a1fbc73 2020-07-23 stsp */
1178 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 5a1fbc73 2020-07-23 stsp }
1192 5a1fbc73 2020-07-23 stsp
1193 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1194 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1195 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1196 5a1fbc73 2020-07-23 stsp return err;
1197 5a1fbc73 2020-07-23 stsp }
1198 5a1fbc73 2020-07-23 stsp
1199 5a1fbc73 2020-07-23 stsp static const struct got_error *
1200 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1201 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1202 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1203 2e1fa222 2020-07-23 stsp struct got_repository *repo,
1204 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1205 9d31a1d8 2018-03-11 stsp {
1206 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1207 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1208 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1209 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1210 906c123b 2020-07-23 stsp char *path_got = NULL;
1211 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1213 8ba819a3 2020-07-23 stsp
1214 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1215 2e1fa222 2020-07-23 stsp
1216 8ba819a3 2020-07-23 stsp /*
1217 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1218 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1219 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1220 8ba819a3 2020-07-23 stsp * in the blob object.
1221 8ba819a3 2020-07-23 stsp */
1222 8ba819a3 2020-07-23 stsp do {
1223 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1224 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1225 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1226 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1227 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1228 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1229 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1230 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1231 bd6aa359 2020-07-23 stsp 1, repo, progress_cb, progress_arg);
1232 8ba819a3 2020-07-23 stsp }
1233 8ba819a3 2020-07-23 stsp if (len > 0) {
1234 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1235 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1236 8ba819a3 2020-07-23 stsp len - hdrlen);
1237 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1238 8ba819a3 2020-07-23 stsp hdrlen = 0;
1239 8ba819a3 2020-07-23 stsp }
1240 8ba819a3 2020-07-23 stsp } while (len != 0);
1241 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1242 8ba819a3 2020-07-23 stsp
1243 8ba819a3 2020-07-23 stsp /*
1244 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1245 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1246 8ba819a3 2020-07-23 stsp */
1247 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1248 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1249 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1250 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1251 ef68ca6f 2020-07-23 stsp goto done;
1252 ef68ca6f 2020-07-23 stsp }
1253 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1254 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1255 8ba819a3 2020-07-23 stsp goto done;
1256 8ba819a3 2020-07-23 stsp }
1257 8ba819a3 2020-07-23 stsp }
1258 8ba819a3 2020-07-23 stsp
1259 8ba819a3 2020-07-23 stsp /*
1260 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1261 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1262 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1263 8ba819a3 2020-07-23 stsp */
1264 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1265 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1266 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1267 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1268 b15bc87b 2020-07-23 stsp goto done;
1269 b15bc87b 2020-07-23 stsp }
1270 8ba819a3 2020-07-23 stsp }
1271 8ba819a3 2020-07-23 stsp
1272 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1273 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1274 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1275 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1276 8ba819a3 2020-07-23 stsp /* install as a regular file */
1277 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1278 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1279 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1280 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1281 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1282 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1283 8ba819a3 2020-07-23 stsp goto done;
1284 8ba819a3 2020-07-23 stsp }
1285 8ba819a3 2020-07-23 stsp
1286 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1287 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1288 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1289 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1290 906c123b 2020-07-23 stsp goto done;
1291 906c123b 2020-07-23 stsp }
1292 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1293 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1294 906c123b 2020-07-23 stsp /* install as a regular file */
1295 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1296 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1297 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1298 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1300 906c123b 2020-07-23 stsp repo, progress_cb, progress_arg);
1301 906c123b 2020-07-23 stsp goto done;
1302 906c123b 2020-07-23 stsp }
1303 906c123b 2020-07-23 stsp
1304 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1305 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1306 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1307 5a1fbc73 2020-07-23 stsp target_path, target_len);
1308 5a1fbc73 2020-07-23 stsp if (err)
1309 f35fa46a 2020-07-23 stsp goto done;
1310 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1311 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1312 5a1fbc73 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1313 f35fa46a 2020-07-23 stsp }
1314 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1315 f35fa46a 2020-07-23 stsp }
1316 f35fa46a 2020-07-23 stsp
1317 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1318 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1319 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1320 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1321 f35fa46a 2020-07-23 stsp ondisk_path);
1322 f35fa46a 2020-07-23 stsp goto done;
1323 f35fa46a 2020-07-23 stsp }
1324 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1325 f35fa46a 2020-07-23 stsp if (err)
1326 f35fa46a 2020-07-23 stsp goto done;
1327 f35fa46a 2020-07-23 stsp /*
1328 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1329 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1330 f35fa46a 2020-07-23 stsp */
1331 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1332 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1333 f35fa46a 2020-07-23 stsp goto done;
1334 f35fa46a 2020-07-23 stsp }
1335 f35fa46a 2020-07-23 stsp }
1336 f35fa46a 2020-07-23 stsp
1337 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1338 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1339 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1340 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1341 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1342 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1343 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1344 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1345 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1346 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1347 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1348 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1349 8ba819a3 2020-07-23 stsp } else {
1350 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1351 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1352 8ba819a3 2020-07-23 stsp }
1353 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1354 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1355 8ba819a3 2020-07-23 stsp done:
1356 8ba819a3 2020-07-23 stsp free(resolved_path);
1357 8ba819a3 2020-07-23 stsp free(abspath);
1358 906c123b 2020-07-23 stsp free(path_got);
1359 8ba819a3 2020-07-23 stsp return err;
1360 8ba819a3 2020-07-23 stsp }
1361 8ba819a3 2020-07-23 stsp
1362 8ba819a3 2020-07-23 stsp static const struct got_error *
1363 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1364 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1365 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1366 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1367 bd6aa359 2020-07-23 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1368 bd6aa359 2020-07-23 stsp void *progress_arg)
1369 8ba819a3 2020-07-23 stsp {
1370 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1371 507dc3bb 2018-12-29 stsp int fd = -1;
1372 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1373 507dc3bb 2018-12-29 stsp int update = 0;
1374 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1375 8ba819a3 2020-07-23 stsp
1376 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1377 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1378 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1379 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1380 21908da4 2019-01-13 stsp char *parent = dirname(path);
1381 21908da4 2019-01-13 stsp if (parent == NULL)
1382 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1383 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1384 21908da4 2019-01-13 stsp if (err)
1385 21908da4 2019-01-13 stsp return err;
1386 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1387 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1388 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1389 21908da4 2019-01-13 stsp if (fd == -1)
1390 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1391 230a42bd 2019-05-11 jcs ondisk_path);
1392 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1393 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1394 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1395 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1396 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1397 507dc3bb 2018-12-29 stsp goto done;
1398 d70b8e30 2018-12-27 stsp } else {
1399 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1400 507dc3bb 2018-12-29 stsp ondisk_path);
1401 507dc3bb 2018-12-29 stsp if (err)
1402 507dc3bb 2018-12-29 stsp goto done;
1403 507dc3bb 2018-12-29 stsp update = 1;
1404 9d31a1d8 2018-03-11 stsp }
1405 507dc3bb 2018-12-29 stsp } else
1406 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1407 9d31a1d8 2018-03-11 stsp }
1408 9d31a1d8 2018-03-11 stsp
1409 bd6aa359 2020-07-23 stsp if (progress_cb) {
1410 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1411 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1412 bd6aa359 2020-07-23 stsp path);
1413 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1414 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1415 bd6aa359 2020-07-23 stsp path);
1416 bd6aa359 2020-07-23 stsp else
1417 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1418 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1419 bd6aa359 2020-07-23 stsp if (err)
1420 bd6aa359 2020-07-23 stsp goto done;
1421 bd6aa359 2020-07-23 stsp }
1422 d7b62c98 2018-12-27 stsp
1423 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1424 9d31a1d8 2018-03-11 stsp do {
1425 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1426 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1427 9d31a1d8 2018-03-11 stsp if (err)
1428 9d31a1d8 2018-03-11 stsp break;
1429 9d31a1d8 2018-03-11 stsp if (len > 0) {
1430 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1431 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1432 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1433 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1434 b87c6f83 2018-12-24 stsp goto done;
1435 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1436 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1437 b87c6f83 2018-12-24 stsp goto done;
1438 9d31a1d8 2018-03-11 stsp }
1439 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1440 9d31a1d8 2018-03-11 stsp }
1441 9d31a1d8 2018-03-11 stsp } while (len != 0);
1442 9d31a1d8 2018-03-11 stsp
1443 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1444 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1445 816dc654 2019-02-16 stsp goto done;
1446 816dc654 2019-02-16 stsp }
1447 9d31a1d8 2018-03-11 stsp
1448 507dc3bb 2018-12-29 stsp if (update) {
1449 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1450 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1451 230a42bd 2019-05-11 jcs ondisk_path);
1452 2a57020b 2019-02-20 stsp unlink(tmppath);
1453 68ed9ba5 2019-02-10 stsp goto done;
1454 68ed9ba5 2019-02-10 stsp }
1455 68ed9ba5 2019-02-10 stsp }
1456 ba8a0d4d 2019-02-10 stsp
1457 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1458 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1459 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1460 1ebedb77 2019-10-19 stsp goto done;
1461 507dc3bb 2018-12-29 stsp }
1462 507dc3bb 2018-12-29 stsp
1463 9d31a1d8 2018-03-11 stsp done:
1464 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1465 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1466 507dc3bb 2018-12-29 stsp free(tmppath);
1467 6353ad76 2019-02-08 stsp return err;
1468 6353ad76 2019-02-08 stsp }
1469 6353ad76 2019-02-08 stsp
1470 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1471 6353ad76 2019-02-08 stsp static const struct got_error *
1472 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1473 7154f6ce 2019-03-27 stsp {
1474 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1475 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1476 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1477 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1478 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1479 7154f6ce 2019-03-27 stsp };
1480 7154f6ce 2019-03-27 stsp int i = 0;
1481 7154f6ce 2019-03-27 stsp char *line;
1482 7154f6ce 2019-03-27 stsp size_t len;
1483 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1484 7154f6ce 2019-03-27 stsp
1485 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1486 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1487 7154f6ce 2019-03-27 stsp if (line == NULL) {
1488 7154f6ce 2019-03-27 stsp if (feof(f))
1489 7154f6ce 2019-03-27 stsp break;
1490 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1491 7154f6ce 2019-03-27 stsp break;
1492 7154f6ce 2019-03-27 stsp }
1493 7154f6ce 2019-03-27 stsp
1494 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1495 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1496 19332e6d 2019-05-13 stsp == 0)
1497 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1498 7154f6ce 2019-03-27 stsp else
1499 7154f6ce 2019-03-27 stsp i++;
1500 7154f6ce 2019-03-27 stsp }
1501 7154f6ce 2019-03-27 stsp }
1502 7154f6ce 2019-03-27 stsp
1503 7154f6ce 2019-03-27 stsp return err;
1504 e2b1e152 2019-07-13 stsp }
1505 e2b1e152 2019-07-13 stsp
1506 e2b1e152 2019-07-13 stsp static int
1507 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1508 1ebedb77 2019-10-19 stsp {
1509 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1510 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1511 1ebedb77 2019-10-19 stsp }
1512 1ebedb77 2019-10-19 stsp
1513 1ebedb77 2019-10-19 stsp static int
1514 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1515 e2b1e152 2019-07-13 stsp {
1516 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1517 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1518 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1519 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1520 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1521 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1522 c363b2c1 2019-08-03 stsp }
1523 c363b2c1 2019-08-03 stsp
1524 c363b2c1 2019-08-03 stsp static unsigned char
1525 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1526 c363b2c1 2019-08-03 stsp {
1527 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1528 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1529 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1530 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1531 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1532 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1533 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1534 c363b2c1 2019-08-03 stsp default:
1535 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1536 a919d5c4 2020-07-23 stsp }
1537 a919d5c4 2020-07-23 stsp }
1538 a919d5c4 2020-07-23 stsp
1539 a919d5c4 2020-07-23 stsp static const struct got_error *
1540 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1541 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1542 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1543 a919d5c4 2020-07-23 stsp {
1544 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1545 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1546 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1547 a919d5c4 2020-07-23 stsp ssize_t elen;
1548 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1549 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1551 a919d5c4 2020-07-23 stsp
1552 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1553 a919d5c4 2020-07-23 stsp
1554 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1555 a919d5c4 2020-07-23 stsp do {
1556 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1557 a919d5c4 2020-07-23 stsp if (err)
1558 a919d5c4 2020-07-23 stsp return err;
1559 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1560 a919d5c4 2020-07-23 stsp /*
1561 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1562 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1563 a919d5c4 2020-07-23 stsp */
1564 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1565 a919d5c4 2020-07-23 stsp }
1566 a919d5c4 2020-07-23 stsp if (len > 0) {
1567 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1568 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1569 a919d5c4 2020-07-23 stsp len - hdrlen);
1570 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1571 a919d5c4 2020-07-23 stsp hdrlen = 0;
1572 a919d5c4 2020-07-23 stsp }
1573 a919d5c4 2020-07-23 stsp } while (len != 0);
1574 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1575 a919d5c4 2020-07-23 stsp
1576 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1577 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1578 a919d5c4 2020-07-23 stsp if (elen == -1)
1579 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1580 a919d5c4 2020-07-23 stsp } else {
1581 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1582 a919d5c4 2020-07-23 stsp if (elen == -1)
1583 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1584 c363b2c1 2019-08-03 stsp }
1585 a919d5c4 2020-07-23 stsp
1586 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1587 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1588 a919d5c4 2020-07-23 stsp
1589 a919d5c4 2020-07-23 stsp return NULL;
1590 7154f6ce 2019-03-27 stsp }
1591 7154f6ce 2019-03-27 stsp
1592 7154f6ce 2019-03-27 stsp static const struct got_error *
1593 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1594 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1595 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1596 6353ad76 2019-02-08 stsp {
1597 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1598 6353ad76 2019-02-08 stsp struct got_object_id id;
1599 6353ad76 2019-02-08 stsp size_t hdrlen;
1600 1338848f 2019-12-13 stsp int fd = -1;
1601 6353ad76 2019-02-08 stsp FILE *f = NULL;
1602 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1603 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1604 6353ad76 2019-02-08 stsp size_t flen, blen;
1605 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1606 6353ad76 2019-02-08 stsp
1607 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1608 6353ad76 2019-02-08 stsp
1609 7f91a133 2019-12-13 stsp /*
1610 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1611 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1612 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1613 7f91a133 2019-12-13 stsp */
1614 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1615 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1616 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1617 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1618 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1619 882ef1b9 2019-12-13 stsp else
1620 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1621 882ef1b9 2019-12-13 stsp goto done;
1622 882ef1b9 2019-12-13 stsp }
1623 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1624 3d35a492 2019-12-13 stsp goto done;
1625 3d35a492 2019-12-13 stsp }
1626 7f91a133 2019-12-13 stsp } else {
1627 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1628 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1629 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1630 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1631 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1632 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1633 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1634 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1635 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1636 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1637 3d35a492 2019-12-13 stsp else
1638 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1639 3d35a492 2019-12-13 stsp goto done;
1640 3d35a492 2019-12-13 stsp }
1641 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1642 1338848f 2019-12-13 stsp goto done;
1643 a378724f 2019-02-10 stsp }
1644 a378724f 2019-02-10 stsp }
1645 6353ad76 2019-02-08 stsp
1646 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1647 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1648 1338848f 2019-12-13 stsp goto done;
1649 3f148bc6 2019-07-27 stsp }
1650 efdd40df 2019-07-27 stsp
1651 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1652 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1653 1338848f 2019-12-13 stsp goto done;
1654 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1655 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1656 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1657 1338848f 2019-12-13 stsp goto done;
1658 d00136be 2019-03-26 stsp }
1659 b8f41171 2019-02-10 stsp
1660 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1661 1338848f 2019-12-13 stsp goto done;
1662 6353ad76 2019-02-08 stsp
1663 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1664 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1665 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1666 c363b2c1 2019-08-03 stsp else
1667 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1668 c363b2c1 2019-08-03 stsp
1669 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1670 6353ad76 2019-02-08 stsp if (err)
1671 1338848f 2019-12-13 stsp goto done;
1672 6353ad76 2019-02-08 stsp
1673 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1674 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1675 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1676 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1677 a919d5c4 2020-07-23 stsp goto done;
1678 a919d5c4 2020-07-23 stsp }
1679 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1680 a919d5c4 2020-07-23 stsp de_name, blob);
1681 a919d5c4 2020-07-23 stsp goto done;
1682 a919d5c4 2020-07-23 stsp }
1683 a919d5c4 2020-07-23 stsp
1684 a919d5c4 2020-07-23 stsp
1685 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1686 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1687 ab0d4361 2019-12-13 stsp if (fd == -1) {
1688 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1689 ab0d4361 2019-12-13 stsp goto done;
1690 ab0d4361 2019-12-13 stsp }
1691 3d35a492 2019-12-13 stsp }
1692 3d35a492 2019-12-13 stsp
1693 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1694 6353ad76 2019-02-08 stsp if (f == NULL) {
1695 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1696 6353ad76 2019-02-08 stsp goto done;
1697 6353ad76 2019-02-08 stsp }
1698 1338848f 2019-12-13 stsp fd = -1;
1699 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1700 656b1f76 2019-05-11 jcs for (;;) {
1701 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1702 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1703 6353ad76 2019-02-08 stsp if (err)
1704 7154f6ce 2019-03-27 stsp goto done;
1705 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1706 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1707 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1709 7154f6ce 2019-03-27 stsp goto done;
1710 80c5c120 2019-02-19 stsp }
1711 6353ad76 2019-02-08 stsp if (blen == 0) {
1712 6353ad76 2019-02-08 stsp if (flen != 0)
1713 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1714 6353ad76 2019-02-08 stsp break;
1715 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1716 6353ad76 2019-02-08 stsp if (blen != 0)
1717 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1718 6353ad76 2019-02-08 stsp break;
1719 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1720 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1721 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1722 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1723 6353ad76 2019-02-08 stsp break;
1724 6353ad76 2019-02-08 stsp }
1725 6353ad76 2019-02-08 stsp } else {
1726 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1727 6353ad76 2019-02-08 stsp break;
1728 6353ad76 2019-02-08 stsp }
1729 6353ad76 2019-02-08 stsp hdrlen = 0;
1730 6353ad76 2019-02-08 stsp }
1731 7154f6ce 2019-03-27 stsp
1732 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1733 7154f6ce 2019-03-27 stsp rewind(f);
1734 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1735 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1736 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1737 6353ad76 2019-02-08 stsp done:
1738 6353ad76 2019-02-08 stsp if (blob)
1739 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1740 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1741 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1742 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1743 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1744 9d31a1d8 2018-03-11 stsp return err;
1745 e2b1e152 2019-07-13 stsp }
1746 e2b1e152 2019-07-13 stsp
1747 e2b1e152 2019-07-13 stsp /*
1748 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1749 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1750 e2b1e152 2019-07-13 stsp */
1751 e2b1e152 2019-07-13 stsp static const struct got_error *
1752 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1753 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1754 e2b1e152 2019-07-13 stsp {
1755 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1756 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1757 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1758 e2b1e152 2019-07-13 stsp
1759 e2b1e152 2019-07-13 stsp return NULL;
1760 9d31a1d8 2018-03-11 stsp }
1761 9d31a1d8 2018-03-11 stsp
1762 9d31a1d8 2018-03-11 stsp static const struct got_error *
1763 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1764 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1765 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1766 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1767 0584f854 2019-04-06 stsp void *progress_arg)
1768 9d31a1d8 2018-03-11 stsp {
1769 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1770 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1771 6353ad76 2019-02-08 stsp char *ondisk_path;
1772 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1773 b8f41171 2019-02-10 stsp struct stat sb;
1774 9d31a1d8 2018-03-11 stsp
1775 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1776 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1777 6353ad76 2019-02-08 stsp
1778 abb4604f 2019-07-27 stsp if (ie) {
1779 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1780 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1781 a76c42e6 2019-08-03 stsp goto done;
1782 a76c42e6 2019-08-03 stsp }
1783 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1784 7f91a133 2019-12-13 stsp repo);
1785 abb4604f 2019-07-27 stsp if (err)
1786 abb4604f 2019-07-27 stsp goto done;
1787 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1788 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1789 abb4604f 2019-07-27 stsp } else
1790 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1791 6353ad76 2019-02-08 stsp
1792 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1793 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1794 b8f41171 2019-02-10 stsp goto done;
1795 b8f41171 2019-02-10 stsp }
1796 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1797 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1798 5036ab18 2020-04-18 stsp path);
1799 5036ab18 2020-04-18 stsp goto done;
1800 5036ab18 2020-04-18 stsp }
1801 b8f41171 2019-02-10 stsp
1802 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1803 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1804 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1805 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1806 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1807 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1808 e2b1e152 2019-07-13 stsp if (err)
1809 e2b1e152 2019-07-13 stsp goto done;
1810 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1811 b8f41171 2019-02-10 stsp path);
1812 6353ad76 2019-02-08 stsp goto done;
1813 a378724f 2019-02-10 stsp }
1814 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1815 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1816 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1817 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1818 b8f41171 2019-02-10 stsp goto done;
1819 e2b1e152 2019-07-13 stsp }
1820 9d31a1d8 2018-03-11 stsp }
1821 9d31a1d8 2018-03-11 stsp
1822 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1823 8da9e5f4 2019-01-12 stsp if (err)
1824 6353ad76 2019-02-08 stsp goto done;
1825 512f0d0e 2019-01-02 stsp
1826 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1827 234035bc 2019-06-01 stsp int update_timestamps;
1828 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1829 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1830 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1831 234035bc 2019-06-01 stsp struct got_object_id id2;
1832 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1833 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1834 234035bc 2019-06-01 stsp if (err)
1835 f69721c3 2019-10-21 stsp goto done;
1836 f69721c3 2019-10-21 stsp }
1837 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1838 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1839 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1840 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1841 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1842 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1843 f69721c3 2019-10-21 stsp goto done;
1844 f69721c3 2019-10-21 stsp }
1845 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1846 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1847 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1848 234035bc 2019-06-01 stsp goto done;
1849 f69721c3 2019-10-21 stsp }
1850 234035bc 2019-06-01 stsp }
1851 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1852 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1853 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1854 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1855 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1856 993e2a1b 2020-07-23 stsp } else {
1857 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1858 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1859 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1860 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1861 993e2a1b 2020-07-23 stsp }
1862 f69721c3 2019-10-21 stsp free(label_orig);
1863 234035bc 2019-06-01 stsp if (blob2)
1864 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1865 909d120e 2019-09-22 stsp if (err)
1866 909d120e 2019-09-22 stsp goto done;
1867 234035bc 2019-06-01 stsp /*
1868 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1869 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1870 234035bc 2019-06-01 stsp * unmodified files again.
1871 234035bc 2019-06-01 stsp */
1872 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1873 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1874 234035bc 2019-06-01 stsp update_timestamps);
1875 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1876 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1877 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1878 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1879 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1880 1ee397ad 2019-07-12 stsp if (err)
1881 1ee397ad 2019-07-12 stsp goto done;
1882 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1883 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1884 13d9040b 2019-03-27 stsp if (err)
1885 13d9040b 2019-03-27 stsp goto done;
1886 13d9040b 2019-03-27 stsp } else {
1887 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1888 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1889 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1890 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1891 2e1fa222 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, repo,
1892 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1893 2e1fa222 2020-07-23 stsp } else {
1894 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1895 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1896 2e1fa222 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0, repo,
1897 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1898 2e1fa222 2020-07-23 stsp }
1899 13d9040b 2019-03-27 stsp if (err)
1900 13d9040b 2019-03-27 stsp goto done;
1901 054041d0 2020-06-24 stsp if (ie) {
1902 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1903 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1904 054041d0 2020-06-24 stsp } else {
1905 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1906 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1907 054041d0 2020-06-24 stsp &blob->id);
1908 054041d0 2020-06-24 stsp }
1909 13d9040b 2019-03-27 stsp if (err)
1910 13d9040b 2019-03-27 stsp goto done;
1911 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1912 2e1fa222 2020-07-23 stsp if (ie == NULL)
1913 2e1fa222 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, path,
1914 2e1fa222 2020-07-23 stsp strlen(path));
1915 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
1916 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1917 2e1fa222 2020-07-23 stsp if (err)
1918 2e1fa222 2020-07-23 stsp goto done;
1919 2e1fa222 2020-07-23 stsp }
1920 13d9040b 2019-03-27 stsp }
1921 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1922 6353ad76 2019-02-08 stsp done:
1923 6353ad76 2019-02-08 stsp free(ondisk_path);
1924 8da9e5f4 2019-01-12 stsp return err;
1925 90285c3b 2019-01-08 stsp }
1926 90285c3b 2019-01-08 stsp
1927 90285c3b 2019-01-08 stsp static const struct got_error *
1928 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1929 90285c3b 2019-01-08 stsp {
1930 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1931 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1932 90285c3b 2019-01-08 stsp
1933 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1934 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1935 90285c3b 2019-01-08 stsp
1936 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1937 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1938 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1939 c97665ae 2019-01-13 stsp } else {
1940 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1941 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1942 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1943 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1944 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1945 230a42bd 2019-05-11 jcs parent);
1946 90285c3b 2019-01-08 stsp break;
1947 90285c3b 2019-01-08 stsp }
1948 90285c3b 2019-01-08 stsp parent = dirname(parent);
1949 90285c3b 2019-01-08 stsp }
1950 90285c3b 2019-01-08 stsp }
1951 90285c3b 2019-01-08 stsp free(ondisk_path);
1952 90285c3b 2019-01-08 stsp return err;
1953 512f0d0e 2019-01-02 stsp }
1954 512f0d0e 2019-01-02 stsp
1955 708d8e67 2019-03-27 stsp static const struct got_error *
1956 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1957 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1958 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1959 708d8e67 2019-03-27 stsp {
1960 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1961 708d8e67 2019-03-27 stsp unsigned char status;
1962 708d8e67 2019-03-27 stsp struct stat sb;
1963 708d8e67 2019-03-27 stsp char *ondisk_path;
1964 708d8e67 2019-03-27 stsp
1965 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1966 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1967 a76c42e6 2019-08-03 stsp
1968 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1969 708d8e67 2019-03-27 stsp == -1)
1970 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1971 708d8e67 2019-03-27 stsp
1972 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1973 708d8e67 2019-03-27 stsp if (err)
1974 5a58a424 2020-06-23 stsp goto done;
1975 708d8e67 2019-03-27 stsp
1976 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1977 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
1978 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1979 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
1980 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
1981 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
1982 993e2a1b 2020-07-23 stsp goto done;
1983 993e2a1b 2020-07-23 stsp }
1984 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1985 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
1986 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
1987 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
1988 993e2a1b 2020-07-23 stsp if (err)
1989 993e2a1b 2020-07-23 stsp goto done;
1990 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1991 993e2a1b 2020-07-23 stsp ie->path);
1992 993e2a1b 2020-07-23 stsp goto done;
1993 993e2a1b 2020-07-23 stsp }
1994 993e2a1b 2020-07-23 stsp
1995 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1996 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1997 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1998 1ee397ad 2019-07-12 stsp if (err)
1999 5a58a424 2020-06-23 stsp goto done;
2000 fc6346c4 2019-03-27 stsp /*
2001 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2002 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2003 fc6346c4 2019-03-27 stsp */
2004 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2005 fc6346c4 2019-03-27 stsp 0);
2006 fc6346c4 2019-03-27 stsp } else {
2007 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2008 1ee397ad 2019-07-12 stsp if (err)
2009 5a58a424 2020-06-23 stsp goto done;
2010 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2011 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2012 fc6346c4 2019-03-27 stsp if (err)
2013 5a58a424 2020-06-23 stsp goto done;
2014 fc6346c4 2019-03-27 stsp }
2015 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2016 708d8e67 2019-03-27 stsp }
2017 5a58a424 2020-06-23 stsp done:
2018 5a58a424 2020-06-23 stsp free(ondisk_path);
2019 fc6346c4 2019-03-27 stsp return err;
2020 708d8e67 2019-03-27 stsp }
2021 708d8e67 2019-03-27 stsp
2022 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2023 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2024 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2025 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2026 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2027 8da9e5f4 2019-01-12 stsp void *progress_arg;
2028 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2029 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2030 8da9e5f4 2019-01-12 stsp };
2031 8da9e5f4 2019-01-12 stsp
2032 512f0d0e 2019-01-02 stsp static const struct got_error *
2033 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2034 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2035 512f0d0e 2019-01-02 stsp {
2036 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2037 0584f854 2019-04-06 stsp
2038 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2039 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2040 512f0d0e 2019-01-02 stsp
2041 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2042 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2043 8da9e5f4 2019-01-12 stsp }
2044 512f0d0e 2019-01-02 stsp
2045 8da9e5f4 2019-01-12 stsp static const struct got_error *
2046 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2047 8da9e5f4 2019-01-12 stsp {
2048 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2049 7a9df742 2019-01-08 stsp
2050 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2051 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2052 0584f854 2019-04-06 stsp
2053 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2054 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2055 9d31a1d8 2018-03-11 stsp }
2056 9d31a1d8 2018-03-11 stsp
2057 9d31a1d8 2018-03-11 stsp static const struct got_error *
2058 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2059 9d31a1d8 2018-03-11 stsp {
2060 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2061 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2062 8da9e5f4 2019-01-12 stsp char *path;
2063 9d31a1d8 2018-03-11 stsp
2064 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2065 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2066 0584f854 2019-04-06 stsp
2067 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2068 63c5ca5d 2019-08-24 stsp return NULL;
2069 63c5ca5d 2019-08-24 stsp
2070 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2071 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2072 8da9e5f4 2019-01-12 stsp == -1)
2073 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2074 9d31a1d8 2018-03-11 stsp
2075 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2076 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2077 8da9e5f4 2019-01-12 stsp else
2078 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2079 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2080 9d31a1d8 2018-03-11 stsp
2081 8da9e5f4 2019-01-12 stsp free(path);
2082 0cd1c46a 2019-03-11 stsp return err;
2083 0cd1c46a 2019-03-11 stsp }
2084 0cd1c46a 2019-03-11 stsp
2085 818c7501 2019-07-11 stsp static const struct got_error *
2086 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2087 0cd1c46a 2019-03-11 stsp {
2088 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2089 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2090 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2091 0cd1c46a 2019-03-11 stsp
2092 517bab73 2019-03-11 stsp *refname = NULL;
2093 517bab73 2019-03-11 stsp
2094 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2095 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2096 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2097 0cd1c46a 2019-03-11 stsp
2098 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2099 0647c563 2019-03-11 stsp == -1) {
2100 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2101 0647c563 2019-03-11 stsp *refname = NULL;
2102 0cd1c46a 2019-03-11 stsp }
2103 517bab73 2019-03-11 stsp free(uuidstr);
2104 517bab73 2019-03-11 stsp return err;
2105 517bab73 2019-03-11 stsp }
2106 517bab73 2019-03-11 stsp
2107 818c7501 2019-07-11 stsp const struct got_error *
2108 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2109 818c7501 2019-07-11 stsp {
2110 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2111 818c7501 2019-07-11 stsp }
2112 818c7501 2019-07-11 stsp
2113 818c7501 2019-07-11 stsp static const struct got_error *
2114 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2115 818c7501 2019-07-11 stsp {
2116 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2117 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2118 818c7501 2019-07-11 stsp }
2119 818c7501 2019-07-11 stsp
2120 818c7501 2019-07-11 stsp static const struct got_error *
2121 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2122 818c7501 2019-07-11 stsp {
2123 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2124 818c7501 2019-07-11 stsp }
2125 818c7501 2019-07-11 stsp
2126 818c7501 2019-07-11 stsp static const struct got_error *
2127 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2128 818c7501 2019-07-11 stsp {
2129 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2130 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2131 818c7501 2019-07-11 stsp }
2132 818c7501 2019-07-11 stsp
2133 818c7501 2019-07-11 stsp static const struct got_error *
2134 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2135 818c7501 2019-07-11 stsp {
2136 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2137 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2138 0ebf8283 2019-07-24 stsp }
2139 0ebf8283 2019-07-24 stsp
2140 0ebf8283 2019-07-24 stsp static const struct got_error *
2141 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2142 0ebf8283 2019-07-24 stsp {
2143 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2144 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2145 0ebf8283 2019-07-24 stsp }
2146 0ebf8283 2019-07-24 stsp
2147 0ebf8283 2019-07-24 stsp static const struct got_error *
2148 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2149 0ebf8283 2019-07-24 stsp {
2150 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2151 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2152 0ebf8283 2019-07-24 stsp }
2153 0ebf8283 2019-07-24 stsp
2154 0ebf8283 2019-07-24 stsp static const struct got_error *
2155 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2156 0ebf8283 2019-07-24 stsp {
2157 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2158 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2159 818c7501 2019-07-11 stsp }
2160 818c7501 2019-07-11 stsp
2161 0ebf8283 2019-07-24 stsp static const struct got_error *
2162 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2163 0ebf8283 2019-07-24 stsp {
2164 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2165 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2166 0ebf8283 2019-07-24 stsp }
2167 818c7501 2019-07-11 stsp
2168 0ebf8283 2019-07-24 stsp const struct got_error *
2169 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2170 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2171 0ebf8283 2019-07-24 stsp {
2172 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2173 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2174 0ebf8283 2019-07-24 stsp *path = NULL;
2175 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2176 0ebf8283 2019-07-24 stsp }
2177 0ebf8283 2019-07-24 stsp return NULL;
2178 0ebf8283 2019-07-24 stsp }
2179 0ebf8283 2019-07-24 stsp
2180 517bab73 2019-03-11 stsp /*
2181 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2182 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2183 517bab73 2019-03-11 stsp */
2184 517bab73 2019-03-11 stsp static const struct got_error *
2185 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2186 517bab73 2019-03-11 stsp {
2187 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2188 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2189 517bab73 2019-03-11 stsp char *refname;
2190 517bab73 2019-03-11 stsp
2191 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2192 517bab73 2019-03-11 stsp if (err)
2193 517bab73 2019-03-11 stsp return err;
2194 0cd1c46a 2019-03-11 stsp
2195 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2196 0cd1c46a 2019-03-11 stsp if (err)
2197 0cd1c46a 2019-03-11 stsp goto done;
2198 0cd1c46a 2019-03-11 stsp
2199 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2200 0cd1c46a 2019-03-11 stsp done:
2201 0cd1c46a 2019-03-11 stsp free(refname);
2202 0cd1c46a 2019-03-11 stsp if (ref)
2203 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2204 3e3a69f1 2019-07-25 stsp return err;
2205 3e3a69f1 2019-07-25 stsp }
2206 3e3a69f1 2019-07-25 stsp
2207 3e3a69f1 2019-07-25 stsp static const struct got_error *
2208 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2209 3e3a69f1 2019-07-25 stsp {
2210 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2211 3e3a69f1 2019-07-25 stsp
2212 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2213 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2214 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2215 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2216 3e3a69f1 2019-07-25 stsp }
2217 9d31a1d8 2018-03-11 stsp return err;
2218 9d31a1d8 2018-03-11 stsp }
2219 9d31a1d8 2018-03-11 stsp
2220 3e3a69f1 2019-07-25 stsp
2221 ebf99748 2019-05-09 stsp static const struct got_error *
2222 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2223 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2224 ebf99748 2019-05-09 stsp {
2225 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2226 ebf99748 2019-05-09 stsp FILE *index = NULL;
2227 0cd1c46a 2019-03-11 stsp
2228 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2229 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2230 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2231 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2232 ebf99748 2019-05-09 stsp
2233 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2234 3e3a69f1 2019-07-25 stsp if (err)
2235 ebf99748 2019-05-09 stsp goto done;
2236 ebf99748 2019-05-09 stsp
2237 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2238 ebf99748 2019-05-09 stsp if (index == NULL) {
2239 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2240 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2241 ebf99748 2019-05-09 stsp } else {
2242 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2243 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2244 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2245 ebf99748 2019-05-09 stsp }
2246 ebf99748 2019-05-09 stsp done:
2247 ebf99748 2019-05-09 stsp if (err) {
2248 ebf99748 2019-05-09 stsp free(*fileindex_path);
2249 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2250 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2251 ebf99748 2019-05-09 stsp *fileindex = NULL;
2252 ebf99748 2019-05-09 stsp }
2253 ebf99748 2019-05-09 stsp return err;
2254 ebf99748 2019-05-09 stsp }
2255 c932eeeb 2019-05-22 stsp
2256 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2257 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2258 c932eeeb 2019-05-22 stsp const char *path;
2259 c932eeeb 2019-05-22 stsp size_t path_len;
2260 c932eeeb 2019-05-22 stsp const char *entry_name;
2261 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2262 a484d721 2019-06-10 stsp void *progress_arg;
2263 c932eeeb 2019-05-22 stsp };
2264 ebf99748 2019-05-09 stsp
2265 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2266 c932eeeb 2019-05-22 stsp static const struct got_error *
2267 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2268 c932eeeb 2019-05-22 stsp {
2269 1ee397ad 2019-07-12 stsp const struct got_error *err;
2270 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2271 c932eeeb 2019-05-22 stsp
2272 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2273 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2274 c932eeeb 2019-05-22 stsp return NULL;
2275 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2276 102ff934 2019-06-11 stsp return NULL;
2277 102ff934 2019-06-11 stsp
2278 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2279 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2280 c932eeeb 2019-05-22 stsp return NULL;
2281 c932eeeb 2019-05-22 stsp
2282 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2283 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2284 edd02c5e 2019-07-11 stsp ie->path);
2285 1ee397ad 2019-07-12 stsp if (err)
2286 1ee397ad 2019-07-12 stsp return err;
2287 1ee397ad 2019-07-12 stsp }
2288 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2289 c932eeeb 2019-05-22 stsp return NULL;
2290 9c6338c4 2019-06-02 stsp }
2291 9c6338c4 2019-06-02 stsp
2292 9c6338c4 2019-06-02 stsp static const struct got_error *
2293 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2294 9c6338c4 2019-06-02 stsp {
2295 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2296 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2297 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2298 867630bb 2020-01-17 stsp struct timespec timeout;
2299 9c6338c4 2019-06-02 stsp
2300 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2301 9c6338c4 2019-06-02 stsp fileindex_path);
2302 9c6338c4 2019-06-02 stsp if (err)
2303 9c6338c4 2019-06-02 stsp goto done;
2304 9c6338c4 2019-06-02 stsp
2305 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2306 9c6338c4 2019-06-02 stsp if (err)
2307 9c6338c4 2019-06-02 stsp goto done;
2308 9c6338c4 2019-06-02 stsp
2309 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2310 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2311 9c6338c4 2019-06-02 stsp fileindex_path);
2312 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2313 9c6338c4 2019-06-02 stsp }
2314 867630bb 2020-01-17 stsp
2315 867630bb 2020-01-17 stsp /*
2316 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2317 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2318 867630bb 2020-01-17 stsp * was recorded in the file index.
2319 867630bb 2020-01-17 stsp */
2320 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2321 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2322 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2323 9c6338c4 2019-06-02 stsp done:
2324 9c6338c4 2019-06-02 stsp if (new_index)
2325 9c6338c4 2019-06-02 stsp fclose(new_index);
2326 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2327 9c6338c4 2019-06-02 stsp return err;
2328 c932eeeb 2019-05-22 stsp }
2329 6ced4176 2019-07-12 stsp
2330 6ced4176 2019-07-12 stsp static const struct got_error *
2331 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2332 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2333 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2334 6ced4176 2019-07-12 stsp {
2335 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2336 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2337 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2338 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2339 6ced4176 2019-07-12 stsp
2340 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2341 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2342 6ced4176 2019-07-12 stsp *tree_id = NULL;
2343 6ced4176 2019-07-12 stsp
2344 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2345 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2346 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2347 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2348 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2349 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2350 6ced4176 2019-07-12 stsp goto done;
2351 6ced4176 2019-07-12 stsp }
2352 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2353 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2354 6ced4176 2019-07-12 stsp if (err)
2355 6ced4176 2019-07-12 stsp goto done;
2356 6ced4176 2019-07-12 stsp return NULL;
2357 6ced4176 2019-07-12 stsp }
2358 6ced4176 2019-07-12 stsp
2359 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2360 6ced4176 2019-07-12 stsp
2361 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2362 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2363 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2364 6ced4176 2019-07-12 stsp goto done;
2365 6ced4176 2019-07-12 stsp }
2366 6ced4176 2019-07-12 stsp
2367 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2368 6ced4176 2019-07-12 stsp in_repo_path);
2369 6ced4176 2019-07-12 stsp if (err)
2370 6ced4176 2019-07-12 stsp goto done;
2371 6ced4176 2019-07-12 stsp
2372 6ced4176 2019-07-12 stsp free(in_repo_path);
2373 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2374 6ced4176 2019-07-12 stsp
2375 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2376 6ced4176 2019-07-12 stsp if (err)
2377 6ced4176 2019-07-12 stsp goto done;
2378 c932eeeb 2019-05-22 stsp
2379 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2380 6ced4176 2019-07-12 stsp /* Check out a single file. */
2381 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2382 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2383 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2384 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2385 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2386 6ced4176 2019-07-12 stsp goto done;
2387 6ced4176 2019-07-12 stsp }
2388 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2389 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2390 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2391 6ced4176 2019-07-12 stsp goto done;
2392 6ced4176 2019-07-12 stsp }
2393 6ced4176 2019-07-12 stsp } else {
2394 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2395 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2396 6ced4176 2019-07-12 stsp if (err)
2397 6ced4176 2019-07-12 stsp return err;
2398 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2399 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2400 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2401 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2402 6ced4176 2019-07-12 stsp goto done;
2403 6ced4176 2019-07-12 stsp }
2404 6ced4176 2019-07-12 stsp }
2405 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2406 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2407 6ced4176 2019-07-12 stsp } else {
2408 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2409 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2410 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2411 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2412 6ced4176 2019-07-12 stsp goto done;
2413 6ced4176 2019-07-12 stsp }
2414 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2415 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2416 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2417 6ced4176 2019-07-12 stsp goto done;
2418 6ced4176 2019-07-12 stsp }
2419 6ced4176 2019-07-12 stsp }
2420 6ced4176 2019-07-12 stsp done:
2421 6ced4176 2019-07-12 stsp free(id);
2422 6ced4176 2019-07-12 stsp free(in_repo_path);
2423 6ced4176 2019-07-12 stsp if (err) {
2424 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2425 6ced4176 2019-07-12 stsp free(*tree_relpath);
2426 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2427 6ced4176 2019-07-12 stsp free(*tree_id);
2428 6ced4176 2019-07-12 stsp *tree_id = NULL;
2429 6ced4176 2019-07-12 stsp }
2430 07ed472d 2019-07-12 stsp return err;
2431 07ed472d 2019-07-12 stsp }
2432 07ed472d 2019-07-12 stsp
2433 07ed472d 2019-07-12 stsp static const struct got_error *
2434 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2435 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2436 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2437 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2438 07ed472d 2019-07-12 stsp {
2439 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2440 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2441 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2442 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2443 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2444 07ed472d 2019-07-12 stsp
2445 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2446 7f47418f 2019-12-20 stsp if (err) {
2447 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2448 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2449 7f47418f 2019-12-20 stsp goto done;
2450 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2451 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2452 7f47418f 2019-12-20 stsp if (err)
2453 7f47418f 2019-12-20 stsp return err;
2454 7f47418f 2019-12-20 stsp }
2455 07ed472d 2019-07-12 stsp
2456 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2457 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2458 07ed472d 2019-07-12 stsp if (err)
2459 07ed472d 2019-07-12 stsp goto done;
2460 07ed472d 2019-07-12 stsp
2461 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2462 07ed472d 2019-07-12 stsp if (err)
2463 07ed472d 2019-07-12 stsp goto done;
2464 07ed472d 2019-07-12 stsp
2465 07ed472d 2019-07-12 stsp if (entry_name &&
2466 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2467 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2468 07ed472d 2019-07-12 stsp goto done;
2469 07ed472d 2019-07-12 stsp }
2470 07ed472d 2019-07-12 stsp
2471 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2472 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2473 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2474 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2475 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2476 07ed472d 2019-07-12 stsp arg.repo = repo;
2477 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2478 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2479 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2480 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2481 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2482 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2483 07ed472d 2019-07-12 stsp done:
2484 07ed472d 2019-07-12 stsp if (tree)
2485 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2486 07ed472d 2019-07-12 stsp if (commit)
2487 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2488 6ced4176 2019-07-12 stsp return err;
2489 6ced4176 2019-07-12 stsp }
2490 6ced4176 2019-07-12 stsp
2491 9d31a1d8 2018-03-11 stsp const struct got_error *
2492 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2493 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2494 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2495 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2496 9d31a1d8 2018-03-11 stsp {
2497 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2498 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2499 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2500 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2501 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2502 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2503 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2504 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2505 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2506 f2ea84fa 2019-07-27 stsp int entry_type;
2507 f2ea84fa 2019-07-27 stsp char *relpath;
2508 f2ea84fa 2019-07-27 stsp char *entry_name;
2509 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2510 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2511 9d31a1d8 2018-03-11 stsp
2512 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2513 f2ea84fa 2019-07-27 stsp
2514 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2515 9d31a1d8 2018-03-11 stsp if (err)
2516 9d31a1d8 2018-03-11 stsp return err;
2517 93a30277 2018-12-24 stsp
2518 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2519 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2520 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2521 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2522 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2523 f2ea84fa 2019-07-27 stsp goto done;
2524 f2ea84fa 2019-07-27 stsp }
2525 6ced4176 2019-07-12 stsp
2526 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2527 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2528 f2ea84fa 2019-07-27 stsp if (err) {
2529 f2ea84fa 2019-07-27 stsp free(tpd);
2530 6ced4176 2019-07-12 stsp goto done;
2531 6ced4176 2019-07-12 stsp }
2532 f2ea84fa 2019-07-27 stsp
2533 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2534 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2535 f2ea84fa 2019-07-27 stsp if (err) {
2536 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2537 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2538 f2ea84fa 2019-07-27 stsp free(tpd);
2539 f2ea84fa 2019-07-27 stsp goto done;
2540 f2ea84fa 2019-07-27 stsp }
2541 f2ea84fa 2019-07-27 stsp } else
2542 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2543 f2ea84fa 2019-07-27 stsp
2544 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2545 6ced4176 2019-07-12 stsp }
2546 6ced4176 2019-07-12 stsp
2547 51514078 2018-12-25 stsp /*
2548 51514078 2018-12-25 stsp * Read the file index.
2549 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2550 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2551 51514078 2018-12-25 stsp */
2552 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2553 8da9e5f4 2019-01-12 stsp if (err)
2554 c4cdcb68 2019-04-03 stsp goto done;
2555 c4cdcb68 2019-04-03 stsp
2556 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2557 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2558 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2559 f2ea84fa 2019-07-27 stsp
2560 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2561 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2562 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2563 f2ea84fa 2019-07-27 stsp if (err)
2564 f2ea84fa 2019-07-27 stsp break;
2565 f2ea84fa 2019-07-27 stsp
2566 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2567 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2568 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2569 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2570 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2571 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2572 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2573 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2574 f2ea84fa 2019-07-27 stsp if (err)
2575 f2ea84fa 2019-07-27 stsp break;
2576 f2ea84fa 2019-07-27 stsp
2577 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2578 711d9cd9 2019-07-12 stsp }
2579 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2580 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2581 af12c6b9 2019-06-04 stsp err = sync_err;
2582 9d31a1d8 2018-03-11 stsp done:
2583 9c6338c4 2019-06-02 stsp free(fileindex_path);
2584 edfa7d7f 2018-09-11 stsp if (tree)
2585 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2586 9d31a1d8 2018-03-11 stsp if (commit)
2587 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2588 5ade8233 2019-07-12 stsp if (fileindex)
2589 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2590 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2591 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2592 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2593 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2594 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2595 f2ea84fa 2019-07-27 stsp free(tpd);
2596 f2ea84fa 2019-07-27 stsp }
2597 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2598 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2599 9d31a1d8 2018-03-11 stsp err = unlockerr;
2600 f8d1f275 2019-02-04 stsp return err;
2601 f8d1f275 2019-02-04 stsp }
2602 f8d1f275 2019-02-04 stsp
2603 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2604 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2605 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2606 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2607 234035bc 2019-06-01 stsp void *progress_arg;
2608 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2609 234035bc 2019-06-01 stsp void *cancel_arg;
2610 f69721c3 2019-10-21 stsp const char *label_orig;
2611 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2612 234035bc 2019-06-01 stsp };
2613 234035bc 2019-06-01 stsp
2614 234035bc 2019-06-01 stsp static const struct got_error *
2615 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2616 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2617 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2618 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2619 234035bc 2019-06-01 stsp {
2620 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2621 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2622 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2623 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2624 234035bc 2019-06-01 stsp struct stat sb;
2625 234035bc 2019-06-01 stsp unsigned char status;
2626 234035bc 2019-06-01 stsp int local_changes_subsumed;
2627 234035bc 2019-06-01 stsp
2628 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2629 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2630 d6c87207 2019-08-02 stsp strlen(path2));
2631 1ee397ad 2019-07-12 stsp if (ie == NULL)
2632 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2633 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2634 234035bc 2019-06-01 stsp
2635 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2636 234035bc 2019-06-01 stsp path2) == -1)
2637 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2638 234035bc 2019-06-01 stsp
2639 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2640 7f91a133 2019-12-13 stsp repo);
2641 234035bc 2019-06-01 stsp if (err)
2642 234035bc 2019-06-01 stsp goto done;
2643 234035bc 2019-06-01 stsp
2644 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2645 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2646 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2647 234035bc 2019-06-01 stsp goto done;
2648 234035bc 2019-06-01 stsp }
2649 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2650 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2651 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2652 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2653 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2654 234035bc 2019-06-01 stsp goto done;
2655 234035bc 2019-06-01 stsp }
2656 234035bc 2019-06-01 stsp
2657 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2658 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2659 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2660 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2661 af57b12a 2020-07-23 stsp a->progress_arg);
2662 af57b12a 2020-07-23 stsp } else {
2663 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2664 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2665 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2666 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2667 af57b12a 2020-07-23 stsp }
2668 234035bc 2019-06-01 stsp } else if (blob1) {
2669 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2670 d6c87207 2019-08-02 stsp strlen(path1));
2671 1ee397ad 2019-07-12 stsp if (ie == NULL)
2672 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2673 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2674 2b92fad7 2019-06-02 stsp
2675 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2676 2b92fad7 2019-06-02 stsp path1) == -1)
2677 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2678 2b92fad7 2019-06-02 stsp
2679 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2680 7f91a133 2019-12-13 stsp repo);
2681 2b92fad7 2019-06-02 stsp if (err)
2682 2b92fad7 2019-06-02 stsp goto done;
2683 2b92fad7 2019-06-02 stsp
2684 2b92fad7 2019-06-02 stsp switch (status) {
2685 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2686 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2687 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2688 1ee397ad 2019-07-12 stsp if (err)
2689 1ee397ad 2019-07-12 stsp goto done;
2690 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2691 2b92fad7 2019-06-02 stsp if (err)
2692 2b92fad7 2019-06-02 stsp goto done;
2693 2b92fad7 2019-06-02 stsp if (ie)
2694 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2695 2b92fad7 2019-06-02 stsp break;
2696 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2697 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2698 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2699 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2700 1ee397ad 2019-07-12 stsp if (err)
2701 1ee397ad 2019-07-12 stsp goto done;
2702 2b92fad7 2019-06-02 stsp if (ie)
2703 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2704 2b92fad7 2019-06-02 stsp break;
2705 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2706 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2707 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2708 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2709 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2710 1ee397ad 2019-07-12 stsp if (err)
2711 1ee397ad 2019-07-12 stsp goto done;
2712 2b92fad7 2019-06-02 stsp break;
2713 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2714 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2715 1ee397ad 2019-07-12 stsp if (err)
2716 1ee397ad 2019-07-12 stsp goto done;
2717 2b92fad7 2019-06-02 stsp break;
2718 2b92fad7 2019-06-02 stsp default:
2719 2b92fad7 2019-06-02 stsp break;
2720 2b92fad7 2019-06-02 stsp }
2721 234035bc 2019-06-01 stsp } else if (blob2) {
2722 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2723 234035bc 2019-06-01 stsp path2) == -1)
2724 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2725 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2726 d6c87207 2019-08-02 stsp strlen(path2));
2727 234035bc 2019-06-01 stsp if (ie) {
2728 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2729 7f91a133 2019-12-13 stsp -1, NULL, repo);
2730 234035bc 2019-06-01 stsp if (err)
2731 234035bc 2019-06-01 stsp goto done;
2732 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2733 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2734 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2735 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2736 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2737 1ee397ad 2019-07-12 stsp status, path2);
2738 234035bc 2019-06-01 stsp goto done;
2739 234035bc 2019-06-01 stsp }
2740 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2741 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2742 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2743 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2744 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2745 526a746f 2020-07-23 stsp if (err)
2746 526a746f 2020-07-23 stsp goto done;
2747 526a746f 2020-07-23 stsp } else {
2748 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2749 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2750 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2751 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2752 526a746f 2020-07-23 stsp a->progress_arg);
2753 526a746f 2020-07-23 stsp if (err)
2754 526a746f 2020-07-23 stsp goto done;
2755 526a746f 2020-07-23 stsp }
2756 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2757 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2758 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2759 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2760 234035bc 2019-06-01 stsp if (err)
2761 234035bc 2019-06-01 stsp goto done;
2762 234035bc 2019-06-01 stsp }
2763 234035bc 2019-06-01 stsp } else {
2764 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2765 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2766 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2767 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2768 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2769 2e1fa222 2020-07-23 stsp 0, repo, a->progress_cb, a->progress_arg);
2770 2e1fa222 2020-07-23 stsp } else {
2771 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2772 2e1fa222 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2773 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2774 2e1fa222 2020-07-23 stsp }
2775 234035bc 2019-06-01 stsp if (err)
2776 234035bc 2019-06-01 stsp goto done;
2777 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2778 234035bc 2019-06-01 stsp if (err)
2779 234035bc 2019-06-01 stsp goto done;
2780 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2781 3969253a 2020-03-07 stsp NULL, NULL, 1);
2782 3969253a 2020-03-07 stsp if (err) {
2783 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2784 3969253a 2020-03-07 stsp goto done;
2785 3969253a 2020-03-07 stsp }
2786 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2787 2b92fad7 2019-06-02 stsp if (err) {
2788 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2789 2b92fad7 2019-06-02 stsp goto done;
2790 2b92fad7 2019-06-02 stsp }
2791 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2792 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
2793 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2794 2e1fa222 2020-07-23 stsp if (err)
2795 2e1fa222 2020-07-23 stsp goto done;
2796 2e1fa222 2020-07-23 stsp }
2797 234035bc 2019-06-01 stsp }
2798 234035bc 2019-06-01 stsp }
2799 234035bc 2019-06-01 stsp done:
2800 234035bc 2019-06-01 stsp free(ondisk_path);
2801 234035bc 2019-06-01 stsp return err;
2802 234035bc 2019-06-01 stsp }
2803 234035bc 2019-06-01 stsp
2804 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2805 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2806 234035bc 2019-06-01 stsp struct got_repository *repo;
2807 234035bc 2019-06-01 stsp };
2808 234035bc 2019-06-01 stsp
2809 234035bc 2019-06-01 stsp static const struct got_error *
2810 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2811 234035bc 2019-06-01 stsp {
2812 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2813 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2814 234035bc 2019-06-01 stsp unsigned char status;
2815 234035bc 2019-06-01 stsp struct stat sb;
2816 234035bc 2019-06-01 stsp char *ondisk_path;
2817 234035bc 2019-06-01 stsp
2818 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2819 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2820 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2821 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2822 234035bc 2019-06-01 stsp
2823 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2824 234035bc 2019-06-01 stsp == -1)
2825 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2826 234035bc 2019-06-01 stsp
2827 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2828 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2829 234035bc 2019-06-01 stsp if (err)
2830 234035bc 2019-06-01 stsp return err;
2831 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2832 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2833 234035bc 2019-06-01 stsp
2834 234035bc 2019-06-01 stsp return NULL;
2835 234035bc 2019-06-01 stsp }
2836 234035bc 2019-06-01 stsp
2837 818c7501 2019-07-11 stsp static const struct got_error *
2838 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2839 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2840 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2841 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2842 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2843 234035bc 2019-06-01 stsp {
2844 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2845 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2846 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2847 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2848 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2849 234035bc 2019-06-01 stsp
2850 03415a1a 2019-06-02 stsp if (commit_id1) {
2851 f69721c3 2019-10-21 stsp char *id_str;
2852 f69721c3 2019-10-21 stsp
2853 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2854 03415a1a 2019-06-02 stsp worktree->path_prefix);
2855 03415a1a 2019-06-02 stsp if (err)
2856 03415a1a 2019-06-02 stsp goto done;
2857 03415a1a 2019-06-02 stsp
2858 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2859 03415a1a 2019-06-02 stsp if (err)
2860 03415a1a 2019-06-02 stsp goto done;
2861 f69721c3 2019-10-21 stsp
2862 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2863 f69721c3 2019-10-21 stsp if (err)
2864 f69721c3 2019-10-21 stsp goto done;
2865 f69721c3 2019-10-21 stsp
2866 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2867 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2868 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2869 f69721c3 2019-10-21 stsp free(id_str);
2870 f69721c3 2019-10-21 stsp goto done;
2871 f69721c3 2019-10-21 stsp }
2872 f69721c3 2019-10-21 stsp free(id_str);
2873 03415a1a 2019-06-02 stsp }
2874 234035bc 2019-06-01 stsp
2875 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2876 234035bc 2019-06-01 stsp worktree->path_prefix);
2877 234035bc 2019-06-01 stsp if (err)
2878 234035bc 2019-06-01 stsp goto done;
2879 234035bc 2019-06-01 stsp
2880 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2881 234035bc 2019-06-01 stsp if (err)
2882 234035bc 2019-06-01 stsp goto done;
2883 234035bc 2019-06-01 stsp
2884 234035bc 2019-06-01 stsp arg.worktree = worktree;
2885 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2886 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2887 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2888 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2889 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2890 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2891 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2892 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2893 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2894 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2895 af12c6b9 2019-06-04 stsp err = sync_err;
2896 234035bc 2019-06-01 stsp done:
2897 234035bc 2019-06-01 stsp if (tree1)
2898 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2899 234035bc 2019-06-01 stsp if (tree2)
2900 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2901 f69721c3 2019-10-21 stsp free(label_orig);
2902 818c7501 2019-07-11 stsp return err;
2903 818c7501 2019-07-11 stsp }
2904 234035bc 2019-06-01 stsp
2905 818c7501 2019-07-11 stsp const struct got_error *
2906 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2907 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2908 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2909 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2910 818c7501 2019-07-11 stsp {
2911 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2912 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2913 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2914 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2915 818c7501 2019-07-11 stsp
2916 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2917 818c7501 2019-07-11 stsp if (err)
2918 818c7501 2019-07-11 stsp return err;
2919 818c7501 2019-07-11 stsp
2920 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2921 818c7501 2019-07-11 stsp if (err)
2922 818c7501 2019-07-11 stsp goto done;
2923 818c7501 2019-07-11 stsp
2924 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2925 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2926 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2927 818c7501 2019-07-11 stsp &mok_arg);
2928 818c7501 2019-07-11 stsp if (err)
2929 818c7501 2019-07-11 stsp goto done;
2930 818c7501 2019-07-11 stsp
2931 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2932 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2933 818c7501 2019-07-11 stsp done:
2934 818c7501 2019-07-11 stsp if (fileindex)
2935 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2936 818c7501 2019-07-11 stsp free(fileindex_path);
2937 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2938 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2939 234035bc 2019-06-01 stsp err = unlockerr;
2940 234035bc 2019-06-01 stsp return err;
2941 234035bc 2019-06-01 stsp }
2942 234035bc 2019-06-01 stsp
2943 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2944 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2945 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2946 927df6b7 2019-02-10 stsp const char *status_path;
2947 927df6b7 2019-02-10 stsp size_t status_path_len;
2948 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2949 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2950 f8d1f275 2019-02-04 stsp void *status_arg;
2951 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2952 f8d1f275 2019-02-04 stsp void *cancel_arg;
2953 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2954 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2955 f2a9dc41 2019-12-13 tracey int report_unchanged;
2956 3143d852 2020-06-25 stsp int no_ignores;
2957 f8d1f275 2019-02-04 stsp };
2958 88d0e355 2019-08-03 stsp
2959 f8d1f275 2019-02-04 stsp static const struct got_error *
2960 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2961 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2962 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2963 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2964 927df6b7 2019-02-10 stsp {
2965 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2966 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2967 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2968 927df6b7 2019-02-10 stsp struct stat sb;
2969 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2970 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2971 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2972 927df6b7 2019-02-10 stsp
2973 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2974 98eaaa12 2019-08-03 stsp if (err)
2975 98eaaa12 2019-08-03 stsp return err;
2976 98eaaa12 2019-08-03 stsp
2977 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2978 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2979 98eaaa12 2019-08-03 stsp return NULL;
2980 98eaaa12 2019-08-03 stsp
2981 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2982 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2983 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2984 98eaaa12 2019-08-03 stsp }
2985 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2986 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2987 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2988 927df6b7 2019-02-10 stsp }
2989 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2990 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2991 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2992 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2993 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2994 98eaaa12 2019-08-03 stsp }
2995 98eaaa12 2019-08-03 stsp
2996 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2997 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2998 927df6b7 2019-02-10 stsp }
2999 927df6b7 2019-02-10 stsp
3000 927df6b7 2019-02-10 stsp static const struct got_error *
3001 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3002 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3003 f8d1f275 2019-02-04 stsp {
3004 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3005 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3006 f8d1f275 2019-02-04 stsp char *abspath;
3007 f8d1f275 2019-02-04 stsp
3008 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3009 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3010 0584f854 2019-04-06 stsp
3011 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3012 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3013 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3014 927df6b7 2019-02-10 stsp return NULL;
3015 927df6b7 2019-02-10 stsp
3016 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3017 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3018 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3019 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3020 f8d1f275 2019-02-04 stsp } else {
3021 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3022 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3023 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3024 f8d1f275 2019-02-04 stsp }
3025 f8d1f275 2019-02-04 stsp
3026 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3027 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3028 f8d1f275 2019-02-04 stsp free(abspath);
3029 9d31a1d8 2018-03-11 stsp return err;
3030 9d31a1d8 2018-03-11 stsp }
3031 f8d1f275 2019-02-04 stsp
3032 f8d1f275 2019-02-04 stsp static const struct got_error *
3033 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3034 f8d1f275 2019-02-04 stsp {
3035 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3036 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3037 2ec1f75b 2019-03-26 stsp unsigned char status;
3038 927df6b7 2019-02-10 stsp
3039 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3040 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3041 0584f854 2019-04-06 stsp
3042 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3043 927df6b7 2019-02-10 stsp return NULL;
3044 927df6b7 2019-02-10 stsp
3045 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3046 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3047 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3048 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3049 2ec1f75b 2019-03-26 stsp else
3050 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3051 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3052 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3053 6841da00 2019-08-08 stsp }
3054 6841da00 2019-08-08 stsp
3055 6841da00 2019-08-08 stsp void
3056 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3057 6841da00 2019-08-08 stsp {
3058 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3059 6841da00 2019-08-08 stsp
3060 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3061 6841da00 2019-08-08 stsp free((char *)pe->path);
3062 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3063 6841da00 2019-08-08 stsp }
3064 6841da00 2019-08-08 stsp
3065 6841da00 2019-08-08 stsp void
3066 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3067 6841da00 2019-08-08 stsp {
3068 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3069 6841da00 2019-08-08 stsp
3070 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3071 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3072 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3073 6841da00 2019-08-08 stsp free((char *)pe->path);
3074 6841da00 2019-08-08 stsp }
3075 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3076 6841da00 2019-08-08 stsp }
3077 6841da00 2019-08-08 stsp
3078 6841da00 2019-08-08 stsp static const struct got_error *
3079 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3080 6841da00 2019-08-08 stsp {
3081 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3082 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3083 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3084 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3085 6841da00 2019-08-08 stsp size_t linesize = 0;
3086 6841da00 2019-08-08 stsp ssize_t linelen;
3087 6841da00 2019-08-08 stsp
3088 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3089 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3090 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3091 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3092 6841da00 2019-08-08 stsp
3093 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3094 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3095 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3096 bd8de430 2019-10-04 stsp
3097 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3098 bd8de430 2019-10-04 stsp if (line[0] == '#')
3099 bd8de430 2019-10-04 stsp continue;
3100 bd8de430 2019-10-04 stsp
3101 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3102 bd8de430 2019-10-04 stsp if (line[0] == '!')
3103 bd8de430 2019-10-04 stsp continue;
3104 bd8de430 2019-10-04 stsp
3105 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3106 6841da00 2019-08-08 stsp line) == -1) {
3107 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3108 6841da00 2019-08-08 stsp goto done;
3109 6841da00 2019-08-08 stsp }
3110 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3111 6841da00 2019-08-08 stsp if (err)
3112 6841da00 2019-08-08 stsp goto done;
3113 6841da00 2019-08-08 stsp }
3114 6841da00 2019-08-08 stsp if (ferror(f)) {
3115 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3116 6841da00 2019-08-08 stsp goto done;
3117 6841da00 2019-08-08 stsp }
3118 6841da00 2019-08-08 stsp
3119 6841da00 2019-08-08 stsp dirpath = strdup(path);
3120 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3121 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3122 6841da00 2019-08-08 stsp goto done;
3123 6841da00 2019-08-08 stsp }
3124 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3125 6841da00 2019-08-08 stsp done:
3126 6841da00 2019-08-08 stsp free(line);
3127 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3128 6841da00 2019-08-08 stsp free(dirpath);
3129 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3130 6841da00 2019-08-08 stsp }
3131 6841da00 2019-08-08 stsp return err;
3132 6841da00 2019-08-08 stsp }
3133 6841da00 2019-08-08 stsp
3134 6841da00 2019-08-08 stsp int
3135 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3136 6841da00 2019-08-08 stsp {
3137 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3138 bd8de430 2019-10-04 stsp
3139 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3140 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3141 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3142 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3143 bd8de430 2019-10-04 stsp
3144 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3145 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3146 6841da00 2019-08-08 stsp
3147 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3148 bd8de430 2019-10-04 stsp continue;
3149 bd8de430 2019-10-04 stsp pattern += 3;
3150 bd8de430 2019-10-04 stsp p = path;
3151 bd8de430 2019-10-04 stsp while (*p) {
3152 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3153 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3154 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3155 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3156 bd8de430 2019-10-04 stsp p++;
3157 bd8de430 2019-10-04 stsp while (*p == '/')
3158 bd8de430 2019-10-04 stsp p++;
3159 bd8de430 2019-10-04 stsp continue;
3160 bd8de430 2019-10-04 stsp }
3161 bd8de430 2019-10-04 stsp return 1;
3162 bd8de430 2019-10-04 stsp }
3163 bd8de430 2019-10-04 stsp }
3164 bd8de430 2019-10-04 stsp }
3165 bd8de430 2019-10-04 stsp
3166 6841da00 2019-08-08 stsp /*
3167 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3168 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3169 6841da00 2019-08-08 stsp * ignores backwards.
3170 6841da00 2019-08-08 stsp */
3171 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3172 6841da00 2019-08-08 stsp while (pe) {
3173 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3174 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3175 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3176 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3177 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3178 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3179 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3180 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3181 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3182 6841da00 2019-08-08 stsp continue;
3183 6841da00 2019-08-08 stsp return 1;
3184 6841da00 2019-08-08 stsp }
3185 6841da00 2019-08-08 stsp }
3186 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3187 6841da00 2019-08-08 stsp }
3188 6841da00 2019-08-08 stsp
3189 6841da00 2019-08-08 stsp return 0;
3190 6841da00 2019-08-08 stsp }
3191 6841da00 2019-08-08 stsp
3192 6841da00 2019-08-08 stsp static const struct got_error *
3193 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3194 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3195 6841da00 2019-08-08 stsp {
3196 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3197 6841da00 2019-08-08 stsp char *ignorespath;
3198 886cec17 2019-12-15 stsp int fd = -1;
3199 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3200 6841da00 2019-08-08 stsp
3201 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3202 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3203 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3204 6841da00 2019-08-08 stsp
3205 886cec17 2019-12-15 stsp if (dirfd != -1) {
3206 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3207 886cec17 2019-12-15 stsp if (fd == -1) {
3208 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3209 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3210 886cec17 2019-12-15 stsp ignorespath);
3211 886cec17 2019-12-15 stsp } else {
3212 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3213 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3214 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3215 886cec17 2019-12-15 stsp ignorespath);
3216 886cec17 2019-12-15 stsp else {
3217 886cec17 2019-12-15 stsp fd = -1;
3218 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3219 886cec17 2019-12-15 stsp }
3220 886cec17 2019-12-15 stsp }
3221 886cec17 2019-12-15 stsp } else {
3222 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3223 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3224 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3225 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3226 886cec17 2019-12-15 stsp ignorespath);
3227 886cec17 2019-12-15 stsp } else
3228 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3229 886cec17 2019-12-15 stsp }
3230 6841da00 2019-08-08 stsp
3231 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3232 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3233 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3234 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3235 6841da00 2019-08-08 stsp free(ignorespath);
3236 6841da00 2019-08-08 stsp return err;
3237 f8d1f275 2019-02-04 stsp }
3238 f8d1f275 2019-02-04 stsp
3239 f8d1f275 2019-02-04 stsp static const struct got_error *
3240 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3241 f8d1f275 2019-02-04 stsp {
3242 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3243 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3244 f8d1f275 2019-02-04 stsp char *path = NULL;
3245 f8d1f275 2019-02-04 stsp
3246 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3247 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3248 0584f854 2019-04-06 stsp
3249 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3250 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3251 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3252 f8d1f275 2019-02-04 stsp } else {
3253 f8d1f275 2019-02-04 stsp path = de->d_name;
3254 f8d1f275 2019-02-04 stsp }
3255 f8d1f275 2019-02-04 stsp
3256 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3257 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3258 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3259 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3260 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3261 f8d1f275 2019-02-04 stsp if (parent_path[0])
3262 f8d1f275 2019-02-04 stsp free(path);
3263 b72f483a 2019-02-05 stsp return err;
3264 f8d1f275 2019-02-04 stsp }
3265 f8d1f275 2019-02-04 stsp
3266 347d1d3e 2019-07-12 stsp static const struct got_error *
3267 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3268 3143d852 2020-06-25 stsp {
3269 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3270 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3271 3143d852 2020-06-25 stsp
3272 3143d852 2020-06-25 stsp if (a->no_ignores)
3273 3143d852 2020-06-25 stsp return NULL;
3274 3143d852 2020-06-25 stsp
3275 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3276 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3277 3143d852 2020-06-25 stsp if (err)
3278 3143d852 2020-06-25 stsp return err;
3279 3143d852 2020-06-25 stsp
3280 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3281 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3282 3143d852 2020-06-25 stsp
3283 3143d852 2020-06-25 stsp return err;
3284 3143d852 2020-06-25 stsp }
3285 3143d852 2020-06-25 stsp
3286 3143d852 2020-06-25 stsp static const struct got_error *
3287 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3288 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3289 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3290 abb4604f 2019-07-27 stsp {
3291 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3292 abb4604f 2019-07-27 stsp struct stat sb;
3293 abb4604f 2019-07-27 stsp
3294 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3295 abb4604f 2019-07-27 stsp if (ie)
3296 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3297 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3298 abb4604f 2019-07-27 stsp
3299 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3300 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3301 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3302 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3303 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3304 abb4604f 2019-07-27 stsp return NULL;
3305 abb4604f 2019-07-27 stsp }
3306 abb4604f 2019-07-27 stsp
3307 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3308 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3309 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3310 abb4604f 2019-07-27 stsp
3311 abb4604f 2019-07-27 stsp return NULL;
3312 3143d852 2020-06-25 stsp }
3313 3143d852 2020-06-25 stsp
3314 3143d852 2020-06-25 stsp static const struct got_error *
3315 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3316 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3317 3143d852 2020-06-25 stsp {
3318 3143d852 2020-06-25 stsp const struct got_error *err;
3319 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3320 3143d852 2020-06-25 stsp
3321 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3322 3143d852 2020-06-25 stsp ".cvsignore");
3323 3143d852 2020-06-25 stsp if (err)
3324 3143d852 2020-06-25 stsp return err;
3325 3143d852 2020-06-25 stsp
3326 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3327 3143d852 2020-06-25 stsp ".gitignore");
3328 3143d852 2020-06-25 stsp if (err)
3329 3143d852 2020-06-25 stsp return err;
3330 3143d852 2020-06-25 stsp
3331 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3332 3143d852 2020-06-25 stsp if (err) {
3333 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3334 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3335 3143d852 2020-06-25 stsp return err;
3336 3143d852 2020-06-25 stsp }
3337 3143d852 2020-06-25 stsp for (;;) {
3338 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3339 3143d852 2020-06-25 stsp ".cvsignore");
3340 3143d852 2020-06-25 stsp if (err)
3341 3143d852 2020-06-25 stsp break;
3342 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3343 3143d852 2020-06-25 stsp ".gitignore");
3344 3143d852 2020-06-25 stsp if (err)
3345 3143d852 2020-06-25 stsp break;
3346 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3347 3143d852 2020-06-25 stsp if (err) {
3348 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3349 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3350 3143d852 2020-06-25 stsp break;
3351 3143d852 2020-06-25 stsp }
3352 b737c85a 2020-06-26 stsp free(parent_path);
3353 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3354 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3355 3143d852 2020-06-25 stsp }
3356 3143d852 2020-06-25 stsp
3357 b737c85a 2020-06-26 stsp free(parent_path);
3358 b737c85a 2020-06-26 stsp free(next_parent_path);
3359 3143d852 2020-06-25 stsp return err;
3360 abb4604f 2019-07-27 stsp }
3361 abb4604f 2019-07-27 stsp
3362 abb4604f 2019-07-27 stsp static const struct got_error *
3363 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3364 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3365 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3366 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3367 f2a9dc41 2019-12-13 tracey int report_unchanged)
3368 f8d1f275 2019-02-04 stsp {
3369 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3370 6fc93f37 2019-12-13 stsp int fd = -1;
3371 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3372 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3373 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3374 3143d852 2020-06-25 stsp
3375 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3376 f8d1f275 2019-02-04 stsp
3377 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3378 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3379 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3380 8dc303cc 2019-07-27 stsp
3381 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3382 6fc93f37 2019-12-13 stsp if (fd == -1) {
3383 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3384 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3385 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3386 abb4604f 2019-07-27 stsp else
3387 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3388 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3389 f2a9dc41 2019-12-13 tracey report_unchanged);
3390 abb4604f 2019-07-27 stsp } else {
3391 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3392 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3393 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3394 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3395 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3396 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3397 abb4604f 2019-07-27 stsp arg.status_path = path;
3398 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3399 abb4604f 2019-07-27 stsp arg.repo = repo;
3400 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3401 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3402 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3403 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3404 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3405 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3406 022fae89 2019-12-06 tracey if (!no_ignores) {
3407 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3408 3143d852 2020-06-25 stsp worktree->root_path, path);
3409 3143d852 2020-06-25 stsp if (err)
3410 3143d852 2020-06-25 stsp goto done;
3411 022fae89 2019-12-06 tracey }
3412 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3413 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3414 927df6b7 2019-02-10 stsp }
3415 3143d852 2020-06-25 stsp done:
3416 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3417 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3418 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3419 927df6b7 2019-02-10 stsp free(ondisk_path);
3420 347d1d3e 2019-07-12 stsp return err;
3421 347d1d3e 2019-07-12 stsp }
3422 347d1d3e 2019-07-12 stsp
3423 347d1d3e 2019-07-12 stsp const struct got_error *
3424 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3425 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3426 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3427 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3428 347d1d3e 2019-07-12 stsp {
3429 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3430 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3431 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3432 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3433 347d1d3e 2019-07-12 stsp
3434 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3435 347d1d3e 2019-07-12 stsp if (err)
3436 347d1d3e 2019-07-12 stsp return err;
3437 347d1d3e 2019-07-12 stsp
3438 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3439 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3440 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3441 72ea6654 2019-07-27 stsp if (err)
3442 72ea6654 2019-07-27 stsp break;
3443 72ea6654 2019-07-27 stsp }
3444 f8d1f275 2019-02-04 stsp free(fileindex_path);
3445 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3446 f8d1f275 2019-02-04 stsp return err;
3447 f8d1f275 2019-02-04 stsp }
3448 6c7ab921 2019-03-18 stsp
3449 6c7ab921 2019-03-18 stsp const struct got_error *
3450 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3451 6c7ab921 2019-03-18 stsp const char *arg)
3452 6c7ab921 2019-03-18 stsp {
3453 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3454 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3455 6c7ab921 2019-03-18 stsp size_t len;
3456 00bb5ea0 2020-07-23 stsp struct stat sb;
3457 6c7ab921 2019-03-18 stsp
3458 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3459 6c7ab921 2019-03-18 stsp
3460 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3461 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3462 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3463 00bb5ea0 2020-07-23 stsp
3464 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3465 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3466 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3467 00bb5ea0 2020-07-23 stsp goto done;
3468 00bb5ea0 2020-07-23 stsp }
3469 00bb5ea0 2020-07-23 stsp }
3470 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3471 00bb5ea0 2020-07-23 stsp /*
3472 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3473 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3474 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3475 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3476 00bb5ea0 2020-07-23 stsp */
3477 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3478 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3479 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3480 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3481 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3482 00bb5ea0 2020-07-23 stsp goto done;
3483 00bb5ea0 2020-07-23 stsp }
3484 00bb5ea0 2020-07-23 stsp
3485 00bb5ea0 2020-07-23 stsp }
3486 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3487 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3488 00bb5ea0 2020-07-23 stsp if (err)
3489 d0710d08 2019-07-22 stsp goto done;
3490 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3491 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3492 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3493 00bb5ea0 2020-07-23 stsp goto done;
3494 00bb5ea0 2020-07-23 stsp }
3495 00bb5ea0 2020-07-23 stsp } else {
3496 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3497 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3498 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3499 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3500 00bb5ea0 2020-07-23 stsp goto done;
3501 00bb5ea0 2020-07-23 stsp }
3502 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3503 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3504 00bb5ea0 2020-07-23 stsp goto done;
3505 00bb5ea0 2020-07-23 stsp }
3506 d0710d08 2019-07-22 stsp }
3507 d0710d08 2019-07-22 stsp }
3508 6c7ab921 2019-03-18 stsp
3509 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3510 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3511 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3512 6c7ab921 2019-03-18 stsp goto done;
3513 6c7ab921 2019-03-18 stsp }
3514 6c7ab921 2019-03-18 stsp
3515 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3516 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3517 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3518 f6d88e1a 2019-05-29 stsp if (err)
3519 f6d88e1a 2019-05-29 stsp goto done;
3520 f6d88e1a 2019-05-29 stsp } else {
3521 f6d88e1a 2019-05-29 stsp path = strdup("");
3522 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3523 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3524 f6d88e1a 2019-05-29 stsp goto done;
3525 f6d88e1a 2019-05-29 stsp }
3526 6c7ab921 2019-03-18 stsp }
3527 6c7ab921 2019-03-18 stsp
3528 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3529 6c7ab921 2019-03-18 stsp len = strlen(path);
3530 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3531 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3532 6c7ab921 2019-03-18 stsp len--;
3533 6c7ab921 2019-03-18 stsp }
3534 6c7ab921 2019-03-18 stsp done:
3535 6c7ab921 2019-03-18 stsp free(resolved);
3536 d0710d08 2019-07-22 stsp free(cwd);
3537 6c7ab921 2019-03-18 stsp if (err == NULL)
3538 6c7ab921 2019-03-18 stsp *wt_path = path;
3539 6c7ab921 2019-03-18 stsp else
3540 6c7ab921 2019-03-18 stsp free(path);
3541 d00136be 2019-03-26 stsp return err;
3542 d00136be 2019-03-26 stsp }
3543 d00136be 2019-03-26 stsp
3544 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3545 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3546 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3547 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3548 4e68cba3 2019-11-23 stsp void *progress_arg;
3549 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3550 4e68cba3 2019-11-23 stsp };
3551 4e68cba3 2019-11-23 stsp
3552 4e68cba3 2019-11-23 stsp static const struct got_error *
3553 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3554 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3555 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3556 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3557 07f5b47a 2019-06-02 stsp {
3558 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3559 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3560 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3561 6d022e97 2019-08-04 stsp struct stat sb;
3562 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3563 07f5b47a 2019-06-02 stsp
3564 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3565 dbb83fbd 2019-12-12 stsp relpath) == -1)
3566 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3567 dbb83fbd 2019-12-12 stsp
3568 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3569 6d022e97 2019-08-04 stsp if (ie) {
3570 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3571 12463d8b 2019-12-13 stsp de_name, a->repo);
3572 6d022e97 2019-08-04 stsp if (err)
3573 dbb83fbd 2019-12-12 stsp goto done;
3574 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3575 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3576 dbb83fbd 2019-12-12 stsp goto done;
3577 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3578 dbb83fbd 2019-12-12 stsp if (err)
3579 dbb83fbd 2019-12-12 stsp goto done;
3580 6d022e97 2019-08-04 stsp }
3581 07f5b47a 2019-06-02 stsp
3582 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3583 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3584 dbb83fbd 2019-12-12 stsp goto done;
3585 4e68cba3 2019-11-23 stsp }
3586 07f5b47a 2019-06-02 stsp
3587 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3588 4e68cba3 2019-11-23 stsp if (err)
3589 4e68cba3 2019-11-23 stsp goto done;
3590 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3591 3969253a 2020-03-07 stsp if (err) {
3592 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3593 3969253a 2020-03-07 stsp goto done;
3594 3969253a 2020-03-07 stsp }
3595 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3596 07f5b47a 2019-06-02 stsp if (err) {
3597 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3598 4e68cba3 2019-11-23 stsp goto done;
3599 07f5b47a 2019-06-02 stsp }
3600 4e68cba3 2019-11-23 stsp done:
3601 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3602 dbb83fbd 2019-12-12 stsp if (err)
3603 dbb83fbd 2019-12-12 stsp return err;
3604 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3605 dbb83fbd 2019-12-12 stsp return NULL;
3606 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3607 07f5b47a 2019-06-02 stsp }
3608 07f5b47a 2019-06-02 stsp
3609 d00136be 2019-03-26 stsp const struct got_error *
3610 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3611 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3612 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3613 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3614 d00136be 2019-03-26 stsp {
3615 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3616 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3617 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3618 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3619 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3620 d00136be 2019-03-26 stsp
3621 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3622 d00136be 2019-03-26 stsp if (err)
3623 d00136be 2019-03-26 stsp return err;
3624 d00136be 2019-03-26 stsp
3625 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3626 d00136be 2019-03-26 stsp if (err)
3627 d00136be 2019-03-26 stsp goto done;
3628 d00136be 2019-03-26 stsp
3629 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3630 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3631 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3632 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3633 4e68cba3 2019-11-23 stsp saa.repo = repo;
3634 4e68cba3 2019-11-23 stsp
3635 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3636 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3637 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3638 1dd54920 2019-05-11 stsp if (err)
3639 af12c6b9 2019-06-04 stsp break;
3640 1dd54920 2019-05-11 stsp }
3641 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3642 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3643 af12c6b9 2019-06-04 stsp err = sync_err;
3644 d00136be 2019-03-26 stsp done:
3645 fb399478 2019-07-12 stsp free(fileindex_path);
3646 d00136be 2019-03-26 stsp if (fileindex)
3647 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3648 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3649 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3650 d00136be 2019-03-26 stsp err = unlockerr;
3651 6c7ab921 2019-03-18 stsp return err;
3652 6c7ab921 2019-03-18 stsp }
3653 17ed4618 2019-06-02 stsp
3654 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3655 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3656 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3657 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3658 f2a9dc41 2019-12-13 tracey void *progress_arg;
3659 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3660 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3661 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3662 f2a9dc41 2019-12-13 tracey };
3663 f2a9dc41 2019-12-13 tracey
3664 f2a9dc41 2019-12-13 tracey static const struct got_error *
3665 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3666 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3667 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3668 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3669 17ed4618 2019-06-02 stsp {
3670 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3671 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3672 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3673 17ed4618 2019-06-02 stsp struct stat sb;
3674 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3675 17ed4618 2019-06-02 stsp
3676 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3677 17ed4618 2019-06-02 stsp if (ie == NULL)
3678 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3679 2ec1f75b 2019-03-26 stsp
3680 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3681 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3682 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3683 9acbc4fa 2019-08-03 stsp return NULL;
3684 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3685 9acbc4fa 2019-08-03 stsp }
3686 9acbc4fa 2019-08-03 stsp
3687 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3688 f2a9dc41 2019-12-13 tracey relpath) == -1)
3689 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3690 f2a9dc41 2019-12-13 tracey
3691 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3692 12463d8b 2019-12-13 stsp a->repo);
3693 17ed4618 2019-06-02 stsp if (err)
3694 f2a9dc41 2019-12-13 tracey goto done;
3695 17ed4618 2019-06-02 stsp
3696 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3697 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3698 f2a9dc41 2019-12-13 tracey goto done;
3699 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3700 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3701 f2a9dc41 2019-12-13 tracey goto done;
3702 f2a9dc41 2019-12-13 tracey }
3703 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3704 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3705 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3706 f2a9dc41 2019-12-13 tracey goto done;
3707 f2a9dc41 2019-12-13 tracey }
3708 17ed4618 2019-06-02 stsp }
3709 17ed4618 2019-06-02 stsp
3710 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3711 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3712 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3713 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3714 12463d8b 2019-12-13 stsp ondisk_path);
3715 12463d8b 2019-12-13 stsp goto done;
3716 12463d8b 2019-12-13 stsp }
3717 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3718 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3719 12463d8b 2019-12-13 stsp goto done;
3720 15341bfd 2020-03-05 tracey }
3721 15341bfd 2020-03-05 tracey
3722 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3723 15341bfd 2020-03-05 tracey
3724 15341bfd 2020-03-05 tracey if (parent == NULL) {
3725 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3726 15341bfd 2020-03-05 tracey goto done;
3727 15341bfd 2020-03-05 tracey }
3728 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3729 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3730 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3731 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3732 15341bfd 2020-03-05 tracey parent);
3733 15341bfd 2020-03-05 tracey break;
3734 15341bfd 2020-03-05 tracey }
3735 15341bfd 2020-03-05 tracey parent = dirname(parent);
3736 15341bfd 2020-03-05 tracey if (parent == NULL) {
3737 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3738 15341bfd 2020-03-05 tracey goto done;
3739 15341bfd 2020-03-05 tracey }
3740 12463d8b 2019-12-13 stsp }
3741 f2a9dc41 2019-12-13 tracey }
3742 17ed4618 2019-06-02 stsp
3743 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3744 f2a9dc41 2019-12-13 tracey done:
3745 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3746 f2a9dc41 2019-12-13 tracey if (err)
3747 f2a9dc41 2019-12-13 tracey return err;
3748 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3749 f2a9dc41 2019-12-13 tracey return NULL;
3750 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3751 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3752 17ed4618 2019-06-02 stsp }
3753 17ed4618 2019-06-02 stsp
3754 2ec1f75b 2019-03-26 stsp const struct got_error *
3755 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3756 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3757 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3758 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3759 2ec1f75b 2019-03-26 stsp {
3760 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3761 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3762 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3763 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3764 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3765 2ec1f75b 2019-03-26 stsp
3766 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3767 2ec1f75b 2019-03-26 stsp if (err)
3768 2ec1f75b 2019-03-26 stsp return err;
3769 2ec1f75b 2019-03-26 stsp
3770 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3771 2ec1f75b 2019-03-26 stsp if (err)
3772 2ec1f75b 2019-03-26 stsp goto done;
3773 f2a9dc41 2019-12-13 tracey
3774 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3775 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3776 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3777 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3778 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3779 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3780 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3781 2ec1f75b 2019-03-26 stsp
3782 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3783 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3784 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3785 17ed4618 2019-06-02 stsp if (err)
3786 af12c6b9 2019-06-04 stsp break;
3787 2ec1f75b 2019-03-26 stsp }
3788 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3789 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3790 af12c6b9 2019-06-04 stsp err = sync_err;
3791 2ec1f75b 2019-03-26 stsp done:
3792 fb399478 2019-07-12 stsp free(fileindex_path);
3793 2ec1f75b 2019-03-26 stsp if (fileindex)
3794 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3795 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3796 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3797 2ec1f75b 2019-03-26 stsp err = unlockerr;
3798 33aa809d 2019-08-08 stsp return err;
3799 33aa809d 2019-08-08 stsp }
3800 33aa809d 2019-08-08 stsp
3801 33aa809d 2019-08-08 stsp static const struct got_error *
3802 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3803 33aa809d 2019-08-08 stsp {
3804 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3805 33aa809d 2019-08-08 stsp char *line = NULL;
3806 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3807 33aa809d 2019-08-08 stsp ssize_t linelen;
3808 33aa809d 2019-08-08 stsp
3809 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3810 33aa809d 2019-08-08 stsp if (linelen == -1) {
3811 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3812 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3813 33aa809d 2019-08-08 stsp goto done;
3814 33aa809d 2019-08-08 stsp }
3815 33aa809d 2019-08-08 stsp return NULL;
3816 33aa809d 2019-08-08 stsp }
3817 33aa809d 2019-08-08 stsp if (outfile) {
3818 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3819 33aa809d 2019-08-08 stsp if (n != linelen) {
3820 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3821 33aa809d 2019-08-08 stsp goto done;
3822 33aa809d 2019-08-08 stsp }
3823 33aa809d 2019-08-08 stsp }
3824 33aa809d 2019-08-08 stsp if (rejectfile) {
3825 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3826 33aa809d 2019-08-08 stsp if (n != linelen)
3827 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3828 33aa809d 2019-08-08 stsp }
3829 33aa809d 2019-08-08 stsp done:
3830 33aa809d 2019-08-08 stsp free(line);
3831 2ec1f75b 2019-03-26 stsp return err;
3832 2ec1f75b 2019-03-26 stsp }
3833 1f1abb7e 2019-08-08 stsp
3834 33aa809d 2019-08-08 stsp static const struct got_error *
3835 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3836 33aa809d 2019-08-08 stsp {
3837 33aa809d 2019-08-08 stsp char *line = NULL;
3838 33aa809d 2019-08-08 stsp size_t linesize = 0;
3839 33aa809d 2019-08-08 stsp ssize_t linelen;
3840 33aa809d 2019-08-08 stsp
3841 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3842 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3843 500467ff 2019-09-25 hiltjo if (ferror(f))
3844 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3845 500467ff 2019-09-25 hiltjo return NULL;
3846 500467ff 2019-09-25 hiltjo }
3847 33aa809d 2019-08-08 stsp free(line);
3848 33aa809d 2019-08-08 stsp return NULL;
3849 33aa809d 2019-08-08 stsp }
3850 33aa809d 2019-08-08 stsp
3851 33aa809d 2019-08-08 stsp static const struct got_error *
3852 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3853 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3854 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3855 33aa809d 2019-08-08 stsp {
3856 33aa809d 2019-08-08 stsp const struct got_error *err;
3857 33aa809d 2019-08-08 stsp
3858 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3859 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3860 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3861 33aa809d 2019-08-08 stsp if (err)
3862 33aa809d 2019-08-08 stsp return err;
3863 33aa809d 2019-08-08 stsp (*line_cur1)++;
3864 33aa809d 2019-08-08 stsp }
3865 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3866 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3867 33aa809d 2019-08-08 stsp if (rejectfile)
3868 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3869 33aa809d 2019-08-08 stsp else
3870 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3871 33aa809d 2019-08-08 stsp if (err)
3872 33aa809d 2019-08-08 stsp return err;
3873 33aa809d 2019-08-08 stsp (*line_cur2)++;
3874 33aa809d 2019-08-08 stsp }
3875 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3876 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3877 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3878 33aa809d 2019-08-08 stsp if (err)
3879 33aa809d 2019-08-08 stsp return err;
3880 33aa809d 2019-08-08 stsp (*line_cur2)++;
3881 33aa809d 2019-08-08 stsp }
3882 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3883 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3884 33aa809d 2019-08-08 stsp if (rejectfile)
3885 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3886 33aa809d 2019-08-08 stsp else
3887 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3888 33aa809d 2019-08-08 stsp if (err)
3889 33aa809d 2019-08-08 stsp return err;
3890 33aa809d 2019-08-08 stsp (*line_cur1)++;
3891 33aa809d 2019-08-08 stsp }
3892 f1e81a05 2019-08-10 stsp
3893 f1e81a05 2019-08-10 stsp return NULL;
3894 f1e81a05 2019-08-10 stsp }
3895 f1e81a05 2019-08-10 stsp
3896 f1e81a05 2019-08-10 stsp static const struct got_error *
3897 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3898 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3899 f1e81a05 2019-08-10 stsp {
3900 f1e81a05 2019-08-10 stsp const struct got_error *err;
3901 f1e81a05 2019-08-10 stsp
3902 f1e81a05 2019-08-10 stsp if (outfile) {
3903 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3904 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3905 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3906 f1e81a05 2019-08-10 stsp if (err)
3907 f1e81a05 2019-08-10 stsp return err;
3908 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3909 f1e81a05 2019-08-10 stsp }
3910 f1e81a05 2019-08-10 stsp }
3911 f1e81a05 2019-08-10 stsp if (rejectfile) {
3912 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3913 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3914 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3915 f1e81a05 2019-08-10 stsp if (err)
3916 f1e81a05 2019-08-10 stsp return err;
3917 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3918 f1e81a05 2019-08-10 stsp }
3919 33aa809d 2019-08-08 stsp }
3920 33aa809d 2019-08-08 stsp
3921 33aa809d 2019-08-08 stsp return NULL;
3922 33aa809d 2019-08-08 stsp }
3923 33aa809d 2019-08-08 stsp
3924 33aa809d 2019-08-08 stsp static const struct got_error *
3925 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3926 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3927 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3928 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3929 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3930 33aa809d 2019-08-08 stsp {
3931 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3932 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3933 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3934 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3935 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3936 33aa809d 2019-08-08 stsp long pos1, pos2;
3937 33aa809d 2019-08-08 stsp FILE *hunkfile;
3938 33aa809d 2019-08-08 stsp
3939 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3940 33aa809d 2019-08-08 stsp
3941 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3942 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3943 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3944 33aa809d 2019-08-08 stsp
3945 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3946 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3947 33aa809d 2019-08-08 stsp
3948 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3949 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3950 33aa809d 2019-08-08 stsp
3951 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3952 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3953 33aa809d 2019-08-08 stsp goto done;
3954 33aa809d 2019-08-08 stsp }
3955 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3956 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3957 33aa809d 2019-08-08 stsp goto done;
3958 33aa809d 2019-08-08 stsp }
3959 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3960 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3961 33aa809d 2019-08-08 stsp goto done;
3962 33aa809d 2019-08-08 stsp }
3963 33aa809d 2019-08-08 stsp
3964 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3965 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3966 33aa809d 2019-08-08 stsp if (err)
3967 33aa809d 2019-08-08 stsp goto done;
3968 33aa809d 2019-08-08 stsp
3969 33aa809d 2019-08-08 stsp switch (*choice) {
3970 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3971 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3972 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3973 33aa809d 2019-08-08 stsp break;
3974 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3975 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3976 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3977 33aa809d 2019-08-08 stsp break;
3978 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3979 33aa809d 2019-08-08 stsp break;
3980 33aa809d 2019-08-08 stsp default:
3981 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3982 33aa809d 2019-08-08 stsp break;
3983 33aa809d 2019-08-08 stsp }
3984 33aa809d 2019-08-08 stsp done:
3985 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3986 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3987 33aa809d 2019-08-08 stsp return err;
3988 33aa809d 2019-08-08 stsp }
3989 33aa809d 2019-08-08 stsp
3990 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3991 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3992 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3993 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3994 1f1abb7e 2019-08-08 stsp void *progress_arg;
3995 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3996 33aa809d 2019-08-08 stsp void *patch_arg;
3997 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3998 1f1abb7e 2019-08-08 stsp };
3999 a129376b 2019-03-28 stsp
4000 e20a8b6f 2019-06-04 stsp static const struct got_error *
4001 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4002 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4003 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4004 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4005 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4006 33aa809d 2019-08-08 stsp {
4007 33aa809d 2019-08-08 stsp const struct got_error *err;
4008 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4009 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4010 1ebedb77 2019-10-19 stsp int fd2 = -1;
4011 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4012 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4013 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4014 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4015 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4016 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4017 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4018 33aa809d 2019-08-08 stsp int n = 0;
4019 33aa809d 2019-08-08 stsp
4020 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4021 33aa809d 2019-08-08 stsp
4022 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4023 33aa809d 2019-08-08 stsp if (err)
4024 33aa809d 2019-08-08 stsp return err;
4025 33aa809d 2019-08-08 stsp
4026 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4027 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4028 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4029 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
4030 12463d8b 2019-12-13 stsp goto done;
4031 12463d8b 2019-12-13 stsp }
4032 12463d8b 2019-12-13 stsp } else {
4033 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4034 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4035 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4036 12463d8b 2019-12-13 stsp goto done;
4037 12463d8b 2019-12-13 stsp }
4038 1ebedb77 2019-10-19 stsp }
4039 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4040 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4041 1ebedb77 2019-10-19 stsp goto done;
4042 1ebedb77 2019-10-19 stsp }
4043 1ebedb77 2019-10-19 stsp
4044 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4045 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4046 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4047 33aa809d 2019-08-08 stsp goto done;
4048 33aa809d 2019-08-08 stsp }
4049 1ebedb77 2019-10-19 stsp fd2 = -1;
4050 33aa809d 2019-08-08 stsp
4051 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4052 33aa809d 2019-08-08 stsp if (err)
4053 33aa809d 2019-08-08 stsp goto done;
4054 33aa809d 2019-08-08 stsp
4055 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4056 33aa809d 2019-08-08 stsp if (err)
4057 33aa809d 2019-08-08 stsp goto done;
4058 33aa809d 2019-08-08 stsp
4059 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4060 33aa809d 2019-08-08 stsp if (err)
4061 33aa809d 2019-08-08 stsp goto done;
4062 33aa809d 2019-08-08 stsp
4063 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4064 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4065 33aa809d 2019-08-08 stsp goto done;
4066 33aa809d 2019-08-08 stsp }
4067 33aa809d 2019-08-08 stsp
4068 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4069 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4070 33aa809d 2019-08-08 stsp if (err)
4071 33aa809d 2019-08-08 stsp goto done;
4072 33aa809d 2019-08-08 stsp
4073 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4074 33aa809d 2019-08-08 stsp if (err)
4075 33aa809d 2019-08-08 stsp goto done;
4076 33aa809d 2019-08-08 stsp
4077 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4078 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4079 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4080 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4081 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4082 33aa809d 2019-08-08 stsp int choice;
4083 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4084 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4085 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4086 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4087 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4088 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4089 33aa809d 2019-08-08 stsp if (err)
4090 33aa809d 2019-08-08 stsp goto done;
4091 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4092 33aa809d 2019-08-08 stsp have_content = 1;
4093 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4094 33aa809d 2019-08-08 stsp break;
4095 33aa809d 2019-08-08 stsp }
4096 1ebedb77 2019-10-19 stsp if (have_content) {
4097 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4098 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4099 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4100 1ebedb77 2019-10-19 stsp if (err)
4101 1ebedb77 2019-10-19 stsp goto done;
4102 1ebedb77 2019-10-19 stsp
4103 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4104 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4105 1ebedb77 2019-10-19 stsp goto done;
4106 1ebedb77 2019-10-19 stsp }
4107 1ebedb77 2019-10-19 stsp }
4108 33aa809d 2019-08-08 stsp done:
4109 33aa809d 2019-08-08 stsp free(id_str);
4110 33aa809d 2019-08-08 stsp if (blob)
4111 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4112 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4113 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4114 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4115 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4116 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4117 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4118 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4119 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4120 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4121 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4122 33aa809d 2019-08-08 stsp if (err || !have_content) {
4123 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4124 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4125 33aa809d 2019-08-08 stsp free(*path_outfile);
4126 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4127 33aa809d 2019-08-08 stsp }
4128 33aa809d 2019-08-08 stsp free(args);
4129 33aa809d 2019-08-08 stsp if (ds) {
4130 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4131 33aa809d 2019-08-08 stsp free(ds);
4132 33aa809d 2019-08-08 stsp }
4133 33aa809d 2019-08-08 stsp if (changes)
4134 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4135 33aa809d 2019-08-08 stsp free(path1);
4136 33aa809d 2019-08-08 stsp return err;
4137 33aa809d 2019-08-08 stsp }
4138 33aa809d 2019-08-08 stsp
4139 33aa809d 2019-08-08 stsp static const struct got_error *
4140 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4141 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4142 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4143 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4144 a129376b 2019-03-28 stsp {
4145 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4146 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4147 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4148 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4149 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4150 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4151 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4152 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4153 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4154 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4155 a129376b 2019-03-28 stsp
4156 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4157 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4158 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4159 d3bcc3d1 2019-08-08 stsp return NULL;
4160 3d69ad8d 2019-08-17 semarie
4161 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4162 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4163 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4164 d3bcc3d1 2019-08-08 stsp
4165 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4166 65084dad 2019-08-08 stsp if (ie == NULL)
4167 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4168 a129376b 2019-03-28 stsp
4169 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4170 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4171 a129376b 2019-03-28 stsp if (err) {
4172 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4173 a129376b 2019-03-28 stsp goto done;
4174 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4175 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4176 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4177 e20a8b6f 2019-06-04 stsp goto done;
4178 e20a8b6f 2019-06-04 stsp }
4179 a129376b 2019-03-28 stsp }
4180 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4181 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4182 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4183 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4184 a129376b 2019-03-28 stsp goto done;
4185 a129376b 2019-03-28 stsp }
4186 a129376b 2019-03-28 stsp } else {
4187 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4188 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4189 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4190 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4191 a129376b 2019-03-28 stsp goto done;
4192 a129376b 2019-03-28 stsp }
4193 a129376b 2019-03-28 stsp } else {
4194 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4195 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4196 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4197 a129376b 2019-03-28 stsp goto done;
4198 a129376b 2019-03-28 stsp }
4199 a129376b 2019-03-28 stsp }
4200 a129376b 2019-03-28 stsp }
4201 a129376b 2019-03-28 stsp
4202 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4203 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4204 a9fa2909 2019-07-27 stsp if (err) {
4205 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4206 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4207 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4208 a9fa2909 2019-07-27 stsp goto done;
4209 a9fa2909 2019-07-27 stsp } else {
4210 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4211 a9fa2909 2019-07-27 stsp if (err)
4212 a9fa2909 2019-07-27 stsp goto done;
4213 a9fa2909 2019-07-27 stsp
4214 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4215 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4216 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4217 a9fa2909 2019-07-27 stsp goto done;
4218 a9fa2909 2019-07-27 stsp }
4219 a9fa2909 2019-07-27 stsp
4220 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4221 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4222 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4223 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4224 a9fa2909 2019-07-27 stsp goto done;
4225 a9fa2909 2019-07-27 stsp }
4226 a129376b 2019-03-28 stsp }
4227 a129376b 2019-03-28 stsp
4228 a129376b 2019-03-28 stsp switch (status) {
4229 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4230 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4231 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4232 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4233 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4234 33aa809d 2019-08-08 stsp if (err)
4235 33aa809d 2019-08-08 stsp goto done;
4236 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4237 33aa809d 2019-08-08 stsp break;
4238 33aa809d 2019-08-08 stsp }
4239 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4240 1f1abb7e 2019-08-08 stsp ie->path);
4241 1ee397ad 2019-07-12 stsp if (err)
4242 1ee397ad 2019-07-12 stsp goto done;
4243 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4244 a129376b 2019-03-28 stsp break;
4245 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4246 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4247 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4248 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4249 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4250 33aa809d 2019-08-08 stsp if (err)
4251 33aa809d 2019-08-08 stsp goto done;
4252 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4253 33aa809d 2019-08-08 stsp break;
4254 33aa809d 2019-08-08 stsp }
4255 33aa809d 2019-08-08 stsp /* fall through */
4256 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4257 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4258 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4259 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4260 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4261 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4262 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4263 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4264 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4265 24278f30 2019-08-03 stsp } else
4266 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4267 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4268 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4269 a129376b 2019-03-28 stsp if (err)
4270 65084dad 2019-08-08 stsp goto done;
4271 65084dad 2019-08-08 stsp
4272 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4273 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4274 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4275 a129376b 2019-03-28 stsp goto done;
4276 65084dad 2019-08-08 stsp }
4277 65084dad 2019-08-08 stsp
4278 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4279 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4280 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4281 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4282 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4283 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4284 33aa809d 2019-08-08 stsp break;
4285 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4286 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4287 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4288 33aa809d 2019-08-08 stsp goto done;
4289 33aa809d 2019-08-08 stsp }
4290 33aa809d 2019-08-08 stsp } else {
4291 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4292 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4293 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4294 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4295 2e1fa222 2020-07-23 stsp blob, 0, 1, a->repo,
4296 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4297 2e1fa222 2020-07-23 stsp } else {
4298 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4299 2e1fa222 2020-07-23 stsp ie->path,
4300 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4301 2e1fa222 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4302 2e1fa222 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
4303 2e1fa222 2020-07-23 stsp }
4304 a129376b 2019-03-28 stsp if (err)
4305 a129376b 2019-03-28 stsp goto done;
4306 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4307 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4308 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4309 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4310 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4311 2e1fa222 2020-07-23 stsp if (err)
4312 2e1fa222 2020-07-23 stsp goto done;
4313 2e1fa222 2020-07-23 stsp }
4314 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4315 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
4316 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4317 33aa809d 2019-08-08 stsp if (err)
4318 33aa809d 2019-08-08 stsp goto done;
4319 33aa809d 2019-08-08 stsp }
4320 a129376b 2019-03-28 stsp }
4321 a129376b 2019-03-28 stsp break;
4322 e20a8b6f 2019-06-04 stsp }
4323 a129376b 2019-03-28 stsp default:
4324 1f1abb7e 2019-08-08 stsp break;
4325 a129376b 2019-03-28 stsp }
4326 a129376b 2019-03-28 stsp done:
4327 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4328 33aa809d 2019-08-08 stsp free(path_content);
4329 e20a8b6f 2019-06-04 stsp free(parent_path);
4330 a129376b 2019-03-28 stsp free(tree_path);
4331 a129376b 2019-03-28 stsp if (blob)
4332 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4333 a129376b 2019-03-28 stsp if (tree)
4334 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4335 a129376b 2019-03-28 stsp free(tree_id);
4336 e20a8b6f 2019-06-04 stsp return err;
4337 e20a8b6f 2019-06-04 stsp }
4338 e20a8b6f 2019-06-04 stsp
4339 e20a8b6f 2019-06-04 stsp const struct got_error *
4340 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4341 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4342 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4343 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4344 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4345 e20a8b6f 2019-06-04 stsp {
4346 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4347 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4348 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4349 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4350 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4351 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4352 e20a8b6f 2019-06-04 stsp
4353 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4354 e20a8b6f 2019-06-04 stsp if (err)
4355 e20a8b6f 2019-06-04 stsp return err;
4356 e20a8b6f 2019-06-04 stsp
4357 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4358 e20a8b6f 2019-06-04 stsp if (err)
4359 e20a8b6f 2019-06-04 stsp goto done;
4360 e20a8b6f 2019-06-04 stsp
4361 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4362 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4363 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4364 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4365 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4366 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4367 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4368 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4369 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4370 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4371 e20a8b6f 2019-06-04 stsp if (err)
4372 af12c6b9 2019-06-04 stsp break;
4373 e20a8b6f 2019-06-04 stsp }
4374 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4375 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4376 e20a8b6f 2019-06-04 stsp err = sync_err;
4377 af12c6b9 2019-06-04 stsp done:
4378 fb399478 2019-07-12 stsp free(fileindex_path);
4379 a129376b 2019-03-28 stsp if (fileindex)
4380 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4381 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4382 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4383 a129376b 2019-03-28 stsp err = unlockerr;
4384 c4296144 2019-05-09 stsp return err;
4385 c4296144 2019-05-09 stsp }
4386 c4296144 2019-05-09 stsp
4387 cf066bf8 2019-05-09 stsp static void
4388 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4389 cf066bf8 2019-05-09 stsp {
4390 24519714 2019-05-09 stsp free(ct->path);
4391 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4392 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4393 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4394 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4395 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4396 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4397 cf066bf8 2019-05-09 stsp free(ct);
4398 cf066bf8 2019-05-09 stsp }
4399 24519714 2019-05-09 stsp
4400 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4401 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4402 24519714 2019-05-09 stsp struct got_repository *repo;
4403 24519714 2019-05-09 stsp struct got_worktree *worktree;
4404 5f8a88c6 2019-08-03 stsp int have_staged_files;
4405 24519714 2019-05-09 stsp };
4406 cf066bf8 2019-05-09 stsp
4407 c4296144 2019-05-09 stsp static const struct got_error *
4408 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4409 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4410 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4411 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4412 c4296144 2019-05-09 stsp {
4413 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4414 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4415 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4416 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4417 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4418 768aea60 2019-05-09 stsp struct stat sb;
4419 c4296144 2019-05-09 stsp
4420 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4421 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4422 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4423 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4424 5f8a88c6 2019-08-03 stsp return NULL;
4425 5f8a88c6 2019-08-03 stsp } else {
4426 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4427 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4428 c4296144 2019-05-09 stsp
4429 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4430 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4431 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4432 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4433 5f8a88c6 2019-08-03 stsp return NULL;
4434 5f8a88c6 2019-08-03 stsp }
4435 0b5cc0d6 2019-05-09 stsp
4436 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4438 036813ee 2019-05-09 stsp goto done;
4439 036813ee 2019-05-09 stsp }
4440 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4441 036813ee 2019-05-09 stsp parent_path = strdup("");
4442 69960a46 2019-05-09 stsp if (parent_path == NULL)
4443 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4444 69960a46 2019-05-09 stsp } else {
4445 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4446 69960a46 2019-05-09 stsp if (err)
4447 69960a46 2019-05-09 stsp return err;
4448 69960a46 2019-05-09 stsp }
4449 c4296144 2019-05-09 stsp
4450 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4451 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4452 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4453 c4296144 2019-05-09 stsp goto done;
4454 768aea60 2019-05-09 stsp }
4455 768aea60 2019-05-09 stsp
4456 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4457 768aea60 2019-05-09 stsp relpath) == -1) {
4458 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4459 768aea60 2019-05-09 stsp goto done;
4460 768aea60 2019-05-09 stsp }
4461 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4462 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4463 768aea60 2019-05-09 stsp } else {
4464 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4465 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4466 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4467 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4468 12463d8b 2019-12-13 stsp ct->ondisk_path);
4469 12463d8b 2019-12-13 stsp goto done;
4470 12463d8b 2019-12-13 stsp }
4471 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4472 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4473 768aea60 2019-05-09 stsp goto done;
4474 768aea60 2019-05-09 stsp }
4475 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4476 c4296144 2019-05-09 stsp }
4477 c4296144 2019-05-09 stsp
4478 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4479 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4480 44d03001 2019-05-09 stsp relpath) == -1) {
4481 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4482 44d03001 2019-05-09 stsp goto done;
4483 44d03001 2019-05-09 stsp }
4484 44d03001 2019-05-09 stsp
4485 cf066bf8 2019-05-09 stsp ct->status = status;
4486 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4487 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4488 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4489 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4490 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4491 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4492 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4493 b416585c 2019-05-13 stsp goto done;
4494 b416585c 2019-05-13 stsp }
4495 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4496 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4497 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4498 f0b75401 2019-08-03 stsp goto done;
4499 f0b75401 2019-08-03 stsp }
4500 f0b75401 2019-08-03 stsp }
4501 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4502 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4503 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4504 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4505 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4506 036813ee 2019-05-09 stsp goto done;
4507 036813ee 2019-05-09 stsp }
4508 ca2503ea 2019-05-09 stsp }
4509 24519714 2019-05-09 stsp ct->path = strdup(path);
4510 24519714 2019-05-09 stsp if (ct->path == NULL) {
4511 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4512 24519714 2019-05-09 stsp goto done;
4513 24519714 2019-05-09 stsp }
4514 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4515 c4296144 2019-05-09 stsp done:
4516 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4517 ed175427 2019-05-09 stsp free_commitable(ct);
4518 24519714 2019-05-09 stsp free(parent_path);
4519 036813ee 2019-05-09 stsp free(path);
4520 a129376b 2019-03-28 stsp return err;
4521 a129376b 2019-03-28 stsp }
4522 c4296144 2019-05-09 stsp
4523 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4524 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4525 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4526 036813ee 2019-05-09 stsp struct got_repository *);
4527 ed175427 2019-05-09 stsp
4528 ed175427 2019-05-09 stsp static const struct got_error *
4529 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4530 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4531 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4532 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4533 afa376bf 2019-05-09 stsp struct got_repository *repo)
4534 ed175427 2019-05-09 stsp {
4535 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4536 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4537 036813ee 2019-05-09 stsp char *subpath;
4538 ed175427 2019-05-09 stsp
4539 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4540 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4541 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4542 ed175427 2019-05-09 stsp
4543 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4544 ed175427 2019-05-09 stsp if (err)
4545 ed175427 2019-05-09 stsp return err;
4546 ed175427 2019-05-09 stsp
4547 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4548 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4549 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4550 036813ee 2019-05-09 stsp free(subpath);
4551 036813ee 2019-05-09 stsp return err;
4552 036813ee 2019-05-09 stsp }
4553 ed175427 2019-05-09 stsp
4554 036813ee 2019-05-09 stsp static const struct got_error *
4555 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4556 036813ee 2019-05-09 stsp {
4557 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4558 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4559 ed175427 2019-05-09 stsp
4560 036813ee 2019-05-09 stsp *match = 0;
4561 ed175427 2019-05-09 stsp
4562 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4563 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4564 0f63689d 2019-05-10 stsp return NULL;
4565 036813ee 2019-05-09 stsp }
4566 0b5cc0d6 2019-05-09 stsp
4567 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4568 0f63689d 2019-05-10 stsp if (err)
4569 0f63689d 2019-05-10 stsp return err;
4570 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4571 036813ee 2019-05-09 stsp free(ct_parent_path);
4572 036813ee 2019-05-09 stsp return err;
4573 036813ee 2019-05-09 stsp }
4574 0b5cc0d6 2019-05-09 stsp
4575 768aea60 2019-05-09 stsp static mode_t
4576 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4577 768aea60 2019-05-09 stsp {
4578 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4579 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4580 3d9a4ec4 2020-07-23 stsp
4581 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4582 768aea60 2019-05-09 stsp }
4583 768aea60 2019-05-09 stsp
4584 036813ee 2019-05-09 stsp static const struct got_error *
4585 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4586 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4587 036813ee 2019-05-09 stsp {
4588 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4589 ca2503ea 2019-05-09 stsp
4590 036813ee 2019-05-09 stsp *new_te = NULL;
4591 0b5cc0d6 2019-05-09 stsp
4592 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4593 036813ee 2019-05-09 stsp if (err)
4594 036813ee 2019-05-09 stsp goto done;
4595 0b5cc0d6 2019-05-09 stsp
4596 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4597 036813ee 2019-05-09 stsp
4598 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4599 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4600 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4601 5f8a88c6 2019-08-03 stsp else
4602 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4603 036813ee 2019-05-09 stsp done:
4604 036813ee 2019-05-09 stsp if (err && *new_te) {
4605 56e0773d 2019-11-28 stsp free(*new_te);
4606 036813ee 2019-05-09 stsp *new_te = NULL;
4607 036813ee 2019-05-09 stsp }
4608 036813ee 2019-05-09 stsp return err;
4609 036813ee 2019-05-09 stsp }
4610 036813ee 2019-05-09 stsp
4611 036813ee 2019-05-09 stsp static const struct got_error *
4612 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4613 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4614 036813ee 2019-05-09 stsp {
4615 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4616 036813ee 2019-05-09 stsp char *ct_name;
4617 036813ee 2019-05-09 stsp
4618 036813ee 2019-05-09 stsp *new_te = NULL;
4619 036813ee 2019-05-09 stsp
4620 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4621 036813ee 2019-05-09 stsp if (*new_te == NULL)
4622 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4623 036813ee 2019-05-09 stsp
4624 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4625 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4626 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4627 036813ee 2019-05-09 stsp goto done;
4628 036813ee 2019-05-09 stsp }
4629 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4630 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4631 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4632 036813ee 2019-05-09 stsp goto done;
4633 036813ee 2019-05-09 stsp }
4634 036813ee 2019-05-09 stsp
4635 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4636 036813ee 2019-05-09 stsp
4637 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4638 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4639 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4640 5f8a88c6 2019-08-03 stsp else
4641 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4642 036813ee 2019-05-09 stsp done:
4643 036813ee 2019-05-09 stsp if (err && *new_te) {
4644 56e0773d 2019-11-28 stsp free(*new_te);
4645 036813ee 2019-05-09 stsp *new_te = NULL;
4646 036813ee 2019-05-09 stsp }
4647 036813ee 2019-05-09 stsp return err;
4648 036813ee 2019-05-09 stsp }
4649 036813ee 2019-05-09 stsp
4650 036813ee 2019-05-09 stsp static const struct got_error *
4651 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4652 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4653 036813ee 2019-05-09 stsp {
4654 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4655 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4656 036813ee 2019-05-09 stsp
4657 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4658 036813ee 2019-05-09 stsp if (err)
4659 036813ee 2019-05-09 stsp return err;
4660 036813ee 2019-05-09 stsp if (new_pe == NULL)
4661 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4662 036813ee 2019-05-09 stsp return NULL;
4663 afa376bf 2019-05-09 stsp }
4664 afa376bf 2019-05-09 stsp
4665 afa376bf 2019-05-09 stsp static const struct got_error *
4666 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4668 afa376bf 2019-05-09 stsp {
4669 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4670 5f8a88c6 2019-08-03 stsp unsigned char status;
4671 5f8a88c6 2019-08-03 stsp
4672 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4673 afa376bf 2019-05-09 stsp ct_path++;
4674 5f8a88c6 2019-08-03 stsp
4675 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4676 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4677 5f8a88c6 2019-08-03 stsp else
4678 5f8a88c6 2019-08-03 stsp status = ct->status;
4679 5f8a88c6 2019-08-03 stsp
4680 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4681 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4682 036813ee 2019-05-09 stsp }
4683 036813ee 2019-05-09 stsp
4684 036813ee 2019-05-09 stsp static const struct got_error *
4685 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4686 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4687 44d03001 2019-05-09 stsp {
4688 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4689 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4690 44d03001 2019-05-09 stsp char *te_path;
4691 44d03001 2019-05-09 stsp
4692 44d03001 2019-05-09 stsp *modified = 0;
4693 44d03001 2019-05-09 stsp
4694 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4695 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4696 44d03001 2019-05-09 stsp te->name) == -1)
4697 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4698 44d03001 2019-05-09 stsp
4699 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4700 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4701 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4702 44d03001 2019-05-09 stsp strlen(te_path));
4703 44d03001 2019-05-09 stsp if (*modified)
4704 44d03001 2019-05-09 stsp break;
4705 44d03001 2019-05-09 stsp }
4706 44d03001 2019-05-09 stsp
4707 44d03001 2019-05-09 stsp free(te_path);
4708 44d03001 2019-05-09 stsp return err;
4709 44d03001 2019-05-09 stsp }
4710 44d03001 2019-05-09 stsp
4711 44d03001 2019-05-09 stsp static const struct got_error *
4712 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4713 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4714 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4715 036813ee 2019-05-09 stsp {
4716 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4717 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4718 036813ee 2019-05-09 stsp
4719 036813ee 2019-05-09 stsp *ctp = NULL;
4720 036813ee 2019-05-09 stsp
4721 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4722 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4723 036813ee 2019-05-09 stsp char *ct_name = NULL;
4724 036813ee 2019-05-09 stsp int path_matches;
4725 036813ee 2019-05-09 stsp
4726 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4727 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4728 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4729 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4730 5f8a88c6 2019-08-03 stsp continue;
4731 5f8a88c6 2019-08-03 stsp } else {
4732 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4733 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4734 5f8a88c6 2019-08-03 stsp continue;
4735 5f8a88c6 2019-08-03 stsp }
4736 036813ee 2019-05-09 stsp
4737 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4738 036813ee 2019-05-09 stsp continue;
4739 036813ee 2019-05-09 stsp
4740 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4741 036813ee 2019-05-09 stsp if (err)
4742 036813ee 2019-05-09 stsp return err;
4743 036813ee 2019-05-09 stsp if (!path_matches)
4744 036813ee 2019-05-09 stsp continue;
4745 036813ee 2019-05-09 stsp
4746 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4747 036813ee 2019-05-09 stsp if (ct_name == NULL)
4748 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4749 036813ee 2019-05-09 stsp
4750 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4751 036813ee 2019-05-09 stsp continue;
4752 036813ee 2019-05-09 stsp
4753 036813ee 2019-05-09 stsp *ctp = ct;
4754 036813ee 2019-05-09 stsp break;
4755 036813ee 2019-05-09 stsp }
4756 2b496619 2019-07-10 stsp
4757 2b496619 2019-07-10 stsp return err;
4758 2b496619 2019-07-10 stsp }
4759 2b496619 2019-07-10 stsp
4760 2b496619 2019-07-10 stsp static const struct got_error *
4761 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4762 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4763 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4764 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4765 2b496619 2019-07-10 stsp struct got_repository *repo)
4766 2b496619 2019-07-10 stsp {
4767 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4768 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4769 2b496619 2019-07-10 stsp char *subtree_path;
4770 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4771 ba580f68 2020-03-22 stsp int nentries;
4772 2b496619 2019-07-10 stsp
4773 2b496619 2019-07-10 stsp *new_tep = NULL;
4774 2b496619 2019-07-10 stsp
4775 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4776 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4777 2b496619 2019-07-10 stsp child_path) == -1)
4778 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4779 036813ee 2019-05-09 stsp
4780 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4781 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4782 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4783 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4784 56e0773d 2019-11-28 stsp
4785 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4786 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4787 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4788 2b496619 2019-07-10 stsp goto done;
4789 2b496619 2019-07-10 stsp }
4790 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4791 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4792 2b496619 2019-07-10 stsp if (err) {
4793 56e0773d 2019-11-28 stsp free(new_te);
4794 2b496619 2019-07-10 stsp goto done;
4795 2b496619 2019-07-10 stsp }
4796 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4797 2b496619 2019-07-10 stsp done:
4798 56e0773d 2019-11-28 stsp free(id);
4799 2b496619 2019-07-10 stsp free(subtree_path);
4800 2b496619 2019-07-10 stsp if (err == NULL)
4801 2b496619 2019-07-10 stsp *new_tep = new_te;
4802 036813ee 2019-05-09 stsp return err;
4803 036813ee 2019-05-09 stsp }
4804 036813ee 2019-05-09 stsp
4805 036813ee 2019-05-09 stsp static const struct got_error *
4806 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4807 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4808 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4809 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4810 036813ee 2019-05-09 stsp struct got_repository *repo)
4811 036813ee 2019-05-09 stsp {
4812 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4813 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4814 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4815 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4816 036813ee 2019-05-09 stsp
4817 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4818 ba580f68 2020-03-22 stsp *nentries = 0;
4819 036813ee 2019-05-09 stsp
4820 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4821 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4822 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4823 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4824 036813ee 2019-05-09 stsp
4825 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4826 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4827 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4828 036813ee 2019-05-09 stsp continue;
4829 036813ee 2019-05-09 stsp
4830 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4831 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4832 036813ee 2019-05-09 stsp continue;
4833 036813ee 2019-05-09 stsp
4834 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4835 036813ee 2019-05-09 stsp pe->path);
4836 036813ee 2019-05-09 stsp if (err)
4837 036813ee 2019-05-09 stsp goto done;
4838 036813ee 2019-05-09 stsp
4839 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4840 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4841 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4842 036813ee 2019-05-09 stsp if (err)
4843 036813ee 2019-05-09 stsp goto done;
4844 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4845 afa376bf 2019-05-09 stsp if (err)
4846 afa376bf 2019-05-09 stsp goto done;
4847 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4848 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4849 2b496619 2019-07-10 stsp if (err)
4850 2f51b5b3 2019-05-09 stsp goto done;
4851 ba580f68 2020-03-22 stsp (*nentries)++;
4852 2b496619 2019-07-10 stsp } else {
4853 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4854 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4855 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4856 2b496619 2019-07-10 stsp == NULL) {
4857 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4858 2b496619 2019-07-10 stsp child_path, path_base_tree,
4859 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4860 2b496619 2019-07-10 stsp repo);
4861 2b496619 2019-07-10 stsp if (err)
4862 2b496619 2019-07-10 stsp goto done;
4863 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4864 2b496619 2019-07-10 stsp if (err)
4865 2b496619 2019-07-10 stsp goto done;
4866 ba580f68 2020-03-22 stsp (*nentries)++;
4867 9ba0479c 2019-05-10 stsp }
4868 ed175427 2019-05-09 stsp }
4869 2f51b5b3 2019-05-09 stsp }
4870 2f51b5b3 2019-05-09 stsp
4871 2f51b5b3 2019-05-09 stsp if (base_tree) {
4872 56e0773d 2019-11-28 stsp int i, nbase_entries;
4873 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4874 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4875 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4876 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4877 63c5ca5d 2019-08-24 stsp
4878 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4879 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4880 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4881 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4882 63c5ca5d 2019-08-24 stsp if (err)
4883 63c5ca5d 2019-08-24 stsp goto done;
4884 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4885 63c5ca5d 2019-08-24 stsp if (err)
4886 63c5ca5d 2019-08-24 stsp goto done;
4887 ba580f68 2020-03-22 stsp (*nentries)++;
4888 63c5ca5d 2019-08-24 stsp continue;
4889 63c5ca5d 2019-08-24 stsp }
4890 2f51b5b3 2019-05-09 stsp
4891 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4892 44d03001 2019-05-09 stsp int modified;
4893 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4894 036813ee 2019-05-09 stsp if (err)
4895 036813ee 2019-05-09 stsp goto done;
4896 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4897 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4898 2f51b5b3 2019-05-09 stsp if (err)
4899 2f51b5b3 2019-05-09 stsp goto done;
4900 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4901 44d03001 2019-05-09 stsp if (modified) {
4902 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4903 ba580f68 2020-03-22 stsp int nsubentries;
4904 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4905 ba580f68 2020-03-22 stsp &nsubentries, te,
4906 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4907 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4908 44d03001 2019-05-09 stsp if (err)
4909 44d03001 2019-05-09 stsp goto done;
4910 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4911 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4912 ba580f68 2020-03-22 stsp free(new_id);
4913 ba580f68 2020-03-22 stsp continue;
4914 ba580f68 2020-03-22 stsp }
4915 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4916 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4917 56e0773d 2019-11-28 stsp free(new_id);
4918 44d03001 2019-05-09 stsp }
4919 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4920 036813ee 2019-05-09 stsp if (err)
4921 036813ee 2019-05-09 stsp goto done;
4922 ba580f68 2020-03-22 stsp (*nentries)++;
4923 2f51b5b3 2019-05-09 stsp continue;
4924 036813ee 2019-05-09 stsp }
4925 2f51b5b3 2019-05-09 stsp
4926 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4927 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4928 f66c734c 2019-09-22 stsp if (err)
4929 f66c734c 2019-09-22 stsp goto done;
4930 2f51b5b3 2019-05-09 stsp if (ct) {
4931 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4932 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4933 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4934 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4935 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4936 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4937 2f51b5b3 2019-05-09 stsp if (err)
4938 2f51b5b3 2019-05-09 stsp goto done;
4939 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4940 2f51b5b3 2019-05-09 stsp if (err)
4941 2f51b5b3 2019-05-09 stsp goto done;
4942 ba580f68 2020-03-22 stsp (*nentries)++;
4943 2f51b5b3 2019-05-09 stsp }
4944 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4945 b416585c 2019-05-13 stsp status_arg);
4946 afa376bf 2019-05-09 stsp if (err)
4947 afa376bf 2019-05-09 stsp goto done;
4948 2f51b5b3 2019-05-09 stsp } else {
4949 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4950 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4951 2f51b5b3 2019-05-09 stsp if (err)
4952 2f51b5b3 2019-05-09 stsp goto done;
4953 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4954 2f51b5b3 2019-05-09 stsp if (err)
4955 2f51b5b3 2019-05-09 stsp goto done;
4956 ba580f68 2020-03-22 stsp (*nentries)++;
4957 2f51b5b3 2019-05-09 stsp }
4958 ca2503ea 2019-05-09 stsp }
4959 ed175427 2019-05-09 stsp }
4960 0b5cc0d6 2019-05-09 stsp
4961 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4962 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4963 ed175427 2019-05-09 stsp done:
4964 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4965 ed175427 2019-05-09 stsp return err;
4966 ed175427 2019-05-09 stsp }
4967 ed175427 2019-05-09 stsp
4968 ebf99748 2019-05-09 stsp static const struct got_error *
4969 2e1fa222 2020-07-23 stsp reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
4970 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
4971 2e1fa222 2020-07-23 stsp struct got_repository *repo)
4972 2e1fa222 2020-07-23 stsp {
4973 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
4974 2e1fa222 2020-07-23 stsp struct got_blob_object *blob = NULL;
4975 2e1fa222 2020-07-23 stsp struct got_object_id *tree_id = NULL;
4976 2e1fa222 2020-07-23 stsp char *tree_path = NULL;
4977 2e1fa222 2020-07-23 stsp struct got_tree_object *tree = NULL;
4978 2e1fa222 2020-07-23 stsp struct got_tree_entry *te;
4979 2e1fa222 2020-07-23 stsp char *entry_name;
4980 2e1fa222 2020-07-23 stsp
4981 2e1fa222 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
4982 2e1fa222 2020-07-23 stsp if (err)
4983 2e1fa222 2020-07-23 stsp return err;
4984 2e1fa222 2020-07-23 stsp
4985 2e1fa222 2020-07-23 stsp err = got_path_dirname(&tree_path, ct->in_repo_path);
4986 2e1fa222 2020-07-23 stsp if (err) {
4987 2e1fa222 2020-07-23 stsp if (err->code != GOT_ERR_BAD_PATH)
4988 2e1fa222 2020-07-23 stsp goto done;
4989 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
4990 2e1fa222 2020-07-23 stsp new_base_commit_id, "");
4991 2e1fa222 2020-07-23 stsp if (err)
4992 2e1fa222 2020-07-23 stsp goto done;
4993 2e1fa222 2020-07-23 stsp } else {
4994 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
4995 2e1fa222 2020-07-23 stsp new_base_commit_id, tree_path);
4996 2e1fa222 2020-07-23 stsp if (err)
4997 2e1fa222 2020-07-23 stsp goto done;
4998 2e1fa222 2020-07-23 stsp }
4999 2e1fa222 2020-07-23 stsp
5000 2e1fa222 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5001 2e1fa222 2020-07-23 stsp if (err)
5002 2e1fa222 2020-07-23 stsp goto done;
5003 2e1fa222 2020-07-23 stsp
5004 2e1fa222 2020-07-23 stsp entry_name = basename(ct->path);
5005 2e1fa222 2020-07-23 stsp if (entry_name == NULL) {
5006 2e1fa222 2020-07-23 stsp err = got_error_from_errno2("basename", ct->path);
5007 2e1fa222 2020-07-23 stsp goto done;
5008 2e1fa222 2020-07-23 stsp }
5009 2e1fa222 2020-07-23 stsp
5010 2e1fa222 2020-07-23 stsp te = got_object_tree_find_entry(tree, entry_name);
5011 2e1fa222 2020-07-23 stsp if (te == NULL) {
5012 2e1fa222 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5013 2e1fa222 2020-07-23 stsp goto done;
5014 2e1fa222 2020-07-23 stsp }
5015 2e1fa222 2020-07-23 stsp
5016 2e1fa222 2020-07-23 stsp err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5017 2e1fa222 2020-07-23 stsp ct->path, blob, 0, 0, repo, NULL, NULL);
5018 2e1fa222 2020-07-23 stsp done:
5019 2e1fa222 2020-07-23 stsp if (blob)
5020 2e1fa222 2020-07-23 stsp got_object_blob_close(blob);
5021 2e1fa222 2020-07-23 stsp if (tree)
5022 2e1fa222 2020-07-23 stsp got_object_tree_close(tree);
5023 2e1fa222 2020-07-23 stsp free(tree_id);
5024 2e1fa222 2020-07-23 stsp free(tree_path);
5025 2e1fa222 2020-07-23 stsp return err;
5026 2e1fa222 2020-07-23 stsp }
5027 2e1fa222 2020-07-23 stsp
5028 2e1fa222 2020-07-23 stsp /*
5029 2e1fa222 2020-07-23 stsp * After comitting a symlink we have a chance to convert "bad" symlinks
5030 2e1fa222 2020-07-23 stsp * (those which point outside the work tree or into .got) to regular files.
5031 2e1fa222 2020-07-23 stsp * This way, the post-commit work tree state matches a fresh checkout of
5032 2e1fa222 2020-07-23 stsp * the tree which was just committed. We also mark such newly committed
5033 2e1fa222 2020-07-23 stsp * symlinks as "bad" in the work tree's fileindex.
5034 2e1fa222 2020-07-23 stsp */
5035 2e1fa222 2020-07-23 stsp static const struct got_error *
5036 2e1fa222 2020-07-23 stsp reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5037 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5038 2e1fa222 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo)
5039 2e1fa222 2020-07-23 stsp {
5040 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5041 2e1fa222 2020-07-23 stsp struct got_pathlist_entry *pe;
5042 2e1fa222 2020-07-23 stsp
5043 2e1fa222 2020-07-23 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 2e1fa222 2020-07-23 stsp struct got_commitable *ct = pe->data;
5045 2e1fa222 2020-07-23 stsp struct got_fileindex_entry *ie;
5046 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5047 2e1fa222 2020-07-23 stsp
5048 2e1fa222 2020-07-23 stsp if (!S_ISLNK(get_ct_file_mode(ct)))
5049 2e1fa222 2020-07-23 stsp continue;
5050 2e1fa222 2020-07-23 stsp
5051 2e1fa222 2020-07-23 stsp err = reinstall_symlink_after_commit(&is_bad_symlink,
5052 2e1fa222 2020-07-23 stsp ct, new_base_commit_id, worktree, repo);
5053 2e1fa222 2020-07-23 stsp if (err)
5054 2e1fa222 2020-07-23 stsp break;
5055 2e1fa222 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, ct->path,
5056 2e1fa222 2020-07-23 stsp strlen(ct->path));
5057 2e1fa222 2020-07-23 stsp if (ie && is_bad_symlink) {
5058 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
5059 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5060 2e1fa222 2020-07-23 stsp if (err)
5061 2e1fa222 2020-07-23 stsp break;
5062 2e1fa222 2020-07-23 stsp }
5063 2e1fa222 2020-07-23 stsp }
5064 2e1fa222 2020-07-23 stsp
5065 2e1fa222 2020-07-23 stsp return err;
5066 2e1fa222 2020-07-23 stsp }
5067 2e1fa222 2020-07-23 stsp
5068 2e1fa222 2020-07-23 stsp static const struct got_error *
5069 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5070 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5071 72fd46fa 2019-09-06 stsp int have_staged_files)
5072 ebf99748 2019-05-09 stsp {
5073 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5074 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5075 ebf99748 2019-05-09 stsp
5076 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5077 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5078 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5079 ebf99748 2019-05-09 stsp
5080 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5081 ebf99748 2019-05-09 stsp if (ie) {
5082 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5083 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5084 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5085 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5086 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5087 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5088 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5089 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5090 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5091 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5092 72fd46fa 2019-09-06 stsp !have_staged_files);
5093 ebf99748 2019-05-09 stsp } else
5094 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5095 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5096 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5097 72fd46fa 2019-09-06 stsp !have_staged_files);
5098 ebf99748 2019-05-09 stsp } else {
5099 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5100 ebf99748 2019-05-09 stsp if (err)
5101 af12c6b9 2019-06-04 stsp break;
5102 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5103 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5104 3969253a 2020-03-07 stsp if (err) {
5105 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5106 3969253a 2020-03-07 stsp break;
5107 3969253a 2020-03-07 stsp }
5108 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5109 3969253a 2020-03-07 stsp if (err) {
5110 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5111 af12c6b9 2019-06-04 stsp break;
5112 3969253a 2020-03-07 stsp }
5113 ebf99748 2019-05-09 stsp }
5114 ebf99748 2019-05-09 stsp }
5115 d56d26ce 2019-05-10 stsp return err;
5116 d56d26ce 2019-05-10 stsp }
5117 735ef5ac 2019-08-03 stsp
5118 d56d26ce 2019-05-10 stsp
5119 d56d26ce 2019-05-10 stsp static const struct got_error *
5120 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5121 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5122 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5123 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5124 735ef5ac 2019-08-03 stsp int ood_errcode)
5125 d56d26ce 2019-05-10 stsp {
5126 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5127 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5128 1a36436d 2019-06-10 stsp
5129 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5130 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5131 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5132 1a36436d 2019-06-10 stsp return NULL;
5133 9bead371 2019-07-28 stsp /*
5134 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5135 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5136 9bead371 2019-07-28 stsp */
5137 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5138 735ef5ac 2019-08-03 stsp in_repo_path);
5139 9bead371 2019-07-28 stsp if (err) {
5140 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5141 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5142 1a36436d 2019-06-10 stsp goto done;
5143 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5144 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5145 1a36436d 2019-06-10 stsp } else {
5146 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5147 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5148 735ef5ac 2019-08-03 stsp in_repo_path);
5149 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5150 1a36436d 2019-06-10 stsp goto done;
5151 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5152 1a36436d 2019-06-10 stsp }
5153 1a36436d 2019-06-10 stsp done:
5154 1a36436d 2019-06-10 stsp free(id);
5155 1a36436d 2019-06-10 stsp return err;
5156 ebf99748 2019-05-09 stsp }
5157 ebf99748 2019-05-09 stsp
5158 c4296144 2019-05-09 stsp const struct got_error *
5159 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5160 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5161 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5162 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5163 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5164 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5165 afa376bf 2019-05-09 stsp struct got_repository *repo)
5166 c4296144 2019-05-09 stsp {
5167 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5168 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5169 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5170 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5171 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5172 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5173 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5174 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5175 ba580f68 2020-03-22 stsp int nentries;
5176 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5177 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5178 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5179 c4296144 2019-05-09 stsp
5180 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5181 c4296144 2019-05-09 stsp
5182 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5183 675c7539 2019-05-09 stsp
5184 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5185 588edf97 2019-05-10 stsp if (err)
5186 588edf97 2019-05-10 stsp goto done;
5187 de18fc63 2019-05-09 stsp
5188 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5189 588edf97 2019-05-10 stsp if (err)
5190 588edf97 2019-05-10 stsp goto done;
5191 588edf97 2019-05-10 stsp
5192 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5193 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5194 33ad4cbe 2019-05-12 jcs if (err)
5195 33ad4cbe 2019-05-12 jcs goto done;
5196 33ad4cbe 2019-05-12 jcs }
5197 c4296144 2019-05-09 stsp
5198 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5199 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5200 33ad4cbe 2019-05-12 jcs goto done;
5201 33ad4cbe 2019-05-12 jcs }
5202 33ad4cbe 2019-05-12 jcs
5203 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5204 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5205 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5206 cf066bf8 2019-05-09 stsp char *ondisk_path;
5207 cf066bf8 2019-05-09 stsp
5208 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5209 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5210 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5211 5f8a88c6 2019-08-03 stsp continue;
5212 5f8a88c6 2019-08-03 stsp
5213 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5214 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5215 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5216 cf066bf8 2019-05-09 stsp continue;
5217 cf066bf8 2019-05-09 stsp
5218 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5219 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5220 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5221 cf066bf8 2019-05-09 stsp goto done;
5222 cf066bf8 2019-05-09 stsp }
5223 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5224 2e1fa222 2020-07-23 stsp free(ondisk_path);
5225 2e1fa222 2020-07-23 stsp if (err)
5226 cf066bf8 2019-05-09 stsp goto done;
5227 cf066bf8 2019-05-09 stsp }
5228 036813ee 2019-05-09 stsp
5229 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5230 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5231 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5232 036813ee 2019-05-09 stsp if (err)
5233 036813ee 2019-05-09 stsp goto done;
5234 036813ee 2019-05-09 stsp
5235 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5236 de18fc63 2019-05-09 stsp if (err)
5237 de18fc63 2019-05-09 stsp goto done;
5238 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5239 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5240 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5241 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5242 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5243 33ad4cbe 2019-05-12 jcs free(logmsg);
5244 9d40349a 2019-05-09 stsp if (err)
5245 9d40349a 2019-05-09 stsp goto done;
5246 9d40349a 2019-05-09 stsp
5247 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5248 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5249 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5250 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5251 09f5bd90 2019-05-09 stsp goto done;
5252 09f5bd90 2019-05-09 stsp }
5253 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5254 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5255 09f5bd90 2019-05-09 stsp if (err)
5256 09f5bd90 2019-05-09 stsp goto done;
5257 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5258 ebf99748 2019-05-09 stsp if (err)
5259 ebf99748 2019-05-09 stsp goto done;
5260 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5261 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5262 09f5bd90 2019-05-09 stsp goto done;
5263 09f5bd90 2019-05-09 stsp }
5264 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5265 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5266 f2c16586 2019-05-09 stsp if (err)
5267 f2c16586 2019-05-09 stsp goto done;
5268 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5269 f2c16586 2019-05-09 stsp if (err)
5270 f2c16586 2019-05-09 stsp goto done;
5271 f2c16586 2019-05-09 stsp
5272 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5273 09f5bd90 2019-05-09 stsp if (err)
5274 09f5bd90 2019-05-09 stsp goto done;
5275 09f5bd90 2019-05-09 stsp
5276 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5277 ebf99748 2019-05-09 stsp if (err)
5278 ebf99748 2019-05-09 stsp goto done;
5279 c4296144 2019-05-09 stsp done:
5280 588edf97 2019-05-10 stsp if (head_tree)
5281 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5282 588edf97 2019-05-10 stsp if (head_commit)
5283 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5284 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5285 2f17228e 2019-05-12 stsp if (head_ref2) {
5286 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5287 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5288 2f17228e 2019-05-12 stsp err = unlockerr;
5289 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5290 39cd0ff6 2019-07-12 stsp }
5291 39cd0ff6 2019-07-12 stsp return err;
5292 39cd0ff6 2019-07-12 stsp }
5293 5c1e53bc 2019-07-28 stsp
5294 5c1e53bc 2019-07-28 stsp static const struct got_error *
5295 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5296 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5297 5c1e53bc 2019-07-28 stsp {
5298 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5299 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5300 39cd0ff6 2019-07-12 stsp
5301 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5302 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5303 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5304 5c1e53bc 2019-07-28 stsp
5305 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5306 5c1e53bc 2019-07-28 stsp ct_path++;
5307 5c1e53bc 2019-07-28 stsp
5308 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5309 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5310 5c1e53bc 2019-07-28 stsp break;
5311 5c1e53bc 2019-07-28 stsp }
5312 5c1e53bc 2019-07-28 stsp
5313 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5314 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5315 5c1e53bc 2019-07-28 stsp
5316 5c1e53bc 2019-07-28 stsp return NULL;
5317 5c1e53bc 2019-07-28 stsp }
5318 5c1e53bc 2019-07-28 stsp
5319 f0b75401 2019-08-03 stsp static const struct got_error *
5320 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5321 f0b75401 2019-08-03 stsp {
5322 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5323 f0b75401 2019-08-03 stsp
5324 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5325 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5326 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5327 f0b75401 2019-08-03 stsp }
5328 f0b75401 2019-08-03 stsp
5329 f0b75401 2019-08-03 stsp return NULL;
5330 f0b75401 2019-08-03 stsp }
5331 f0b75401 2019-08-03 stsp
5332 5f8a88c6 2019-08-03 stsp static const struct got_error *
5333 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5334 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5335 5f8a88c6 2019-08-03 stsp {
5336 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5337 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5338 5f8a88c6 2019-08-03 stsp
5339 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5340 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5341 5f8a88c6 2019-08-03 stsp continue;
5342 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5343 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5344 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5345 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5346 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5347 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5348 5f8a88c6 2019-08-03 stsp }
5349 5f8a88c6 2019-08-03 stsp
5350 5f8a88c6 2019-08-03 stsp return NULL;
5351 5f8a88c6 2019-08-03 stsp }
5352 5f8a88c6 2019-08-03 stsp
5353 39cd0ff6 2019-07-12 stsp const struct got_error *
5354 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5355 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5356 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5357 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5358 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5359 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5360 39cd0ff6 2019-07-12 stsp {
5361 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5362 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5363 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5364 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5365 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5366 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5367 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5368 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5369 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5370 39cd0ff6 2019-07-12 stsp
5371 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5372 39cd0ff6 2019-07-12 stsp
5373 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5374 39cd0ff6 2019-07-12 stsp
5375 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5376 39cd0ff6 2019-07-12 stsp if (err)
5377 39cd0ff6 2019-07-12 stsp goto done;
5378 39cd0ff6 2019-07-12 stsp
5379 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5380 39cd0ff6 2019-07-12 stsp if (err)
5381 39cd0ff6 2019-07-12 stsp goto done;
5382 39cd0ff6 2019-07-12 stsp
5383 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5384 39cd0ff6 2019-07-12 stsp if (err)
5385 39cd0ff6 2019-07-12 stsp goto done;
5386 39cd0ff6 2019-07-12 stsp
5387 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5388 39cd0ff6 2019-07-12 stsp if (err)
5389 39cd0ff6 2019-07-12 stsp goto done;
5390 39cd0ff6 2019-07-12 stsp
5391 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5392 5f8a88c6 2019-08-03 stsp &have_staged_files);
5393 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5394 5f8a88c6 2019-08-03 stsp goto done;
5395 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5396 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5397 5f8a88c6 2019-08-03 stsp if (err)
5398 5f8a88c6 2019-08-03 stsp goto done;
5399 5f8a88c6 2019-08-03 stsp }
5400 5f8a88c6 2019-08-03 stsp
5401 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5402 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5403 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5404 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5405 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5406 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5407 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5408 5c1e53bc 2019-07-28 stsp if (err)
5409 5c1e53bc 2019-07-28 stsp goto done;
5410 5c1e53bc 2019-07-28 stsp }
5411 39cd0ff6 2019-07-12 stsp
5412 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5413 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5414 39cd0ff6 2019-07-12 stsp goto done;
5415 5c1e53bc 2019-07-28 stsp }
5416 5c1e53bc 2019-07-28 stsp
5417 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5418 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5419 5c1e53bc 2019-07-28 stsp if (err)
5420 5c1e53bc 2019-07-28 stsp goto done;
5421 39cd0ff6 2019-07-12 stsp }
5422 f0b75401 2019-08-03 stsp
5423 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5424 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5425 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5426 f0b75401 2019-08-03 stsp
5427 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5428 f0b75401 2019-08-03 stsp ct_path++;
5429 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5430 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5431 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5432 b50cabdf 2019-07-12 stsp if (err)
5433 b50cabdf 2019-07-12 stsp goto done;
5434 f0b75401 2019-08-03 stsp
5435 b50cabdf 2019-07-12 stsp }
5436 b50cabdf 2019-07-12 stsp
5437 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5438 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5439 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5440 39cd0ff6 2019-07-12 stsp if (err)
5441 39cd0ff6 2019-07-12 stsp goto done;
5442 39cd0ff6 2019-07-12 stsp
5443 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5444 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5445 2e1fa222 2020-07-23 stsp if (err == NULL) {
5446 2e1fa222 2020-07-23 stsp err = reinstall_symlinks_after_commit(&commitable_paths,
5447 2e1fa222 2020-07-23 stsp *new_commit_id, fileindex, worktree, repo);
5448 2e1fa222 2020-07-23 stsp }
5449 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5450 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5451 39cd0ff6 2019-07-12 stsp err = sync_err;
5452 39cd0ff6 2019-07-12 stsp done:
5453 39cd0ff6 2019-07-12 stsp if (fileindex)
5454 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5455 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5456 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5457 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5458 39cd0ff6 2019-07-12 stsp err = unlockerr;
5459 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5460 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5461 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5462 39cd0ff6 2019-07-12 stsp }
5463 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5464 c4296144 2019-05-09 stsp return err;
5465 8656d6c4 2019-05-20 stsp }
5466 8656d6c4 2019-05-20 stsp
5467 8656d6c4 2019-05-20 stsp const char *
5468 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5469 8656d6c4 2019-05-20 stsp {
5470 8656d6c4 2019-05-20 stsp return ct->path;
5471 8656d6c4 2019-05-20 stsp }
5472 8656d6c4 2019-05-20 stsp
5473 8656d6c4 2019-05-20 stsp unsigned int
5474 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5475 8656d6c4 2019-05-20 stsp {
5476 8656d6c4 2019-05-20 stsp return ct->status;
5477 818c7501 2019-07-11 stsp }
5478 818c7501 2019-07-11 stsp
5479 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5480 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5481 818c7501 2019-07-11 stsp struct got_repository *repo;
5482 818c7501 2019-07-11 stsp };
5483 818c7501 2019-07-11 stsp
5484 818c7501 2019-07-11 stsp static const struct got_error *
5485 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5486 818c7501 2019-07-11 stsp {
5487 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5488 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5489 818c7501 2019-07-11 stsp unsigned char status;
5490 818c7501 2019-07-11 stsp struct stat sb;
5491 818c7501 2019-07-11 stsp char *ondisk_path;
5492 818c7501 2019-07-11 stsp
5493 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5494 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5495 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5496 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5497 818c7501 2019-07-11 stsp
5498 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5499 818c7501 2019-07-11 stsp == -1)
5500 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5501 818c7501 2019-07-11 stsp
5502 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5503 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5504 818c7501 2019-07-11 stsp free(ondisk_path);
5505 818c7501 2019-07-11 stsp if (err)
5506 818c7501 2019-07-11 stsp return err;
5507 818c7501 2019-07-11 stsp
5508 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5509 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5510 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5511 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5512 818c7501 2019-07-11 stsp
5513 818c7501 2019-07-11 stsp return NULL;
5514 818c7501 2019-07-11 stsp }
5515 818c7501 2019-07-11 stsp
5516 818c7501 2019-07-11 stsp const struct got_error *
5517 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5518 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5519 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5520 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5521 818c7501 2019-07-11 stsp {
5522 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5523 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5524 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5525 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5526 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5527 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5528 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5529 818c7501 2019-07-11 stsp
5530 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5531 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5532 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5533 818c7501 2019-07-11 stsp
5534 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5535 818c7501 2019-07-11 stsp if (err)
5536 818c7501 2019-07-11 stsp return err;
5537 818c7501 2019-07-11 stsp
5538 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5539 818c7501 2019-07-11 stsp if (err)
5540 818c7501 2019-07-11 stsp goto done;
5541 818c7501 2019-07-11 stsp
5542 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5543 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5544 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5545 818c7501 2019-07-11 stsp &ok_arg);
5546 818c7501 2019-07-11 stsp if (err)
5547 818c7501 2019-07-11 stsp goto done;
5548 818c7501 2019-07-11 stsp
5549 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5550 818c7501 2019-07-11 stsp if (err)
5551 818c7501 2019-07-11 stsp goto done;
5552 818c7501 2019-07-11 stsp
5553 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5554 818c7501 2019-07-11 stsp if (err)
5555 818c7501 2019-07-11 stsp goto done;
5556 818c7501 2019-07-11 stsp
5557 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5558 818c7501 2019-07-11 stsp if (err)
5559 818c7501 2019-07-11 stsp goto done;
5560 818c7501 2019-07-11 stsp
5561 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5562 818c7501 2019-07-11 stsp 0);
5563 e51d7b55 2020-01-04 stsp if (err)
5564 e51d7b55 2020-01-04 stsp goto done;
5565 e51d7b55 2020-01-04 stsp
5566 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5567 818c7501 2019-07-11 stsp if (err)
5568 818c7501 2019-07-11 stsp goto done;
5569 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5570 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5571 e51d7b55 2020-01-04 stsp goto done;
5572 e51d7b55 2020-01-04 stsp }
5573 818c7501 2019-07-11 stsp
5574 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5575 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5576 818c7501 2019-07-11 stsp if (err)
5577 818c7501 2019-07-11 stsp goto done;
5578 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5579 818c7501 2019-07-11 stsp if (err)
5580 818c7501 2019-07-11 stsp goto done;
5581 818c7501 2019-07-11 stsp
5582 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5583 818c7501 2019-07-11 stsp
5584 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5585 818c7501 2019-07-11 stsp if (err)
5586 818c7501 2019-07-11 stsp goto done;
5587 818c7501 2019-07-11 stsp
5588 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5589 818c7501 2019-07-11 stsp if (err)
5590 818c7501 2019-07-11 stsp goto done;
5591 818c7501 2019-07-11 stsp
5592 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5593 818c7501 2019-07-11 stsp worktree->base_commit_id);
5594 818c7501 2019-07-11 stsp if (err)
5595 818c7501 2019-07-11 stsp goto done;
5596 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5597 818c7501 2019-07-11 stsp if (err)
5598 818c7501 2019-07-11 stsp goto done;
5599 818c7501 2019-07-11 stsp
5600 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5601 818c7501 2019-07-11 stsp if (err)
5602 818c7501 2019-07-11 stsp goto done;
5603 818c7501 2019-07-11 stsp done:
5604 818c7501 2019-07-11 stsp free(fileindex_path);
5605 818c7501 2019-07-11 stsp free(tmp_branch_name);
5606 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5607 818c7501 2019-07-11 stsp free(branch_ref_name);
5608 818c7501 2019-07-11 stsp if (branch_ref)
5609 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5610 818c7501 2019-07-11 stsp if (wt_branch)
5611 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5612 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5613 818c7501 2019-07-11 stsp if (err) {
5614 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5615 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5616 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5617 818c7501 2019-07-11 stsp }
5618 818c7501 2019-07-11 stsp if (*tmp_branch) {
5619 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5620 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5621 818c7501 2019-07-11 stsp }
5622 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5623 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5624 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5625 3e3a69f1 2019-07-25 stsp }
5626 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5627 818c7501 2019-07-11 stsp }
5628 818c7501 2019-07-11 stsp return err;
5629 818c7501 2019-07-11 stsp }
5630 818c7501 2019-07-11 stsp
5631 818c7501 2019-07-11 stsp const struct got_error *
5632 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5633 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5634 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5635 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5636 818c7501 2019-07-11 stsp {
5637 818c7501 2019-07-11 stsp const struct got_error *err;
5638 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5639 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5640 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5641 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5642 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5643 818c7501 2019-07-11 stsp
5644 818c7501 2019-07-11 stsp *commit_id = NULL;
5645 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5646 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5647 3e3a69f1 2019-07-25 stsp *branch = NULL;
5648 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5649 3e3a69f1 2019-07-25 stsp
5650 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5651 3e3a69f1 2019-07-25 stsp if (err)
5652 3e3a69f1 2019-07-25 stsp return err;
5653 818c7501 2019-07-11 stsp
5654 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5655 3e3a69f1 2019-07-25 stsp if (err)
5656 f032f1f7 2019-08-04 stsp goto done;
5657 f032f1f7 2019-08-04 stsp
5658 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5659 f032f1f7 2019-08-04 stsp &have_staged_files);
5660 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5661 f032f1f7 2019-08-04 stsp goto done;
5662 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5663 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5664 3e3a69f1 2019-07-25 stsp goto done;
5665 f032f1f7 2019-08-04 stsp }
5666 3e3a69f1 2019-07-25 stsp
5667 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5668 818c7501 2019-07-11 stsp if (err)
5669 f032f1f7 2019-08-04 stsp goto done;
5670 818c7501 2019-07-11 stsp
5671 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5672 818c7501 2019-07-11 stsp if (err)
5673 818c7501 2019-07-11 stsp goto done;
5674 818c7501 2019-07-11 stsp
5675 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5676 818c7501 2019-07-11 stsp if (err)
5677 818c7501 2019-07-11 stsp goto done;
5678 818c7501 2019-07-11 stsp
5679 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5680 818c7501 2019-07-11 stsp if (err)
5681 818c7501 2019-07-11 stsp goto done;
5682 818c7501 2019-07-11 stsp
5683 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5684 818c7501 2019-07-11 stsp if (err)
5685 818c7501 2019-07-11 stsp goto done;
5686 818c7501 2019-07-11 stsp
5687 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5688 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5689 818c7501 2019-07-11 stsp if (err)
5690 818c7501 2019-07-11 stsp goto done;
5691 818c7501 2019-07-11 stsp
5692 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5693 818c7501 2019-07-11 stsp if (err)
5694 818c7501 2019-07-11 stsp goto done;
5695 818c7501 2019-07-11 stsp
5696 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5697 818c7501 2019-07-11 stsp if (err)
5698 818c7501 2019-07-11 stsp goto done;
5699 818c7501 2019-07-11 stsp
5700 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5701 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5702 818c7501 2019-07-11 stsp if (err)
5703 818c7501 2019-07-11 stsp goto done;
5704 818c7501 2019-07-11 stsp
5705 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5706 818c7501 2019-07-11 stsp if (err)
5707 818c7501 2019-07-11 stsp goto done;
5708 818c7501 2019-07-11 stsp done:
5709 818c7501 2019-07-11 stsp free(commit_ref_name);
5710 818c7501 2019-07-11 stsp free(branch_ref_name);
5711 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5712 818c7501 2019-07-11 stsp if (commit_ref)
5713 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5714 818c7501 2019-07-11 stsp if (branch_ref)
5715 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5716 818c7501 2019-07-11 stsp if (err) {
5717 818c7501 2019-07-11 stsp free(*commit_id);
5718 818c7501 2019-07-11 stsp *commit_id = NULL;
5719 818c7501 2019-07-11 stsp if (*tmp_branch) {
5720 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5721 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5722 818c7501 2019-07-11 stsp }
5723 818c7501 2019-07-11 stsp if (*new_base_branch) {
5724 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5725 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5726 818c7501 2019-07-11 stsp }
5727 818c7501 2019-07-11 stsp if (*branch) {
5728 818c7501 2019-07-11 stsp got_ref_close(*branch);
5729 818c7501 2019-07-11 stsp *branch = NULL;
5730 818c7501 2019-07-11 stsp }
5731 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5732 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5733 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5734 3e3a69f1 2019-07-25 stsp }
5735 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5736 818c7501 2019-07-11 stsp }
5737 818c7501 2019-07-11 stsp return err;
5738 c4296144 2019-05-09 stsp }
5739 818c7501 2019-07-11 stsp
5740 818c7501 2019-07-11 stsp const struct got_error *
5741 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5742 818c7501 2019-07-11 stsp {
5743 818c7501 2019-07-11 stsp const struct got_error *err;
5744 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5745 818c7501 2019-07-11 stsp
5746 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5747 818c7501 2019-07-11 stsp if (err)
5748 818c7501 2019-07-11 stsp return err;
5749 818c7501 2019-07-11 stsp
5750 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5751 818c7501 2019-07-11 stsp free(tmp_branch_name);
5752 818c7501 2019-07-11 stsp return NULL;
5753 818c7501 2019-07-11 stsp }
5754 818c7501 2019-07-11 stsp
5755 818c7501 2019-07-11 stsp static const struct got_error *
5756 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5757 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5758 818c7501 2019-07-11 stsp {
5759 0ebf8283 2019-07-24 stsp *logmsg = arg;
5760 818c7501 2019-07-11 stsp return NULL;
5761 818c7501 2019-07-11 stsp }
5762 818c7501 2019-07-11 stsp
5763 818c7501 2019-07-11 stsp static const struct got_error *
5764 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5765 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5766 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5767 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5768 818c7501 2019-07-11 stsp {
5769 818c7501 2019-07-11 stsp return NULL;
5770 01757395 2019-07-12 stsp }
5771 01757395 2019-07-12 stsp
5772 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5773 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5774 01757395 2019-07-12 stsp void *progress_arg;
5775 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5776 01757395 2019-07-12 stsp };
5777 01757395 2019-07-12 stsp
5778 01757395 2019-07-12 stsp static const struct got_error *
5779 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5780 01757395 2019-07-12 stsp {
5781 01757395 2019-07-12 stsp const struct got_error *err;
5782 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5783 01757395 2019-07-12 stsp char *p;
5784 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5785 01757395 2019-07-12 stsp
5786 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5787 01757395 2019-07-12 stsp if (err)
5788 01757395 2019-07-12 stsp return err;
5789 01757395 2019-07-12 stsp
5790 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5791 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5792 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5793 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5794 01757395 2019-07-12 stsp return NULL;
5795 01757395 2019-07-12 stsp
5796 01757395 2019-07-12 stsp p = strdup(path);
5797 01757395 2019-07-12 stsp if (p == NULL)
5798 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5799 01757395 2019-07-12 stsp
5800 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5801 01757395 2019-07-12 stsp if (err || new == NULL)
5802 01757395 2019-07-12 stsp free(p);
5803 01757395 2019-07-12 stsp return err;
5804 01757395 2019-07-12 stsp }
5805 01757395 2019-07-12 stsp
5806 01757395 2019-07-12 stsp void
5807 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5808 01757395 2019-07-12 stsp {
5809 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5810 01757395 2019-07-12 stsp
5811 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5812 01757395 2019-07-12 stsp free((char *)pe->path);
5813 01757395 2019-07-12 stsp
5814 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5815 818c7501 2019-07-11 stsp }
5816 818c7501 2019-07-11 stsp
5817 0ebf8283 2019-07-24 stsp static const struct got_error *
5818 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5819 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5820 818c7501 2019-07-11 stsp {
5821 818c7501 2019-07-11 stsp const struct got_error *err;
5822 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5823 818c7501 2019-07-11 stsp
5824 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5825 818c7501 2019-07-11 stsp if (err) {
5826 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5827 818c7501 2019-07-11 stsp goto done;
5828 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5829 818c7501 2019-07-11 stsp if (err)
5830 818c7501 2019-07-11 stsp goto done;
5831 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5832 818c7501 2019-07-11 stsp if (err)
5833 818c7501 2019-07-11 stsp goto done;
5834 de05890f 2020-03-05 stsp } else if (is_rebase) {
5835 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5836 818c7501 2019-07-11 stsp int cmp;
5837 818c7501 2019-07-11 stsp
5838 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5839 818c7501 2019-07-11 stsp if (err)
5840 818c7501 2019-07-11 stsp goto done;
5841 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5842 818c7501 2019-07-11 stsp free(stored_id);
5843 818c7501 2019-07-11 stsp if (cmp != 0) {
5844 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5845 818c7501 2019-07-11 stsp goto done;
5846 818c7501 2019-07-11 stsp }
5847 818c7501 2019-07-11 stsp }
5848 0ebf8283 2019-07-24 stsp done:
5849 0ebf8283 2019-07-24 stsp if (commit_ref)
5850 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5851 0ebf8283 2019-07-24 stsp return err;
5852 0ebf8283 2019-07-24 stsp }
5853 0ebf8283 2019-07-24 stsp
5854 0ebf8283 2019-07-24 stsp static const struct got_error *
5855 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5856 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5857 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5858 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5859 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5860 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5861 0ebf8283 2019-07-24 stsp {
5862 0ebf8283 2019-07-24 stsp const struct got_error *err;
5863 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5864 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5865 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5866 818c7501 2019-07-11 stsp
5867 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5868 0ebf8283 2019-07-24 stsp
5869 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5870 0ebf8283 2019-07-24 stsp if (err)
5871 0ebf8283 2019-07-24 stsp return err;
5872 0ebf8283 2019-07-24 stsp
5873 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5874 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5875 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5876 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5877 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5878 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5879 818c7501 2019-07-11 stsp if (commit_ref)
5880 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5881 818c7501 2019-07-11 stsp return err;
5882 818c7501 2019-07-11 stsp }
5883 818c7501 2019-07-11 stsp
5884 818c7501 2019-07-11 stsp const struct got_error *
5885 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5886 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5887 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5888 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5889 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5890 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5891 818c7501 2019-07-11 stsp {
5892 0ebf8283 2019-07-24 stsp const struct got_error *err;
5893 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5894 0ebf8283 2019-07-24 stsp
5895 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5896 0ebf8283 2019-07-24 stsp if (err)
5897 0ebf8283 2019-07-24 stsp return err;
5898 0ebf8283 2019-07-24 stsp
5899 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5900 0ebf8283 2019-07-24 stsp if (err)
5901 0ebf8283 2019-07-24 stsp goto done;
5902 0ebf8283 2019-07-24 stsp
5903 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5904 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5905 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5906 0ebf8283 2019-07-24 stsp done:
5907 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5908 0ebf8283 2019-07-24 stsp return err;
5909 0ebf8283 2019-07-24 stsp }
5910 0ebf8283 2019-07-24 stsp
5911 0ebf8283 2019-07-24 stsp const struct got_error *
5912 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5913 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5914 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5915 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5916 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5917 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5918 0ebf8283 2019-07-24 stsp {
5919 0ebf8283 2019-07-24 stsp const struct got_error *err;
5920 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5921 0ebf8283 2019-07-24 stsp
5922 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5923 0ebf8283 2019-07-24 stsp if (err)
5924 0ebf8283 2019-07-24 stsp return err;
5925 0ebf8283 2019-07-24 stsp
5926 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5927 0ebf8283 2019-07-24 stsp if (err)
5928 0ebf8283 2019-07-24 stsp goto done;
5929 0ebf8283 2019-07-24 stsp
5930 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5931 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5932 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5933 0ebf8283 2019-07-24 stsp done:
5934 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5935 0ebf8283 2019-07-24 stsp return err;
5936 0ebf8283 2019-07-24 stsp }
5937 0ebf8283 2019-07-24 stsp
5938 0ebf8283 2019-07-24 stsp static const struct got_error *
5939 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5940 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5941 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5942 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5943 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5944 0ebf8283 2019-07-24 stsp {
5945 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5946 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5947 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5948 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5949 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5950 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5951 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5952 818c7501 2019-07-11 stsp
5953 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5954 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5955 a0e95631 2019-07-12 stsp
5956 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5957 818c7501 2019-07-11 stsp
5958 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5959 a0e95631 2019-07-12 stsp if (err)
5960 3e3a69f1 2019-07-25 stsp return err;
5961 a0e95631 2019-07-12 stsp
5962 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5963 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5964 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5965 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5966 01757395 2019-07-12 stsp /*
5967 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5968 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5969 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5970 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5971 01757395 2019-07-12 stsp */
5972 01757395 2019-07-12 stsp if (merged_paths) {
5973 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5974 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5975 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5976 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5977 f2a9dc41 2019-12-13 tracey 0);
5978 01757395 2019-07-12 stsp if (err)
5979 01757395 2019-07-12 stsp goto done;
5980 01757395 2019-07-12 stsp }
5981 01757395 2019-07-12 stsp } else {
5982 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5983 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5984 01757395 2019-07-12 stsp if (err)
5985 01757395 2019-07-12 stsp goto done;
5986 01757395 2019-07-12 stsp }
5987 a0e95631 2019-07-12 stsp
5988 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5989 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5990 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5991 a0e95631 2019-07-12 stsp if (err)
5992 a0e95631 2019-07-12 stsp goto done;
5993 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5994 a0e95631 2019-07-12 stsp goto done;
5995 ff0d2220 2019-07-11 stsp }
5996 818c7501 2019-07-11 stsp
5997 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5998 edd02c5e 2019-07-11 stsp if (err)
5999 edd02c5e 2019-07-11 stsp goto done;
6000 edd02c5e 2019-07-11 stsp
6001 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6002 818c7501 2019-07-11 stsp if (err)
6003 818c7501 2019-07-11 stsp goto done;
6004 818c7501 2019-07-11 stsp
6005 5943eee2 2019-08-13 stsp if (new_logmsg) {
6006 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6007 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6008 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6009 5943eee2 2019-08-13 stsp goto done;
6010 5943eee2 2019-08-13 stsp }
6011 5943eee2 2019-08-13 stsp } else {
6012 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6013 5943eee2 2019-08-13 stsp if (err)
6014 5943eee2 2019-08-13 stsp goto done;
6015 5943eee2 2019-08-13 stsp }
6016 0ebf8283 2019-07-24 stsp
6017 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6018 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6019 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6020 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6021 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6022 a0e95631 2019-07-12 stsp if (err)
6023 a0e95631 2019-07-12 stsp goto done;
6024 a0e95631 2019-07-12 stsp
6025 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6026 a0e95631 2019-07-12 stsp if (err)
6027 a0e95631 2019-07-12 stsp goto done;
6028 a0e95631 2019-07-12 stsp
6029 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6030 a0e95631 2019-07-12 stsp if (err)
6031 a0e95631 2019-07-12 stsp goto done;
6032 a0e95631 2019-07-12 stsp
6033 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6034 72fd46fa 2019-09-06 stsp fileindex, 0);
6035 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6036 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6037 a0e95631 2019-07-12 stsp err = sync_err;
6038 818c7501 2019-07-11 stsp done:
6039 a0e95631 2019-07-12 stsp free(fileindex_path);
6040 a0e95631 2019-07-12 stsp free(head_commit_id);
6041 a0e95631 2019-07-12 stsp if (head_ref)
6042 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6043 818c7501 2019-07-11 stsp if (err) {
6044 818c7501 2019-07-11 stsp free(*new_commit_id);
6045 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6046 818c7501 2019-07-11 stsp }
6047 818c7501 2019-07-11 stsp return err;
6048 818c7501 2019-07-11 stsp }
6049 818c7501 2019-07-11 stsp
6050 818c7501 2019-07-11 stsp const struct got_error *
6051 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6052 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6053 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6054 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6055 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6056 0ebf8283 2019-07-24 stsp {
6057 0ebf8283 2019-07-24 stsp const struct got_error *err;
6058 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6059 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6060 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6061 0ebf8283 2019-07-24 stsp
6062 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6063 0ebf8283 2019-07-24 stsp if (err)
6064 0ebf8283 2019-07-24 stsp return err;
6065 0ebf8283 2019-07-24 stsp
6066 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6067 0ebf8283 2019-07-24 stsp if (err)
6068 0ebf8283 2019-07-24 stsp goto done;
6069 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6070 0ebf8283 2019-07-24 stsp if (err)
6071 0ebf8283 2019-07-24 stsp goto done;
6072 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6073 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6074 0ebf8283 2019-07-24 stsp goto done;
6075 0ebf8283 2019-07-24 stsp }
6076 0ebf8283 2019-07-24 stsp
6077 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6078 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6079 0ebf8283 2019-07-24 stsp done:
6080 0ebf8283 2019-07-24 stsp if (commit_ref)
6081 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6082 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6083 0ebf8283 2019-07-24 stsp free(commit_id);
6084 0ebf8283 2019-07-24 stsp return err;
6085 0ebf8283 2019-07-24 stsp }
6086 0ebf8283 2019-07-24 stsp
6087 0ebf8283 2019-07-24 stsp const struct got_error *
6088 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6089 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6090 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6091 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6092 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6093 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6094 0ebf8283 2019-07-24 stsp {
6095 0ebf8283 2019-07-24 stsp const struct got_error *err;
6096 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6097 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6098 0ebf8283 2019-07-24 stsp
6099 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6100 0ebf8283 2019-07-24 stsp if (err)
6101 0ebf8283 2019-07-24 stsp return err;
6102 0ebf8283 2019-07-24 stsp
6103 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6104 0ebf8283 2019-07-24 stsp if (err)
6105 0ebf8283 2019-07-24 stsp goto done;
6106 0ebf8283 2019-07-24 stsp
6107 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6108 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6109 0ebf8283 2019-07-24 stsp done:
6110 0ebf8283 2019-07-24 stsp if (commit_ref)
6111 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6112 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6113 0ebf8283 2019-07-24 stsp return err;
6114 0ebf8283 2019-07-24 stsp }
6115 0ebf8283 2019-07-24 stsp
6116 0ebf8283 2019-07-24 stsp const struct got_error *
6117 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6118 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6119 818c7501 2019-07-11 stsp {
6120 3e3a69f1 2019-07-25 stsp if (fileindex)
6121 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6122 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6123 69844fba 2019-07-11 stsp }
6124 69844fba 2019-07-11 stsp
6125 69844fba 2019-07-11 stsp static const struct got_error *
6126 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6127 69844fba 2019-07-11 stsp {
6128 69844fba 2019-07-11 stsp const struct got_error *err;
6129 69844fba 2019-07-11 stsp struct got_reference *ref;
6130 69844fba 2019-07-11 stsp
6131 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6132 69844fba 2019-07-11 stsp if (err) {
6133 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6134 69844fba 2019-07-11 stsp return NULL;
6135 69844fba 2019-07-11 stsp return err;
6136 69844fba 2019-07-11 stsp }
6137 69844fba 2019-07-11 stsp
6138 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6139 69844fba 2019-07-11 stsp got_ref_close(ref);
6140 69844fba 2019-07-11 stsp return err;
6141 818c7501 2019-07-11 stsp }
6142 818c7501 2019-07-11 stsp
6143 69844fba 2019-07-11 stsp static const struct got_error *
6144 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6145 69844fba 2019-07-11 stsp {
6146 69844fba 2019-07-11 stsp const struct got_error *err;
6147 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6148 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6149 69844fba 2019-07-11 stsp
6150 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6151 69844fba 2019-07-11 stsp if (err)
6152 69844fba 2019-07-11 stsp goto done;
6153 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6154 69844fba 2019-07-11 stsp if (err)
6155 69844fba 2019-07-11 stsp goto done;
6156 69844fba 2019-07-11 stsp
6157 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6158 69844fba 2019-07-11 stsp if (err)
6159 69844fba 2019-07-11 stsp goto done;
6160 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6161 69844fba 2019-07-11 stsp if (err)
6162 69844fba 2019-07-11 stsp goto done;
6163 69844fba 2019-07-11 stsp
6164 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6165 69844fba 2019-07-11 stsp if (err)
6166 69844fba 2019-07-11 stsp goto done;
6167 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6168 69844fba 2019-07-11 stsp if (err)
6169 69844fba 2019-07-11 stsp goto done;
6170 69844fba 2019-07-11 stsp
6171 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6172 69844fba 2019-07-11 stsp if (err)
6173 69844fba 2019-07-11 stsp goto done;
6174 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6175 69844fba 2019-07-11 stsp if (err)
6176 69844fba 2019-07-11 stsp goto done;
6177 69844fba 2019-07-11 stsp
6178 69844fba 2019-07-11 stsp done:
6179 69844fba 2019-07-11 stsp free(tmp_branch_name);
6180 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6181 69844fba 2019-07-11 stsp free(branch_ref_name);
6182 69844fba 2019-07-11 stsp free(commit_ref_name);
6183 69844fba 2019-07-11 stsp return err;
6184 69844fba 2019-07-11 stsp }
6185 69844fba 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp const struct got_error *
6187 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6188 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6189 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6190 818c7501 2019-07-11 stsp struct got_repository *repo)
6191 818c7501 2019-07-11 stsp {
6192 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6193 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6194 818c7501 2019-07-11 stsp
6195 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6196 818c7501 2019-07-11 stsp if (err)
6197 818c7501 2019-07-11 stsp return err;
6198 818c7501 2019-07-11 stsp
6199 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6200 818c7501 2019-07-11 stsp if (err)
6201 818c7501 2019-07-11 stsp goto done;
6202 818c7501 2019-07-11 stsp
6203 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6204 818c7501 2019-07-11 stsp if (err)
6205 818c7501 2019-07-11 stsp goto done;
6206 818c7501 2019-07-11 stsp
6207 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6208 818c7501 2019-07-11 stsp if (err)
6209 818c7501 2019-07-11 stsp goto done;
6210 818c7501 2019-07-11 stsp
6211 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6212 818c7501 2019-07-11 stsp done:
6213 3e3a69f1 2019-07-25 stsp if (fileindex)
6214 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6215 818c7501 2019-07-11 stsp free(new_head_commit_id);
6216 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6217 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6218 818c7501 2019-07-11 stsp err = unlockerr;
6219 818c7501 2019-07-11 stsp return err;
6220 818c7501 2019-07-11 stsp }
6221 818c7501 2019-07-11 stsp
6222 818c7501 2019-07-11 stsp const struct got_error *
6223 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6224 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6225 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6226 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6227 818c7501 2019-07-11 stsp {
6228 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6229 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6230 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6231 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6232 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6233 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6234 818c7501 2019-07-11 stsp
6235 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6236 818c7501 2019-07-11 stsp if (err)
6237 818c7501 2019-07-11 stsp return err;
6238 818c7501 2019-07-11 stsp
6239 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6240 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6241 818c7501 2019-07-11 stsp if (err)
6242 818c7501 2019-07-11 stsp goto done;
6243 818c7501 2019-07-11 stsp
6244 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6245 818c7501 2019-07-11 stsp if (err)
6246 818c7501 2019-07-11 stsp goto done;
6247 818c7501 2019-07-11 stsp
6248 818c7501 2019-07-11 stsp /*
6249 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6250 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6251 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6252 818c7501 2019-07-11 stsp */
6253 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6254 818c7501 2019-07-11 stsp if (err)
6255 818c7501 2019-07-11 stsp goto done;
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6258 818c7501 2019-07-11 stsp if (err)
6259 818c7501 2019-07-11 stsp goto done;
6260 818c7501 2019-07-11 stsp
6261 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6262 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6263 ca355955 2019-07-12 stsp if (err)
6264 ca355955 2019-07-12 stsp goto done;
6265 ca355955 2019-07-12 stsp
6266 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6267 ca355955 2019-07-12 stsp if (err)
6268 ca355955 2019-07-12 stsp goto done;
6269 ca355955 2019-07-12 stsp
6270 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6271 818c7501 2019-07-11 stsp if (err)
6272 818c7501 2019-07-11 stsp goto done;
6273 818c7501 2019-07-11 stsp
6274 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6275 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6276 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6277 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6278 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6279 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6280 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6281 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6282 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6283 55bd499d 2019-07-12 stsp if (err)
6284 1f1abb7e 2019-08-08 stsp goto sync;
6285 55bd499d 2019-07-12 stsp
6286 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6287 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6288 ca355955 2019-07-12 stsp sync:
6289 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6290 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6291 ca355955 2019-07-12 stsp err = sync_err;
6292 818c7501 2019-07-11 stsp done:
6293 818c7501 2019-07-11 stsp got_ref_close(resolved);
6294 a3a2faf2 2019-07-12 stsp free(tree_id);
6295 818c7501 2019-07-11 stsp free(commit_id);
6296 0ebf8283 2019-07-24 stsp if (fileindex)
6297 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6298 0ebf8283 2019-07-24 stsp free(fileindex_path);
6299 0ebf8283 2019-07-24 stsp
6300 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6301 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6302 0ebf8283 2019-07-24 stsp err = unlockerr;
6303 0ebf8283 2019-07-24 stsp return err;
6304 0ebf8283 2019-07-24 stsp }
6305 0ebf8283 2019-07-24 stsp
6306 0ebf8283 2019-07-24 stsp const struct got_error *
6307 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6308 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6309 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6310 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6311 0ebf8283 2019-07-24 stsp {
6312 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6313 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6314 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6315 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6316 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6317 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6318 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6319 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6320 0ebf8283 2019-07-24 stsp
6321 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6322 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6323 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6324 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6325 0ebf8283 2019-07-24 stsp
6326 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6327 0ebf8283 2019-07-24 stsp if (err)
6328 0ebf8283 2019-07-24 stsp return err;
6329 0ebf8283 2019-07-24 stsp
6330 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6331 0ebf8283 2019-07-24 stsp if (err)
6332 0ebf8283 2019-07-24 stsp goto done;
6333 0ebf8283 2019-07-24 stsp
6334 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6335 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6336 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6337 0ebf8283 2019-07-24 stsp &ok_arg);
6338 0ebf8283 2019-07-24 stsp if (err)
6339 0ebf8283 2019-07-24 stsp goto done;
6340 0ebf8283 2019-07-24 stsp
6341 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6342 0ebf8283 2019-07-24 stsp if (err)
6343 0ebf8283 2019-07-24 stsp goto done;
6344 0ebf8283 2019-07-24 stsp
6345 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6346 0ebf8283 2019-07-24 stsp if (err)
6347 0ebf8283 2019-07-24 stsp goto done;
6348 0ebf8283 2019-07-24 stsp
6349 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6350 0ebf8283 2019-07-24 stsp worktree);
6351 0ebf8283 2019-07-24 stsp if (err)
6352 0ebf8283 2019-07-24 stsp goto done;
6353 0ebf8283 2019-07-24 stsp
6354 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6355 0ebf8283 2019-07-24 stsp 0);
6356 0ebf8283 2019-07-24 stsp if (err)
6357 0ebf8283 2019-07-24 stsp goto done;
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6360 0ebf8283 2019-07-24 stsp if (err)
6361 0ebf8283 2019-07-24 stsp goto done;
6362 0ebf8283 2019-07-24 stsp
6363 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6364 0ebf8283 2019-07-24 stsp if (err)
6365 0ebf8283 2019-07-24 stsp goto done;
6366 0ebf8283 2019-07-24 stsp
6367 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6368 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6369 0ebf8283 2019-07-24 stsp if (err)
6370 0ebf8283 2019-07-24 stsp goto done;
6371 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6372 0ebf8283 2019-07-24 stsp if (err)
6373 0ebf8283 2019-07-24 stsp goto done;
6374 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6375 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6376 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6377 0ebf8283 2019-07-24 stsp goto done;
6378 0ebf8283 2019-07-24 stsp }
6379 0ebf8283 2019-07-24 stsp
6380 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6381 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6382 0ebf8283 2019-07-24 stsp if (err)
6383 0ebf8283 2019-07-24 stsp goto done;
6384 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6385 0ebf8283 2019-07-24 stsp if (err)
6386 0ebf8283 2019-07-24 stsp goto done;
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6389 0ebf8283 2019-07-24 stsp if (err)
6390 0ebf8283 2019-07-24 stsp goto done;
6391 0ebf8283 2019-07-24 stsp done:
6392 0ebf8283 2019-07-24 stsp free(fileindex_path);
6393 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6394 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6395 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6396 0ebf8283 2019-07-24 stsp if (wt_branch)
6397 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6398 0ebf8283 2019-07-24 stsp if (err) {
6399 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6400 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6401 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6402 0ebf8283 2019-07-24 stsp }
6403 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6404 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6405 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6406 0ebf8283 2019-07-24 stsp }
6407 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6408 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6409 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6411 3e3a69f1 2019-07-25 stsp }
6412 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6413 0ebf8283 2019-07-24 stsp }
6414 0ebf8283 2019-07-24 stsp return err;
6415 0ebf8283 2019-07-24 stsp }
6416 0ebf8283 2019-07-24 stsp
6417 0ebf8283 2019-07-24 stsp const struct got_error *
6418 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6419 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6420 0ebf8283 2019-07-24 stsp {
6421 3e3a69f1 2019-07-25 stsp if (fileindex)
6422 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6423 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6424 0ebf8283 2019-07-24 stsp }
6425 0ebf8283 2019-07-24 stsp
6426 0ebf8283 2019-07-24 stsp const struct got_error *
6427 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6428 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6429 0ebf8283 2019-07-24 stsp {
6430 0ebf8283 2019-07-24 stsp const struct got_error *err;
6431 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6434 0ebf8283 2019-07-24 stsp if (err)
6435 0ebf8283 2019-07-24 stsp return err;
6436 0ebf8283 2019-07-24 stsp
6437 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6438 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6439 0ebf8283 2019-07-24 stsp return NULL;
6440 0ebf8283 2019-07-24 stsp }
6441 0ebf8283 2019-07-24 stsp
6442 0ebf8283 2019-07-24 stsp const struct got_error *
6443 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6444 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6445 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6446 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6447 0ebf8283 2019-07-24 stsp {
6448 0ebf8283 2019-07-24 stsp const struct got_error *err;
6449 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6450 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6451 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6452 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6453 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6454 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6455 0ebf8283 2019-07-24 stsp
6456 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6457 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6458 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6459 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6460 0ebf8283 2019-07-24 stsp
6461 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6462 3e3a69f1 2019-07-25 stsp if (err)
6463 3e3a69f1 2019-07-25 stsp return err;
6464 3e3a69f1 2019-07-25 stsp
6465 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6466 3e3a69f1 2019-07-25 stsp if (err)
6467 f032f1f7 2019-08-04 stsp goto done;
6468 f032f1f7 2019-08-04 stsp
6469 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6470 f032f1f7 2019-08-04 stsp &have_staged_files);
6471 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6472 f032f1f7 2019-08-04 stsp goto done;
6473 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6474 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6475 3e3a69f1 2019-07-25 stsp goto done;
6476 f032f1f7 2019-08-04 stsp }
6477 3e3a69f1 2019-07-25 stsp
6478 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6479 0ebf8283 2019-07-24 stsp if (err)
6480 f032f1f7 2019-08-04 stsp goto done;
6481 0ebf8283 2019-07-24 stsp
6482 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6483 0ebf8283 2019-07-24 stsp if (err)
6484 0ebf8283 2019-07-24 stsp goto done;
6485 0ebf8283 2019-07-24 stsp
6486 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6487 0ebf8283 2019-07-24 stsp if (err)
6488 0ebf8283 2019-07-24 stsp goto done;
6489 0ebf8283 2019-07-24 stsp
6490 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6491 0ebf8283 2019-07-24 stsp worktree);
6492 0ebf8283 2019-07-24 stsp if (err)
6493 0ebf8283 2019-07-24 stsp goto done;
6494 0ebf8283 2019-07-24 stsp
6495 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6496 0ebf8283 2019-07-24 stsp if (err)
6497 0ebf8283 2019-07-24 stsp goto done;
6498 0ebf8283 2019-07-24 stsp
6499 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6500 0ebf8283 2019-07-24 stsp if (err)
6501 0ebf8283 2019-07-24 stsp goto done;
6502 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6503 0ebf8283 2019-07-24 stsp if (err)
6504 0ebf8283 2019-07-24 stsp goto done;
6505 0ebf8283 2019-07-24 stsp
6506 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6507 0ebf8283 2019-07-24 stsp if (err)
6508 0ebf8283 2019-07-24 stsp goto done;
6509 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6510 0ebf8283 2019-07-24 stsp if (err)
6511 0ebf8283 2019-07-24 stsp goto done;
6512 0ebf8283 2019-07-24 stsp
6513 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6514 0ebf8283 2019-07-24 stsp if (err)
6515 0ebf8283 2019-07-24 stsp goto done;
6516 0ebf8283 2019-07-24 stsp done:
6517 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6518 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6519 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6520 0ebf8283 2019-07-24 stsp if (commit_ref)
6521 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6522 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6523 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6524 0ebf8283 2019-07-24 stsp if (err) {
6525 0ebf8283 2019-07-24 stsp free(*commit_id);
6526 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6527 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6528 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6529 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6530 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6531 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6532 0ebf8283 2019-07-24 stsp }
6533 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6534 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6535 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6536 3e3a69f1 2019-07-25 stsp }
6537 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6538 0ebf8283 2019-07-24 stsp }
6539 0ebf8283 2019-07-24 stsp return err;
6540 0ebf8283 2019-07-24 stsp }
6541 0ebf8283 2019-07-24 stsp
6542 0ebf8283 2019-07-24 stsp static const struct got_error *
6543 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6544 0ebf8283 2019-07-24 stsp {
6545 0ebf8283 2019-07-24 stsp const struct got_error *err;
6546 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6547 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6548 0ebf8283 2019-07-24 stsp
6549 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6553 0ebf8283 2019-07-24 stsp if (err)
6554 0ebf8283 2019-07-24 stsp goto done;
6555 0ebf8283 2019-07-24 stsp
6556 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6557 0ebf8283 2019-07-24 stsp worktree);
6558 0ebf8283 2019-07-24 stsp if (err)
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6561 0ebf8283 2019-07-24 stsp if (err)
6562 0ebf8283 2019-07-24 stsp goto done;
6563 0ebf8283 2019-07-24 stsp
6564 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6565 0ebf8283 2019-07-24 stsp if (err)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6568 0ebf8283 2019-07-24 stsp if (err)
6569 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp
6571 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6572 0ebf8283 2019-07-24 stsp if (err)
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6575 0ebf8283 2019-07-24 stsp if (err)
6576 0ebf8283 2019-07-24 stsp goto done;
6577 0ebf8283 2019-07-24 stsp done:
6578 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6579 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6580 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6581 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6582 0ebf8283 2019-07-24 stsp return err;
6583 0ebf8283 2019-07-24 stsp }
6584 0ebf8283 2019-07-24 stsp
6585 0ebf8283 2019-07-24 stsp const struct got_error *
6586 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6587 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6588 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6589 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6590 0ebf8283 2019-07-24 stsp {
6591 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6592 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6593 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6594 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6595 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6596 0ebf8283 2019-07-24 stsp
6597 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6598 0ebf8283 2019-07-24 stsp if (err)
6599 0ebf8283 2019-07-24 stsp return err;
6600 0ebf8283 2019-07-24 stsp
6601 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6602 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6603 0ebf8283 2019-07-24 stsp if (err)
6604 0ebf8283 2019-07-24 stsp goto done;
6605 0ebf8283 2019-07-24 stsp
6606 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6607 0ebf8283 2019-07-24 stsp if (err)
6608 0ebf8283 2019-07-24 stsp goto done;
6609 0ebf8283 2019-07-24 stsp
6610 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6611 0ebf8283 2019-07-24 stsp if (err)
6612 0ebf8283 2019-07-24 stsp goto done;
6613 0ebf8283 2019-07-24 stsp
6614 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6615 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6616 0ebf8283 2019-07-24 stsp if (err)
6617 0ebf8283 2019-07-24 stsp goto done;
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6620 0ebf8283 2019-07-24 stsp if (err)
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp
6623 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6624 0ebf8283 2019-07-24 stsp if (err)
6625 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp
6627 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6628 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6629 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6630 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6631 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6632 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6633 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6634 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6635 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6636 0ebf8283 2019-07-24 stsp if (err)
6637 1f1abb7e 2019-08-08 stsp goto sync;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6640 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6641 0ebf8283 2019-07-24 stsp sync:
6642 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6643 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6644 0ebf8283 2019-07-24 stsp err = sync_err;
6645 0ebf8283 2019-07-24 stsp done:
6646 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6647 0ebf8283 2019-07-24 stsp free(tree_id);
6648 818c7501 2019-07-11 stsp free(fileindex_path);
6649 818c7501 2019-07-11 stsp
6650 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6651 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6652 818c7501 2019-07-11 stsp err = unlockerr;
6653 818c7501 2019-07-11 stsp return err;
6654 818c7501 2019-07-11 stsp }
6655 0ebf8283 2019-07-24 stsp
6656 0ebf8283 2019-07-24 stsp const struct got_error *
6657 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6658 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6659 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6660 0ebf8283 2019-07-24 stsp {
6661 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6662 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6663 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6664 0ebf8283 2019-07-24 stsp
6665 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6666 0ebf8283 2019-07-24 stsp if (err)
6667 0ebf8283 2019-07-24 stsp return err;
6668 0ebf8283 2019-07-24 stsp
6669 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6670 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6671 0ebf8283 2019-07-24 stsp if (err)
6672 0ebf8283 2019-07-24 stsp goto done;
6673 0ebf8283 2019-07-24 stsp
6674 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6675 0ebf8283 2019-07-24 stsp if (err)
6676 0ebf8283 2019-07-24 stsp goto done;
6677 0ebf8283 2019-07-24 stsp
6678 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6679 0ebf8283 2019-07-24 stsp if (err)
6680 0ebf8283 2019-07-24 stsp goto done;
6681 0ebf8283 2019-07-24 stsp
6682 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6683 0ebf8283 2019-07-24 stsp if (err)
6684 0ebf8283 2019-07-24 stsp goto done;
6685 0ebf8283 2019-07-24 stsp
6686 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6687 0ebf8283 2019-07-24 stsp done:
6688 3e3a69f1 2019-07-25 stsp if (fileindex)
6689 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6690 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6691 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6692 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6693 0ebf8283 2019-07-24 stsp err = unlockerr;
6694 0ebf8283 2019-07-24 stsp return err;
6695 0ebf8283 2019-07-24 stsp }
6696 0ebf8283 2019-07-24 stsp
6697 0ebf8283 2019-07-24 stsp const struct got_error *
6698 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6699 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6700 0ebf8283 2019-07-24 stsp {
6701 0ebf8283 2019-07-24 stsp const struct got_error *err;
6702 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6705 0ebf8283 2019-07-24 stsp if (err)
6706 0ebf8283 2019-07-24 stsp return err;
6707 0ebf8283 2019-07-24 stsp
6708 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6709 0ebf8283 2019-07-24 stsp if (err)
6710 0ebf8283 2019-07-24 stsp goto done;
6711 0ebf8283 2019-07-24 stsp
6712 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6713 0ebf8283 2019-07-24 stsp done:
6714 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6715 2822a352 2019-10-15 stsp return err;
6716 2822a352 2019-10-15 stsp }
6717 2822a352 2019-10-15 stsp
6718 2822a352 2019-10-15 stsp const struct got_error *
6719 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6720 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6721 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6722 2822a352 2019-10-15 stsp struct got_repository *repo)
6723 2822a352 2019-10-15 stsp {
6724 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6725 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6726 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6727 2822a352 2019-10-15 stsp
6728 2822a352 2019-10-15 stsp *fileindex = NULL;
6729 2822a352 2019-10-15 stsp *branch_ref = NULL;
6730 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6731 2822a352 2019-10-15 stsp
6732 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6733 2822a352 2019-10-15 stsp if (err)
6734 2822a352 2019-10-15 stsp return err;
6735 2822a352 2019-10-15 stsp
6736 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6737 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6738 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6739 2822a352 2019-10-15 stsp "update -b or different branch name required");
6740 2822a352 2019-10-15 stsp goto done;
6741 2822a352 2019-10-15 stsp }
6742 2822a352 2019-10-15 stsp
6743 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 2822a352 2019-10-15 stsp if (err)
6745 2822a352 2019-10-15 stsp goto done;
6746 2822a352 2019-10-15 stsp
6747 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6748 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6749 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6750 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6751 2822a352 2019-10-15 stsp &ok_arg);
6752 2822a352 2019-10-15 stsp if (err)
6753 2822a352 2019-10-15 stsp goto done;
6754 2822a352 2019-10-15 stsp
6755 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6756 2822a352 2019-10-15 stsp if (err)
6757 2822a352 2019-10-15 stsp goto done;
6758 2822a352 2019-10-15 stsp
6759 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6760 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6761 2822a352 2019-10-15 stsp done:
6762 2822a352 2019-10-15 stsp if (err) {
6763 2822a352 2019-10-15 stsp if (*branch_ref) {
6764 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6765 2822a352 2019-10-15 stsp *branch_ref = NULL;
6766 2822a352 2019-10-15 stsp }
6767 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6768 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6769 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6770 2822a352 2019-10-15 stsp }
6771 2822a352 2019-10-15 stsp if (*fileindex) {
6772 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6773 2822a352 2019-10-15 stsp *fileindex = NULL;
6774 2822a352 2019-10-15 stsp }
6775 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6776 2822a352 2019-10-15 stsp }
6777 0cb83759 2019-08-03 stsp return err;
6778 0cb83759 2019-08-03 stsp }
6779 0cb83759 2019-08-03 stsp
6780 2822a352 2019-10-15 stsp const struct got_error *
6781 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6782 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6783 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6784 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6786 2822a352 2019-10-15 stsp {
6787 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6788 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6789 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6790 2822a352 2019-10-15 stsp
6791 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6792 2822a352 2019-10-15 stsp if (err)
6793 2822a352 2019-10-15 stsp goto done;
6794 2822a352 2019-10-15 stsp
6795 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6796 2822a352 2019-10-15 stsp if (err)
6797 2822a352 2019-10-15 stsp goto done;
6798 2822a352 2019-10-15 stsp
6799 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6800 2822a352 2019-10-15 stsp worktree->path_prefix);
6801 2822a352 2019-10-15 stsp if (err)
6802 2822a352 2019-10-15 stsp goto done;
6803 2822a352 2019-10-15 stsp
6804 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6805 2822a352 2019-10-15 stsp if (err)
6806 2822a352 2019-10-15 stsp goto done;
6807 2822a352 2019-10-15 stsp
6808 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6809 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6810 2822a352 2019-10-15 stsp if (err)
6811 2822a352 2019-10-15 stsp goto sync;
6812 2822a352 2019-10-15 stsp
6813 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6814 2822a352 2019-10-15 stsp if (err)
6815 2822a352 2019-10-15 stsp goto sync;
6816 2822a352 2019-10-15 stsp
6817 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6818 2822a352 2019-10-15 stsp sync:
6819 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6820 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6821 2822a352 2019-10-15 stsp err = sync_err;
6822 2822a352 2019-10-15 stsp
6823 2822a352 2019-10-15 stsp done:
6824 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6825 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6826 2822a352 2019-10-15 stsp err = unlockerr;
6827 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6828 2822a352 2019-10-15 stsp
6829 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6830 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6831 2822a352 2019-10-15 stsp err = unlockerr;
6832 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6833 2822a352 2019-10-15 stsp
6834 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6835 2822a352 2019-10-15 stsp free(fileindex_path);
6836 2822a352 2019-10-15 stsp free(tree_id);
6837 2822a352 2019-10-15 stsp
6838 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6839 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6840 2822a352 2019-10-15 stsp err = unlockerr;
6841 2822a352 2019-10-15 stsp return err;
6842 2822a352 2019-10-15 stsp }
6843 2822a352 2019-10-15 stsp
6844 2822a352 2019-10-15 stsp const struct got_error *
6845 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6846 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6847 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6848 2822a352 2019-10-15 stsp {
6849 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6850 8b692cd0 2019-10-21 stsp
6851 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6852 8b692cd0 2019-10-21 stsp
6853 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6854 8b692cd0 2019-10-21 stsp
6855 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6856 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6857 8b692cd0 2019-10-21 stsp err = unlockerr;
6858 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6859 8b692cd0 2019-10-21 stsp
6860 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6861 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6862 8b692cd0 2019-10-21 stsp err = unlockerr;
6863 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6864 8b692cd0 2019-10-21 stsp
6865 8b692cd0 2019-10-21 stsp return err;
6866 2822a352 2019-10-15 stsp }
6867 2822a352 2019-10-15 stsp
6868 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6869 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6870 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6871 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6872 2db2652d 2019-08-07 stsp struct got_repository *repo;
6873 2db2652d 2019-08-07 stsp int have_changes;
6874 2db2652d 2019-08-07 stsp };
6875 2db2652d 2019-08-07 stsp
6876 2db2652d 2019-08-07 stsp const struct got_error *
6877 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6878 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6879 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6880 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6881 735ef5ac 2019-08-03 stsp {
6882 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6883 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6884 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6885 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6886 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6887 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6888 8b13ce36 2019-08-08 stsp
6889 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6890 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6891 8b13ce36 2019-08-08 stsp return NULL;
6892 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6893 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6894 735ef5ac 2019-08-03 stsp
6895 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6896 735ef5ac 2019-08-03 stsp if (ie == NULL)
6897 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6898 735ef5ac 2019-08-03 stsp
6899 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6900 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6901 735ef5ac 2019-08-03 stsp relpath) == -1)
6902 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6903 735ef5ac 2019-08-03 stsp
6904 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6905 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6906 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6907 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6908 735ef5ac 2019-08-03 stsp }
6909 735ef5ac 2019-08-03 stsp
6910 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6911 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6912 3aa5969e 2019-08-06 stsp goto done;
6913 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6914 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6915 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6916 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6917 735ef5ac 2019-08-03 stsp goto done;
6918 3aa5969e 2019-08-06 stsp }
6919 735ef5ac 2019-08-03 stsp
6920 2db2652d 2019-08-07 stsp a->have_changes = 1;
6921 2db2652d 2019-08-07 stsp
6922 735ef5ac 2019-08-03 stsp p = in_repo_path;
6923 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6924 735ef5ac 2019-08-03 stsp p++;
6925 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6926 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6927 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6928 735ef5ac 2019-08-03 stsp done:
6929 735ef5ac 2019-08-03 stsp free(in_repo_path);
6930 dc424a06 2019-08-07 stsp return err;
6931 dc424a06 2019-08-07 stsp }
6932 dc424a06 2019-08-07 stsp
6933 2db2652d 2019-08-07 stsp struct stage_path_arg {
6934 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6935 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6936 2db2652d 2019-08-07 stsp struct got_repository *repo;
6937 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6938 2db2652d 2019-08-07 stsp void *status_arg;
6939 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6940 2db2652d 2019-08-07 stsp void *patch_arg;
6941 7b5dc508 2019-10-28 stsp int staged_something;
6942 2db2652d 2019-08-07 stsp };
6943 2db2652d 2019-08-07 stsp
6944 2db2652d 2019-08-07 stsp static const struct got_error *
6945 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6946 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6947 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6948 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6949 0cb83759 2019-08-03 stsp {
6950 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6951 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6952 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6953 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6954 0cb83759 2019-08-03 stsp uint32_t stage;
6955 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6956 8b13ce36 2019-08-08 stsp
6957 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6958 8b13ce36 2019-08-08 stsp return NULL;
6959 0cb83759 2019-08-03 stsp
6960 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6961 d3e7c587 2019-08-03 stsp if (ie == NULL)
6962 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6963 0cb83759 2019-08-03 stsp
6964 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6965 2db2652d 2019-08-07 stsp relpath)== -1)
6966 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6967 0cb83759 2019-08-03 stsp
6968 0cb83759 2019-08-03 stsp switch (status) {
6969 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6970 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6971 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6972 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6973 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6974 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6975 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6976 dc424a06 2019-08-07 stsp if (err)
6977 dc424a06 2019-08-07 stsp break;
6978 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6979 dc424a06 2019-08-07 stsp break;
6980 dc424a06 2019-08-07 stsp } else {
6981 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6982 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6983 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6984 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6985 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6986 dc424a06 2019-08-07 stsp break;
6987 dc424a06 2019-08-07 stsp }
6988 dc424a06 2019-08-07 stsp }
6989 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6990 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6991 0cb83759 2019-08-03 stsp if (err)
6992 dc424a06 2019-08-07 stsp break;
6993 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6994 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6995 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6996 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6997 0cb83759 2019-08-03 stsp else
6998 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6999 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7000 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7001 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7002 dc424a06 2019-08-07 stsp break;
7003 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7004 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7005 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7006 0cb83759 2019-08-03 stsp break;
7007 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7008 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7009 d3e7c587 2019-08-03 stsp break;
7010 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7011 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7012 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7013 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7014 dc424a06 2019-08-07 stsp if (err)
7015 dc424a06 2019-08-07 stsp break;
7016 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7017 88f33a19 2019-08-08 stsp break;
7018 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7019 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7020 dc424a06 2019-08-07 stsp break;
7021 88f33a19 2019-08-08 stsp }
7022 dc424a06 2019-08-07 stsp }
7023 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7024 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7025 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7026 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7027 dc424a06 2019-08-07 stsp break;
7028 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7029 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7030 12463d8b 2019-12-13 stsp de_name);
7031 0cb83759 2019-08-03 stsp break;
7032 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7033 d3e7c587 2019-08-03 stsp break;
7034 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7035 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7036 ebf48fd5 2019-08-03 stsp break;
7037 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7038 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7039 2a06fe5f 2019-08-24 stsp break;
7040 0cb83759 2019-08-03 stsp default:
7041 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7042 537ac44b 2019-08-03 stsp break;
7043 0cb83759 2019-08-03 stsp }
7044 dc424a06 2019-08-07 stsp
7045 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7046 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7047 dc424a06 2019-08-07 stsp free(path_content);
7048 2db2652d 2019-08-07 stsp free(ondisk_path);
7049 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7050 0ebf8283 2019-07-24 stsp return err;
7051 0ebf8283 2019-07-24 stsp }
7052 0cb83759 2019-08-03 stsp
7053 0cb83759 2019-08-03 stsp const struct got_error *
7054 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7055 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7056 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7057 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7058 1e71573e 2019-08-03 stsp struct got_repository *repo)
7059 0cb83759 2019-08-03 stsp {
7060 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7061 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7062 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7063 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7064 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7065 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7066 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7067 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7068 0cb83759 2019-08-03 stsp
7069 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7070 0cb83759 2019-08-03 stsp if (err)
7071 0cb83759 2019-08-03 stsp return err;
7072 0cb83759 2019-08-03 stsp
7073 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7074 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7075 735ef5ac 2019-08-03 stsp if (err)
7076 735ef5ac 2019-08-03 stsp goto done;
7077 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7078 735ef5ac 2019-08-03 stsp if (err)
7079 735ef5ac 2019-08-03 stsp goto done;
7080 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7081 0cb83759 2019-08-03 stsp if (err)
7082 0cb83759 2019-08-03 stsp goto done;
7083 0cb83759 2019-08-03 stsp
7084 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7085 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7086 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7087 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7088 2db2652d 2019-08-07 stsp oka.repo = repo;
7089 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7090 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7091 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7092 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7093 735ef5ac 2019-08-03 stsp if (err)
7094 735ef5ac 2019-08-03 stsp goto done;
7095 735ef5ac 2019-08-03 stsp }
7096 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7097 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7098 2db2652d 2019-08-07 stsp goto done;
7099 2db2652d 2019-08-07 stsp }
7100 735ef5ac 2019-08-03 stsp
7101 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7102 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7103 2db2652d 2019-08-07 stsp spa.repo = repo;
7104 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7105 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7106 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7107 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7108 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7109 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7110 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7111 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7112 42005733 2019-08-03 stsp if (err)
7113 2db2652d 2019-08-07 stsp goto done;
7114 7b5dc508 2019-10-28 stsp }
7115 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7116 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7117 7b5dc508 2019-10-28 stsp goto done;
7118 0cb83759 2019-08-03 stsp }
7119 0cb83759 2019-08-03 stsp
7120 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7121 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7122 0cb83759 2019-08-03 stsp err = sync_err;
7123 0cb83759 2019-08-03 stsp done:
7124 735ef5ac 2019-08-03 stsp if (head_ref)
7125 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7126 735ef5ac 2019-08-03 stsp free(head_commit_id);
7127 0cb83759 2019-08-03 stsp free(fileindex_path);
7128 0cb83759 2019-08-03 stsp if (fileindex)
7129 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7130 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7131 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7132 0cb83759 2019-08-03 stsp err = unlockerr;
7133 0cb83759 2019-08-03 stsp return err;
7134 0cb83759 2019-08-03 stsp }
7135 ad493afc 2019-08-03 stsp
7136 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7137 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7138 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7139 ad493afc 2019-08-03 stsp struct got_repository *repo;
7140 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7141 ad493afc 2019-08-03 stsp void *progress_arg;
7142 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7143 2e1f37b0 2019-08-08 stsp void *patch_arg;
7144 ad493afc 2019-08-03 stsp };
7145 2e1f37b0 2019-08-08 stsp
7146 2e1f37b0 2019-08-08 stsp static const struct got_error *
7147 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7148 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7149 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7150 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7151 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7152 2e1f37b0 2019-08-08 stsp {
7153 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7154 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7155 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7156 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7157 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7158 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7159 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7160 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7161 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7162 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7163 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7164 2e1f37b0 2019-08-08 stsp
7165 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7166 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7167 2e1f37b0 2019-08-08 stsp
7168 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7169 2e1f37b0 2019-08-08 stsp if (err)
7170 2e1f37b0 2019-08-08 stsp return err;
7171 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7172 2e1f37b0 2019-08-08 stsp if (err)
7173 2e1f37b0 2019-08-08 stsp goto done;
7174 2e1f37b0 2019-08-08 stsp
7175 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7176 2e1f37b0 2019-08-08 stsp if (err)
7177 2e1f37b0 2019-08-08 stsp goto done;
7178 2e1f37b0 2019-08-08 stsp
7179 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7180 2e1f37b0 2019-08-08 stsp if (err)
7181 2e1f37b0 2019-08-08 stsp goto done;
7182 2e1f37b0 2019-08-08 stsp
7183 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7184 2e1f37b0 2019-08-08 stsp if (err)
7185 2e1f37b0 2019-08-08 stsp goto done;
7186 2e1f37b0 2019-08-08 stsp
7187 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7188 2e1f37b0 2019-08-08 stsp if (err)
7189 2e1f37b0 2019-08-08 stsp goto done;
7190 ad493afc 2019-08-03 stsp
7191 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7192 2e1f37b0 2019-08-08 stsp if (err)
7193 2e1f37b0 2019-08-08 stsp goto done;
7194 2e1f37b0 2019-08-08 stsp
7195 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7196 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7197 2e1f37b0 2019-08-08 stsp goto done;
7198 2e1f37b0 2019-08-08 stsp }
7199 2e1f37b0 2019-08-08 stsp
7200 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7201 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7202 2e1f37b0 2019-08-08 stsp goto done;
7203 2e1f37b0 2019-08-08 stsp }
7204 2e1f37b0 2019-08-08 stsp
7205 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7206 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7207 2e1f37b0 2019-08-08 stsp if (err)
7208 2e1f37b0 2019-08-08 stsp goto done;
7209 2e1f37b0 2019-08-08 stsp
7210 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7211 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7212 2e1f37b0 2019-08-08 stsp if (err)
7213 2e1f37b0 2019-08-08 stsp goto done;
7214 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7215 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7216 2e1f37b0 2019-08-08 stsp if (err)
7217 2e1f37b0 2019-08-08 stsp goto done;
7218 2e1f37b0 2019-08-08 stsp
7219 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7220 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7221 2e1f37b0 2019-08-08 stsp goto done;
7222 2e1f37b0 2019-08-08 stsp }
7223 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7224 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7225 2e1f37b0 2019-08-08 stsp goto done;
7226 2e1f37b0 2019-08-08 stsp }
7227 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7228 2e1f37b0 2019-08-08 stsp int choice;
7229 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7230 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7231 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7232 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7233 2e1f37b0 2019-08-08 stsp if (err)
7234 2e1f37b0 2019-08-08 stsp goto done;
7235 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7236 2e1f37b0 2019-08-08 stsp have_content = 1;
7237 19e4b907 2019-08-08 stsp else
7238 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7239 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7240 2e1f37b0 2019-08-08 stsp break;
7241 2e1f37b0 2019-08-08 stsp }
7242 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7243 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7244 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7245 2e1f37b0 2019-08-08 stsp done:
7246 2e1f37b0 2019-08-08 stsp free(label1);
7247 2e1f37b0 2019-08-08 stsp if (blob)
7248 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7249 2e1f37b0 2019-08-08 stsp if (staged_blob)
7250 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7251 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7252 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7253 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7254 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7255 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7256 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7257 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7258 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7259 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7260 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7261 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7262 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7263 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7264 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7265 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7266 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7267 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7268 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7269 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7270 2e1f37b0 2019-08-08 stsp }
7271 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7272 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7273 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7274 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7275 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7276 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7277 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7278 2e1f37b0 2019-08-08 stsp }
7279 2e1f37b0 2019-08-08 stsp free(args);
7280 2e1f37b0 2019-08-08 stsp if (ds) {
7281 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7282 2e1f37b0 2019-08-08 stsp free(ds);
7283 2e1f37b0 2019-08-08 stsp }
7284 2e1f37b0 2019-08-08 stsp if (changes)
7285 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7286 2e1f37b0 2019-08-08 stsp free(path1);
7287 2e1f37b0 2019-08-08 stsp free(path2);
7288 2e1f37b0 2019-08-08 stsp return err;
7289 2e1f37b0 2019-08-08 stsp }
7290 2e1f37b0 2019-08-08 stsp
7291 ad493afc 2019-08-03 stsp static const struct got_error *
7292 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7293 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7294 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7295 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7296 ad493afc 2019-08-03 stsp {
7297 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7298 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7299 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7300 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7301 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7302 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7303 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7304 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7305 9bc94a15 2019-08-03 stsp struct stat sb;
7306 ad493afc 2019-08-03 stsp
7307 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7308 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7309 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7310 2e1f37b0 2019-08-08 stsp return NULL;
7311 2e1f37b0 2019-08-08 stsp
7312 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7313 ad493afc 2019-08-03 stsp if (ie == NULL)
7314 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7315 9bc94a15 2019-08-03 stsp
7316 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7317 9bc94a15 2019-08-03 stsp == -1)
7318 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7319 ad493afc 2019-08-03 stsp
7320 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7321 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7322 f69721c3 2019-10-21 stsp if (err)
7323 f69721c3 2019-10-21 stsp goto done;
7324 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7325 f69721c3 2019-10-21 stsp id_str) == -1) {
7326 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7327 f69721c3 2019-10-21 stsp goto done;
7328 f69721c3 2019-10-21 stsp }
7329 f69721c3 2019-10-21 stsp
7330 ad493afc 2019-08-03 stsp switch (staged_status) {
7331 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7332 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7333 ad493afc 2019-08-03 stsp blob_id, 8192);
7334 ad493afc 2019-08-03 stsp if (err)
7335 ad493afc 2019-08-03 stsp break;
7336 ad493afc 2019-08-03 stsp /* fall through */
7337 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7338 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7339 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7340 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7341 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7342 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7343 2e1f37b0 2019-08-08 stsp if (err)
7344 2e1f37b0 2019-08-08 stsp break;
7345 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7346 2e1f37b0 2019-08-08 stsp break;
7347 2e1f37b0 2019-08-08 stsp } else {
7348 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7349 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7350 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7351 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7352 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7353 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7354 2e1f37b0 2019-08-08 stsp break;
7355 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7356 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7357 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7358 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7359 2e1f37b0 2019-08-08 stsp a->repo);
7360 2e1f37b0 2019-08-08 stsp if (err)
7361 2e1f37b0 2019-08-08 stsp break;
7362 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7363 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7364 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7365 2e1f37b0 2019-08-08 stsp }
7366 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7367 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7368 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7369 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7370 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7371 f69721c3 2019-10-21 stsp a->progress_arg);
7372 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7373 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7374 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7375 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7376 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7377 2e1f37b0 2019-08-08 stsp }
7378 2e1f37b0 2019-08-08 stsp }
7379 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7380 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7381 ad493afc 2019-08-03 stsp if (err)
7382 ad493afc 2019-08-03 stsp break;
7383 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7384 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7385 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7386 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7387 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7388 ad493afc 2019-08-03 stsp if (err == NULL)
7389 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7390 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7391 ad493afc 2019-08-03 stsp break;
7392 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7393 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7394 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7395 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7396 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7397 2e1f37b0 2019-08-08 stsp if (err)
7398 2e1f37b0 2019-08-08 stsp break;
7399 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7400 2e1f37b0 2019-08-08 stsp break;
7401 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7402 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7403 2e1f37b0 2019-08-08 stsp break;
7404 2e1f37b0 2019-08-08 stsp }
7405 2e1f37b0 2019-08-08 stsp }
7406 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7407 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7408 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7409 9bc94a15 2019-08-03 stsp if (err)
7410 9bc94a15 2019-08-03 stsp break;
7411 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7412 ad493afc 2019-08-03 stsp break;
7413 ad493afc 2019-08-03 stsp }
7414 f69721c3 2019-10-21 stsp done:
7415 ad493afc 2019-08-03 stsp free(ondisk_path);
7416 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7417 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7419 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7420 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7421 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7422 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7423 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7424 ad493afc 2019-08-03 stsp if (blob_base)
7425 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7426 ad493afc 2019-08-03 stsp if (blob_staged)
7427 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7428 f69721c3 2019-10-21 stsp free(id_str);
7429 f69721c3 2019-10-21 stsp free(label_orig);
7430 ad493afc 2019-08-03 stsp return err;
7431 ad493afc 2019-08-03 stsp }
7432 ad493afc 2019-08-03 stsp
7433 ad493afc 2019-08-03 stsp const struct got_error *
7434 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7435 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7436 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7437 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7438 ad493afc 2019-08-03 stsp struct got_repository *repo)
7439 ad493afc 2019-08-03 stsp {
7440 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7441 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7442 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7443 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7444 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7445 ad493afc 2019-08-03 stsp
7446 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7447 ad493afc 2019-08-03 stsp if (err)
7448 ad493afc 2019-08-03 stsp return err;
7449 ad493afc 2019-08-03 stsp
7450 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7451 ad493afc 2019-08-03 stsp if (err)
7452 ad493afc 2019-08-03 stsp goto done;
7453 ad493afc 2019-08-03 stsp
7454 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7455 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7456 ad493afc 2019-08-03 stsp upa.repo = repo;
7457 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7458 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7459 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7460 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7461 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7462 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7463 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7464 ad493afc 2019-08-03 stsp if (err)
7465 ad493afc 2019-08-03 stsp goto done;
7466 ad493afc 2019-08-03 stsp }
7467 ad493afc 2019-08-03 stsp
7468 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7469 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7470 ad493afc 2019-08-03 stsp err = sync_err;
7471 ad493afc 2019-08-03 stsp done:
7472 ad493afc 2019-08-03 stsp free(fileindex_path);
7473 ad493afc 2019-08-03 stsp if (fileindex)
7474 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7475 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7476 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7477 ad493afc 2019-08-03 stsp err = unlockerr;
7478 ad493afc 2019-08-03 stsp return err;
7479 ad493afc 2019-08-03 stsp }