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