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