Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 eaccb85f 2018-12-25 stsp if (repo)
440 eaccb85f 2018-12-25 stsp got_repo_close(repo);
441 7ac97322 2018-03-11 stsp free(path_got);
442 056e7441 2018-03-11 stsp free(path_lock);
443 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
444 c442a90d 2019-03-10 stsp free(uuidstr);
445 bd165944 2019-03-10 stsp free(formatstr);
446 6d9d28c3 2018-03-11 stsp if (err) {
447 6d9d28c3 2018-03-11 stsp if (fd != -1)
448 6d9d28c3 2018-03-11 stsp close(fd);
449 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
450 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
451 6d9d28c3 2018-03-11 stsp *worktree = NULL;
452 6d9d28c3 2018-03-11 stsp } else
453 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
454 6d9d28c3 2018-03-11 stsp
455 6d9d28c3 2018-03-11 stsp return err;
456 86c3caaf 2018-03-09 stsp }
457 247140b2 2019-02-05 stsp
458 247140b2 2019-02-05 stsp const struct got_error *
459 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
460 247140b2 2019-02-05 stsp {
461 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
462 aedea96d 2020-10-20 stsp char *worktree_path;
463 86c3caaf 2018-03-09 stsp
464 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
465 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
466 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
467 aedea96d 2020-10-20 stsp
468 aedea96d 2020-10-20 stsp for (;;) {
469 aedea96d 2020-10-20 stsp char *parent_path;
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
472 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 aedea96d 2020-10-20 stsp free(worktree_path);
474 247140b2 2019-02-05 stsp return err;
475 aedea96d 2020-10-20 stsp }
476 aedea96d 2020-10-20 stsp if (*worktree) {
477 aedea96d 2020-10-20 stsp free(worktree_path);
478 aedea96d 2020-10-20 stsp return NULL;
479 aedea96d 2020-10-20 stsp }
480 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 aedea96d 2020-10-20 stsp break;
482 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
483 aedea96d 2020-10-20 stsp if (err) {
484 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
485 aedea96d 2020-10-20 stsp free(worktree_path);
486 aedea96d 2020-10-20 stsp return err;
487 aedea96d 2020-10-20 stsp }
488 aedea96d 2020-10-20 stsp break;
489 aedea96d 2020-10-20 stsp }
490 aedea96d 2020-10-20 stsp free(worktree_path);
491 aedea96d 2020-10-20 stsp worktree_path = parent_path;
492 aedea96d 2020-10-20 stsp }
493 247140b2 2019-02-05 stsp
494 aedea96d 2020-10-20 stsp free(worktree_path);
495 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
496 247140b2 2019-02-05 stsp }
497 247140b2 2019-02-05 stsp
498 3a6ce05a 2019-02-11 stsp const struct got_error *
499 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
500 86c3caaf 2018-03-09 stsp {
501 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
502 cde76477 2018-03-11 stsp free(worktree->repo_path);
503 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
504 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
505 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
506 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
507 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
510 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
511 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
512 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
513 6d9d28c3 2018-03-11 stsp free(worktree);
514 437adc9d 2020-12-10 yzhong close(worktree->root_fd);
515 3a6ce05a 2019-02-11 stsp return err;
516 86c3caaf 2018-03-09 stsp }
517 86c3caaf 2018-03-09 stsp
518 2fbdb5ae 2018-12-29 stsp const char *
519 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
520 c7f4312f 2019-02-05 stsp {
521 c7f4312f 2019-02-05 stsp return worktree->root_path;
522 c7f4312f 2019-02-05 stsp }
523 c7f4312f 2019-02-05 stsp
524 c7f4312f 2019-02-05 stsp const char *
525 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
526 86c3caaf 2018-03-09 stsp {
527 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
528 49520a32 2018-12-29 stsp }
529 49520a32 2018-12-29 stsp const char *
530 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
531 49520a32 2018-12-29 stsp {
532 f609be2e 2018-12-29 stsp return worktree->path_prefix;
533 e5dc7198 2018-12-29 stsp }
534 e5dc7198 2018-12-29 stsp
535 e5dc7198 2018-12-29 stsp const struct got_error *
536 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
537 e5dc7198 2018-12-29 stsp const char *path_prefix)
538 e5dc7198 2018-12-29 stsp {
539 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
540 e5dc7198 2018-12-29 stsp
541 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
542 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
543 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
544 e5dc7198 2018-12-29 stsp }
545 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
546 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
547 e5dc7198 2018-12-29 stsp free(absprefix);
548 e5dc7198 2018-12-29 stsp return NULL;
549 86c3caaf 2018-03-09 stsp }
550 86c3caaf 2018-03-09 stsp
551 bc70eb79 2019-05-09 stsp const char *
552 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
553 86c3caaf 2018-03-09 stsp {
554 36a38700 2019-05-10 stsp return worktree->head_ref_name;
555 024e9686 2019-05-14 stsp }
556 024e9686 2019-05-14 stsp
557 024e9686 2019-05-14 stsp const struct got_error *
558 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
559 024e9686 2019-05-14 stsp struct got_reference *head_ref)
560 024e9686 2019-05-14 stsp {
561 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
562 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
565 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
566 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
567 024e9686 2019-05-14 stsp path_got = NULL;
568 024e9686 2019-05-14 stsp goto done;
569 024e9686 2019-05-14 stsp }
570 024e9686 2019-05-14 stsp
571 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
572 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
574 024e9686 2019-05-14 stsp goto done;
575 024e9686 2019-05-14 stsp }
576 024e9686 2019-05-14 stsp
577 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
578 024e9686 2019-05-14 stsp if (err)
579 024e9686 2019-05-14 stsp goto done;
580 024e9686 2019-05-14 stsp
581 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
582 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
583 024e9686 2019-05-14 stsp done:
584 024e9686 2019-05-14 stsp free(path_got);
585 024e9686 2019-05-14 stsp if (err)
586 024e9686 2019-05-14 stsp free(head_ref_name);
587 024e9686 2019-05-14 stsp return err;
588 507dc3bb 2018-12-29 stsp }
589 507dc3bb 2018-12-29 stsp
590 b72f483a 2019-02-05 stsp struct got_object_id *
591 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
592 507dc3bb 2018-12-29 stsp {
593 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
594 86c3caaf 2018-03-09 stsp }
595 86c3caaf 2018-03-09 stsp
596 507dc3bb 2018-12-29 stsp const struct got_error *
597 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
598 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
599 507dc3bb 2018-12-29 stsp {
600 507dc3bb 2018-12-29 stsp const struct got_error *err;
601 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
602 507dc3bb 2018-12-29 stsp char *id_str = NULL;
603 507dc3bb 2018-12-29 stsp char *path_got = NULL;
604 507dc3bb 2018-12-29 stsp
605 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
606 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
607 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
608 507dc3bb 2018-12-29 stsp path_got = NULL;
609 507dc3bb 2018-12-29 stsp goto done;
610 507dc3bb 2018-12-29 stsp }
611 507dc3bb 2018-12-29 stsp
612 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
613 507dc3bb 2018-12-29 stsp if (err)
614 507dc3bb 2018-12-29 stsp return err;
615 507dc3bb 2018-12-29 stsp
616 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
617 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
618 507dc3bb 2018-12-29 stsp goto done;
619 507dc3bb 2018-12-29 stsp }
620 507dc3bb 2018-12-29 stsp
621 507dc3bb 2018-12-29 stsp /* Record our base commit. */
622 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
623 507dc3bb 2018-12-29 stsp if (err)
624 507dc3bb 2018-12-29 stsp goto done;
625 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
626 507dc3bb 2018-12-29 stsp if (err)
627 507dc3bb 2018-12-29 stsp goto done;
628 507dc3bb 2018-12-29 stsp
629 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
630 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
631 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
632 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
633 507dc3bb 2018-12-29 stsp goto done;
634 507dc3bb 2018-12-29 stsp }
635 507dc3bb 2018-12-29 stsp done:
636 507dc3bb 2018-12-29 stsp if (obj)
637 507dc3bb 2018-12-29 stsp got_object_close(obj);
638 507dc3bb 2018-12-29 stsp free(id_str);
639 507dc3bb 2018-12-29 stsp free(path_got);
640 507dc3bb 2018-12-29 stsp return err;
641 50b0790e 2020-09-11 stsp }
642 50b0790e 2020-09-11 stsp
643 50b0790e 2020-09-11 stsp const struct got_gotconfig *
644 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
645 50b0790e 2020-09-11 stsp {
646 50b0790e 2020-09-11 stsp return worktree->gotconfig;
647 507dc3bb 2018-12-29 stsp }
648 507dc3bb 2018-12-29 stsp
649 9d31a1d8 2018-03-11 stsp static const struct got_error *
650 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
651 86c3caaf 2018-03-09 stsp {
652 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
653 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
654 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
655 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
656 86c3caaf 2018-03-09 stsp return NULL;
657 21908da4 2019-01-13 stsp }
658 21908da4 2019-01-13 stsp
659 21908da4 2019-01-13 stsp static const struct got_error *
660 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
661 4a1ddfc2 2019-01-12 stsp {
662 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
663 4a1ddfc2 2019-01-12 stsp char *abspath;
664 4a1ddfc2 2019-01-12 stsp
665 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
666 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
667 4a1ddfc2 2019-01-12 stsp
668 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
669 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
670 ddcd8544 2019-03-11 stsp struct stat sb;
671 ddcd8544 2019-03-11 stsp err = NULL;
672 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
673 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
674 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
675 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
676 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
677 ddcd8544 2019-03-11 stsp }
678 ddcd8544 2019-03-11 stsp }
679 4a1ddfc2 2019-01-12 stsp free(abspath);
680 68c76935 2019-02-19 stsp return err;
681 68c76935 2019-02-19 stsp }
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp static const struct got_error *
684 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
685 68c76935 2019-02-19 stsp {
686 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
687 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
688 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
689 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
690 68c76935 2019-02-19 stsp
691 68c76935 2019-02-19 stsp *same = 1;
692 68c76935 2019-02-19 stsp
693 230a42bd 2019-05-11 jcs for (;;) {
694 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
695 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
696 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
697 68c76935 2019-02-19 stsp break;
698 68c76935 2019-02-19 stsp }
699 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
700 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
701 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
702 68c76935 2019-02-19 stsp break;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp if (flen1 == 0) {
705 68c76935 2019-02-19 stsp if (flen2 != 0)
706 68c76935 2019-02-19 stsp *same = 0;
707 68c76935 2019-02-19 stsp break;
708 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
709 68c76935 2019-02-19 stsp if (flen1 != 0)
710 68c76935 2019-02-19 stsp *same = 0;
711 68c76935 2019-02-19 stsp break;
712 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
713 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
714 68c76935 2019-02-19 stsp *same = 0;
715 68c76935 2019-02-19 stsp break;
716 68c76935 2019-02-19 stsp }
717 68c76935 2019-02-19 stsp } else {
718 68c76935 2019-02-19 stsp *same = 0;
719 68c76935 2019-02-19 stsp break;
720 68c76935 2019-02-19 stsp }
721 68c76935 2019-02-19 stsp }
722 68c76935 2019-02-19 stsp
723 68c76935 2019-02-19 stsp return err;
724 68c76935 2019-02-19 stsp }
725 68c76935 2019-02-19 stsp
726 68c76935 2019-02-19 stsp static const struct got_error *
727 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
728 68c76935 2019-02-19 stsp {
729 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
730 68c76935 2019-02-19 stsp struct stat sb;
731 68c76935 2019-02-19 stsp size_t size1, size2;
732 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
733 68c76935 2019-02-19 stsp
734 68c76935 2019-02-19 stsp *same = 1;
735 68c76935 2019-02-19 stsp
736 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
737 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
738 68c76935 2019-02-19 stsp goto done;
739 68c76935 2019-02-19 stsp }
740 68c76935 2019-02-19 stsp size1 = sb.st_size;
741 68c76935 2019-02-19 stsp
742 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
743 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
744 68c76935 2019-02-19 stsp goto done;
745 68c76935 2019-02-19 stsp }
746 68c76935 2019-02-19 stsp size2 = sb.st_size;
747 68c76935 2019-02-19 stsp
748 68c76935 2019-02-19 stsp if (size1 != size2) {
749 68c76935 2019-02-19 stsp *same = 0;
750 68c76935 2019-02-19 stsp return NULL;
751 68c76935 2019-02-19 stsp }
752 68c76935 2019-02-19 stsp
753 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
754 68c76935 2019-02-19 stsp if (f1 == NULL)
755 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
756 68c76935 2019-02-19 stsp
757 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
758 68c76935 2019-02-19 stsp if (f2 == NULL) {
759 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
760 68c76935 2019-02-19 stsp goto done;
761 68c76935 2019-02-19 stsp }
762 68c76935 2019-02-19 stsp
763 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
764 68c76935 2019-02-19 stsp done:
765 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
766 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
767 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
768 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
769 68c76935 2019-02-19 stsp
770 6353ad76 2019-02-08 stsp return err;
771 6353ad76 2019-02-08 stsp }
772 6353ad76 2019-02-08 stsp
773 6353ad76 2019-02-08 stsp /*
774 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
775 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
776 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
777 6353ad76 2019-02-08 stsp */
778 6353ad76 2019-02-08 stsp static const struct got_error *
779 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
780 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
781 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
782 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
783 f69721c3 2019-10-21 stsp struct got_repository *repo,
784 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
785 6353ad76 2019-02-08 stsp {
786 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
787 6353ad76 2019-02-08 stsp int merged_fd = -1;
788 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
789 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
790 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
791 234035bc 2019-06-01 stsp int overlapcnt = 0;
792 3524bbf9 2020-10-20 stsp char *parent = NULL;
793 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
794 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
795 6353ad76 2019-02-08 stsp
796 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
797 234035bc 2019-06-01 stsp
798 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
799 3524bbf9 2020-10-20 stsp if (err)
800 3524bbf9 2020-10-20 stsp return err;
801 af54ae4a 2019-02-19 stsp
802 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
803 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
804 3524bbf9 2020-10-20 stsp goto done;
805 3524bbf9 2020-10-20 stsp }
806 af54ae4a 2019-02-19 stsp
807 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
808 6353ad76 2019-02-08 stsp if (err)
809 6353ad76 2019-02-08 stsp goto done;
810 6353ad76 2019-02-08 stsp
811 af54ae4a 2019-02-19 stsp free(base_path);
812 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
813 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
814 af54ae4a 2019-02-19 stsp base_path = NULL;
815 af54ae4a 2019-02-19 stsp goto done;
816 af54ae4a 2019-02-19 stsp }
817 af54ae4a 2019-02-19 stsp
818 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
819 6353ad76 2019-02-08 stsp if (err)
820 6353ad76 2019-02-08 stsp goto done;
821 46b6ee73 2019-06-04 stsp if (blob_orig) {
822 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
823 46b6ee73 2019-06-04 stsp blob_orig);
824 1430b4e0 2019-03-27 stsp if (err)
825 1430b4e0 2019-03-27 stsp goto done;
826 1430b4e0 2019-03-27 stsp } else {
827 1430b4e0 2019-03-27 stsp /*
828 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
829 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
830 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
831 1430b4e0 2019-03-27 stsp */
832 1430b4e0 2019-03-27 stsp }
833 6353ad76 2019-02-08 stsp
834 3b9f0f87 2020-07-23 stsp /*
835 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
836 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
837 3b9f0f87 2020-07-23 stsp */
838 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
839 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
840 3b9f0f87 2020-07-23 stsp ssize_t target_len;
841 3b9f0f87 2020-07-23 stsp size_t n;
842 3b9f0f87 2020-07-23 stsp
843 3b9f0f87 2020-07-23 stsp free(base_path);
844 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
845 3b9f0f87 2020-07-23 stsp parent) == -1) {
846 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
847 3b9f0f87 2020-07-23 stsp base_path = NULL;
848 3b9f0f87 2020-07-23 stsp goto done;
849 3b9f0f87 2020-07-23 stsp }
850 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
851 3b9f0f87 2020-07-23 stsp if (err)
852 3b9f0f87 2020-07-23 stsp goto done;
853 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
854 3b9f0f87 2020-07-23 stsp sizeof(target_path));
855 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
856 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
857 3b9f0f87 2020-07-23 stsp goto done;
858 3b9f0f87 2020-07-23 stsp }
859 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
860 3b9f0f87 2020-07-23 stsp if (n != target_len) {
861 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
862 3b9f0f87 2020-07-23 stsp goto done;
863 3b9f0f87 2020-07-23 stsp }
864 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
865 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
866 3b9f0f87 2020-07-23 stsp goto done;
867 3b9f0f87 2020-07-23 stsp }
868 3b9f0f87 2020-07-23 stsp }
869 3b9f0f87 2020-07-23 stsp
870 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
871 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
872 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
873 6353ad76 2019-02-08 stsp if (err)
874 6353ad76 2019-02-08 stsp goto done;
875 6353ad76 2019-02-08 stsp
876 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
877 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
878 1ee397ad 2019-07-12 stsp if (err)
879 1ee397ad 2019-07-12 stsp goto done;
880 6353ad76 2019-02-08 stsp
881 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
882 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
883 816dc654 2019-02-16 stsp goto done;
884 68c76935 2019-02-19 stsp }
885 68c76935 2019-02-19 stsp
886 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
887 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
888 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
889 68c76935 2019-02-19 stsp merged_path);
890 68c76935 2019-02-19 stsp if (err)
891 68c76935 2019-02-19 stsp goto done;
892 816dc654 2019-02-16 stsp }
893 6353ad76 2019-02-08 stsp
894 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
895 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
896 70a0c8ec 2019-02-20 stsp goto done;
897 70a0c8ec 2019-02-20 stsp }
898 70a0c8ec 2019-02-20 stsp
899 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
900 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
901 230a42bd 2019-05-11 jcs ondisk_path);
902 6353ad76 2019-02-08 stsp goto done;
903 6353ad76 2019-02-08 stsp }
904 6353ad76 2019-02-08 stsp done:
905 fdcb7daf 2019-12-15 stsp if (err) {
906 fdcb7daf 2019-12-15 stsp if (merged_path)
907 fdcb7daf 2019-12-15 stsp unlink(merged_path);
908 3b9f0f87 2020-07-23 stsp }
909 3b9f0f87 2020-07-23 stsp if (symlink_path) {
910 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
911 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
912 fdcb7daf 2019-12-15 stsp }
913 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
915 3b9f0f87 2020-07-23 stsp free(symlink_path);
916 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
917 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
918 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
919 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
920 6353ad76 2019-02-08 stsp free(merged_path);
921 af54ae4a 2019-02-19 stsp free(base_path);
922 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
923 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
924 46b6ee73 2019-06-04 stsp free(blob_orig_path);
925 af57b12a 2020-07-23 stsp }
926 3524bbf9 2020-10-20 stsp free(parent);
927 af57b12a 2020-07-23 stsp return err;
928 af57b12a 2020-07-23 stsp }
929 af57b12a 2020-07-23 stsp
930 af57b12a 2020-07-23 stsp static const struct got_error *
931 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
932 af57b12a 2020-07-23 stsp size_t target_len)
933 af57b12a 2020-07-23 stsp {
934 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
935 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
936 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
937 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
938 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
939 af57b12a 2020-07-23 stsp ondisk_path);
940 af57b12a 2020-07-23 stsp return NULL;
941 af57b12a 2020-07-23 stsp }
942 af57b12a 2020-07-23 stsp
943 af57b12a 2020-07-23 stsp /*
944 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
945 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
946 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
947 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
948 993e2a1b 2020-07-23 stsp *
949 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
950 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
951 993e2a1b 2020-07-23 stsp *
952 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
953 993e2a1b 2020-07-23 stsp * has deleted this symlink.
954 11cc08c1 2020-07-23 stsp */
955 11cc08c1 2020-07-23 stsp static const struct got_error *
956 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
957 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
958 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
959 11cc08c1 2020-07-23 stsp {
960 11cc08c1 2020-07-23 stsp const struct got_error *err;
961 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
962 11cc08c1 2020-07-23 stsp FILE *f = NULL;
963 11cc08c1 2020-07-23 stsp
964 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
965 11cc08c1 2020-07-23 stsp if (err)
966 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
967 11cc08c1 2020-07-23 stsp
968 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
969 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
970 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
971 11cc08c1 2020-07-23 stsp goto done;
972 11cc08c1 2020-07-23 stsp }
973 11cc08c1 2020-07-23 stsp
974 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
975 11cc08c1 2020-07-23 stsp if (err)
976 3818e3c4 2020-11-01 naddy goto done;
977 3818e3c4 2020-11-01 naddy
978 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
979 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
980 11cc08c1 2020-07-23 stsp goto done;
981 3818e3c4 2020-11-01 naddy }
982 11cc08c1 2020-07-23 stsp
983 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
984 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
985 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
986 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
987 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
988 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
989 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
990 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
991 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
992 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
993 11cc08c1 2020-07-23 stsp goto done;
994 11cc08c1 2020-07-23 stsp }
995 11cc08c1 2020-07-23 stsp
996 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
997 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
998 11cc08c1 2020-07-23 stsp goto done;
999 11cc08c1 2020-07-23 stsp }
1000 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
1001 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
1002 11cc08c1 2020-07-23 stsp goto done;
1003 11cc08c1 2020-07-23 stsp }
1004 11cc08c1 2020-07-23 stsp done:
1005 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1006 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1007 11cc08c1 2020-07-23 stsp free(path);
1008 11cc08c1 2020-07-23 stsp free(id_str);
1009 11cc08c1 2020-07-23 stsp free(label_deriv);
1010 11cc08c1 2020-07-23 stsp return err;
1011 11cc08c1 2020-07-23 stsp }
1012 d219f183 2020-07-23 stsp
1013 d219f183 2020-07-23 stsp /* forward declaration */
1014 d219f183 2020-07-23 stsp static const struct got_error *
1015 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1016 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1017 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1018 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1019 11cc08c1 2020-07-23 stsp
1020 11cc08c1 2020-07-23 stsp /*
1021 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1022 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1023 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1024 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1025 af57b12a 2020-07-23 stsp */
1026 af57b12a 2020-07-23 stsp static const struct got_error *
1027 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1028 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1029 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1030 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1031 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1032 af57b12a 2020-07-23 stsp {
1033 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1034 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1035 af57b12a 2020-07-23 stsp struct stat sb;
1036 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1037 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1038 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1039 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1040 af57b12a 2020-07-23 stsp
1041 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1042 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1043 af57b12a 2020-07-23 stsp
1044 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1045 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1046 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1047 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1048 af57b12a 2020-07-23 stsp ondisk_path);
1049 af57b12a 2020-07-23 stsp goto done;
1050 af57b12a 2020-07-23 stsp }
1051 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1052 af57b12a 2020-07-23 stsp
1053 526a746f 2020-07-23 stsp if (blob_orig) {
1054 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1055 526a746f 2020-07-23 stsp if (err)
1056 526a746f 2020-07-23 stsp goto done;
1057 526a746f 2020-07-23 stsp }
1058 af57b12a 2020-07-23 stsp
1059 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1060 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1061 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1062 11cc08c1 2020-07-23 stsp have_local_change = 1;
1063 11cc08c1 2020-07-23 stsp
1064 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1065 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1066 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1067 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1068 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1069 11cc08c1 2020-07-23 stsp
1070 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1071 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1072 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1073 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 11cc08c1 2020-07-23 stsp path);
1075 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1076 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1077 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1078 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1079 11cc08c1 2020-07-23 stsp path);
1080 11cc08c1 2020-07-23 stsp } else {
1081 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1082 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1083 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1084 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1085 11cc08c1 2020-07-23 stsp if (err)
1086 11cc08c1 2020-07-23 stsp goto done;
1087 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1088 11cc08c1 2020-07-23 stsp path);
1089 11cc08c1 2020-07-23 stsp }
1090 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1091 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1092 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1093 af57b12a 2020-07-23 stsp strlen(deriv_target));
1094 af57b12a 2020-07-23 stsp if (err)
1095 af57b12a 2020-07-23 stsp goto done;
1096 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1097 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1098 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1099 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1100 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1101 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1102 ea7786be 2020-07-23 stsp path);
1103 ea7786be 2020-07-23 stsp } else {
1104 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1105 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1106 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1107 ea7786be 2020-07-23 stsp if (err)
1108 ea7786be 2020-07-23 stsp goto done;
1109 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1110 ea7786be 2020-07-23 stsp path);
1111 ea7786be 2020-07-23 stsp }
1112 6353ad76 2019-02-08 stsp }
1113 11cc08c1 2020-07-23 stsp
1114 af57b12a 2020-07-23 stsp done:
1115 af57b12a 2020-07-23 stsp free(ancestor_target);
1116 14c901f1 2019-08-08 stsp return err;
1117 14c901f1 2019-08-08 stsp }
1118 14c901f1 2019-08-08 stsp
1119 14c901f1 2019-08-08 stsp /*
1120 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1121 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1122 14c901f1 2019-08-08 stsp * acts as the second derived version.
1123 14c901f1 2019-08-08 stsp */
1124 14c901f1 2019-08-08 stsp static const struct got_error *
1125 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1126 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1127 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1128 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1129 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1130 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1131 14c901f1 2019-08-08 stsp {
1132 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1133 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1134 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1135 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1136 14c901f1 2019-08-08 stsp
1137 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1138 14c901f1 2019-08-08 stsp
1139 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1140 ed6b5030 2020-10-20 stsp if (err)
1141 ed6b5030 2020-10-20 stsp return err;
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp free(base_path);
1144 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1145 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1146 14c901f1 2019-08-08 stsp base_path = NULL;
1147 14c901f1 2019-08-08 stsp goto done;
1148 14c901f1 2019-08-08 stsp }
1149 14c901f1 2019-08-08 stsp
1150 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1151 14c901f1 2019-08-08 stsp if (err)
1152 14c901f1 2019-08-08 stsp goto done;
1153 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1154 14c901f1 2019-08-08 stsp blob_deriv);
1155 14c901f1 2019-08-08 stsp if (err)
1156 14c901f1 2019-08-08 stsp goto done;
1157 14c901f1 2019-08-08 stsp
1158 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1159 14c901f1 2019-08-08 stsp if (err)
1160 14c901f1 2019-08-08 stsp goto done;
1161 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1162 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1163 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1164 14c901f1 2019-08-08 stsp goto done;
1165 14c901f1 2019-08-08 stsp }
1166 14c901f1 2019-08-08 stsp
1167 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1168 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1169 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1170 14c901f1 2019-08-08 stsp done:
1171 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1172 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1173 14c901f1 2019-08-08 stsp free(base_path);
1174 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1175 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1176 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1177 14c901f1 2019-08-08 stsp }
1178 6353ad76 2019-02-08 stsp free(id_str);
1179 818c7501 2019-07-11 stsp free(label_deriv);
1180 ed6b5030 2020-10-20 stsp free(parent);
1181 4a1ddfc2 2019-01-12 stsp return err;
1182 4a1ddfc2 2019-01-12 stsp }
1183 4a1ddfc2 2019-01-12 stsp
1184 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1185 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1186 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1187 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1188 13d9040b 2019-03-27 stsp {
1189 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1190 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1191 13d9040b 2019-03-27 stsp
1192 65b05cec 2020-07-23 stsp *new_iep = NULL;
1193 65b05cec 2020-07-23 stsp
1194 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1195 054041d0 2020-06-24 stsp if (err)
1196 054041d0 2020-06-24 stsp return err;
1197 054041d0 2020-06-24 stsp
1198 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1199 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1200 054041d0 2020-06-24 stsp if (err)
1201 054041d0 2020-06-24 stsp goto done;
1202 054041d0 2020-06-24 stsp
1203 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1204 054041d0 2020-06-24 stsp done:
1205 054041d0 2020-06-24 stsp if (err)
1206 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1207 65b05cec 2020-07-23 stsp else
1208 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1209 13d9040b 2019-03-27 stsp return err;
1210 1ebedb77 2019-10-19 stsp }
1211 1ebedb77 2019-10-19 stsp
1212 1ebedb77 2019-10-19 stsp static mode_t
1213 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1214 1ebedb77 2019-10-19 stsp {
1215 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1216 1ebedb77 2019-10-19 stsp
1217 1ebedb77 2019-10-19 stsp if (executable) {
1218 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1219 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1220 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1221 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1222 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1223 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1224 1ebedb77 2019-10-19 stsp }
1225 1ebedb77 2019-10-19 stsp
1226 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1227 13d9040b 2019-03-27 stsp }
1228 13d9040b 2019-03-27 stsp
1229 8ba819a3 2020-07-23 stsp /* forward declaration */
1230 13d9040b 2019-03-27 stsp static const struct got_error *
1231 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1232 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1233 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1234 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1235 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1236 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1237 8ba819a3 2020-07-23 stsp
1238 5a1fbc73 2020-07-23 stsp /*
1239 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1240 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1241 5a1fbc73 2020-07-23 stsp */
1242 8ba819a3 2020-07-23 stsp static const struct got_error *
1243 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1244 5a1fbc73 2020-07-23 stsp size_t target_len)
1245 5a1fbc73 2020-07-23 stsp {
1246 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1247 5a1fbc73 2020-07-23 stsp ssize_t elen;
1248 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1249 5a1fbc73 2020-07-23 stsp int fd;
1250 5a1fbc73 2020-07-23 stsp
1251 5a1fbc73 2020-07-23 stsp /*
1252 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1253 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1254 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1255 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1256 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1257 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1258 5a1fbc73 2020-07-23 stsp */
1259 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1260 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1261 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1262 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1263 5a1fbc73 2020-07-23 stsp
1264 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1265 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1266 5a1fbc73 2020-07-23 stsp if (elen == -1)
1267 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1268 5a1fbc73 2020-07-23 stsp
1269 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1270 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1271 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1272 5a1fbc73 2020-07-23 stsp }
1273 5a1fbc73 2020-07-23 stsp
1274 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1275 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1276 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1277 5a1fbc73 2020-07-23 stsp return err;
1278 3c1ec782 2020-07-23 stsp }
1279 3c1ec782 2020-07-23 stsp
1280 3c1ec782 2020-07-23 stsp static const struct got_error *
1281 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1282 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1283 3c1ec782 2020-07-23 stsp {
1284 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1285 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1286 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1287 3c1ec782 2020-07-23 stsp
1288 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1289 3c1ec782 2020-07-23 stsp
1290 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1291 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1292 3c1ec782 2020-07-23 stsp return NULL;
1293 3c1ec782 2020-07-23 stsp }
1294 3c1ec782 2020-07-23 stsp
1295 3c1ec782 2020-07-23 stsp /*
1296 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1297 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1298 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1299 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1300 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1301 3c1ec782 2020-07-23 stsp */
1302 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1303 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1304 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1305 ce031e9e 2020-10-20 stsp if (err)
1306 ce031e9e 2020-10-20 stsp return err;
1307 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1308 ce031e9e 2020-10-20 stsp free(parent);
1309 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1310 ce031e9e 2020-10-20 stsp }
1311 ce031e9e 2020-10-20 stsp free(parent);
1312 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1313 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1314 3c1ec782 2020-07-23 stsp free(abspath);
1315 3c1ec782 2020-07-23 stsp return err;
1316 3c1ec782 2020-07-23 stsp }
1317 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1318 3c1ec782 2020-07-23 stsp free(abspath);
1319 3c1ec782 2020-07-23 stsp if (err)
1320 3c1ec782 2020-07-23 stsp return err;
1321 3c1ec782 2020-07-23 stsp } else {
1322 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1323 3c1ec782 2020-07-23 stsp if (err)
1324 3c1ec782 2020-07-23 stsp return err;
1325 3c1ec782 2020-07-23 stsp }
1326 3c1ec782 2020-07-23 stsp
1327 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1328 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1329 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1330 3c1ec782 2020-07-23 stsp return NULL;
1331 3c1ec782 2020-07-23 stsp }
1332 3c1ec782 2020-07-23 stsp
1333 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1334 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1335 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1337 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1338 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1339 3c1ec782 2020-07-23 stsp
1340 3c1ec782 2020-07-23 stsp free(path_got);
1341 3c1ec782 2020-07-23 stsp return NULL;
1342 5a1fbc73 2020-07-23 stsp }
1343 5a1fbc73 2020-07-23 stsp
1344 5a1fbc73 2020-07-23 stsp static const struct got_error *
1345 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1346 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1347 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1348 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1349 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1350 9d31a1d8 2018-03-11 stsp {
1351 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1352 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1353 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1354 906c123b 2020-07-23 stsp char *path_got = NULL;
1355 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1356 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1357 8ba819a3 2020-07-23 stsp
1358 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1359 2e1fa222 2020-07-23 stsp
1360 8ba819a3 2020-07-23 stsp /*
1361 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1362 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1363 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1364 8ba819a3 2020-07-23 stsp * in the blob object.
1365 8ba819a3 2020-07-23 stsp */
1366 8ba819a3 2020-07-23 stsp do {
1367 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1368 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1369 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1375 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1376 3b9f0f87 2020-07-23 stsp progress_arg);
1377 8ba819a3 2020-07-23 stsp }
1378 8ba819a3 2020-07-23 stsp if (len > 0) {
1379 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1380 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1381 8ba819a3 2020-07-23 stsp len - hdrlen);
1382 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1383 8ba819a3 2020-07-23 stsp hdrlen = 0;
1384 8ba819a3 2020-07-23 stsp }
1385 8ba819a3 2020-07-23 stsp } while (len != 0);
1386 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1387 8ba819a3 2020-07-23 stsp
1388 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1389 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1390 3c1ec782 2020-07-23 stsp if (err)
1391 3c1ec782 2020-07-23 stsp return err;
1392 8ba819a3 2020-07-23 stsp
1393 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1394 906c123b 2020-07-23 stsp /* install as a regular file */
1395 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1396 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1397 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1398 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1400 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1401 906c123b 2020-07-23 stsp goto done;
1402 906c123b 2020-07-23 stsp }
1403 906c123b 2020-07-23 stsp
1404 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1405 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1406 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1407 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1408 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1409 c90c8ce3 2020-07-23 stsp goto done;
1410 c90c8ce3 2020-07-23 stsp }
1411 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1412 5a1fbc73 2020-07-23 stsp target_path, target_len);
1413 5a1fbc73 2020-07-23 stsp if (err)
1414 f35fa46a 2020-07-23 stsp goto done;
1415 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1416 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1417 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1418 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1419 6e1eade5 2020-07-23 stsp path);
1420 f35fa46a 2020-07-23 stsp }
1421 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1422 f35fa46a 2020-07-23 stsp }
1423 f35fa46a 2020-07-23 stsp
1424 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1425 f4994adc 2020-10-20 stsp char *parent;
1426 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1427 f4994adc 2020-10-20 stsp if (err)
1428 f4994adc 2020-10-20 stsp goto done;
1429 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1430 f4994adc 2020-10-20 stsp free(parent);
1431 f35fa46a 2020-07-23 stsp if (err)
1432 f35fa46a 2020-07-23 stsp goto done;
1433 f35fa46a 2020-07-23 stsp /*
1434 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1435 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1436 f35fa46a 2020-07-23 stsp */
1437 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1438 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1439 f35fa46a 2020-07-23 stsp goto done;
1440 f35fa46a 2020-07-23 stsp }
1441 f35fa46a 2020-07-23 stsp }
1442 f35fa46a 2020-07-23 stsp
1443 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1444 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1445 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1446 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1447 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1448 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1449 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1450 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1451 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1452 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1453 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1454 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1455 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1456 8ba819a3 2020-07-23 stsp } else {
1457 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1458 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1459 8ba819a3 2020-07-23 stsp }
1460 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1461 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1462 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1463 8ba819a3 2020-07-23 stsp done:
1464 906c123b 2020-07-23 stsp free(path_got);
1465 8ba819a3 2020-07-23 stsp return err;
1466 8ba819a3 2020-07-23 stsp }
1467 8ba819a3 2020-07-23 stsp
1468 8ba819a3 2020-07-23 stsp static const struct got_error *
1469 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1470 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1471 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1472 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1473 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1474 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1475 8ba819a3 2020-07-23 stsp {
1476 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1477 507dc3bb 2018-12-29 stsp int fd = -1;
1478 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1479 507dc3bb 2018-12-29 stsp int update = 0;
1480 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1481 8ba819a3 2020-07-23 stsp
1482 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1483 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1484 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1485 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1486 f5375317 2020-10-20 stsp char *parent;
1487 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1488 f5375317 2020-10-20 stsp if (err)
1489 f5375317 2020-10-20 stsp return err;
1490 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1491 f5375317 2020-10-20 stsp free(parent);
1492 21908da4 2019-01-13 stsp if (err)
1493 21908da4 2019-01-13 stsp return err;
1494 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1495 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1496 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1497 21908da4 2019-01-13 stsp if (fd == -1)
1498 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1499 230a42bd 2019-05-11 jcs ondisk_path);
1500 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1501 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1502 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1503 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1504 3b9f0f87 2020-07-23 stsp goto done;
1505 3b9f0f87 2020-07-23 stsp }
1506 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1507 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1508 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1509 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1510 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1511 507dc3bb 2018-12-29 stsp goto done;
1512 d70b8e30 2018-12-27 stsp } else {
1513 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1514 507dc3bb 2018-12-29 stsp ondisk_path);
1515 507dc3bb 2018-12-29 stsp if (err)
1516 507dc3bb 2018-12-29 stsp goto done;
1517 507dc3bb 2018-12-29 stsp update = 1;
1518 9d31a1d8 2018-03-11 stsp }
1519 507dc3bb 2018-12-29 stsp } else
1520 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1521 9d31a1d8 2018-03-11 stsp }
1522 9d31a1d8 2018-03-11 stsp
1523 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1525 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1526 3818e3c4 2020-11-01 naddy goto done;
1527 3818e3c4 2020-11-01 naddy }
1528 3818e3c4 2020-11-01 naddy
1529 bd6aa359 2020-07-23 stsp if (progress_cb) {
1530 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1531 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1532 bd6aa359 2020-07-23 stsp path);
1533 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1534 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1535 bd6aa359 2020-07-23 stsp path);
1536 bd6aa359 2020-07-23 stsp else
1537 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1538 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1539 bd6aa359 2020-07-23 stsp if (err)
1540 bd6aa359 2020-07-23 stsp goto done;
1541 bd6aa359 2020-07-23 stsp }
1542 d7b62c98 2018-12-27 stsp
1543 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1544 9d31a1d8 2018-03-11 stsp do {
1545 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1546 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1547 9d31a1d8 2018-03-11 stsp if (err)
1548 9d31a1d8 2018-03-11 stsp break;
1549 9d31a1d8 2018-03-11 stsp if (len > 0) {
1550 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1551 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1552 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1553 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1554 b87c6f83 2018-12-24 stsp goto done;
1555 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1556 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1557 b87c6f83 2018-12-24 stsp goto done;
1558 9d31a1d8 2018-03-11 stsp }
1559 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1560 9d31a1d8 2018-03-11 stsp }
1561 9d31a1d8 2018-03-11 stsp } while (len != 0);
1562 9d31a1d8 2018-03-11 stsp
1563 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1565 816dc654 2019-02-16 stsp goto done;
1566 816dc654 2019-02-16 stsp }
1567 9d31a1d8 2018-03-11 stsp
1568 507dc3bb 2018-12-29 stsp if (update) {
1569 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1570 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1571 f6d8c0ac 2020-11-09 stsp goto done;
1572 f6d8c0ac 2020-11-09 stsp }
1573 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1574 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1575 230a42bd 2019-05-11 jcs ondisk_path);
1576 68ed9ba5 2019-02-10 stsp goto done;
1577 68ed9ba5 2019-02-10 stsp }
1578 63df146d 2020-11-09 stsp free(tmppath);
1579 63df146d 2020-11-09 stsp tmppath = NULL;
1580 507dc3bb 2018-12-29 stsp }
1581 507dc3bb 2018-12-29 stsp
1582 9d31a1d8 2018-03-11 stsp done:
1583 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1584 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1585 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1586 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1587 507dc3bb 2018-12-29 stsp free(tmppath);
1588 6353ad76 2019-02-08 stsp return err;
1589 6353ad76 2019-02-08 stsp }
1590 6353ad76 2019-02-08 stsp
1591 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1592 6353ad76 2019-02-08 stsp static const struct got_error *
1593 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1594 7154f6ce 2019-03-27 stsp {
1595 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1596 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1597 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1598 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1599 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1600 7154f6ce 2019-03-27 stsp };
1601 7154f6ce 2019-03-27 stsp int i = 0;
1602 7154f6ce 2019-03-27 stsp char *line;
1603 7154f6ce 2019-03-27 stsp size_t len;
1604 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1605 7154f6ce 2019-03-27 stsp
1606 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1607 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1608 7154f6ce 2019-03-27 stsp if (line == NULL) {
1609 7154f6ce 2019-03-27 stsp if (feof(f))
1610 7154f6ce 2019-03-27 stsp break;
1611 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1612 7154f6ce 2019-03-27 stsp break;
1613 7154f6ce 2019-03-27 stsp }
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1616 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1617 19332e6d 2019-05-13 stsp == 0)
1618 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1619 7154f6ce 2019-03-27 stsp else
1620 7154f6ce 2019-03-27 stsp i++;
1621 7154f6ce 2019-03-27 stsp }
1622 7154f6ce 2019-03-27 stsp }
1623 7154f6ce 2019-03-27 stsp
1624 7154f6ce 2019-03-27 stsp return err;
1625 e2b1e152 2019-07-13 stsp }
1626 e2b1e152 2019-07-13 stsp
1627 e2b1e152 2019-07-13 stsp static int
1628 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1629 1ebedb77 2019-10-19 stsp {
1630 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1631 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1632 1ebedb77 2019-10-19 stsp }
1633 1ebedb77 2019-10-19 stsp
1634 1ebedb77 2019-10-19 stsp static int
1635 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1636 e2b1e152 2019-07-13 stsp {
1637 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1638 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1639 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1641 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1642 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1643 c363b2c1 2019-08-03 stsp }
1644 c363b2c1 2019-08-03 stsp
1645 c363b2c1 2019-08-03 stsp static unsigned char
1646 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1647 c363b2c1 2019-08-03 stsp {
1648 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1649 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1650 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1655 c363b2c1 2019-08-03 stsp default:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1657 a919d5c4 2020-07-23 stsp }
1658 a919d5c4 2020-07-23 stsp }
1659 a919d5c4 2020-07-23 stsp
1660 a919d5c4 2020-07-23 stsp static const struct got_error *
1661 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1662 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1663 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1664 a919d5c4 2020-07-23 stsp {
1665 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1666 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1667 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1668 a919d5c4 2020-07-23 stsp ssize_t elen;
1669 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1670 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1671 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1672 a919d5c4 2020-07-23 stsp
1673 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1674 a919d5c4 2020-07-23 stsp
1675 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1676 a919d5c4 2020-07-23 stsp do {
1677 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1678 a919d5c4 2020-07-23 stsp if (err)
1679 a919d5c4 2020-07-23 stsp return err;
1680 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1681 a919d5c4 2020-07-23 stsp /*
1682 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1683 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1684 a919d5c4 2020-07-23 stsp */
1685 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1686 a919d5c4 2020-07-23 stsp }
1687 a919d5c4 2020-07-23 stsp if (len > 0) {
1688 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1689 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1690 a919d5c4 2020-07-23 stsp len - hdrlen);
1691 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1692 a919d5c4 2020-07-23 stsp hdrlen = 0;
1693 a919d5c4 2020-07-23 stsp }
1694 a919d5c4 2020-07-23 stsp } while (len != 0);
1695 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1696 a919d5c4 2020-07-23 stsp
1697 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1698 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1699 a919d5c4 2020-07-23 stsp if (elen == -1)
1700 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1701 a919d5c4 2020-07-23 stsp } else {
1702 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1703 a919d5c4 2020-07-23 stsp if (elen == -1)
1704 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1705 c363b2c1 2019-08-03 stsp }
1706 a919d5c4 2020-07-23 stsp
1707 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1708 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1709 a919d5c4 2020-07-23 stsp
1710 a919d5c4 2020-07-23 stsp return NULL;
1711 7154f6ce 2019-03-27 stsp }
1712 7154f6ce 2019-03-27 stsp
1713 7154f6ce 2019-03-27 stsp static const struct got_error *
1714 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1715 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1716 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1717 6353ad76 2019-02-08 stsp {
1718 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1719 6353ad76 2019-02-08 stsp struct got_object_id id;
1720 6353ad76 2019-02-08 stsp size_t hdrlen;
1721 1338848f 2019-12-13 stsp int fd = -1;
1722 6353ad76 2019-02-08 stsp FILE *f = NULL;
1723 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1724 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1725 6353ad76 2019-02-08 stsp size_t flen, blen;
1726 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1727 6353ad76 2019-02-08 stsp
1728 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1729 6353ad76 2019-02-08 stsp
1730 7f91a133 2019-12-13 stsp /*
1731 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1732 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1733 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1734 7f91a133 2019-12-13 stsp */
1735 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1736 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1737 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1738 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1739 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1740 882ef1b9 2019-12-13 stsp else
1741 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1742 882ef1b9 2019-12-13 stsp goto done;
1743 882ef1b9 2019-12-13 stsp }
1744 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1745 3d35a492 2019-12-13 stsp goto done;
1746 3d35a492 2019-12-13 stsp }
1747 7f91a133 2019-12-13 stsp } else {
1748 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1749 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1750 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1751 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1752 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1753 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1754 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1755 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1756 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1757 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1758 3d35a492 2019-12-13 stsp else
1759 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1760 3d35a492 2019-12-13 stsp goto done;
1761 3d35a492 2019-12-13 stsp }
1762 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1763 1338848f 2019-12-13 stsp goto done;
1764 a378724f 2019-02-10 stsp }
1765 a378724f 2019-02-10 stsp }
1766 6353ad76 2019-02-08 stsp
1767 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1768 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1769 1338848f 2019-12-13 stsp goto done;
1770 3f148bc6 2019-07-27 stsp }
1771 efdd40df 2019-07-27 stsp
1772 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1773 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1774 1338848f 2019-12-13 stsp goto done;
1775 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1776 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1777 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1778 1338848f 2019-12-13 stsp goto done;
1779 d00136be 2019-03-26 stsp }
1780 b8f41171 2019-02-10 stsp
1781 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1782 f179e66d 2020-07-23 stsp goto done;
1783 f179e66d 2020-07-23 stsp
1784 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1785 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1786 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1787 1338848f 2019-12-13 stsp goto done;
1788 f179e66d 2020-07-23 stsp }
1789 6353ad76 2019-02-08 stsp
1790 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1791 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1792 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1793 c363b2c1 2019-08-03 stsp else
1794 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1795 c363b2c1 2019-08-03 stsp
1796 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1797 6353ad76 2019-02-08 stsp if (err)
1798 1338848f 2019-12-13 stsp goto done;
1799 6353ad76 2019-02-08 stsp
1800 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1801 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1802 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1803 a919d5c4 2020-07-23 stsp goto done;
1804 a919d5c4 2020-07-23 stsp }
1805 a919d5c4 2020-07-23 stsp
1806 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1807 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1808 ab0d4361 2019-12-13 stsp if (fd == -1) {
1809 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1810 ab0d4361 2019-12-13 stsp goto done;
1811 ab0d4361 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp }
1813 3d35a492 2019-12-13 stsp
1814 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1815 6353ad76 2019-02-08 stsp if (f == NULL) {
1816 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1817 6353ad76 2019-02-08 stsp goto done;
1818 6353ad76 2019-02-08 stsp }
1819 1338848f 2019-12-13 stsp fd = -1;
1820 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1821 656b1f76 2019-05-11 jcs for (;;) {
1822 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1824 6353ad76 2019-02-08 stsp if (err)
1825 7154f6ce 2019-03-27 stsp goto done;
1826 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1827 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1829 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1830 7154f6ce 2019-03-27 stsp goto done;
1831 80c5c120 2019-02-19 stsp }
1832 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1833 6353ad76 2019-02-08 stsp if (flen != 0)
1834 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1835 6353ad76 2019-02-08 stsp break;
1836 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1837 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1838 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1839 6353ad76 2019-02-08 stsp break;
1840 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1841 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1842 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1844 6353ad76 2019-02-08 stsp break;
1845 6353ad76 2019-02-08 stsp }
1846 6353ad76 2019-02-08 stsp } else {
1847 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1848 6353ad76 2019-02-08 stsp break;
1849 6353ad76 2019-02-08 stsp }
1850 6353ad76 2019-02-08 stsp hdrlen = 0;
1851 6353ad76 2019-02-08 stsp }
1852 7154f6ce 2019-03-27 stsp
1853 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1854 7154f6ce 2019-03-27 stsp rewind(f);
1855 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1856 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1857 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 6353ad76 2019-02-08 stsp done:
1859 6353ad76 2019-02-08 stsp if (blob)
1860 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1861 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1862 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1863 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1864 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1865 9d31a1d8 2018-03-11 stsp return err;
1866 e2b1e152 2019-07-13 stsp }
1867 e2b1e152 2019-07-13 stsp
1868 e2b1e152 2019-07-13 stsp /*
1869 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1870 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1871 e2b1e152 2019-07-13 stsp */
1872 e2b1e152 2019-07-13 stsp static const struct got_error *
1873 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1875 e2b1e152 2019-07-13 stsp {
1876 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1878 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1879 e2b1e152 2019-07-13 stsp
1880 e2b1e152 2019-07-13 stsp return NULL;
1881 9d31a1d8 2018-03-11 stsp }
1882 9d31a1d8 2018-03-11 stsp
1883 9d31a1d8 2018-03-11 stsp static const struct got_error *
1884 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1885 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1887 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 0584f854 2019-04-06 stsp void *progress_arg)
1889 9d31a1d8 2018-03-11 stsp {
1890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1891 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1892 6353ad76 2019-02-08 stsp char *ondisk_path;
1893 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1894 b8f41171 2019-02-10 stsp struct stat sb;
1895 9d31a1d8 2018-03-11 stsp
1896 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1897 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1898 6353ad76 2019-02-08 stsp
1899 abb4604f 2019-07-27 stsp if (ie) {
1900 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1901 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1902 a76c42e6 2019-08-03 stsp goto done;
1903 a76c42e6 2019-08-03 stsp }
1904 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1905 7f91a133 2019-12-13 stsp repo);
1906 abb4604f 2019-07-27 stsp if (err)
1907 abb4604f 2019-07-27 stsp goto done;
1908 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1909 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1910 c90c8ce3 2020-07-23 stsp } else {
1911 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1912 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1913 c90c8ce3 2020-07-23 stsp }
1914 6353ad76 2019-02-08 stsp
1915 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1916 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1917 b8f41171 2019-02-10 stsp goto done;
1918 b8f41171 2019-02-10 stsp }
1919 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1920 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1921 5036ab18 2020-04-18 stsp path);
1922 5036ab18 2020-04-18 stsp goto done;
1923 5036ab18 2020-04-18 stsp }
1924 b8f41171 2019-02-10 stsp
1925 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1926 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1927 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1928 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1929 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1930 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1931 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1932 e2b1e152 2019-07-13 stsp if (err)
1933 e2b1e152 2019-07-13 stsp goto done;
1934 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1935 b8f41171 2019-02-10 stsp path);
1936 6353ad76 2019-02-08 stsp goto done;
1937 a378724f 2019-02-10 stsp }
1938 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1939 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1940 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1941 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1942 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1943 b8f41171 2019-02-10 stsp goto done;
1944 e2b1e152 2019-07-13 stsp }
1945 9d31a1d8 2018-03-11 stsp }
1946 9d31a1d8 2018-03-11 stsp
1947 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1948 8da9e5f4 2019-01-12 stsp if (err)
1949 6353ad76 2019-02-08 stsp goto done;
1950 512f0d0e 2019-01-02 stsp
1951 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1952 234035bc 2019-06-01 stsp int update_timestamps;
1953 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1954 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1955 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1956 234035bc 2019-06-01 stsp struct got_object_id id2;
1957 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1958 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1959 234035bc 2019-06-01 stsp if (err)
1960 f69721c3 2019-10-21 stsp goto done;
1961 f69721c3 2019-10-21 stsp }
1962 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1963 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1964 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1965 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1966 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1967 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1968 f69721c3 2019-10-21 stsp goto done;
1969 f69721c3 2019-10-21 stsp }
1970 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1971 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1972 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1973 234035bc 2019-06-01 stsp goto done;
1974 f69721c3 2019-10-21 stsp }
1975 234035bc 2019-06-01 stsp }
1976 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1977 36bf999c 2020-07-23 stsp char *link_target;
1978 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1979 36bf999c 2020-07-23 stsp if (err)
1980 36bf999c 2020-07-23 stsp goto done;
1981 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1982 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1983 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1984 36bf999c 2020-07-23 stsp free(link_target);
1985 993e2a1b 2020-07-23 stsp } else {
1986 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1987 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1988 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1989 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1990 993e2a1b 2020-07-23 stsp }
1991 f69721c3 2019-10-21 stsp free(label_orig);
1992 234035bc 2019-06-01 stsp if (blob2)
1993 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1994 909d120e 2019-09-22 stsp if (err)
1995 909d120e 2019-09-22 stsp goto done;
1996 234035bc 2019-06-01 stsp /*
1997 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1998 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1999 234035bc 2019-06-01 stsp * unmodified files again.
2000 234035bc 2019-06-01 stsp */
2001 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2002 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2003 234035bc 2019-06-01 stsp update_timestamps);
2004 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2005 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2006 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2007 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2008 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2009 1ee397ad 2019-07-12 stsp if (err)
2010 1ee397ad 2019-07-12 stsp goto done;
2011 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2012 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2013 13d9040b 2019-03-27 stsp if (err)
2014 13d9040b 2019-03-27 stsp goto done;
2015 13d9040b 2019-03-27 stsp } else {
2016 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2017 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2018 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2019 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2020 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2021 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2022 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2023 2e1fa222 2020-07-23 stsp } else {
2024 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2025 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2026 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2027 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2028 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2029 2e1fa222 2020-07-23 stsp }
2030 13d9040b 2019-03-27 stsp if (err)
2031 13d9040b 2019-03-27 stsp goto done;
2032 65b05cec 2020-07-23 stsp
2033 054041d0 2020-06-24 stsp if (ie) {
2034 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2035 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2036 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2037 054041d0 2020-06-24 stsp } else {
2038 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2039 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2040 054041d0 2020-06-24 stsp &blob->id);
2041 054041d0 2020-06-24 stsp }
2042 13d9040b 2019-03-27 stsp if (err)
2043 13d9040b 2019-03-27 stsp goto done;
2044 65b05cec 2020-07-23 stsp
2045 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2046 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2047 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2048 2e1fa222 2020-07-23 stsp }
2049 13d9040b 2019-03-27 stsp }
2050 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2051 6353ad76 2019-02-08 stsp done:
2052 6353ad76 2019-02-08 stsp free(ondisk_path);
2053 8da9e5f4 2019-01-12 stsp return err;
2054 90285c3b 2019-01-08 stsp }
2055 90285c3b 2019-01-08 stsp
2056 90285c3b 2019-01-08 stsp static const struct got_error *
2057 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2058 90285c3b 2019-01-08 stsp {
2059 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2060 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2061 90285c3b 2019-01-08 stsp
2062 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2063 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2064 90285c3b 2019-01-08 stsp
2065 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2066 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2067 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2068 c97665ae 2019-01-13 stsp } else {
2069 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2070 fddefe3b 2020-10-20 stsp do {
2071 fddefe3b 2020-10-20 stsp char *parent;
2072 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2073 fddefe3b 2020-10-20 stsp if (err)
2074 2513f20a 2020-10-20 stsp break;
2075 fddefe3b 2020-10-20 stsp free(ondisk_path);
2076 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2077 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2078 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2079 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2080 fddefe3b 2020-10-20 stsp ondisk_path);
2081 90285c3b 2019-01-08 stsp break;
2082 90285c3b 2019-01-08 stsp }
2083 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2084 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2085 90285c3b 2019-01-08 stsp }
2086 90285c3b 2019-01-08 stsp free(ondisk_path);
2087 90285c3b 2019-01-08 stsp return err;
2088 512f0d0e 2019-01-02 stsp }
2089 512f0d0e 2019-01-02 stsp
2090 708d8e67 2019-03-27 stsp static const struct got_error *
2091 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2092 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2093 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2094 708d8e67 2019-03-27 stsp {
2095 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2096 708d8e67 2019-03-27 stsp unsigned char status;
2097 708d8e67 2019-03-27 stsp struct stat sb;
2098 708d8e67 2019-03-27 stsp char *ondisk_path;
2099 708d8e67 2019-03-27 stsp
2100 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2101 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2102 a76c42e6 2019-08-03 stsp
2103 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2104 708d8e67 2019-03-27 stsp == -1)
2105 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2106 708d8e67 2019-03-27 stsp
2107 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2108 708d8e67 2019-03-27 stsp if (err)
2109 5a58a424 2020-06-23 stsp goto done;
2110 708d8e67 2019-03-27 stsp
2111 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2112 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2113 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2114 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2115 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2116 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2117 993e2a1b 2020-07-23 stsp goto done;
2118 993e2a1b 2020-07-23 stsp }
2119 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2120 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2121 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2122 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2123 993e2a1b 2020-07-23 stsp if (err)
2124 993e2a1b 2020-07-23 stsp goto done;
2125 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2126 993e2a1b 2020-07-23 stsp ie->path);
2127 993e2a1b 2020-07-23 stsp goto done;
2128 993e2a1b 2020-07-23 stsp }
2129 993e2a1b 2020-07-23 stsp
2130 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2131 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2132 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2133 1ee397ad 2019-07-12 stsp if (err)
2134 5a58a424 2020-06-23 stsp goto done;
2135 fc6346c4 2019-03-27 stsp /*
2136 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2137 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2138 fc6346c4 2019-03-27 stsp */
2139 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2140 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2141 fc6346c4 2019-03-27 stsp } else {
2142 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2143 1ee397ad 2019-07-12 stsp if (err)
2144 5a58a424 2020-06-23 stsp goto done;
2145 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2146 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2147 fc6346c4 2019-03-27 stsp if (err)
2148 5a58a424 2020-06-23 stsp goto done;
2149 fc6346c4 2019-03-27 stsp }
2150 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2151 708d8e67 2019-03-27 stsp }
2152 5a58a424 2020-06-23 stsp done:
2153 5a58a424 2020-06-23 stsp free(ondisk_path);
2154 fc6346c4 2019-03-27 stsp return err;
2155 708d8e67 2019-03-27 stsp }
2156 708d8e67 2019-03-27 stsp
2157 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2158 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2159 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2160 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2161 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2162 8da9e5f4 2019-01-12 stsp void *progress_arg;
2163 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2164 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2165 8da9e5f4 2019-01-12 stsp };
2166 8da9e5f4 2019-01-12 stsp
2167 512f0d0e 2019-01-02 stsp static const struct got_error *
2168 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2169 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2170 512f0d0e 2019-01-02 stsp {
2171 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2172 0584f854 2019-04-06 stsp
2173 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2175 512f0d0e 2019-01-02 stsp
2176 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2177 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2178 8da9e5f4 2019-01-12 stsp }
2179 512f0d0e 2019-01-02 stsp
2180 8da9e5f4 2019-01-12 stsp static const struct got_error *
2181 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2182 8da9e5f4 2019-01-12 stsp {
2183 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2184 7a9df742 2019-01-08 stsp
2185 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2186 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2187 0584f854 2019-04-06 stsp
2188 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2189 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2190 9d31a1d8 2018-03-11 stsp }
2191 9d31a1d8 2018-03-11 stsp
2192 9d31a1d8 2018-03-11 stsp static const struct got_error *
2193 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2194 9d31a1d8 2018-03-11 stsp {
2195 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2196 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2197 8da9e5f4 2019-01-12 stsp char *path;
2198 9d31a1d8 2018-03-11 stsp
2199 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2200 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2201 0584f854 2019-04-06 stsp
2202 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2203 63c5ca5d 2019-08-24 stsp return NULL;
2204 63c5ca5d 2019-08-24 stsp
2205 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2206 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2207 8da9e5f4 2019-01-12 stsp == -1)
2208 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2209 9d31a1d8 2018-03-11 stsp
2210 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2211 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2212 8da9e5f4 2019-01-12 stsp else
2213 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2214 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2215 9d31a1d8 2018-03-11 stsp
2216 8da9e5f4 2019-01-12 stsp free(path);
2217 0cd1c46a 2019-03-11 stsp return err;
2218 0cd1c46a 2019-03-11 stsp }
2219 0cd1c46a 2019-03-11 stsp
2220 b2118c49 2020-07-28 stsp const struct got_error *
2221 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2222 b2118c49 2020-07-28 stsp {
2223 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2224 b2118c49 2020-07-28 stsp
2225 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2226 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2227 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2228 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2229 b2118c49 2020-07-28 stsp }
2230 b2118c49 2020-07-28 stsp
2231 b2118c49 2020-07-28 stsp return NULL;
2232 b2118c49 2020-07-28 stsp }
2233 b2118c49 2020-07-28 stsp
2234 818c7501 2019-07-11 stsp static const struct got_error *
2235 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2236 0cd1c46a 2019-03-11 stsp {
2237 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2238 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2239 0cd1c46a 2019-03-11 stsp
2240 517bab73 2019-03-11 stsp *refname = NULL;
2241 517bab73 2019-03-11 stsp
2242 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2243 b2118c49 2020-07-28 stsp if (err)
2244 b2118c49 2020-07-28 stsp return err;
2245 0cd1c46a 2019-03-11 stsp
2246 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2247 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2248 0647c563 2019-03-11 stsp *refname = NULL;
2249 0cd1c46a 2019-03-11 stsp }
2250 517bab73 2019-03-11 stsp free(uuidstr);
2251 517bab73 2019-03-11 stsp return err;
2252 517bab73 2019-03-11 stsp }
2253 517bab73 2019-03-11 stsp
2254 818c7501 2019-07-11 stsp const struct got_error *
2255 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2256 818c7501 2019-07-11 stsp {
2257 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 818c7501 2019-07-11 stsp }
2259 818c7501 2019-07-11 stsp
2260 818c7501 2019-07-11 stsp static const struct got_error *
2261 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 818c7501 2019-07-11 stsp {
2263 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2264 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 818c7501 2019-07-11 stsp }
2266 818c7501 2019-07-11 stsp
2267 818c7501 2019-07-11 stsp static const struct got_error *
2268 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2269 818c7501 2019-07-11 stsp {
2270 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 818c7501 2019-07-11 stsp }
2272 818c7501 2019-07-11 stsp
2273 818c7501 2019-07-11 stsp static const struct got_error *
2274 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2275 818c7501 2019-07-11 stsp {
2276 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2277 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 818c7501 2019-07-11 stsp }
2279 818c7501 2019-07-11 stsp
2280 818c7501 2019-07-11 stsp static const struct got_error *
2281 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 818c7501 2019-07-11 stsp {
2283 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2284 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 0ebf8283 2019-07-24 stsp }
2286 0ebf8283 2019-07-24 stsp
2287 0ebf8283 2019-07-24 stsp static const struct got_error *
2288 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2289 0ebf8283 2019-07-24 stsp {
2290 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2291 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 0ebf8283 2019-07-24 stsp }
2293 0ebf8283 2019-07-24 stsp
2294 0ebf8283 2019-07-24 stsp static const struct got_error *
2295 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2296 0ebf8283 2019-07-24 stsp {
2297 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2298 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 0ebf8283 2019-07-24 stsp }
2300 0ebf8283 2019-07-24 stsp
2301 0ebf8283 2019-07-24 stsp static const struct got_error *
2302 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2303 0ebf8283 2019-07-24 stsp {
2304 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2305 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 818c7501 2019-07-11 stsp }
2307 818c7501 2019-07-11 stsp
2308 0ebf8283 2019-07-24 stsp static const struct got_error *
2309 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 0ebf8283 2019-07-24 stsp {
2311 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2312 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 0ebf8283 2019-07-24 stsp }
2314 818c7501 2019-07-11 stsp
2315 0ebf8283 2019-07-24 stsp const struct got_error *
2316 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2317 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2318 0ebf8283 2019-07-24 stsp {
2319 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2320 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2321 0ebf8283 2019-07-24 stsp *path = NULL;
2322 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2323 0ebf8283 2019-07-24 stsp }
2324 0ebf8283 2019-07-24 stsp return NULL;
2325 0ebf8283 2019-07-24 stsp }
2326 0ebf8283 2019-07-24 stsp
2327 517bab73 2019-03-11 stsp /*
2328 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2329 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2330 517bab73 2019-03-11 stsp */
2331 517bab73 2019-03-11 stsp static const struct got_error *
2332 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2333 517bab73 2019-03-11 stsp {
2334 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2335 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2336 517bab73 2019-03-11 stsp char *refname;
2337 517bab73 2019-03-11 stsp
2338 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2339 517bab73 2019-03-11 stsp if (err)
2340 517bab73 2019-03-11 stsp return err;
2341 0cd1c46a 2019-03-11 stsp
2342 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2343 0cd1c46a 2019-03-11 stsp if (err)
2344 0cd1c46a 2019-03-11 stsp goto done;
2345 0cd1c46a 2019-03-11 stsp
2346 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2347 0cd1c46a 2019-03-11 stsp done:
2348 0cd1c46a 2019-03-11 stsp free(refname);
2349 0cd1c46a 2019-03-11 stsp if (ref)
2350 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2351 3e3a69f1 2019-07-25 stsp return err;
2352 3e3a69f1 2019-07-25 stsp }
2353 3e3a69f1 2019-07-25 stsp
2354 3e3a69f1 2019-07-25 stsp static const struct got_error *
2355 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2356 3e3a69f1 2019-07-25 stsp {
2357 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2358 3e3a69f1 2019-07-25 stsp
2359 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2360 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2361 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2362 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2363 3e3a69f1 2019-07-25 stsp }
2364 9d31a1d8 2018-03-11 stsp return err;
2365 9d31a1d8 2018-03-11 stsp }
2366 9d31a1d8 2018-03-11 stsp
2367 3e3a69f1 2019-07-25 stsp
2368 ebf99748 2019-05-09 stsp static const struct got_error *
2369 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2370 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2371 ebf99748 2019-05-09 stsp {
2372 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2373 ebf99748 2019-05-09 stsp FILE *index = NULL;
2374 0cd1c46a 2019-03-11 stsp
2375 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2376 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2377 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2378 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2379 ebf99748 2019-05-09 stsp
2380 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2381 3e3a69f1 2019-07-25 stsp if (err)
2382 ebf99748 2019-05-09 stsp goto done;
2383 ebf99748 2019-05-09 stsp
2384 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2385 ebf99748 2019-05-09 stsp if (index == NULL) {
2386 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2387 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2388 ebf99748 2019-05-09 stsp } else {
2389 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2390 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2391 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2392 ebf99748 2019-05-09 stsp }
2393 ebf99748 2019-05-09 stsp done:
2394 ebf99748 2019-05-09 stsp if (err) {
2395 ebf99748 2019-05-09 stsp free(*fileindex_path);
2396 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2397 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2398 ebf99748 2019-05-09 stsp *fileindex = NULL;
2399 ebf99748 2019-05-09 stsp }
2400 ebf99748 2019-05-09 stsp return err;
2401 ebf99748 2019-05-09 stsp }
2402 c932eeeb 2019-05-22 stsp
2403 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2404 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2405 c932eeeb 2019-05-22 stsp const char *path;
2406 c932eeeb 2019-05-22 stsp size_t path_len;
2407 c932eeeb 2019-05-22 stsp const char *entry_name;
2408 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2409 a484d721 2019-06-10 stsp void *progress_arg;
2410 c932eeeb 2019-05-22 stsp };
2411 ebf99748 2019-05-09 stsp
2412 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2413 c932eeeb 2019-05-22 stsp static const struct got_error *
2414 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2415 c932eeeb 2019-05-22 stsp {
2416 1ee397ad 2019-07-12 stsp const struct got_error *err;
2417 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2418 c932eeeb 2019-05-22 stsp
2419 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2420 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2421 c932eeeb 2019-05-22 stsp return NULL;
2422 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2423 102ff934 2019-06-11 stsp return NULL;
2424 102ff934 2019-06-11 stsp
2425 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2426 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2427 c932eeeb 2019-05-22 stsp return NULL;
2428 c932eeeb 2019-05-22 stsp
2429 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2430 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2431 edd02c5e 2019-07-11 stsp ie->path);
2432 1ee397ad 2019-07-12 stsp if (err)
2433 1ee397ad 2019-07-12 stsp return err;
2434 1ee397ad 2019-07-12 stsp }
2435 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2436 c932eeeb 2019-05-22 stsp return NULL;
2437 9c6338c4 2019-06-02 stsp }
2438 9c6338c4 2019-06-02 stsp
2439 9c6338c4 2019-06-02 stsp static const struct got_error *
2440 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2441 9c6338c4 2019-06-02 stsp {
2442 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2443 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2444 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2445 867630bb 2020-01-17 stsp struct timespec timeout;
2446 9c6338c4 2019-06-02 stsp
2447 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2448 9c6338c4 2019-06-02 stsp fileindex_path);
2449 9c6338c4 2019-06-02 stsp if (err)
2450 9c6338c4 2019-06-02 stsp goto done;
2451 9c6338c4 2019-06-02 stsp
2452 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2453 9c6338c4 2019-06-02 stsp if (err)
2454 9c6338c4 2019-06-02 stsp goto done;
2455 9c6338c4 2019-06-02 stsp
2456 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2457 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2458 9c6338c4 2019-06-02 stsp fileindex_path);
2459 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2460 9c6338c4 2019-06-02 stsp }
2461 867630bb 2020-01-17 stsp
2462 867630bb 2020-01-17 stsp /*
2463 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2464 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2465 867630bb 2020-01-17 stsp * was recorded in the file index.
2466 867630bb 2020-01-17 stsp */
2467 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2468 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2469 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2470 9c6338c4 2019-06-02 stsp done:
2471 9c6338c4 2019-06-02 stsp if (new_index)
2472 9c6338c4 2019-06-02 stsp fclose(new_index);
2473 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2474 9c6338c4 2019-06-02 stsp return err;
2475 c932eeeb 2019-05-22 stsp }
2476 6ced4176 2019-07-12 stsp
2477 6ced4176 2019-07-12 stsp static const struct got_error *
2478 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2479 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2480 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2481 6ced4176 2019-07-12 stsp {
2482 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2483 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2484 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2485 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2486 6ced4176 2019-07-12 stsp
2487 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2488 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2489 6ced4176 2019-07-12 stsp *tree_id = NULL;
2490 6ced4176 2019-07-12 stsp
2491 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2492 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2493 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2494 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2495 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2496 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2497 6ced4176 2019-07-12 stsp goto done;
2498 6ced4176 2019-07-12 stsp }
2499 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2500 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2501 6ced4176 2019-07-12 stsp if (err)
2502 6ced4176 2019-07-12 stsp goto done;
2503 6ced4176 2019-07-12 stsp return NULL;
2504 6ced4176 2019-07-12 stsp }
2505 6ced4176 2019-07-12 stsp
2506 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2507 6ced4176 2019-07-12 stsp
2508 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2509 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2510 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2511 6ced4176 2019-07-12 stsp goto done;
2512 6ced4176 2019-07-12 stsp }
2513 6ced4176 2019-07-12 stsp
2514 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2515 6ced4176 2019-07-12 stsp in_repo_path);
2516 6ced4176 2019-07-12 stsp if (err)
2517 6ced4176 2019-07-12 stsp goto done;
2518 6ced4176 2019-07-12 stsp
2519 6ced4176 2019-07-12 stsp free(in_repo_path);
2520 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2521 6ced4176 2019-07-12 stsp
2522 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2523 6ced4176 2019-07-12 stsp if (err)
2524 6ced4176 2019-07-12 stsp goto done;
2525 c932eeeb 2019-05-22 stsp
2526 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2527 6ced4176 2019-07-12 stsp /* Check out a single file. */
2528 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2529 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2530 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2531 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2532 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2533 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp }
2535 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2536 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2537 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2538 6ced4176 2019-07-12 stsp goto done;
2539 6ced4176 2019-07-12 stsp }
2540 6ced4176 2019-07-12 stsp } else {
2541 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2542 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2543 6ced4176 2019-07-12 stsp if (err)
2544 6ced4176 2019-07-12 stsp return err;
2545 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2546 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2547 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2548 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2549 6ced4176 2019-07-12 stsp goto done;
2550 6ced4176 2019-07-12 stsp }
2551 6ced4176 2019-07-12 stsp }
2552 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2553 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2554 6ced4176 2019-07-12 stsp } else {
2555 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2556 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2557 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2558 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2559 6ced4176 2019-07-12 stsp goto done;
2560 6ced4176 2019-07-12 stsp }
2561 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2562 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2563 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2564 6ced4176 2019-07-12 stsp goto done;
2565 6ced4176 2019-07-12 stsp }
2566 6ced4176 2019-07-12 stsp }
2567 6ced4176 2019-07-12 stsp done:
2568 6ced4176 2019-07-12 stsp free(id);
2569 6ced4176 2019-07-12 stsp free(in_repo_path);
2570 6ced4176 2019-07-12 stsp if (err) {
2571 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2572 6ced4176 2019-07-12 stsp free(*tree_relpath);
2573 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2574 6ced4176 2019-07-12 stsp free(*tree_id);
2575 6ced4176 2019-07-12 stsp *tree_id = NULL;
2576 6ced4176 2019-07-12 stsp }
2577 07ed472d 2019-07-12 stsp return err;
2578 07ed472d 2019-07-12 stsp }
2579 07ed472d 2019-07-12 stsp
2580 07ed472d 2019-07-12 stsp static const struct got_error *
2581 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2582 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2583 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2584 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2585 07ed472d 2019-07-12 stsp {
2586 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2587 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2588 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2589 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2590 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2591 07ed472d 2019-07-12 stsp
2592 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2593 7f47418f 2019-12-20 stsp if (err) {
2594 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2595 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2596 7f47418f 2019-12-20 stsp goto done;
2597 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2598 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2599 7f47418f 2019-12-20 stsp if (err)
2600 7f47418f 2019-12-20 stsp return err;
2601 7f47418f 2019-12-20 stsp }
2602 07ed472d 2019-07-12 stsp
2603 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2604 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2605 07ed472d 2019-07-12 stsp if (err)
2606 07ed472d 2019-07-12 stsp goto done;
2607 07ed472d 2019-07-12 stsp
2608 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2609 07ed472d 2019-07-12 stsp if (err)
2610 07ed472d 2019-07-12 stsp goto done;
2611 07ed472d 2019-07-12 stsp
2612 07ed472d 2019-07-12 stsp if (entry_name &&
2613 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2614 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2615 07ed472d 2019-07-12 stsp goto done;
2616 07ed472d 2019-07-12 stsp }
2617 07ed472d 2019-07-12 stsp
2618 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2619 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2620 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2621 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2622 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2623 07ed472d 2019-07-12 stsp arg.repo = repo;
2624 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2625 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2626 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2627 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2628 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2629 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2630 07ed472d 2019-07-12 stsp done:
2631 07ed472d 2019-07-12 stsp if (tree)
2632 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2633 07ed472d 2019-07-12 stsp if (commit)
2634 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2635 6ced4176 2019-07-12 stsp return err;
2636 6ced4176 2019-07-12 stsp }
2637 6ced4176 2019-07-12 stsp
2638 9d31a1d8 2018-03-11 stsp const struct got_error *
2639 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2640 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2641 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2642 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2643 9d31a1d8 2018-03-11 stsp {
2644 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2645 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2646 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2647 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2648 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2649 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2650 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2651 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2652 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2653 f2ea84fa 2019-07-27 stsp int entry_type;
2654 f2ea84fa 2019-07-27 stsp char *relpath;
2655 f2ea84fa 2019-07-27 stsp char *entry_name;
2656 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2657 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2658 9d31a1d8 2018-03-11 stsp
2659 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2660 f2ea84fa 2019-07-27 stsp
2661 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2662 9d31a1d8 2018-03-11 stsp if (err)
2663 9d31a1d8 2018-03-11 stsp return err;
2664 93a30277 2018-12-24 stsp
2665 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2666 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2667 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2668 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2669 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2670 f2ea84fa 2019-07-27 stsp goto done;
2671 f2ea84fa 2019-07-27 stsp }
2672 6ced4176 2019-07-12 stsp
2673 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2674 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2675 f2ea84fa 2019-07-27 stsp if (err) {
2676 f2ea84fa 2019-07-27 stsp free(tpd);
2677 6ced4176 2019-07-12 stsp goto done;
2678 6ced4176 2019-07-12 stsp }
2679 f2ea84fa 2019-07-27 stsp
2680 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2681 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2682 f2ea84fa 2019-07-27 stsp if (err) {
2683 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2684 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2685 f2ea84fa 2019-07-27 stsp free(tpd);
2686 f2ea84fa 2019-07-27 stsp goto done;
2687 f2ea84fa 2019-07-27 stsp }
2688 f2ea84fa 2019-07-27 stsp } else
2689 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2690 f2ea84fa 2019-07-27 stsp
2691 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2692 6ced4176 2019-07-12 stsp }
2693 6ced4176 2019-07-12 stsp
2694 51514078 2018-12-25 stsp /*
2695 51514078 2018-12-25 stsp * Read the file index.
2696 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2697 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2698 51514078 2018-12-25 stsp */
2699 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2700 8da9e5f4 2019-01-12 stsp if (err)
2701 c4cdcb68 2019-04-03 stsp goto done;
2702 c4cdcb68 2019-04-03 stsp
2703 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2704 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2705 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2706 f2ea84fa 2019-07-27 stsp
2707 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2708 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2709 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2710 f2ea84fa 2019-07-27 stsp if (err)
2711 f2ea84fa 2019-07-27 stsp break;
2712 f2ea84fa 2019-07-27 stsp
2713 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2714 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2715 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2716 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2717 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2718 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2719 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2720 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2721 f2ea84fa 2019-07-27 stsp if (err)
2722 f2ea84fa 2019-07-27 stsp break;
2723 f2ea84fa 2019-07-27 stsp
2724 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2725 711d9cd9 2019-07-12 stsp }
2726 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2727 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2728 af12c6b9 2019-06-04 stsp err = sync_err;
2729 9d31a1d8 2018-03-11 stsp done:
2730 9c6338c4 2019-06-02 stsp free(fileindex_path);
2731 edfa7d7f 2018-09-11 stsp if (tree)
2732 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2733 9d31a1d8 2018-03-11 stsp if (commit)
2734 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2735 5ade8233 2019-07-12 stsp if (fileindex)
2736 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2737 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2738 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2739 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2740 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2741 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2742 f2ea84fa 2019-07-27 stsp free(tpd);
2743 f2ea84fa 2019-07-27 stsp }
2744 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2745 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2746 9d31a1d8 2018-03-11 stsp err = unlockerr;
2747 f8d1f275 2019-02-04 stsp return err;
2748 f8d1f275 2019-02-04 stsp }
2749 f8d1f275 2019-02-04 stsp
2750 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2751 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2752 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2753 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2754 234035bc 2019-06-01 stsp void *progress_arg;
2755 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2756 234035bc 2019-06-01 stsp void *cancel_arg;
2757 f69721c3 2019-10-21 stsp const char *label_orig;
2758 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2759 234035bc 2019-06-01 stsp };
2760 234035bc 2019-06-01 stsp
2761 234035bc 2019-06-01 stsp static const struct got_error *
2762 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2763 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2764 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2765 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2766 234035bc 2019-06-01 stsp {
2767 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2768 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2769 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2770 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2771 234035bc 2019-06-01 stsp struct stat sb;
2772 234035bc 2019-06-01 stsp unsigned char status;
2773 234035bc 2019-06-01 stsp int local_changes_subsumed;
2774 234035bc 2019-06-01 stsp
2775 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2776 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2777 d6c87207 2019-08-02 stsp strlen(path2));
2778 1ee397ad 2019-07-12 stsp if (ie == NULL)
2779 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2780 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2781 234035bc 2019-06-01 stsp
2782 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2783 234035bc 2019-06-01 stsp path2) == -1)
2784 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2785 234035bc 2019-06-01 stsp
2786 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2787 7f91a133 2019-12-13 stsp repo);
2788 234035bc 2019-06-01 stsp if (err)
2789 234035bc 2019-06-01 stsp goto done;
2790 234035bc 2019-06-01 stsp
2791 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2792 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2793 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2794 234035bc 2019-06-01 stsp goto done;
2795 234035bc 2019-06-01 stsp }
2796 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2797 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2798 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2799 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2800 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2801 234035bc 2019-06-01 stsp goto done;
2802 234035bc 2019-06-01 stsp }
2803 234035bc 2019-06-01 stsp
2804 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2805 36bf999c 2020-07-23 stsp char *link_target2;
2806 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2807 36bf999c 2020-07-23 stsp if (err)
2808 36bf999c 2020-07-23 stsp goto done;
2809 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2810 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2811 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2812 36bf999c 2020-07-23 stsp free(link_target2);
2813 af57b12a 2020-07-23 stsp } else {
2814 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2815 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2816 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2817 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2818 af57b12a 2020-07-23 stsp }
2819 234035bc 2019-06-01 stsp } else if (blob1) {
2820 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2821 d6c87207 2019-08-02 stsp strlen(path1));
2822 1ee397ad 2019-07-12 stsp if (ie == NULL)
2823 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2824 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2825 2b92fad7 2019-06-02 stsp
2826 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2827 2b92fad7 2019-06-02 stsp path1) == -1)
2828 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2829 2b92fad7 2019-06-02 stsp
2830 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2831 7f91a133 2019-12-13 stsp repo);
2832 2b92fad7 2019-06-02 stsp if (err)
2833 2b92fad7 2019-06-02 stsp goto done;
2834 2b92fad7 2019-06-02 stsp
2835 2b92fad7 2019-06-02 stsp switch (status) {
2836 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2837 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2838 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2839 1ee397ad 2019-07-12 stsp if (err)
2840 1ee397ad 2019-07-12 stsp goto done;
2841 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2842 2b92fad7 2019-06-02 stsp if (err)
2843 2b92fad7 2019-06-02 stsp goto done;
2844 2b92fad7 2019-06-02 stsp if (ie)
2845 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2846 2b92fad7 2019-06-02 stsp break;
2847 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2848 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2849 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2850 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2851 1ee397ad 2019-07-12 stsp if (err)
2852 1ee397ad 2019-07-12 stsp goto done;
2853 2b92fad7 2019-06-02 stsp if (ie)
2854 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2855 2b92fad7 2019-06-02 stsp break;
2856 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2857 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2858 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2859 0a22ca1a 2020-09-23 stsp /*
2860 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2861 0a22ca1a 2020-09-23 stsp * exists in the repository.
2862 0a22ca1a 2020-09-23 stsp */
2863 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2864 0a22ca1a 2020-09-23 stsp if (err)
2865 0a22ca1a 2020-09-23 stsp goto done;
2866 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2867 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2868 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2869 0a22ca1a 2020-09-23 stsp if (err)
2870 0a22ca1a 2020-09-23 stsp goto done;
2871 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2872 0a22ca1a 2020-09-23 stsp path1);
2873 0a22ca1a 2020-09-23 stsp if (err)
2874 0a22ca1a 2020-09-23 stsp goto done;
2875 0a22ca1a 2020-09-23 stsp if (ie)
2876 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2877 0a22ca1a 2020-09-23 stsp ie);
2878 0a22ca1a 2020-09-23 stsp } else {
2879 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2880 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2881 0a22ca1a 2020-09-23 stsp }
2882 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2883 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2884 0a22ca1a 2020-09-23 stsp free(id);
2885 0a22ca1a 2020-09-23 stsp if (err)
2886 0a22ca1a 2020-09-23 stsp goto done;
2887 0a22ca1a 2020-09-23 stsp break;
2888 0a22ca1a 2020-09-23 stsp }
2889 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2890 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2891 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2892 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2893 1ee397ad 2019-07-12 stsp if (err)
2894 1ee397ad 2019-07-12 stsp goto done;
2895 2b92fad7 2019-06-02 stsp break;
2896 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2897 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2898 1ee397ad 2019-07-12 stsp if (err)
2899 1ee397ad 2019-07-12 stsp goto done;
2900 2b92fad7 2019-06-02 stsp break;
2901 2b92fad7 2019-06-02 stsp default:
2902 2b92fad7 2019-06-02 stsp break;
2903 2b92fad7 2019-06-02 stsp }
2904 234035bc 2019-06-01 stsp } else if (blob2) {
2905 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2906 234035bc 2019-06-01 stsp path2) == -1)
2907 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2908 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2909 d6c87207 2019-08-02 stsp strlen(path2));
2910 234035bc 2019-06-01 stsp if (ie) {
2911 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2912 7f91a133 2019-12-13 stsp -1, NULL, repo);
2913 234035bc 2019-06-01 stsp if (err)
2914 234035bc 2019-06-01 stsp goto done;
2915 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2916 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2917 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2918 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2919 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2920 1ee397ad 2019-07-12 stsp status, path2);
2921 234035bc 2019-06-01 stsp goto done;
2922 234035bc 2019-06-01 stsp }
2923 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2924 36bf999c 2020-07-23 stsp char *link_target2;
2925 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2926 36bf999c 2020-07-23 stsp blob2);
2927 36bf999c 2020-07-23 stsp if (err)
2928 36bf999c 2020-07-23 stsp goto done;
2929 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2930 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2931 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2932 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2933 36bf999c 2020-07-23 stsp free(link_target2);
2934 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2935 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2936 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2937 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2938 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2939 526a746f 2020-07-23 stsp a->progress_arg);
2940 dfe9fba0 2020-07-23 stsp } else {
2941 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2942 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2943 526a746f 2020-07-23 stsp }
2944 dfe9fba0 2020-07-23 stsp if (err)
2945 dfe9fba0 2020-07-23 stsp goto done;
2946 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2947 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2948 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2949 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2950 234035bc 2019-06-01 stsp if (err)
2951 234035bc 2019-06-01 stsp goto done;
2952 234035bc 2019-06-01 stsp }
2953 234035bc 2019-06-01 stsp } else {
2954 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2955 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2956 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2957 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2958 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2959 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2960 2e1fa222 2020-07-23 stsp } else {
2961 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2962 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2963 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2964 2e1fa222 2020-07-23 stsp }
2965 234035bc 2019-06-01 stsp if (err)
2966 234035bc 2019-06-01 stsp goto done;
2967 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2968 234035bc 2019-06-01 stsp if (err)
2969 234035bc 2019-06-01 stsp goto done;
2970 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2971 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
2972 3969253a 2020-03-07 stsp if (err) {
2973 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2974 3969253a 2020-03-07 stsp goto done;
2975 3969253a 2020-03-07 stsp }
2976 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2977 2b92fad7 2019-06-02 stsp if (err) {
2978 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2979 2b92fad7 2019-06-02 stsp goto done;
2980 2b92fad7 2019-06-02 stsp }
2981 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2982 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2983 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2984 2e1fa222 2020-07-23 stsp }
2985 234035bc 2019-06-01 stsp }
2986 234035bc 2019-06-01 stsp }
2987 234035bc 2019-06-01 stsp done:
2988 234035bc 2019-06-01 stsp free(ondisk_path);
2989 234035bc 2019-06-01 stsp return err;
2990 234035bc 2019-06-01 stsp }
2991 234035bc 2019-06-01 stsp
2992 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2993 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2994 234035bc 2019-06-01 stsp struct got_repository *repo;
2995 234035bc 2019-06-01 stsp };
2996 234035bc 2019-06-01 stsp
2997 234035bc 2019-06-01 stsp static const struct got_error *
2998 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2999 234035bc 2019-06-01 stsp {
3000 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3001 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3002 234035bc 2019-06-01 stsp unsigned char status;
3003 234035bc 2019-06-01 stsp struct stat sb;
3004 234035bc 2019-06-01 stsp char *ondisk_path;
3005 234035bc 2019-06-01 stsp
3006 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3007 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3008 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3009 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3010 234035bc 2019-06-01 stsp
3011 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3012 234035bc 2019-06-01 stsp == -1)
3013 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3014 234035bc 2019-06-01 stsp
3015 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3016 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3017 234035bc 2019-06-01 stsp if (err)
3018 234035bc 2019-06-01 stsp return err;
3019 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3020 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3021 234035bc 2019-06-01 stsp
3022 234035bc 2019-06-01 stsp return NULL;
3023 234035bc 2019-06-01 stsp }
3024 234035bc 2019-06-01 stsp
3025 818c7501 2019-07-11 stsp static const struct got_error *
3026 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3027 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3028 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3029 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3030 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3031 234035bc 2019-06-01 stsp {
3032 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3033 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3034 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3035 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3036 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3037 234035bc 2019-06-01 stsp
3038 03415a1a 2019-06-02 stsp if (commit_id1) {
3039 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3040 03415a1a 2019-06-02 stsp worktree->path_prefix);
3041 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3042 03415a1a 2019-06-02 stsp goto done;
3043 69d57f3d 2020-07-31 stsp }
3044 69d57f3d 2020-07-31 stsp if (tree_id1) {
3045 69d57f3d 2020-07-31 stsp char *id_str;
3046 03415a1a 2019-06-02 stsp
3047 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3048 03415a1a 2019-06-02 stsp if (err)
3049 03415a1a 2019-06-02 stsp goto done;
3050 f69721c3 2019-10-21 stsp
3051 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3052 f69721c3 2019-10-21 stsp if (err)
3053 f69721c3 2019-10-21 stsp goto done;
3054 f69721c3 2019-10-21 stsp
3055 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3056 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3057 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3058 f69721c3 2019-10-21 stsp free(id_str);
3059 f69721c3 2019-10-21 stsp goto done;
3060 f69721c3 2019-10-21 stsp }
3061 f69721c3 2019-10-21 stsp free(id_str);
3062 03415a1a 2019-06-02 stsp }
3063 234035bc 2019-06-01 stsp
3064 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3065 234035bc 2019-06-01 stsp worktree->path_prefix);
3066 234035bc 2019-06-01 stsp if (err)
3067 234035bc 2019-06-01 stsp goto done;
3068 234035bc 2019-06-01 stsp
3069 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3070 234035bc 2019-06-01 stsp if (err)
3071 234035bc 2019-06-01 stsp goto done;
3072 234035bc 2019-06-01 stsp
3073 234035bc 2019-06-01 stsp arg.worktree = worktree;
3074 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3075 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3076 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3077 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3078 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3079 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3080 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3081 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3082 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3083 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3084 af12c6b9 2019-06-04 stsp err = sync_err;
3085 234035bc 2019-06-01 stsp done:
3086 234035bc 2019-06-01 stsp if (tree1)
3087 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3088 234035bc 2019-06-01 stsp if (tree2)
3089 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3090 f69721c3 2019-10-21 stsp free(label_orig);
3091 818c7501 2019-07-11 stsp return err;
3092 818c7501 2019-07-11 stsp }
3093 234035bc 2019-06-01 stsp
3094 818c7501 2019-07-11 stsp const struct got_error *
3095 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3096 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3097 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3098 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3099 818c7501 2019-07-11 stsp {
3100 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3101 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3102 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3103 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3104 818c7501 2019-07-11 stsp
3105 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3106 818c7501 2019-07-11 stsp if (err)
3107 818c7501 2019-07-11 stsp return err;
3108 818c7501 2019-07-11 stsp
3109 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3110 818c7501 2019-07-11 stsp if (err)
3111 818c7501 2019-07-11 stsp goto done;
3112 818c7501 2019-07-11 stsp
3113 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3114 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3115 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3116 818c7501 2019-07-11 stsp &mok_arg);
3117 818c7501 2019-07-11 stsp if (err)
3118 818c7501 2019-07-11 stsp goto done;
3119 818c7501 2019-07-11 stsp
3120 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3121 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3122 818c7501 2019-07-11 stsp done:
3123 818c7501 2019-07-11 stsp if (fileindex)
3124 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3125 818c7501 2019-07-11 stsp free(fileindex_path);
3126 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3127 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3128 234035bc 2019-06-01 stsp err = unlockerr;
3129 234035bc 2019-06-01 stsp return err;
3130 234035bc 2019-06-01 stsp }
3131 234035bc 2019-06-01 stsp
3132 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3133 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3134 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3135 927df6b7 2019-02-10 stsp const char *status_path;
3136 927df6b7 2019-02-10 stsp size_t status_path_len;
3137 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3138 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3139 f8d1f275 2019-02-04 stsp void *status_arg;
3140 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3141 f8d1f275 2019-02-04 stsp void *cancel_arg;
3142 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3143 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3144 f2a9dc41 2019-12-13 tracey int report_unchanged;
3145 3143d852 2020-06-25 stsp int no_ignores;
3146 f8d1f275 2019-02-04 stsp };
3147 88d0e355 2019-08-03 stsp
3148 f8d1f275 2019-02-04 stsp static const struct got_error *
3149 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3150 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3151 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3152 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3153 927df6b7 2019-02-10 stsp {
3154 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3155 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3156 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3157 927df6b7 2019-02-10 stsp struct stat sb;
3158 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3159 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3160 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3161 927df6b7 2019-02-10 stsp
3162 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3163 98eaaa12 2019-08-03 stsp if (err)
3164 98eaaa12 2019-08-03 stsp return err;
3165 98eaaa12 2019-08-03 stsp
3166 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3167 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3168 98eaaa12 2019-08-03 stsp return NULL;
3169 98eaaa12 2019-08-03 stsp
3170 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3171 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3172 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3173 98eaaa12 2019-08-03 stsp }
3174 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3175 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3176 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3177 927df6b7 2019-02-10 stsp }
3178 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3179 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3180 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3181 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3182 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3183 98eaaa12 2019-08-03 stsp }
3184 98eaaa12 2019-08-03 stsp
3185 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3186 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3187 927df6b7 2019-02-10 stsp }
3188 927df6b7 2019-02-10 stsp
3189 927df6b7 2019-02-10 stsp static const struct got_error *
3190 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3191 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3192 f8d1f275 2019-02-04 stsp {
3193 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3194 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3195 f8d1f275 2019-02-04 stsp char *abspath;
3196 f8d1f275 2019-02-04 stsp
3197 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3198 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3199 0584f854 2019-04-06 stsp
3200 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3201 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3202 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3203 927df6b7 2019-02-10 stsp return NULL;
3204 927df6b7 2019-02-10 stsp
3205 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3206 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3207 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3208 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3209 f8d1f275 2019-02-04 stsp } else {
3210 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3211 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3212 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3213 f8d1f275 2019-02-04 stsp }
3214 f8d1f275 2019-02-04 stsp
3215 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3216 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3217 f8d1f275 2019-02-04 stsp free(abspath);
3218 9d31a1d8 2018-03-11 stsp return err;
3219 9d31a1d8 2018-03-11 stsp }
3220 f8d1f275 2019-02-04 stsp
3221 f8d1f275 2019-02-04 stsp static const struct got_error *
3222 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3223 f8d1f275 2019-02-04 stsp {
3224 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3225 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3226 2ec1f75b 2019-03-26 stsp unsigned char status;
3227 927df6b7 2019-02-10 stsp
3228 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3229 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3230 0584f854 2019-04-06 stsp
3231 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3232 927df6b7 2019-02-10 stsp return NULL;
3233 927df6b7 2019-02-10 stsp
3234 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3235 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3236 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3237 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3238 2ec1f75b 2019-03-26 stsp else
3239 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3240 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3241 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3242 6841da00 2019-08-08 stsp }
3243 6841da00 2019-08-08 stsp
3244 6841da00 2019-08-08 stsp void
3245 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3246 6841da00 2019-08-08 stsp {
3247 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3248 6841da00 2019-08-08 stsp
3249 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3250 6841da00 2019-08-08 stsp free((char *)pe->path);
3251 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3252 6841da00 2019-08-08 stsp }
3253 6841da00 2019-08-08 stsp
3254 6841da00 2019-08-08 stsp void
3255 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3256 6841da00 2019-08-08 stsp {
3257 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3258 6841da00 2019-08-08 stsp
3259 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3260 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3261 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3262 6841da00 2019-08-08 stsp free((char *)pe->path);
3263 6841da00 2019-08-08 stsp }
3264 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3265 6841da00 2019-08-08 stsp }
3266 6841da00 2019-08-08 stsp
3267 6841da00 2019-08-08 stsp static const struct got_error *
3268 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3269 6841da00 2019-08-08 stsp {
3270 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3271 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3272 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3273 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3274 6841da00 2019-08-08 stsp size_t linesize = 0;
3275 6841da00 2019-08-08 stsp ssize_t linelen;
3276 6841da00 2019-08-08 stsp
3277 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3278 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3279 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3280 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3281 6841da00 2019-08-08 stsp
3282 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3283 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3284 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3285 bd8de430 2019-10-04 stsp
3286 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3287 bd8de430 2019-10-04 stsp if (line[0] == '#')
3288 bd8de430 2019-10-04 stsp continue;
3289 bd8de430 2019-10-04 stsp
3290 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3291 bd8de430 2019-10-04 stsp if (line[0] == '!')
3292 bd8de430 2019-10-04 stsp continue;
3293 bd8de430 2019-10-04 stsp
3294 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3295 6841da00 2019-08-08 stsp line) == -1) {
3296 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3297 6841da00 2019-08-08 stsp goto done;
3298 6841da00 2019-08-08 stsp }
3299 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3300 6841da00 2019-08-08 stsp if (err)
3301 6841da00 2019-08-08 stsp goto done;
3302 6841da00 2019-08-08 stsp }
3303 6841da00 2019-08-08 stsp if (ferror(f)) {
3304 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3305 6841da00 2019-08-08 stsp goto done;
3306 6841da00 2019-08-08 stsp }
3307 6841da00 2019-08-08 stsp
3308 6841da00 2019-08-08 stsp dirpath = strdup(path);
3309 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3310 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3311 6841da00 2019-08-08 stsp goto done;
3312 6841da00 2019-08-08 stsp }
3313 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3314 6841da00 2019-08-08 stsp done:
3315 6841da00 2019-08-08 stsp free(line);
3316 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3317 6841da00 2019-08-08 stsp free(dirpath);
3318 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3319 6841da00 2019-08-08 stsp }
3320 6841da00 2019-08-08 stsp return err;
3321 6841da00 2019-08-08 stsp }
3322 6841da00 2019-08-08 stsp
3323 6841da00 2019-08-08 stsp int
3324 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3325 6841da00 2019-08-08 stsp {
3326 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3327 bd8de430 2019-10-04 stsp
3328 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3329 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3330 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3331 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3332 bd8de430 2019-10-04 stsp
3333 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3334 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3335 6841da00 2019-08-08 stsp
3336 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3337 bd8de430 2019-10-04 stsp continue;
3338 bd8de430 2019-10-04 stsp pattern += 3;
3339 bd8de430 2019-10-04 stsp p = path;
3340 bd8de430 2019-10-04 stsp while (*p) {
3341 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3342 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3343 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3344 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3345 bd8de430 2019-10-04 stsp p++;
3346 bd8de430 2019-10-04 stsp while (*p == '/')
3347 bd8de430 2019-10-04 stsp p++;
3348 bd8de430 2019-10-04 stsp continue;
3349 bd8de430 2019-10-04 stsp }
3350 bd8de430 2019-10-04 stsp return 1;
3351 bd8de430 2019-10-04 stsp }
3352 bd8de430 2019-10-04 stsp }
3353 bd8de430 2019-10-04 stsp }
3354 bd8de430 2019-10-04 stsp
3355 6841da00 2019-08-08 stsp /*
3356 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3357 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3358 6841da00 2019-08-08 stsp * ignores backwards.
3359 6841da00 2019-08-08 stsp */
3360 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3361 6841da00 2019-08-08 stsp while (pe) {
3362 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3363 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3364 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3365 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3366 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3367 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3368 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3369 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3370 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3371 6841da00 2019-08-08 stsp continue;
3372 6841da00 2019-08-08 stsp return 1;
3373 6841da00 2019-08-08 stsp }
3374 6841da00 2019-08-08 stsp }
3375 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3376 6841da00 2019-08-08 stsp }
3377 6841da00 2019-08-08 stsp
3378 6841da00 2019-08-08 stsp return 0;
3379 6841da00 2019-08-08 stsp }
3380 6841da00 2019-08-08 stsp
3381 6841da00 2019-08-08 stsp static const struct got_error *
3382 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3383 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3384 6841da00 2019-08-08 stsp {
3385 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3386 6841da00 2019-08-08 stsp char *ignorespath;
3387 886cec17 2019-12-15 stsp int fd = -1;
3388 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3389 6841da00 2019-08-08 stsp
3390 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3391 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3392 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3393 6841da00 2019-08-08 stsp
3394 886cec17 2019-12-15 stsp if (dirfd != -1) {
3395 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3396 886cec17 2019-12-15 stsp if (fd == -1) {
3397 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3398 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3399 886cec17 2019-12-15 stsp ignorespath);
3400 886cec17 2019-12-15 stsp } else {
3401 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3402 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3403 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3404 886cec17 2019-12-15 stsp ignorespath);
3405 886cec17 2019-12-15 stsp else {
3406 886cec17 2019-12-15 stsp fd = -1;
3407 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3408 886cec17 2019-12-15 stsp }
3409 886cec17 2019-12-15 stsp }
3410 886cec17 2019-12-15 stsp } else {
3411 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3412 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3413 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3414 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3415 886cec17 2019-12-15 stsp ignorespath);
3416 886cec17 2019-12-15 stsp } else
3417 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3418 886cec17 2019-12-15 stsp }
3419 6841da00 2019-08-08 stsp
3420 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3421 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3422 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3423 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3424 6841da00 2019-08-08 stsp free(ignorespath);
3425 6841da00 2019-08-08 stsp return err;
3426 f8d1f275 2019-02-04 stsp }
3427 f8d1f275 2019-02-04 stsp
3428 f8d1f275 2019-02-04 stsp static const struct got_error *
3429 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3430 f8d1f275 2019-02-04 stsp {
3431 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3432 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3433 f8d1f275 2019-02-04 stsp char *path = NULL;
3434 f8d1f275 2019-02-04 stsp
3435 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3436 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3437 0584f854 2019-04-06 stsp
3438 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3439 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3440 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3441 f8d1f275 2019-02-04 stsp } else {
3442 f8d1f275 2019-02-04 stsp path = de->d_name;
3443 f8d1f275 2019-02-04 stsp }
3444 f8d1f275 2019-02-04 stsp
3445 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3446 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3447 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3448 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3449 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3450 f8d1f275 2019-02-04 stsp if (parent_path[0])
3451 f8d1f275 2019-02-04 stsp free(path);
3452 b72f483a 2019-02-05 stsp return err;
3453 f8d1f275 2019-02-04 stsp }
3454 f8d1f275 2019-02-04 stsp
3455 347d1d3e 2019-07-12 stsp static const struct got_error *
3456 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3457 3143d852 2020-06-25 stsp {
3458 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3459 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3460 3143d852 2020-06-25 stsp
3461 3143d852 2020-06-25 stsp if (a->no_ignores)
3462 3143d852 2020-06-25 stsp return NULL;
3463 3143d852 2020-06-25 stsp
3464 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3465 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3466 3143d852 2020-06-25 stsp if (err)
3467 3143d852 2020-06-25 stsp return err;
3468 3143d852 2020-06-25 stsp
3469 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3470 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3471 3143d852 2020-06-25 stsp
3472 3143d852 2020-06-25 stsp return err;
3473 3143d852 2020-06-25 stsp }
3474 3143d852 2020-06-25 stsp
3475 3143d852 2020-06-25 stsp static const struct got_error *
3476 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3477 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3478 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3479 abb4604f 2019-07-27 stsp {
3480 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3481 abb4604f 2019-07-27 stsp struct stat sb;
3482 abb4604f 2019-07-27 stsp
3483 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3484 abb4604f 2019-07-27 stsp if (ie)
3485 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3486 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3487 abb4604f 2019-07-27 stsp
3488 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3489 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3490 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3491 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3492 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3493 abb4604f 2019-07-27 stsp return NULL;
3494 abb4604f 2019-07-27 stsp }
3495 abb4604f 2019-07-27 stsp
3496 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3497 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3498 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3499 abb4604f 2019-07-27 stsp
3500 abb4604f 2019-07-27 stsp return NULL;
3501 3143d852 2020-06-25 stsp }
3502 3143d852 2020-06-25 stsp
3503 3143d852 2020-06-25 stsp static const struct got_error *
3504 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3505 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3506 3143d852 2020-06-25 stsp {
3507 3143d852 2020-06-25 stsp const struct got_error *err;
3508 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3509 3143d852 2020-06-25 stsp
3510 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3511 3143d852 2020-06-25 stsp ".cvsignore");
3512 3143d852 2020-06-25 stsp if (err)
3513 3143d852 2020-06-25 stsp return err;
3514 3143d852 2020-06-25 stsp
3515 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3516 3143d852 2020-06-25 stsp ".gitignore");
3517 3143d852 2020-06-25 stsp if (err)
3518 3143d852 2020-06-25 stsp return err;
3519 3143d852 2020-06-25 stsp
3520 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3521 3143d852 2020-06-25 stsp if (err) {
3522 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3523 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3524 3143d852 2020-06-25 stsp return err;
3525 3143d852 2020-06-25 stsp }
3526 3143d852 2020-06-25 stsp for (;;) {
3527 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3528 3143d852 2020-06-25 stsp ".cvsignore");
3529 3143d852 2020-06-25 stsp if (err)
3530 3143d852 2020-06-25 stsp break;
3531 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3532 3143d852 2020-06-25 stsp ".gitignore");
3533 3143d852 2020-06-25 stsp if (err)
3534 3143d852 2020-06-25 stsp break;
3535 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3536 3143d852 2020-06-25 stsp if (err) {
3537 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3538 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3539 3143d852 2020-06-25 stsp break;
3540 3143d852 2020-06-25 stsp }
3541 b737c85a 2020-06-26 stsp free(parent_path);
3542 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3543 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3544 3143d852 2020-06-25 stsp }
3545 3143d852 2020-06-25 stsp
3546 b737c85a 2020-06-26 stsp free(parent_path);
3547 b737c85a 2020-06-26 stsp free(next_parent_path);
3548 3143d852 2020-06-25 stsp return err;
3549 abb4604f 2019-07-27 stsp }
3550 abb4604f 2019-07-27 stsp
3551 abb4604f 2019-07-27 stsp static const struct got_error *
3552 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3553 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3554 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3555 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3556 f2a9dc41 2019-12-13 tracey int report_unchanged)
3557 f8d1f275 2019-02-04 stsp {
3558 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3559 6fc93f37 2019-12-13 stsp int fd = -1;
3560 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3561 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3562 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3563 3143d852 2020-06-25 stsp
3564 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3565 f8d1f275 2019-02-04 stsp
3566 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3567 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3568 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3569 8dc303cc 2019-07-27 stsp
3570 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3571 6fc93f37 2019-12-13 stsp if (fd == -1) {
3572 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3573 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3574 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3575 abb4604f 2019-07-27 stsp else
3576 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3577 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3578 f2a9dc41 2019-12-13 tracey report_unchanged);
3579 abb4604f 2019-07-27 stsp } else {
3580 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3581 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3582 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3583 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3584 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3585 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3586 abb4604f 2019-07-27 stsp arg.status_path = path;
3587 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3588 abb4604f 2019-07-27 stsp arg.repo = repo;
3589 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3590 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3591 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3592 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3593 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3594 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3595 022fae89 2019-12-06 tracey if (!no_ignores) {
3596 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3597 3143d852 2020-06-25 stsp worktree->root_path, path);
3598 3143d852 2020-06-25 stsp if (err)
3599 3143d852 2020-06-25 stsp goto done;
3600 022fae89 2019-12-06 tracey }
3601 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3602 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3603 927df6b7 2019-02-10 stsp }
3604 3143d852 2020-06-25 stsp done:
3605 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3606 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3607 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3608 927df6b7 2019-02-10 stsp free(ondisk_path);
3609 347d1d3e 2019-07-12 stsp return err;
3610 347d1d3e 2019-07-12 stsp }
3611 347d1d3e 2019-07-12 stsp
3612 347d1d3e 2019-07-12 stsp const struct got_error *
3613 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3614 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3615 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3616 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3617 347d1d3e 2019-07-12 stsp {
3618 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3619 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3620 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3621 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3622 347d1d3e 2019-07-12 stsp
3623 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3624 347d1d3e 2019-07-12 stsp if (err)
3625 347d1d3e 2019-07-12 stsp return err;
3626 347d1d3e 2019-07-12 stsp
3627 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3628 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3629 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3630 72ea6654 2019-07-27 stsp if (err)
3631 72ea6654 2019-07-27 stsp break;
3632 72ea6654 2019-07-27 stsp }
3633 f8d1f275 2019-02-04 stsp free(fileindex_path);
3634 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3635 f8d1f275 2019-02-04 stsp return err;
3636 f8d1f275 2019-02-04 stsp }
3637 6c7ab921 2019-03-18 stsp
3638 6c7ab921 2019-03-18 stsp const struct got_error *
3639 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3640 6c7ab921 2019-03-18 stsp const char *arg)
3641 6c7ab921 2019-03-18 stsp {
3642 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3643 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3644 6c7ab921 2019-03-18 stsp size_t len;
3645 00bb5ea0 2020-07-23 stsp struct stat sb;
3646 01740607 2020-11-04 stsp char *abspath = NULL;
3647 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3648 6c7ab921 2019-03-18 stsp
3649 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3650 6c7ab921 2019-03-18 stsp
3651 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3652 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3653 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3654 00bb5ea0 2020-07-23 stsp
3655 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3656 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3657 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3658 00bb5ea0 2020-07-23 stsp goto done;
3659 00bb5ea0 2020-07-23 stsp }
3660 727173c3 2020-11-06 stsp sb.st_mode = 0;
3661 00bb5ea0 2020-07-23 stsp }
3662 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3663 00bb5ea0 2020-07-23 stsp /*
3664 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3665 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3666 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3667 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3668 00bb5ea0 2020-07-23 stsp */
3669 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3670 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3671 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3672 00bb5ea0 2020-07-23 stsp goto done;
3673 00bb5ea0 2020-07-23 stsp }
3674 00bb5ea0 2020-07-23 stsp
3675 00bb5ea0 2020-07-23 stsp }
3676 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3677 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3678 00bb5ea0 2020-07-23 stsp if (err)
3679 d0710d08 2019-07-22 stsp goto done;
3680 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3681 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3682 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3683 00bb5ea0 2020-07-23 stsp goto done;
3684 00bb5ea0 2020-07-23 stsp }
3685 00bb5ea0 2020-07-23 stsp } else {
3686 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3687 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3688 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3689 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3690 00bb5ea0 2020-07-23 stsp goto done;
3691 00bb5ea0 2020-07-23 stsp }
3692 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3693 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3694 01740607 2020-11-04 stsp goto done;
3695 01740607 2020-11-04 stsp }
3696 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3697 01740607 2020-11-04 stsp sizeof(canonpath));
3698 01740607 2020-11-04 stsp if (err)
3699 00bb5ea0 2020-07-23 stsp goto done;
3700 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3701 01740607 2020-11-04 stsp if (resolved == NULL) {
3702 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3703 01740607 2020-11-04 stsp goto done;
3704 00bb5ea0 2020-07-23 stsp }
3705 d0710d08 2019-07-22 stsp }
3706 d0710d08 2019-07-22 stsp }
3707 6c7ab921 2019-03-18 stsp
3708 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3709 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3710 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3711 6c7ab921 2019-03-18 stsp goto done;
3712 6c7ab921 2019-03-18 stsp }
3713 6c7ab921 2019-03-18 stsp
3714 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3715 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3716 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3717 f6d88e1a 2019-05-29 stsp if (err)
3718 f6d88e1a 2019-05-29 stsp goto done;
3719 f6d88e1a 2019-05-29 stsp } else {
3720 f6d88e1a 2019-05-29 stsp path = strdup("");
3721 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3722 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3723 f6d88e1a 2019-05-29 stsp goto done;
3724 f6d88e1a 2019-05-29 stsp }
3725 6c7ab921 2019-03-18 stsp }
3726 6c7ab921 2019-03-18 stsp
3727 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3728 6c7ab921 2019-03-18 stsp len = strlen(path);
3729 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3730 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3731 6c7ab921 2019-03-18 stsp len--;
3732 6c7ab921 2019-03-18 stsp }
3733 6c7ab921 2019-03-18 stsp done:
3734 01740607 2020-11-04 stsp free(abspath);
3735 6c7ab921 2019-03-18 stsp free(resolved);
3736 d0710d08 2019-07-22 stsp free(cwd);
3737 6c7ab921 2019-03-18 stsp if (err == NULL)
3738 6c7ab921 2019-03-18 stsp *wt_path = path;
3739 6c7ab921 2019-03-18 stsp else
3740 6c7ab921 2019-03-18 stsp free(path);
3741 d00136be 2019-03-26 stsp return err;
3742 d00136be 2019-03-26 stsp }
3743 d00136be 2019-03-26 stsp
3744 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3745 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3746 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3747 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3748 4e68cba3 2019-11-23 stsp void *progress_arg;
3749 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3750 4e68cba3 2019-11-23 stsp };
3751 4e68cba3 2019-11-23 stsp
3752 4e68cba3 2019-11-23 stsp static const struct got_error *
3753 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3754 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3755 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3756 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3757 07f5b47a 2019-06-02 stsp {
3758 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3759 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3760 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3761 6d022e97 2019-08-04 stsp struct stat sb;
3762 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3763 07f5b47a 2019-06-02 stsp
3764 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3765 dbb83fbd 2019-12-12 stsp relpath) == -1)
3766 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3767 dbb83fbd 2019-12-12 stsp
3768 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3769 6d022e97 2019-08-04 stsp if (ie) {
3770 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3771 12463d8b 2019-12-13 stsp de_name, a->repo);
3772 6d022e97 2019-08-04 stsp if (err)
3773 dbb83fbd 2019-12-12 stsp goto done;
3774 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3775 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3776 dbb83fbd 2019-12-12 stsp goto done;
3777 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3778 dbb83fbd 2019-12-12 stsp if (err)
3779 dbb83fbd 2019-12-12 stsp goto done;
3780 6d022e97 2019-08-04 stsp }
3781 07f5b47a 2019-06-02 stsp
3782 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3783 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3784 dbb83fbd 2019-12-12 stsp goto done;
3785 4e68cba3 2019-11-23 stsp }
3786 07f5b47a 2019-06-02 stsp
3787 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3788 4e68cba3 2019-11-23 stsp if (err)
3789 4e68cba3 2019-11-23 stsp goto done;
3790 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3791 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3792 3969253a 2020-03-07 stsp if (err) {
3793 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3794 3969253a 2020-03-07 stsp goto done;
3795 3969253a 2020-03-07 stsp }
3796 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3797 07f5b47a 2019-06-02 stsp if (err) {
3798 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3799 4e68cba3 2019-11-23 stsp goto done;
3800 07f5b47a 2019-06-02 stsp }
3801 4e68cba3 2019-11-23 stsp done:
3802 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3803 dbb83fbd 2019-12-12 stsp if (err)
3804 dbb83fbd 2019-12-12 stsp return err;
3805 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3806 dbb83fbd 2019-12-12 stsp return NULL;
3807 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3808 07f5b47a 2019-06-02 stsp }
3809 07f5b47a 2019-06-02 stsp
3810 d00136be 2019-03-26 stsp const struct got_error *
3811 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3812 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3813 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3814 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3815 d00136be 2019-03-26 stsp {
3816 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3817 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3818 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3819 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3820 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3821 d00136be 2019-03-26 stsp
3822 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3823 d00136be 2019-03-26 stsp if (err)
3824 d00136be 2019-03-26 stsp return err;
3825 d00136be 2019-03-26 stsp
3826 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3827 d00136be 2019-03-26 stsp if (err)
3828 d00136be 2019-03-26 stsp goto done;
3829 d00136be 2019-03-26 stsp
3830 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3831 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3832 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3833 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3834 4e68cba3 2019-11-23 stsp saa.repo = repo;
3835 4e68cba3 2019-11-23 stsp
3836 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3837 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3838 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3839 1dd54920 2019-05-11 stsp if (err)
3840 af12c6b9 2019-06-04 stsp break;
3841 1dd54920 2019-05-11 stsp }
3842 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3843 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3844 af12c6b9 2019-06-04 stsp err = sync_err;
3845 d00136be 2019-03-26 stsp done:
3846 fb399478 2019-07-12 stsp free(fileindex_path);
3847 d00136be 2019-03-26 stsp if (fileindex)
3848 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3849 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3850 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3851 d00136be 2019-03-26 stsp err = unlockerr;
3852 6c7ab921 2019-03-18 stsp return err;
3853 6c7ab921 2019-03-18 stsp }
3854 17ed4618 2019-06-02 stsp
3855 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3856 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3857 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3858 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3859 f2a9dc41 2019-12-13 tracey void *progress_arg;
3860 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3861 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3862 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3863 766841c2 2020-08-13 stsp const char *status_codes;
3864 f2a9dc41 2019-12-13 tracey };
3865 f2a9dc41 2019-12-13 tracey
3866 f2a9dc41 2019-12-13 tracey static const struct got_error *
3867 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3868 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3869 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3870 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3871 17ed4618 2019-06-02 stsp {
3872 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3873 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3874 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3875 17ed4618 2019-06-02 stsp struct stat sb;
3876 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3877 17ed4618 2019-06-02 stsp
3878 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3879 17ed4618 2019-06-02 stsp if (ie == NULL)
3880 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3881 2ec1f75b 2019-03-26 stsp
3882 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3883 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3884 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3885 9acbc4fa 2019-08-03 stsp return NULL;
3886 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3887 9acbc4fa 2019-08-03 stsp }
3888 9acbc4fa 2019-08-03 stsp
3889 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3890 f2a9dc41 2019-12-13 tracey relpath) == -1)
3891 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3892 f2a9dc41 2019-12-13 tracey
3893 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3894 12463d8b 2019-12-13 stsp a->repo);
3895 17ed4618 2019-06-02 stsp if (err)
3896 f2a9dc41 2019-12-13 tracey goto done;
3897 17ed4618 2019-06-02 stsp
3898 766841c2 2020-08-13 stsp if (a->status_codes) {
3899 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3900 766841c2 2020-08-13 stsp int i;
3901 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3902 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3903 766841c2 2020-08-13 stsp break;
3904 766841c2 2020-08-13 stsp }
3905 766841c2 2020-08-13 stsp if (i == ncodes) {
3906 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3907 766841c2 2020-08-13 stsp free(ondisk_path);
3908 766841c2 2020-08-13 stsp return NULL;
3909 766841c2 2020-08-13 stsp }
3910 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3911 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3912 766841c2 2020-08-13 stsp static char msg[64];
3913 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3914 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3915 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3916 766841c2 2020-08-13 stsp goto done;
3917 766841c2 2020-08-13 stsp }
3918 766841c2 2020-08-13 stsp }
3919 766841c2 2020-08-13 stsp
3920 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3921 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3922 f2a9dc41 2019-12-13 tracey goto done;
3923 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3924 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3925 f2a9dc41 2019-12-13 tracey goto done;
3926 f2a9dc41 2019-12-13 tracey }
3927 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3928 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3929 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3930 f2a9dc41 2019-12-13 tracey goto done;
3931 f2a9dc41 2019-12-13 tracey }
3932 17ed4618 2019-06-02 stsp }
3933 17ed4618 2019-06-02 stsp
3934 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3935 20a2ad1c 2020-10-20 stsp size_t root_len;
3936 20a2ad1c 2020-10-20 stsp
3937 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3938 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3939 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3940 12463d8b 2019-12-13 stsp ondisk_path);
3941 12463d8b 2019-12-13 stsp goto done;
3942 12463d8b 2019-12-13 stsp }
3943 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3944 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3945 12463d8b 2019-12-13 stsp goto done;
3946 15341bfd 2020-03-05 tracey }
3947 15341bfd 2020-03-05 tracey
3948 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3949 20a2ad1c 2020-10-20 stsp do {
3950 20a2ad1c 2020-10-20 stsp char *parent;
3951 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3952 20a2ad1c 2020-10-20 stsp if (err)
3953 2513f20a 2020-10-20 stsp goto done;
3954 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3955 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3956 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3957 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3958 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3959 20a2ad1c 2020-10-20 stsp ondisk_path);
3960 15341bfd 2020-03-05 tracey break;
3961 15341bfd 2020-03-05 tracey }
3962 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3963 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3964 f2a9dc41 2019-12-13 tracey }
3965 17ed4618 2019-06-02 stsp
3966 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3967 f2a9dc41 2019-12-13 tracey done:
3968 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3969 f2a9dc41 2019-12-13 tracey if (err)
3970 f2a9dc41 2019-12-13 tracey return err;
3971 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3972 f2a9dc41 2019-12-13 tracey return NULL;
3973 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3974 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3975 17ed4618 2019-06-02 stsp }
3976 17ed4618 2019-06-02 stsp
3977 2ec1f75b 2019-03-26 stsp const struct got_error *
3978 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3979 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3980 766841c2 2020-08-13 stsp const char *status_codes,
3981 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3982 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3983 2ec1f75b 2019-03-26 stsp {
3984 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3985 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3986 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3987 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3988 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3989 2ec1f75b 2019-03-26 stsp
3990 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3991 2ec1f75b 2019-03-26 stsp if (err)
3992 2ec1f75b 2019-03-26 stsp return err;
3993 2ec1f75b 2019-03-26 stsp
3994 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3995 2ec1f75b 2019-03-26 stsp if (err)
3996 2ec1f75b 2019-03-26 stsp goto done;
3997 f2a9dc41 2019-12-13 tracey
3998 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3999 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4000 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4001 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4002 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4003 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4004 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4005 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4006 2ec1f75b 2019-03-26 stsp
4007 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4008 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4009 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4010 17ed4618 2019-06-02 stsp if (err)
4011 af12c6b9 2019-06-04 stsp break;
4012 2ec1f75b 2019-03-26 stsp }
4013 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4014 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4015 af12c6b9 2019-06-04 stsp err = sync_err;
4016 2ec1f75b 2019-03-26 stsp done:
4017 fb399478 2019-07-12 stsp free(fileindex_path);
4018 2ec1f75b 2019-03-26 stsp if (fileindex)
4019 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4020 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4021 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4022 2ec1f75b 2019-03-26 stsp err = unlockerr;
4023 33aa809d 2019-08-08 stsp return err;
4024 33aa809d 2019-08-08 stsp }
4025 33aa809d 2019-08-08 stsp
4026 33aa809d 2019-08-08 stsp static const struct got_error *
4027 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4028 33aa809d 2019-08-08 stsp {
4029 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4030 33aa809d 2019-08-08 stsp char *line = NULL;
4031 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4032 33aa809d 2019-08-08 stsp ssize_t linelen;
4033 33aa809d 2019-08-08 stsp
4034 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4035 33aa809d 2019-08-08 stsp if (linelen == -1) {
4036 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4037 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4038 33aa809d 2019-08-08 stsp goto done;
4039 33aa809d 2019-08-08 stsp }
4040 33aa809d 2019-08-08 stsp return NULL;
4041 33aa809d 2019-08-08 stsp }
4042 33aa809d 2019-08-08 stsp if (outfile) {
4043 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4044 33aa809d 2019-08-08 stsp if (n != linelen) {
4045 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4046 33aa809d 2019-08-08 stsp goto done;
4047 33aa809d 2019-08-08 stsp }
4048 33aa809d 2019-08-08 stsp }
4049 33aa809d 2019-08-08 stsp if (rejectfile) {
4050 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4051 33aa809d 2019-08-08 stsp if (n != linelen)
4052 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4053 33aa809d 2019-08-08 stsp }
4054 33aa809d 2019-08-08 stsp done:
4055 33aa809d 2019-08-08 stsp free(line);
4056 2ec1f75b 2019-03-26 stsp return err;
4057 2ec1f75b 2019-03-26 stsp }
4058 1f1abb7e 2019-08-08 stsp
4059 33aa809d 2019-08-08 stsp static const struct got_error *
4060 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4061 33aa809d 2019-08-08 stsp {
4062 33aa809d 2019-08-08 stsp char *line = NULL;
4063 33aa809d 2019-08-08 stsp size_t linesize = 0;
4064 33aa809d 2019-08-08 stsp ssize_t linelen;
4065 33aa809d 2019-08-08 stsp
4066 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4067 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4068 500467ff 2019-09-25 hiltjo if (ferror(f))
4069 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4070 500467ff 2019-09-25 hiltjo return NULL;
4071 500467ff 2019-09-25 hiltjo }
4072 33aa809d 2019-08-08 stsp free(line);
4073 33aa809d 2019-08-08 stsp return NULL;
4074 33aa809d 2019-08-08 stsp }
4075 33aa809d 2019-08-08 stsp
4076 33aa809d 2019-08-08 stsp static const struct got_error *
4077 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4078 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4079 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4080 33aa809d 2019-08-08 stsp {
4081 33aa809d 2019-08-08 stsp const struct got_error *err;
4082 33aa809d 2019-08-08 stsp
4083 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4084 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4085 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4086 33aa809d 2019-08-08 stsp if (err)
4087 33aa809d 2019-08-08 stsp return err;
4088 33aa809d 2019-08-08 stsp (*line_cur1)++;
4089 33aa809d 2019-08-08 stsp }
4090 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4091 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4092 33aa809d 2019-08-08 stsp if (rejectfile)
4093 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4094 33aa809d 2019-08-08 stsp else
4095 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4096 33aa809d 2019-08-08 stsp if (err)
4097 33aa809d 2019-08-08 stsp return err;
4098 33aa809d 2019-08-08 stsp (*line_cur2)++;
4099 33aa809d 2019-08-08 stsp }
4100 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4101 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4102 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4103 33aa809d 2019-08-08 stsp if (err)
4104 33aa809d 2019-08-08 stsp return err;
4105 33aa809d 2019-08-08 stsp (*line_cur2)++;
4106 33aa809d 2019-08-08 stsp }
4107 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4108 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4109 33aa809d 2019-08-08 stsp if (rejectfile)
4110 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4111 33aa809d 2019-08-08 stsp else
4112 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4113 33aa809d 2019-08-08 stsp if (err)
4114 33aa809d 2019-08-08 stsp return err;
4115 33aa809d 2019-08-08 stsp (*line_cur1)++;
4116 33aa809d 2019-08-08 stsp }
4117 f1e81a05 2019-08-10 stsp
4118 f1e81a05 2019-08-10 stsp return NULL;
4119 f1e81a05 2019-08-10 stsp }
4120 f1e81a05 2019-08-10 stsp
4121 f1e81a05 2019-08-10 stsp static const struct got_error *
4122 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4123 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4124 f1e81a05 2019-08-10 stsp {
4125 f1e81a05 2019-08-10 stsp const struct got_error *err;
4126 f1e81a05 2019-08-10 stsp
4127 f1e81a05 2019-08-10 stsp if (outfile) {
4128 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4129 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4130 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4131 f1e81a05 2019-08-10 stsp if (err)
4132 f1e81a05 2019-08-10 stsp return err;
4133 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4134 f1e81a05 2019-08-10 stsp }
4135 f1e81a05 2019-08-10 stsp }
4136 f1e81a05 2019-08-10 stsp if (rejectfile) {
4137 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4138 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4139 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4140 f1e81a05 2019-08-10 stsp if (err)
4141 f1e81a05 2019-08-10 stsp return err;
4142 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4143 f1e81a05 2019-08-10 stsp }
4144 33aa809d 2019-08-08 stsp }
4145 33aa809d 2019-08-08 stsp
4146 33aa809d 2019-08-08 stsp return NULL;
4147 33aa809d 2019-08-08 stsp }
4148 33aa809d 2019-08-08 stsp
4149 33aa809d 2019-08-08 stsp static const struct got_error *
4150 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4151 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4152 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4153 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4154 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4155 33aa809d 2019-08-08 stsp {
4156 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4157 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4158 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4159 33aa809d 2019-08-08 stsp FILE *hunkfile;
4160 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4161 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4162 fe621944 2020-11-10 stsp int rc;
4163 33aa809d 2019-08-08 stsp
4164 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4165 33aa809d 2019-08-08 stsp
4166 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4167 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4168 fe621944 2020-11-10 stsp start_old = cc.left.start;
4169 fe621944 2020-11-10 stsp end_old = cc.left.end;
4170 fe621944 2020-11-10 stsp start_new = cc.right.start;
4171 fe621944 2020-11-10 stsp end_new = cc.right.end;
4172 33aa809d 2019-08-08 stsp
4173 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4174 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4175 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4176 33aa809d 2019-08-08 stsp
4177 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4178 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4179 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4180 33aa809d 2019-08-08 stsp
4181 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4182 fe621944 2020-11-10 stsp if (diff_state == NULL)
4183 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4184 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4185 fe621944 2020-11-10 stsp
4186 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4187 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4188 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4189 33aa809d 2019-08-08 stsp goto done;
4190 33aa809d 2019-08-08 stsp }
4191 fe621944 2020-11-10 stsp
4192 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4193 fe621944 2020-11-10 stsp diff_result, &cc);
4194 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4195 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4196 33aa809d 2019-08-08 stsp goto done;
4197 33aa809d 2019-08-08 stsp }
4198 fe621944 2020-11-10 stsp
4199 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4200 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4201 33aa809d 2019-08-08 stsp goto done;
4202 33aa809d 2019-08-08 stsp }
4203 33aa809d 2019-08-08 stsp
4204 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4205 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4206 33aa809d 2019-08-08 stsp if (err)
4207 33aa809d 2019-08-08 stsp goto done;
4208 33aa809d 2019-08-08 stsp
4209 33aa809d 2019-08-08 stsp switch (*choice) {
4210 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4211 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4212 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4213 33aa809d 2019-08-08 stsp break;
4214 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4215 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4216 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4217 33aa809d 2019-08-08 stsp break;
4218 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4219 33aa809d 2019-08-08 stsp break;
4220 33aa809d 2019-08-08 stsp default:
4221 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4222 33aa809d 2019-08-08 stsp break;
4223 33aa809d 2019-08-08 stsp }
4224 33aa809d 2019-08-08 stsp done:
4225 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4226 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4227 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4228 33aa809d 2019-08-08 stsp return err;
4229 33aa809d 2019-08-08 stsp }
4230 33aa809d 2019-08-08 stsp
4231 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4232 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4233 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4234 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4235 1f1abb7e 2019-08-08 stsp void *progress_arg;
4236 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4237 33aa809d 2019-08-08 stsp void *patch_arg;
4238 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4239 1f1abb7e 2019-08-08 stsp };
4240 a129376b 2019-03-28 stsp
4241 e20a8b6f 2019-06-04 stsp static const struct got_error *
4242 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4243 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4244 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4245 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4246 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4247 33aa809d 2019-08-08 stsp {
4248 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4249 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4250 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4251 1ebedb77 2019-10-19 stsp int fd2 = -1;
4252 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4253 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4254 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4255 fe621944 2020-11-10 stsp struct stat sb2;
4256 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4257 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4258 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4259 33aa809d 2019-08-08 stsp
4260 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4261 33aa809d 2019-08-08 stsp
4262 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4263 33aa809d 2019-08-08 stsp if (err)
4264 33aa809d 2019-08-08 stsp return err;
4265 33aa809d 2019-08-08 stsp
4266 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4267 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4268 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4269 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4270 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4271 fa3cef63 2020-07-23 stsp goto done;
4272 fa3cef63 2020-07-23 stsp }
4273 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4274 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4275 fa3cef63 2020-07-23 stsp if (link_len == -1)
4276 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4277 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4278 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4279 12463d8b 2019-12-13 stsp }
4280 12463d8b 2019-12-13 stsp } else {
4281 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4282 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4283 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4284 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4285 fa3cef63 2020-07-23 stsp goto done;
4286 fa3cef63 2020-07-23 stsp }
4287 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4288 fa3cef63 2020-07-23 stsp sizeof(link_target));
4289 fa3cef63 2020-07-23 stsp if (link_len == -1)
4290 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4291 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4292 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4293 12463d8b 2019-12-13 stsp }
4294 1ebedb77 2019-10-19 stsp }
4295 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4296 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4297 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4298 fa3cef63 2020-07-23 stsp goto done;
4299 fa3cef63 2020-07-23 stsp }
4300 1ebedb77 2019-10-19 stsp
4301 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4302 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4303 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4304 fa3cef63 2020-07-23 stsp goto done;
4305 fa3cef63 2020-07-23 stsp }
4306 fa3cef63 2020-07-23 stsp fd2 = -1;
4307 fa3cef63 2020-07-23 stsp } else {
4308 fa3cef63 2020-07-23 stsp size_t n;
4309 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4310 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4311 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4312 fa3cef63 2020-07-23 stsp goto done;
4313 fa3cef63 2020-07-23 stsp }
4314 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4315 fa3cef63 2020-07-23 stsp if (n != link_len) {
4316 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4317 fa3cef63 2020-07-23 stsp goto done;
4318 fa3cef63 2020-07-23 stsp }
4319 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4320 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4321 fa3cef63 2020-07-23 stsp goto done;
4322 fa3cef63 2020-07-23 stsp }
4323 fa3cef63 2020-07-23 stsp rewind(f2);
4324 33aa809d 2019-08-08 stsp }
4325 33aa809d 2019-08-08 stsp
4326 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4327 33aa809d 2019-08-08 stsp if (err)
4328 33aa809d 2019-08-08 stsp goto done;
4329 33aa809d 2019-08-08 stsp
4330 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4331 33aa809d 2019-08-08 stsp if (err)
4332 33aa809d 2019-08-08 stsp goto done;
4333 33aa809d 2019-08-08 stsp
4334 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4335 33aa809d 2019-08-08 stsp if (err)
4336 33aa809d 2019-08-08 stsp goto done;
4337 33aa809d 2019-08-08 stsp
4338 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4339 fe621944 2020-11-10 stsp NULL);
4340 33aa809d 2019-08-08 stsp if (err)
4341 33aa809d 2019-08-08 stsp goto done;
4342 33aa809d 2019-08-08 stsp
4343 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4344 33aa809d 2019-08-08 stsp if (err)
4345 33aa809d 2019-08-08 stsp goto done;
4346 33aa809d 2019-08-08 stsp
4347 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4348 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4349 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4350 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4351 fe621944 2020-11-10 stsp
4352 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4353 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4354 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4355 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4356 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4357 fe621944 2020-11-10 stsp nchanges++;
4358 fe621944 2020-11-10 stsp }
4359 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4360 33aa809d 2019-08-08 stsp int choice;
4361 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4362 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4363 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4364 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4365 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4366 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4367 33aa809d 2019-08-08 stsp if (err)
4368 33aa809d 2019-08-08 stsp goto done;
4369 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4370 33aa809d 2019-08-08 stsp have_content = 1;
4371 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4372 33aa809d 2019-08-08 stsp break;
4373 33aa809d 2019-08-08 stsp }
4374 1ebedb77 2019-10-19 stsp if (have_content) {
4375 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4376 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4377 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4378 1ebedb77 2019-10-19 stsp if (err)
4379 1ebedb77 2019-10-19 stsp goto done;
4380 1ebedb77 2019-10-19 stsp
4381 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4382 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4383 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4384 fa3cef63 2020-07-23 stsp goto done;
4385 fa3cef63 2020-07-23 stsp }
4386 1ebedb77 2019-10-19 stsp }
4387 1ebedb77 2019-10-19 stsp }
4388 33aa809d 2019-08-08 stsp done:
4389 33aa809d 2019-08-08 stsp free(id_str);
4390 33aa809d 2019-08-08 stsp if (blob)
4391 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4392 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4393 fe621944 2020-11-10 stsp if (err == NULL)
4394 fe621944 2020-11-10 stsp err = free_err;
4395 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4396 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4397 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4398 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4399 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4400 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4401 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4402 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4403 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4404 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4405 33aa809d 2019-08-08 stsp if (err || !have_content) {
4406 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4407 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4408 33aa809d 2019-08-08 stsp free(*path_outfile);
4409 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4410 33aa809d 2019-08-08 stsp }
4411 33aa809d 2019-08-08 stsp free(path1);
4412 33aa809d 2019-08-08 stsp return err;
4413 33aa809d 2019-08-08 stsp }
4414 33aa809d 2019-08-08 stsp
4415 33aa809d 2019-08-08 stsp static const struct got_error *
4416 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4417 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4418 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4419 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4420 a129376b 2019-03-28 stsp {
4421 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4422 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4423 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4424 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4425 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4426 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4427 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4428 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4429 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4430 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4431 a129376b 2019-03-28 stsp
4432 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4433 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4434 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4435 d3bcc3d1 2019-08-08 stsp return NULL;
4436 3d69ad8d 2019-08-17 semarie
4437 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4438 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4439 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4440 d3bcc3d1 2019-08-08 stsp
4441 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4442 65084dad 2019-08-08 stsp if (ie == NULL)
4443 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4444 a129376b 2019-03-28 stsp
4445 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4446 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4447 a129376b 2019-03-28 stsp if (err) {
4448 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4449 a129376b 2019-03-28 stsp goto done;
4450 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4451 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4452 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4453 e20a8b6f 2019-06-04 stsp goto done;
4454 e20a8b6f 2019-06-04 stsp }
4455 a129376b 2019-03-28 stsp }
4456 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4457 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4458 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4459 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4460 a129376b 2019-03-28 stsp goto done;
4461 a129376b 2019-03-28 stsp }
4462 a129376b 2019-03-28 stsp } else {
4463 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4464 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4465 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4466 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4467 a129376b 2019-03-28 stsp goto done;
4468 a129376b 2019-03-28 stsp }
4469 a129376b 2019-03-28 stsp } else {
4470 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4471 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4472 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4473 a129376b 2019-03-28 stsp goto done;
4474 a129376b 2019-03-28 stsp }
4475 a129376b 2019-03-28 stsp }
4476 a129376b 2019-03-28 stsp }
4477 a129376b 2019-03-28 stsp
4478 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4479 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4480 a9fa2909 2019-07-27 stsp if (err) {
4481 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4482 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4483 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4484 a9fa2909 2019-07-27 stsp goto done;
4485 a9fa2909 2019-07-27 stsp } else {
4486 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4487 a9fa2909 2019-07-27 stsp if (err)
4488 a9fa2909 2019-07-27 stsp goto done;
4489 a9fa2909 2019-07-27 stsp
4490 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4491 1233e6b6 2020-10-19 stsp if (err)
4492 a9fa2909 2019-07-27 stsp goto done;
4493 a9fa2909 2019-07-27 stsp
4494 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4495 1233e6b6 2020-10-19 stsp free(te_name);
4496 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4497 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4498 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4499 a9fa2909 2019-07-27 stsp goto done;
4500 a9fa2909 2019-07-27 stsp }
4501 a129376b 2019-03-28 stsp }
4502 a129376b 2019-03-28 stsp
4503 a129376b 2019-03-28 stsp switch (status) {
4504 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4505 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4506 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4507 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4508 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4509 33aa809d 2019-08-08 stsp if (err)
4510 33aa809d 2019-08-08 stsp goto done;
4511 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4512 33aa809d 2019-08-08 stsp break;
4513 33aa809d 2019-08-08 stsp }
4514 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4515 1f1abb7e 2019-08-08 stsp ie->path);
4516 1ee397ad 2019-07-12 stsp if (err)
4517 1ee397ad 2019-07-12 stsp goto done;
4518 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4519 a129376b 2019-03-28 stsp break;
4520 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4521 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4522 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4523 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4524 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4525 33aa809d 2019-08-08 stsp if (err)
4526 33aa809d 2019-08-08 stsp goto done;
4527 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4528 33aa809d 2019-08-08 stsp break;
4529 33aa809d 2019-08-08 stsp }
4530 33aa809d 2019-08-08 stsp /* fall through */
4531 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4532 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4533 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4534 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4535 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4536 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4537 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4538 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4539 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4540 24278f30 2019-08-03 stsp } else
4541 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4542 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4543 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4544 a129376b 2019-03-28 stsp if (err)
4545 65084dad 2019-08-08 stsp goto done;
4546 65084dad 2019-08-08 stsp
4547 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4548 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4549 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4550 a129376b 2019-03-28 stsp goto done;
4551 65084dad 2019-08-08 stsp }
4552 65084dad 2019-08-08 stsp
4553 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4554 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4555 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4556 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4557 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4558 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4559 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4560 33aa809d 2019-08-08 stsp break;
4561 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4562 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4563 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4564 369fd7e5 2020-07-23 stsp path_content);
4565 369fd7e5 2020-07-23 stsp break;
4566 369fd7e5 2020-07-23 stsp }
4567 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4568 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4569 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4570 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4571 369fd7e5 2020-07-23 stsp } else {
4572 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4573 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4574 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4575 369fd7e5 2020-07-23 stsp goto done;
4576 369fd7e5 2020-07-23 stsp }
4577 33aa809d 2019-08-08 stsp }
4578 33aa809d 2019-08-08 stsp } else {
4579 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4580 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4581 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4582 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4583 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4584 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4585 2e1fa222 2020-07-23 stsp } else {
4586 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4587 2e1fa222 2020-07-23 stsp ie->path,
4588 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4589 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4590 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4591 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4592 2e1fa222 2020-07-23 stsp }
4593 a129376b 2019-03-28 stsp if (err)
4594 a129376b 2019-03-28 stsp goto done;
4595 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4596 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4597 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4598 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4599 437adc9d 2020-12-10 yzhong blob->id.sha1,
4600 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4601 2e1fa222 2020-07-23 stsp if (err)
4602 2e1fa222 2020-07-23 stsp goto done;
4603 2e1fa222 2020-07-23 stsp }
4604 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4605 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4606 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4607 33aa809d 2019-08-08 stsp }
4608 a129376b 2019-03-28 stsp }
4609 a129376b 2019-03-28 stsp break;
4610 e20a8b6f 2019-06-04 stsp }
4611 a129376b 2019-03-28 stsp default:
4612 1f1abb7e 2019-08-08 stsp break;
4613 a129376b 2019-03-28 stsp }
4614 a129376b 2019-03-28 stsp done:
4615 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4616 33aa809d 2019-08-08 stsp free(path_content);
4617 e20a8b6f 2019-06-04 stsp free(parent_path);
4618 a129376b 2019-03-28 stsp free(tree_path);
4619 a129376b 2019-03-28 stsp if (blob)
4620 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4621 a129376b 2019-03-28 stsp if (tree)
4622 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4623 a129376b 2019-03-28 stsp free(tree_id);
4624 e20a8b6f 2019-06-04 stsp return err;
4625 e20a8b6f 2019-06-04 stsp }
4626 e20a8b6f 2019-06-04 stsp
4627 e20a8b6f 2019-06-04 stsp const struct got_error *
4628 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4629 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4630 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4631 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4632 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4633 e20a8b6f 2019-06-04 stsp {
4634 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4635 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4636 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4637 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4638 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4639 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4640 e20a8b6f 2019-06-04 stsp
4641 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4642 e20a8b6f 2019-06-04 stsp if (err)
4643 e20a8b6f 2019-06-04 stsp return err;
4644 e20a8b6f 2019-06-04 stsp
4645 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4646 e20a8b6f 2019-06-04 stsp if (err)
4647 e20a8b6f 2019-06-04 stsp goto done;
4648 e20a8b6f 2019-06-04 stsp
4649 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4650 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4651 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4652 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4653 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4654 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4655 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4656 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4657 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4658 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4659 e20a8b6f 2019-06-04 stsp if (err)
4660 af12c6b9 2019-06-04 stsp break;
4661 e20a8b6f 2019-06-04 stsp }
4662 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4663 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4664 e20a8b6f 2019-06-04 stsp err = sync_err;
4665 af12c6b9 2019-06-04 stsp done:
4666 fb399478 2019-07-12 stsp free(fileindex_path);
4667 a129376b 2019-03-28 stsp if (fileindex)
4668 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4669 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4670 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4671 a129376b 2019-03-28 stsp err = unlockerr;
4672 c4296144 2019-05-09 stsp return err;
4673 c4296144 2019-05-09 stsp }
4674 c4296144 2019-05-09 stsp
4675 cf066bf8 2019-05-09 stsp static void
4676 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4677 cf066bf8 2019-05-09 stsp {
4678 24519714 2019-05-09 stsp free(ct->path);
4679 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4680 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4681 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4682 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4683 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4684 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4685 cf066bf8 2019-05-09 stsp free(ct);
4686 cf066bf8 2019-05-09 stsp }
4687 24519714 2019-05-09 stsp
4688 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4689 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4690 24519714 2019-05-09 stsp struct got_repository *repo;
4691 24519714 2019-05-09 stsp struct got_worktree *worktree;
4692 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4693 5f8a88c6 2019-08-03 stsp int have_staged_files;
4694 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4695 24519714 2019-05-09 stsp };
4696 cf066bf8 2019-05-09 stsp
4697 c4296144 2019-05-09 stsp static const struct got_error *
4698 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4699 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4700 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4701 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4702 c4296144 2019-05-09 stsp {
4703 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4704 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4705 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4706 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4707 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4708 768aea60 2019-05-09 stsp struct stat sb;
4709 c4296144 2019-05-09 stsp
4710 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4711 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4712 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4713 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4714 5f8a88c6 2019-08-03 stsp return NULL;
4715 5f8a88c6 2019-08-03 stsp } else {
4716 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4717 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4718 c4296144 2019-05-09 stsp
4719 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4720 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4721 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4722 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4723 5f8a88c6 2019-08-03 stsp return NULL;
4724 5f8a88c6 2019-08-03 stsp }
4725 0b5cc0d6 2019-05-09 stsp
4726 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4727 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4728 036813ee 2019-05-09 stsp goto done;
4729 036813ee 2019-05-09 stsp }
4730 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4731 036813ee 2019-05-09 stsp parent_path = strdup("");
4732 69960a46 2019-05-09 stsp if (parent_path == NULL)
4733 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4734 69960a46 2019-05-09 stsp } else {
4735 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4736 69960a46 2019-05-09 stsp if (err)
4737 69960a46 2019-05-09 stsp return err;
4738 69960a46 2019-05-09 stsp }
4739 c4296144 2019-05-09 stsp
4740 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4741 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4742 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4743 c4296144 2019-05-09 stsp goto done;
4744 768aea60 2019-05-09 stsp }
4745 768aea60 2019-05-09 stsp
4746 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4747 768aea60 2019-05-09 stsp relpath) == -1) {
4748 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4749 768aea60 2019-05-09 stsp goto done;
4750 768aea60 2019-05-09 stsp }
4751 0aeb8099 2020-07-23 stsp
4752 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4753 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4754 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4755 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4756 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4757 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4758 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4759 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4760 0aeb8099 2020-07-23 stsp break;
4761 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4762 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4763 0aeb8099 2020-07-23 stsp break;
4764 0aeb8099 2020-07-23 stsp default:
4765 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4766 0aeb8099 2020-07-23 stsp goto done;
4767 0aeb8099 2020-07-23 stsp }
4768 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4769 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4770 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4771 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4772 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4773 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4774 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4775 12463d8b 2019-12-13 stsp ct->ondisk_path);
4776 12463d8b 2019-12-13 stsp goto done;
4777 12463d8b 2019-12-13 stsp }
4778 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4779 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4780 768aea60 2019-05-09 stsp goto done;
4781 768aea60 2019-05-09 stsp }
4782 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4783 c4296144 2019-05-09 stsp }
4784 c4296144 2019-05-09 stsp
4785 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4786 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4787 44d03001 2019-05-09 stsp relpath) == -1) {
4788 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4789 44d03001 2019-05-09 stsp goto done;
4790 44d03001 2019-05-09 stsp }
4791 44d03001 2019-05-09 stsp
4792 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4793 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4794 35213c7c 2020-07-23 stsp int is_bad_symlink;
4795 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4796 35213c7c 2020-07-23 stsp ssize_t target_len;
4797 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4798 35213c7c 2020-07-23 stsp sizeof(target_path));
4799 35213c7c 2020-07-23 stsp if (target_len == -1) {
4800 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4801 35213c7c 2020-07-23 stsp ct->ondisk_path);
4802 35213c7c 2020-07-23 stsp goto done;
4803 35213c7c 2020-07-23 stsp }
4804 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4805 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4806 35213c7c 2020-07-23 stsp if (err)
4807 35213c7c 2020-07-23 stsp goto done;
4808 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4809 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4810 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4811 35213c7c 2020-07-23 stsp goto done;
4812 35213c7c 2020-07-23 stsp }
4813 35213c7c 2020-07-23 stsp }
4814 35213c7c 2020-07-23 stsp
4815 35213c7c 2020-07-23 stsp
4816 cf066bf8 2019-05-09 stsp ct->status = status;
4817 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4818 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4819 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4820 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4821 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4822 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4823 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4824 b416585c 2019-05-13 stsp goto done;
4825 b416585c 2019-05-13 stsp }
4826 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4827 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4828 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4829 f0b75401 2019-08-03 stsp goto done;
4830 f0b75401 2019-08-03 stsp }
4831 f0b75401 2019-08-03 stsp }
4832 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4833 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4834 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4835 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4836 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4837 036813ee 2019-05-09 stsp goto done;
4838 036813ee 2019-05-09 stsp }
4839 ca2503ea 2019-05-09 stsp }
4840 24519714 2019-05-09 stsp ct->path = strdup(path);
4841 24519714 2019-05-09 stsp if (ct->path == NULL) {
4842 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4843 24519714 2019-05-09 stsp goto done;
4844 24519714 2019-05-09 stsp }
4845 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4846 c4296144 2019-05-09 stsp done:
4847 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4848 ed175427 2019-05-09 stsp free_commitable(ct);
4849 24519714 2019-05-09 stsp free(parent_path);
4850 036813ee 2019-05-09 stsp free(path);
4851 a129376b 2019-03-28 stsp return err;
4852 a129376b 2019-03-28 stsp }
4853 c4296144 2019-05-09 stsp
4854 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4855 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4856 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4857 036813ee 2019-05-09 stsp struct got_repository *);
4858 ed175427 2019-05-09 stsp
4859 ed175427 2019-05-09 stsp static const struct got_error *
4860 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4861 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4862 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4863 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4864 afa376bf 2019-05-09 stsp struct got_repository *repo)
4865 ed175427 2019-05-09 stsp {
4866 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4867 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4868 036813ee 2019-05-09 stsp char *subpath;
4869 ed175427 2019-05-09 stsp
4870 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4871 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4872 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4873 ed175427 2019-05-09 stsp
4874 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4875 ed175427 2019-05-09 stsp if (err)
4876 ed175427 2019-05-09 stsp return err;
4877 ed175427 2019-05-09 stsp
4878 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4879 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4880 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4881 036813ee 2019-05-09 stsp free(subpath);
4882 036813ee 2019-05-09 stsp return err;
4883 036813ee 2019-05-09 stsp }
4884 ed175427 2019-05-09 stsp
4885 036813ee 2019-05-09 stsp static const struct got_error *
4886 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4887 036813ee 2019-05-09 stsp {
4888 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4889 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4890 ed175427 2019-05-09 stsp
4891 036813ee 2019-05-09 stsp *match = 0;
4892 ed175427 2019-05-09 stsp
4893 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4894 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4895 0f63689d 2019-05-10 stsp return NULL;
4896 036813ee 2019-05-09 stsp }
4897 0b5cc0d6 2019-05-09 stsp
4898 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4899 0f63689d 2019-05-10 stsp if (err)
4900 0f63689d 2019-05-10 stsp return err;
4901 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4902 036813ee 2019-05-09 stsp free(ct_parent_path);
4903 036813ee 2019-05-09 stsp return err;
4904 036813ee 2019-05-09 stsp }
4905 0b5cc0d6 2019-05-09 stsp
4906 768aea60 2019-05-09 stsp static mode_t
4907 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4908 768aea60 2019-05-09 stsp {
4909 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4910 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4911 3d9a4ec4 2020-07-23 stsp
4912 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4913 768aea60 2019-05-09 stsp }
4914 768aea60 2019-05-09 stsp
4915 036813ee 2019-05-09 stsp static const struct got_error *
4916 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4917 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4918 036813ee 2019-05-09 stsp {
4919 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4920 ca2503ea 2019-05-09 stsp
4921 036813ee 2019-05-09 stsp *new_te = NULL;
4922 0b5cc0d6 2019-05-09 stsp
4923 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4924 036813ee 2019-05-09 stsp if (err)
4925 036813ee 2019-05-09 stsp goto done;
4926 0b5cc0d6 2019-05-09 stsp
4927 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4928 036813ee 2019-05-09 stsp
4929 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4930 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4931 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4932 5f8a88c6 2019-08-03 stsp else
4933 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4934 036813ee 2019-05-09 stsp done:
4935 036813ee 2019-05-09 stsp if (err && *new_te) {
4936 56e0773d 2019-11-28 stsp free(*new_te);
4937 036813ee 2019-05-09 stsp *new_te = NULL;
4938 036813ee 2019-05-09 stsp }
4939 036813ee 2019-05-09 stsp return err;
4940 036813ee 2019-05-09 stsp }
4941 036813ee 2019-05-09 stsp
4942 036813ee 2019-05-09 stsp static const struct got_error *
4943 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4944 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4945 036813ee 2019-05-09 stsp {
4946 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4947 102b254e 2020-10-19 stsp char *ct_name = NULL;
4948 036813ee 2019-05-09 stsp
4949 036813ee 2019-05-09 stsp *new_te = NULL;
4950 036813ee 2019-05-09 stsp
4951 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4952 036813ee 2019-05-09 stsp if (*new_te == NULL)
4953 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4954 036813ee 2019-05-09 stsp
4955 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4956 102b254e 2020-10-19 stsp if (err)
4957 036813ee 2019-05-09 stsp goto done;
4958 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4959 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4960 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4961 036813ee 2019-05-09 stsp goto done;
4962 036813ee 2019-05-09 stsp }
4963 036813ee 2019-05-09 stsp
4964 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4965 036813ee 2019-05-09 stsp
4966 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4967 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4968 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4969 5f8a88c6 2019-08-03 stsp else
4970 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4971 036813ee 2019-05-09 stsp done:
4972 102b254e 2020-10-19 stsp free(ct_name);
4973 036813ee 2019-05-09 stsp if (err && *new_te) {
4974 56e0773d 2019-11-28 stsp free(*new_te);
4975 036813ee 2019-05-09 stsp *new_te = NULL;
4976 036813ee 2019-05-09 stsp }
4977 036813ee 2019-05-09 stsp return err;
4978 036813ee 2019-05-09 stsp }
4979 036813ee 2019-05-09 stsp
4980 036813ee 2019-05-09 stsp static const struct got_error *
4981 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4982 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4983 036813ee 2019-05-09 stsp {
4984 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4985 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4986 036813ee 2019-05-09 stsp
4987 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4988 036813ee 2019-05-09 stsp if (err)
4989 036813ee 2019-05-09 stsp return err;
4990 036813ee 2019-05-09 stsp if (new_pe == NULL)
4991 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4992 036813ee 2019-05-09 stsp return NULL;
4993 afa376bf 2019-05-09 stsp }
4994 afa376bf 2019-05-09 stsp
4995 afa376bf 2019-05-09 stsp static const struct got_error *
4996 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4997 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4998 afa376bf 2019-05-09 stsp {
4999 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5000 5f8a88c6 2019-08-03 stsp unsigned char status;
5001 5f8a88c6 2019-08-03 stsp
5002 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5003 afa376bf 2019-05-09 stsp ct_path++;
5004 5f8a88c6 2019-08-03 stsp
5005 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5006 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5007 5f8a88c6 2019-08-03 stsp else
5008 5f8a88c6 2019-08-03 stsp status = ct->status;
5009 5f8a88c6 2019-08-03 stsp
5010 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5011 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5012 036813ee 2019-05-09 stsp }
5013 036813ee 2019-05-09 stsp
5014 036813ee 2019-05-09 stsp static const struct got_error *
5015 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5016 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5017 44d03001 2019-05-09 stsp {
5018 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5019 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5020 44d03001 2019-05-09 stsp char *te_path;
5021 44d03001 2019-05-09 stsp
5022 44d03001 2019-05-09 stsp *modified = 0;
5023 44d03001 2019-05-09 stsp
5024 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5025 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5026 44d03001 2019-05-09 stsp te->name) == -1)
5027 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5028 44d03001 2019-05-09 stsp
5029 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5030 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5031 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5032 44d03001 2019-05-09 stsp strlen(te_path));
5033 62d463ca 2020-10-20 naddy if (*modified)
5034 44d03001 2019-05-09 stsp break;
5035 44d03001 2019-05-09 stsp }
5036 44d03001 2019-05-09 stsp
5037 44d03001 2019-05-09 stsp free(te_path);
5038 44d03001 2019-05-09 stsp return err;
5039 44d03001 2019-05-09 stsp }
5040 44d03001 2019-05-09 stsp
5041 44d03001 2019-05-09 stsp static const struct got_error *
5042 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5043 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5044 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5045 036813ee 2019-05-09 stsp {
5046 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5047 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5048 036813ee 2019-05-09 stsp
5049 036813ee 2019-05-09 stsp *ctp = NULL;
5050 036813ee 2019-05-09 stsp
5051 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5052 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5053 036813ee 2019-05-09 stsp char *ct_name = NULL;
5054 036813ee 2019-05-09 stsp int path_matches;
5055 036813ee 2019-05-09 stsp
5056 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5057 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5058 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5059 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5060 5f8a88c6 2019-08-03 stsp continue;
5061 5f8a88c6 2019-08-03 stsp } else {
5062 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5063 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5064 5f8a88c6 2019-08-03 stsp continue;
5065 5f8a88c6 2019-08-03 stsp }
5066 036813ee 2019-05-09 stsp
5067 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5068 036813ee 2019-05-09 stsp continue;
5069 036813ee 2019-05-09 stsp
5070 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5071 62d463ca 2020-10-20 naddy if (err)
5072 036813ee 2019-05-09 stsp return err;
5073 036813ee 2019-05-09 stsp if (!path_matches)
5074 036813ee 2019-05-09 stsp continue;
5075 036813ee 2019-05-09 stsp
5076 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5077 d34b633e 2020-10-19 stsp if (err)
5078 d34b633e 2020-10-19 stsp return err;
5079 d34b633e 2020-10-19 stsp
5080 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5081 d34b633e 2020-10-19 stsp free(ct_name);
5082 036813ee 2019-05-09 stsp continue;
5083 d34b633e 2020-10-19 stsp }
5084 d34b633e 2020-10-19 stsp free(ct_name);
5085 036813ee 2019-05-09 stsp
5086 036813ee 2019-05-09 stsp *ctp = ct;
5087 036813ee 2019-05-09 stsp break;
5088 036813ee 2019-05-09 stsp }
5089 2b496619 2019-07-10 stsp
5090 2b496619 2019-07-10 stsp return err;
5091 2b496619 2019-07-10 stsp }
5092 2b496619 2019-07-10 stsp
5093 2b496619 2019-07-10 stsp static const struct got_error *
5094 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5095 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5096 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5097 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5098 2b496619 2019-07-10 stsp struct got_repository *repo)
5099 2b496619 2019-07-10 stsp {
5100 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5101 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5102 2b496619 2019-07-10 stsp char *subtree_path;
5103 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5104 ba580f68 2020-03-22 stsp int nentries;
5105 2b496619 2019-07-10 stsp
5106 2b496619 2019-07-10 stsp *new_tep = NULL;
5107 2b496619 2019-07-10 stsp
5108 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5109 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5110 2b496619 2019-07-10 stsp child_path) == -1)
5111 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5112 036813ee 2019-05-09 stsp
5113 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5114 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5115 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5116 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5117 56e0773d 2019-11-28 stsp
5118 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5119 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5120 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5121 2b496619 2019-07-10 stsp goto done;
5122 2b496619 2019-07-10 stsp }
5123 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5124 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5125 2b496619 2019-07-10 stsp if (err) {
5126 56e0773d 2019-11-28 stsp free(new_te);
5127 2b496619 2019-07-10 stsp goto done;
5128 2b496619 2019-07-10 stsp }
5129 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5130 2b496619 2019-07-10 stsp done:
5131 56e0773d 2019-11-28 stsp free(id);
5132 2b496619 2019-07-10 stsp free(subtree_path);
5133 2b496619 2019-07-10 stsp if (err == NULL)
5134 2b496619 2019-07-10 stsp *new_tep = new_te;
5135 036813ee 2019-05-09 stsp return err;
5136 036813ee 2019-05-09 stsp }
5137 036813ee 2019-05-09 stsp
5138 036813ee 2019-05-09 stsp static const struct got_error *
5139 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5140 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5141 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5142 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5143 036813ee 2019-05-09 stsp struct got_repository *repo)
5144 036813ee 2019-05-09 stsp {
5145 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5146 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5147 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5148 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5149 036813ee 2019-05-09 stsp
5150 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5151 ba580f68 2020-03-22 stsp *nentries = 0;
5152 036813ee 2019-05-09 stsp
5153 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5154 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5155 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5156 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5157 036813ee 2019-05-09 stsp
5158 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5159 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5160 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5161 036813ee 2019-05-09 stsp continue;
5162 036813ee 2019-05-09 stsp
5163 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5164 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5165 036813ee 2019-05-09 stsp continue;
5166 036813ee 2019-05-09 stsp
5167 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5168 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5169 036813ee 2019-05-09 stsp if (err)
5170 036813ee 2019-05-09 stsp goto done;
5171 036813ee 2019-05-09 stsp
5172 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5173 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5174 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5175 036813ee 2019-05-09 stsp if (err)
5176 036813ee 2019-05-09 stsp goto done;
5177 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5178 afa376bf 2019-05-09 stsp if (err)
5179 afa376bf 2019-05-09 stsp goto done;
5180 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5181 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5182 2b496619 2019-07-10 stsp if (err)
5183 2f51b5b3 2019-05-09 stsp goto done;
5184 ba580f68 2020-03-22 stsp (*nentries)++;
5185 2b496619 2019-07-10 stsp } else {
5186 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5187 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5188 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5189 2b496619 2019-07-10 stsp == NULL) {
5190 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5191 2b496619 2019-07-10 stsp child_path, path_base_tree,
5192 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5193 2b496619 2019-07-10 stsp repo);
5194 2b496619 2019-07-10 stsp if (err)
5195 2b496619 2019-07-10 stsp goto done;
5196 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5197 2b496619 2019-07-10 stsp if (err)
5198 2b496619 2019-07-10 stsp goto done;
5199 ba580f68 2020-03-22 stsp (*nentries)++;
5200 9ba0479c 2019-05-10 stsp }
5201 ed175427 2019-05-09 stsp }
5202 2f51b5b3 2019-05-09 stsp }
5203 2f51b5b3 2019-05-09 stsp
5204 2f51b5b3 2019-05-09 stsp if (base_tree) {
5205 56e0773d 2019-11-28 stsp int i, nbase_entries;
5206 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5207 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5208 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5209 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5210 63c5ca5d 2019-08-24 stsp
5211 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5212 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5213 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5214 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5215 63c5ca5d 2019-08-24 stsp if (err)
5216 63c5ca5d 2019-08-24 stsp goto done;
5217 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5218 63c5ca5d 2019-08-24 stsp if (err)
5219 63c5ca5d 2019-08-24 stsp goto done;
5220 ba580f68 2020-03-22 stsp (*nentries)++;
5221 63c5ca5d 2019-08-24 stsp continue;
5222 63c5ca5d 2019-08-24 stsp }
5223 2f51b5b3 2019-05-09 stsp
5224 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5225 44d03001 2019-05-09 stsp int modified;
5226 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5227 036813ee 2019-05-09 stsp if (err)
5228 036813ee 2019-05-09 stsp goto done;
5229 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5230 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5231 2f51b5b3 2019-05-09 stsp if (err)
5232 2f51b5b3 2019-05-09 stsp goto done;
5233 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5234 44d03001 2019-05-09 stsp if (modified) {
5235 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5236 ba580f68 2020-03-22 stsp int nsubentries;
5237 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5238 ba580f68 2020-03-22 stsp &nsubentries, te,
5239 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5240 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5241 44d03001 2019-05-09 stsp if (err)
5242 44d03001 2019-05-09 stsp goto done;
5243 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5244 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5245 ba580f68 2020-03-22 stsp free(new_id);
5246 ba580f68 2020-03-22 stsp continue;
5247 ba580f68 2020-03-22 stsp }
5248 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5249 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5250 56e0773d 2019-11-28 stsp free(new_id);
5251 44d03001 2019-05-09 stsp }
5252 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5253 036813ee 2019-05-09 stsp if (err)
5254 036813ee 2019-05-09 stsp goto done;
5255 ba580f68 2020-03-22 stsp (*nentries)++;
5256 2f51b5b3 2019-05-09 stsp continue;
5257 036813ee 2019-05-09 stsp }
5258 2f51b5b3 2019-05-09 stsp
5259 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5260 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5261 f66c734c 2019-09-22 stsp if (err)
5262 f66c734c 2019-09-22 stsp goto done;
5263 2f51b5b3 2019-05-09 stsp if (ct) {
5264 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5265 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5266 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5267 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5268 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5269 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5270 2f51b5b3 2019-05-09 stsp if (err)
5271 2f51b5b3 2019-05-09 stsp goto done;
5272 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5273 2f51b5b3 2019-05-09 stsp if (err)
5274 2f51b5b3 2019-05-09 stsp goto done;
5275 ba580f68 2020-03-22 stsp (*nentries)++;
5276 2f51b5b3 2019-05-09 stsp }
5277 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5278 b416585c 2019-05-13 stsp status_arg);
5279 afa376bf 2019-05-09 stsp if (err)
5280 afa376bf 2019-05-09 stsp goto done;
5281 2f51b5b3 2019-05-09 stsp } else {
5282 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5283 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5284 2f51b5b3 2019-05-09 stsp if (err)
5285 2f51b5b3 2019-05-09 stsp goto done;
5286 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5287 2f51b5b3 2019-05-09 stsp if (err)
5288 2f51b5b3 2019-05-09 stsp goto done;
5289 ba580f68 2020-03-22 stsp (*nentries)++;
5290 2f51b5b3 2019-05-09 stsp }
5291 ca2503ea 2019-05-09 stsp }
5292 ed175427 2019-05-09 stsp }
5293 0b5cc0d6 2019-05-09 stsp
5294 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5295 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5296 ed175427 2019-05-09 stsp done:
5297 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5298 2e1fa222 2020-07-23 stsp return err;
5299 2e1fa222 2020-07-23 stsp }
5300 2e1fa222 2020-07-23 stsp
5301 2e1fa222 2020-07-23 stsp static const struct got_error *
5302 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5303 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5304 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5305 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5306 ebf99748 2019-05-09 stsp {
5307 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5308 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5309 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5310 ebf99748 2019-05-09 stsp
5311 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5312 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5313 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5314 ebf99748 2019-05-09 stsp
5315 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5316 437adc9d 2020-12-10 yzhong
5317 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5318 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5319 437adc9d 2020-12-10 yzhong if (err)
5320 437adc9d 2020-12-10 yzhong goto done;
5321 437adc9d 2020-12-10 yzhong
5322 ebf99748 2019-05-09 stsp if (ie) {
5323 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5324 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5325 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5326 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5327 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5328 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5329 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5330 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5331 437adc9d 2020-12-10 yzhong
5332 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5333 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5334 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5335 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5336 72fd46fa 2019-09-06 stsp !have_staged_files);
5337 ebf99748 2019-05-09 stsp } else
5338 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5339 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5340 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5341 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5342 72fd46fa 2019-09-06 stsp !have_staged_files);
5343 ebf99748 2019-05-09 stsp } else {
5344 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5345 ebf99748 2019-05-09 stsp if (err)
5346 437adc9d 2020-12-10 yzhong goto done;
5347 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5348 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5349 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5350 3969253a 2020-03-07 stsp if (err) {
5351 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5352 437adc9d 2020-12-10 yzhong goto done;
5353 3969253a 2020-03-07 stsp }
5354 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5355 3969253a 2020-03-07 stsp if (err) {
5356 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5357 437adc9d 2020-12-10 yzhong goto done;
5358 3969253a 2020-03-07 stsp }
5359 ebf99748 2019-05-09 stsp }
5360 437adc9d 2020-12-10 yzhong free(relpath);
5361 437adc9d 2020-12-10 yzhong relpath = NULL;
5362 ebf99748 2019-05-09 stsp }
5363 437adc9d 2020-12-10 yzhong done:
5364 437adc9d 2020-12-10 yzhong free(relpath);
5365 d56d26ce 2019-05-10 stsp return err;
5366 d56d26ce 2019-05-10 stsp }
5367 735ef5ac 2019-08-03 stsp
5368 d56d26ce 2019-05-10 stsp
5369 d56d26ce 2019-05-10 stsp static const struct got_error *
5370 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5371 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5372 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5373 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5374 735ef5ac 2019-08-03 stsp int ood_errcode)
5375 d56d26ce 2019-05-10 stsp {
5376 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5377 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5378 1a36436d 2019-06-10 stsp
5379 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5380 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5381 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5382 1a36436d 2019-06-10 stsp return NULL;
5383 9bead371 2019-07-28 stsp /*
5384 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5385 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5386 9bead371 2019-07-28 stsp */
5387 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5388 735ef5ac 2019-08-03 stsp in_repo_path);
5389 9bead371 2019-07-28 stsp if (err) {
5390 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5391 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5392 1a36436d 2019-06-10 stsp goto done;
5393 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5394 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5395 1a36436d 2019-06-10 stsp } else {
5396 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5397 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5398 735ef5ac 2019-08-03 stsp in_repo_path);
5399 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5400 1a36436d 2019-06-10 stsp goto done;
5401 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5402 1a36436d 2019-06-10 stsp }
5403 1a36436d 2019-06-10 stsp done:
5404 1a36436d 2019-06-10 stsp free(id);
5405 1a36436d 2019-06-10 stsp return err;
5406 ebf99748 2019-05-09 stsp }
5407 ebf99748 2019-05-09 stsp
5408 c4296144 2019-05-09 stsp const struct got_error *
5409 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5410 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5411 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5412 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5413 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5414 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5415 afa376bf 2019-05-09 stsp struct got_repository *repo)
5416 c4296144 2019-05-09 stsp {
5417 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5418 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5419 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5420 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5421 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5422 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5423 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5424 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5425 ba580f68 2020-03-22 stsp int nentries;
5426 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5427 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5428 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5429 c4296144 2019-05-09 stsp
5430 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5431 c4296144 2019-05-09 stsp
5432 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5433 675c7539 2019-05-09 stsp
5434 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5435 588edf97 2019-05-10 stsp if (err)
5436 588edf97 2019-05-10 stsp goto done;
5437 de18fc63 2019-05-09 stsp
5438 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5439 588edf97 2019-05-10 stsp if (err)
5440 588edf97 2019-05-10 stsp goto done;
5441 588edf97 2019-05-10 stsp
5442 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5443 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5444 33ad4cbe 2019-05-12 jcs if (err)
5445 33ad4cbe 2019-05-12 jcs goto done;
5446 33ad4cbe 2019-05-12 jcs }
5447 c4296144 2019-05-09 stsp
5448 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5449 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5450 33ad4cbe 2019-05-12 jcs goto done;
5451 33ad4cbe 2019-05-12 jcs }
5452 33ad4cbe 2019-05-12 jcs
5453 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5454 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5455 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5456 cf066bf8 2019-05-09 stsp char *ondisk_path;
5457 cf066bf8 2019-05-09 stsp
5458 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5459 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5460 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5461 5f8a88c6 2019-08-03 stsp continue;
5462 5f8a88c6 2019-08-03 stsp
5463 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5464 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5465 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5466 cf066bf8 2019-05-09 stsp continue;
5467 cf066bf8 2019-05-09 stsp
5468 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5469 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5470 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5471 cf066bf8 2019-05-09 stsp goto done;
5472 cf066bf8 2019-05-09 stsp }
5473 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5474 2e1fa222 2020-07-23 stsp free(ondisk_path);
5475 2e1fa222 2020-07-23 stsp if (err)
5476 cf066bf8 2019-05-09 stsp goto done;
5477 cf066bf8 2019-05-09 stsp }
5478 036813ee 2019-05-09 stsp
5479 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5480 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5481 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5482 036813ee 2019-05-09 stsp if (err)
5483 036813ee 2019-05-09 stsp goto done;
5484 036813ee 2019-05-09 stsp
5485 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5486 de18fc63 2019-05-09 stsp if (err)
5487 de18fc63 2019-05-09 stsp goto done;
5488 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5489 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5490 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5491 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5492 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5493 33ad4cbe 2019-05-12 jcs free(logmsg);
5494 9d40349a 2019-05-09 stsp if (err)
5495 9d40349a 2019-05-09 stsp goto done;
5496 9d40349a 2019-05-09 stsp
5497 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5498 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5499 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5500 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5501 09f5bd90 2019-05-09 stsp goto done;
5502 09f5bd90 2019-05-09 stsp }
5503 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5504 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5505 09f5bd90 2019-05-09 stsp if (err)
5506 09f5bd90 2019-05-09 stsp goto done;
5507 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5508 ebf99748 2019-05-09 stsp if (err)
5509 ebf99748 2019-05-09 stsp goto done;
5510 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5511 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5512 09f5bd90 2019-05-09 stsp goto done;
5513 09f5bd90 2019-05-09 stsp }
5514 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5515 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5516 f2c16586 2019-05-09 stsp if (err)
5517 f2c16586 2019-05-09 stsp goto done;
5518 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5519 f2c16586 2019-05-09 stsp if (err)
5520 f2c16586 2019-05-09 stsp goto done;
5521 f2c16586 2019-05-09 stsp
5522 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5523 09f5bd90 2019-05-09 stsp if (err)
5524 09f5bd90 2019-05-09 stsp goto done;
5525 09f5bd90 2019-05-09 stsp
5526 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5527 ebf99748 2019-05-09 stsp if (err)
5528 ebf99748 2019-05-09 stsp goto done;
5529 c4296144 2019-05-09 stsp done:
5530 588edf97 2019-05-10 stsp if (head_tree)
5531 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5532 588edf97 2019-05-10 stsp if (head_commit)
5533 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5534 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5535 2f17228e 2019-05-12 stsp if (head_ref2) {
5536 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5537 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5538 2f17228e 2019-05-12 stsp err = unlockerr;
5539 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5540 39cd0ff6 2019-07-12 stsp }
5541 39cd0ff6 2019-07-12 stsp return err;
5542 39cd0ff6 2019-07-12 stsp }
5543 5c1e53bc 2019-07-28 stsp
5544 5c1e53bc 2019-07-28 stsp static const struct got_error *
5545 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5546 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5547 5c1e53bc 2019-07-28 stsp {
5548 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5549 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5550 39cd0ff6 2019-07-12 stsp
5551 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5552 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5553 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5554 5c1e53bc 2019-07-28 stsp
5555 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5556 5c1e53bc 2019-07-28 stsp ct_path++;
5557 5c1e53bc 2019-07-28 stsp
5558 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5559 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5560 5c1e53bc 2019-07-28 stsp break;
5561 5c1e53bc 2019-07-28 stsp }
5562 5c1e53bc 2019-07-28 stsp
5563 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5564 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5565 5c1e53bc 2019-07-28 stsp
5566 5c1e53bc 2019-07-28 stsp return NULL;
5567 5c1e53bc 2019-07-28 stsp }
5568 5c1e53bc 2019-07-28 stsp
5569 f0b75401 2019-08-03 stsp static const struct got_error *
5570 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5571 f0b75401 2019-08-03 stsp {
5572 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5573 f0b75401 2019-08-03 stsp
5574 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5575 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5576 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5577 f0b75401 2019-08-03 stsp }
5578 f0b75401 2019-08-03 stsp
5579 f0b75401 2019-08-03 stsp return NULL;
5580 f0b75401 2019-08-03 stsp }
5581 f0b75401 2019-08-03 stsp
5582 5f8a88c6 2019-08-03 stsp static const struct got_error *
5583 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5584 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5585 5f8a88c6 2019-08-03 stsp {
5586 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5587 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5588 5f8a88c6 2019-08-03 stsp
5589 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5590 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5591 5f8a88c6 2019-08-03 stsp continue;
5592 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5593 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5594 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5595 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5596 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5597 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5598 5f8a88c6 2019-08-03 stsp }
5599 5f8a88c6 2019-08-03 stsp
5600 5f8a88c6 2019-08-03 stsp return NULL;
5601 5f8a88c6 2019-08-03 stsp }
5602 5f8a88c6 2019-08-03 stsp
5603 39cd0ff6 2019-07-12 stsp const struct got_error *
5604 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5605 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5606 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5607 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5608 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5609 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5610 39cd0ff6 2019-07-12 stsp {
5611 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5612 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5613 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5614 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5615 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5616 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5617 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5618 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5619 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5620 39cd0ff6 2019-07-12 stsp
5621 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5622 39cd0ff6 2019-07-12 stsp
5623 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5624 39cd0ff6 2019-07-12 stsp
5625 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5626 39cd0ff6 2019-07-12 stsp if (err)
5627 39cd0ff6 2019-07-12 stsp goto done;
5628 39cd0ff6 2019-07-12 stsp
5629 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5630 39cd0ff6 2019-07-12 stsp if (err)
5631 39cd0ff6 2019-07-12 stsp goto done;
5632 39cd0ff6 2019-07-12 stsp
5633 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5634 39cd0ff6 2019-07-12 stsp if (err)
5635 39cd0ff6 2019-07-12 stsp goto done;
5636 39cd0ff6 2019-07-12 stsp
5637 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5638 39cd0ff6 2019-07-12 stsp if (err)
5639 39cd0ff6 2019-07-12 stsp goto done;
5640 39cd0ff6 2019-07-12 stsp
5641 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5642 5f8a88c6 2019-08-03 stsp &have_staged_files);
5643 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5644 5f8a88c6 2019-08-03 stsp goto done;
5645 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5646 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5647 5f8a88c6 2019-08-03 stsp if (err)
5648 5f8a88c6 2019-08-03 stsp goto done;
5649 5f8a88c6 2019-08-03 stsp }
5650 5f8a88c6 2019-08-03 stsp
5651 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5652 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5653 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5654 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5655 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5656 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5657 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5658 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5659 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5660 5c1e53bc 2019-07-28 stsp if (err)
5661 5c1e53bc 2019-07-28 stsp goto done;
5662 5c1e53bc 2019-07-28 stsp }
5663 39cd0ff6 2019-07-12 stsp
5664 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5665 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5666 39cd0ff6 2019-07-12 stsp goto done;
5667 5c1e53bc 2019-07-28 stsp }
5668 5c1e53bc 2019-07-28 stsp
5669 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5670 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5671 5c1e53bc 2019-07-28 stsp if (err)
5672 5c1e53bc 2019-07-28 stsp goto done;
5673 39cd0ff6 2019-07-12 stsp }
5674 f0b75401 2019-08-03 stsp
5675 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5676 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5677 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5678 f0b75401 2019-08-03 stsp
5679 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5680 f0b75401 2019-08-03 stsp ct_path++;
5681 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5682 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5683 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5684 b50cabdf 2019-07-12 stsp if (err)
5685 b50cabdf 2019-07-12 stsp goto done;
5686 f0b75401 2019-08-03 stsp
5687 b50cabdf 2019-07-12 stsp }
5688 b50cabdf 2019-07-12 stsp
5689 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5690 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5691 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5692 39cd0ff6 2019-07-12 stsp if (err)
5693 39cd0ff6 2019-07-12 stsp goto done;
5694 39cd0ff6 2019-07-12 stsp
5695 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5696 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5697 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5698 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5699 39cd0ff6 2019-07-12 stsp err = sync_err;
5700 39cd0ff6 2019-07-12 stsp done:
5701 39cd0ff6 2019-07-12 stsp if (fileindex)
5702 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5703 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5704 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5705 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5706 39cd0ff6 2019-07-12 stsp err = unlockerr;
5707 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5708 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5709 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5710 39cd0ff6 2019-07-12 stsp }
5711 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5712 c4296144 2019-05-09 stsp return err;
5713 8656d6c4 2019-05-20 stsp }
5714 8656d6c4 2019-05-20 stsp
5715 8656d6c4 2019-05-20 stsp const char *
5716 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5717 8656d6c4 2019-05-20 stsp {
5718 8656d6c4 2019-05-20 stsp return ct->path;
5719 8656d6c4 2019-05-20 stsp }
5720 8656d6c4 2019-05-20 stsp
5721 8656d6c4 2019-05-20 stsp unsigned int
5722 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5723 8656d6c4 2019-05-20 stsp {
5724 8656d6c4 2019-05-20 stsp return ct->status;
5725 818c7501 2019-07-11 stsp }
5726 818c7501 2019-07-11 stsp
5727 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5728 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5729 818c7501 2019-07-11 stsp struct got_repository *repo;
5730 818c7501 2019-07-11 stsp };
5731 818c7501 2019-07-11 stsp
5732 818c7501 2019-07-11 stsp static const struct got_error *
5733 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5734 818c7501 2019-07-11 stsp {
5735 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5736 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5737 818c7501 2019-07-11 stsp unsigned char status;
5738 818c7501 2019-07-11 stsp struct stat sb;
5739 818c7501 2019-07-11 stsp char *ondisk_path;
5740 818c7501 2019-07-11 stsp
5741 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5742 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5743 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5744 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5745 818c7501 2019-07-11 stsp
5746 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5747 818c7501 2019-07-11 stsp == -1)
5748 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5749 818c7501 2019-07-11 stsp
5750 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5751 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5752 818c7501 2019-07-11 stsp free(ondisk_path);
5753 818c7501 2019-07-11 stsp if (err)
5754 818c7501 2019-07-11 stsp return err;
5755 818c7501 2019-07-11 stsp
5756 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5757 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5758 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5759 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5760 818c7501 2019-07-11 stsp
5761 818c7501 2019-07-11 stsp return NULL;
5762 818c7501 2019-07-11 stsp }
5763 818c7501 2019-07-11 stsp
5764 818c7501 2019-07-11 stsp const struct got_error *
5765 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5766 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5767 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5768 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5769 818c7501 2019-07-11 stsp {
5770 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5771 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5772 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5773 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5774 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5775 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5776 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5777 818c7501 2019-07-11 stsp
5778 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5779 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5780 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5781 818c7501 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5783 818c7501 2019-07-11 stsp if (err)
5784 818c7501 2019-07-11 stsp return err;
5785 818c7501 2019-07-11 stsp
5786 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5787 818c7501 2019-07-11 stsp if (err)
5788 818c7501 2019-07-11 stsp goto done;
5789 818c7501 2019-07-11 stsp
5790 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5791 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5792 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5793 818c7501 2019-07-11 stsp &ok_arg);
5794 818c7501 2019-07-11 stsp if (err)
5795 818c7501 2019-07-11 stsp goto done;
5796 818c7501 2019-07-11 stsp
5797 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5798 818c7501 2019-07-11 stsp if (err)
5799 818c7501 2019-07-11 stsp goto done;
5800 818c7501 2019-07-11 stsp
5801 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5802 818c7501 2019-07-11 stsp if (err)
5803 818c7501 2019-07-11 stsp goto done;
5804 818c7501 2019-07-11 stsp
5805 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5806 818c7501 2019-07-11 stsp if (err)
5807 818c7501 2019-07-11 stsp goto done;
5808 818c7501 2019-07-11 stsp
5809 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5810 818c7501 2019-07-11 stsp 0);
5811 e51d7b55 2020-01-04 stsp if (err)
5812 e51d7b55 2020-01-04 stsp goto done;
5813 e51d7b55 2020-01-04 stsp
5814 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5815 818c7501 2019-07-11 stsp if (err)
5816 818c7501 2019-07-11 stsp goto done;
5817 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5818 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5819 e51d7b55 2020-01-04 stsp goto done;
5820 e51d7b55 2020-01-04 stsp }
5821 818c7501 2019-07-11 stsp
5822 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5823 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5824 818c7501 2019-07-11 stsp if (err)
5825 818c7501 2019-07-11 stsp goto done;
5826 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5827 818c7501 2019-07-11 stsp if (err)
5828 818c7501 2019-07-11 stsp goto done;
5829 818c7501 2019-07-11 stsp
5830 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5831 818c7501 2019-07-11 stsp
5832 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5833 818c7501 2019-07-11 stsp if (err)
5834 818c7501 2019-07-11 stsp goto done;
5835 818c7501 2019-07-11 stsp
5836 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5837 818c7501 2019-07-11 stsp if (err)
5838 818c7501 2019-07-11 stsp goto done;
5839 818c7501 2019-07-11 stsp
5840 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5841 818c7501 2019-07-11 stsp worktree->base_commit_id);
5842 818c7501 2019-07-11 stsp if (err)
5843 818c7501 2019-07-11 stsp goto done;
5844 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5845 818c7501 2019-07-11 stsp if (err)
5846 818c7501 2019-07-11 stsp goto done;
5847 818c7501 2019-07-11 stsp
5848 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5849 818c7501 2019-07-11 stsp if (err)
5850 818c7501 2019-07-11 stsp goto done;
5851 818c7501 2019-07-11 stsp done:
5852 818c7501 2019-07-11 stsp free(fileindex_path);
5853 818c7501 2019-07-11 stsp free(tmp_branch_name);
5854 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5855 818c7501 2019-07-11 stsp free(branch_ref_name);
5856 818c7501 2019-07-11 stsp if (branch_ref)
5857 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5858 818c7501 2019-07-11 stsp if (wt_branch)
5859 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5860 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5861 818c7501 2019-07-11 stsp if (err) {
5862 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5863 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5864 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5865 818c7501 2019-07-11 stsp }
5866 818c7501 2019-07-11 stsp if (*tmp_branch) {
5867 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5868 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5869 818c7501 2019-07-11 stsp }
5870 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5871 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5872 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5873 3e3a69f1 2019-07-25 stsp }
5874 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5875 818c7501 2019-07-11 stsp }
5876 818c7501 2019-07-11 stsp return err;
5877 818c7501 2019-07-11 stsp }
5878 818c7501 2019-07-11 stsp
5879 818c7501 2019-07-11 stsp const struct got_error *
5880 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5881 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5882 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5883 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5884 818c7501 2019-07-11 stsp {
5885 818c7501 2019-07-11 stsp const struct got_error *err;
5886 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5887 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5888 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5889 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5890 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5891 818c7501 2019-07-11 stsp
5892 818c7501 2019-07-11 stsp *commit_id = NULL;
5893 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5894 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5895 3e3a69f1 2019-07-25 stsp *branch = NULL;
5896 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5897 3e3a69f1 2019-07-25 stsp
5898 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5899 3e3a69f1 2019-07-25 stsp if (err)
5900 3e3a69f1 2019-07-25 stsp return err;
5901 818c7501 2019-07-11 stsp
5902 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5903 3e3a69f1 2019-07-25 stsp if (err)
5904 f032f1f7 2019-08-04 stsp goto done;
5905 f032f1f7 2019-08-04 stsp
5906 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5907 f032f1f7 2019-08-04 stsp &have_staged_files);
5908 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5909 f032f1f7 2019-08-04 stsp goto done;
5910 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5911 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5912 3e3a69f1 2019-07-25 stsp goto done;
5913 f032f1f7 2019-08-04 stsp }
5914 3e3a69f1 2019-07-25 stsp
5915 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5916 818c7501 2019-07-11 stsp if (err)
5917 f032f1f7 2019-08-04 stsp goto done;
5918 818c7501 2019-07-11 stsp
5919 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5920 818c7501 2019-07-11 stsp if (err)
5921 818c7501 2019-07-11 stsp goto done;
5922 818c7501 2019-07-11 stsp
5923 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5924 818c7501 2019-07-11 stsp if (err)
5925 818c7501 2019-07-11 stsp goto done;
5926 818c7501 2019-07-11 stsp
5927 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
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_open(&branch_ref, repo, branch_ref_name, 0);
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_ref_open(branch, repo,
5936 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5937 818c7501 2019-07-11 stsp if (err)
5938 818c7501 2019-07-11 stsp goto done;
5939 818c7501 2019-07-11 stsp
5940 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5941 818c7501 2019-07-11 stsp if (err)
5942 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp
5944 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5945 818c7501 2019-07-11 stsp if (err)
5946 818c7501 2019-07-11 stsp goto done;
5947 818c7501 2019-07-11 stsp
5948 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5949 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5950 818c7501 2019-07-11 stsp if (err)
5951 818c7501 2019-07-11 stsp goto done;
5952 818c7501 2019-07-11 stsp
5953 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5954 818c7501 2019-07-11 stsp if (err)
5955 818c7501 2019-07-11 stsp goto done;
5956 818c7501 2019-07-11 stsp done:
5957 818c7501 2019-07-11 stsp free(commit_ref_name);
5958 818c7501 2019-07-11 stsp free(branch_ref_name);
5959 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5960 818c7501 2019-07-11 stsp if (commit_ref)
5961 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5962 818c7501 2019-07-11 stsp if (branch_ref)
5963 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5964 818c7501 2019-07-11 stsp if (err) {
5965 818c7501 2019-07-11 stsp free(*commit_id);
5966 818c7501 2019-07-11 stsp *commit_id = NULL;
5967 818c7501 2019-07-11 stsp if (*tmp_branch) {
5968 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5969 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5970 818c7501 2019-07-11 stsp }
5971 818c7501 2019-07-11 stsp if (*new_base_branch) {
5972 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5973 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5974 818c7501 2019-07-11 stsp }
5975 818c7501 2019-07-11 stsp if (*branch) {
5976 818c7501 2019-07-11 stsp got_ref_close(*branch);
5977 818c7501 2019-07-11 stsp *branch = NULL;
5978 818c7501 2019-07-11 stsp }
5979 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5980 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5981 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5982 3e3a69f1 2019-07-25 stsp }
5983 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5984 818c7501 2019-07-11 stsp }
5985 818c7501 2019-07-11 stsp return err;
5986 c4296144 2019-05-09 stsp }
5987 818c7501 2019-07-11 stsp
5988 818c7501 2019-07-11 stsp const struct got_error *
5989 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5990 818c7501 2019-07-11 stsp {
5991 818c7501 2019-07-11 stsp const struct got_error *err;
5992 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5993 818c7501 2019-07-11 stsp
5994 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5995 818c7501 2019-07-11 stsp if (err)
5996 818c7501 2019-07-11 stsp return err;
5997 818c7501 2019-07-11 stsp
5998 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5999 818c7501 2019-07-11 stsp free(tmp_branch_name);
6000 818c7501 2019-07-11 stsp return NULL;
6001 818c7501 2019-07-11 stsp }
6002 818c7501 2019-07-11 stsp
6003 818c7501 2019-07-11 stsp static const struct got_error *
6004 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6005 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6006 818c7501 2019-07-11 stsp {
6007 0ebf8283 2019-07-24 stsp *logmsg = arg;
6008 818c7501 2019-07-11 stsp return NULL;
6009 818c7501 2019-07-11 stsp }
6010 818c7501 2019-07-11 stsp
6011 818c7501 2019-07-11 stsp static const struct got_error *
6012 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6013 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6014 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6015 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6016 818c7501 2019-07-11 stsp {
6017 818c7501 2019-07-11 stsp return NULL;
6018 01757395 2019-07-12 stsp }
6019 01757395 2019-07-12 stsp
6020 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6021 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6022 01757395 2019-07-12 stsp void *progress_arg;
6023 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6024 01757395 2019-07-12 stsp };
6025 01757395 2019-07-12 stsp
6026 01757395 2019-07-12 stsp static const struct got_error *
6027 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6028 01757395 2019-07-12 stsp {
6029 01757395 2019-07-12 stsp const struct got_error *err;
6030 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6031 01757395 2019-07-12 stsp char *p;
6032 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6033 01757395 2019-07-12 stsp
6034 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6035 01757395 2019-07-12 stsp if (err)
6036 01757395 2019-07-12 stsp return err;
6037 01757395 2019-07-12 stsp
6038 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6039 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6040 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6041 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6042 01757395 2019-07-12 stsp return NULL;
6043 01757395 2019-07-12 stsp
6044 01757395 2019-07-12 stsp p = strdup(path);
6045 01757395 2019-07-12 stsp if (p == NULL)
6046 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6047 01757395 2019-07-12 stsp
6048 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6049 01757395 2019-07-12 stsp if (err || new == NULL)
6050 01757395 2019-07-12 stsp free(p);
6051 01757395 2019-07-12 stsp return err;
6052 01757395 2019-07-12 stsp }
6053 01757395 2019-07-12 stsp
6054 01757395 2019-07-12 stsp void
6055 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6056 01757395 2019-07-12 stsp {
6057 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6058 01757395 2019-07-12 stsp
6059 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6060 01757395 2019-07-12 stsp free((char *)pe->path);
6061 01757395 2019-07-12 stsp
6062 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6063 818c7501 2019-07-11 stsp }
6064 818c7501 2019-07-11 stsp
6065 0ebf8283 2019-07-24 stsp static const struct got_error *
6066 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6067 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6068 818c7501 2019-07-11 stsp {
6069 818c7501 2019-07-11 stsp const struct got_error *err;
6070 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6073 818c7501 2019-07-11 stsp if (err) {
6074 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6075 818c7501 2019-07-11 stsp goto done;
6076 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6077 818c7501 2019-07-11 stsp if (err)
6078 818c7501 2019-07-11 stsp goto done;
6079 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6080 818c7501 2019-07-11 stsp if (err)
6081 818c7501 2019-07-11 stsp goto done;
6082 de05890f 2020-03-05 stsp } else if (is_rebase) {
6083 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6084 818c7501 2019-07-11 stsp int cmp;
6085 818c7501 2019-07-11 stsp
6086 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6087 818c7501 2019-07-11 stsp if (err)
6088 818c7501 2019-07-11 stsp goto done;
6089 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6090 818c7501 2019-07-11 stsp free(stored_id);
6091 818c7501 2019-07-11 stsp if (cmp != 0) {
6092 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6093 818c7501 2019-07-11 stsp goto done;
6094 818c7501 2019-07-11 stsp }
6095 818c7501 2019-07-11 stsp }
6096 0ebf8283 2019-07-24 stsp done:
6097 0ebf8283 2019-07-24 stsp if (commit_ref)
6098 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6099 0ebf8283 2019-07-24 stsp return err;
6100 0ebf8283 2019-07-24 stsp }
6101 0ebf8283 2019-07-24 stsp
6102 0ebf8283 2019-07-24 stsp static const struct got_error *
6103 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6104 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6105 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6106 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6107 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6108 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6109 0ebf8283 2019-07-24 stsp {
6110 0ebf8283 2019-07-24 stsp const struct got_error *err;
6111 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6112 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6113 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6114 818c7501 2019-07-11 stsp
6115 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6116 0ebf8283 2019-07-24 stsp
6117 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6118 0ebf8283 2019-07-24 stsp if (err)
6119 0ebf8283 2019-07-24 stsp return err;
6120 0ebf8283 2019-07-24 stsp
6121 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6122 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6123 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6124 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6125 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6126 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6127 818c7501 2019-07-11 stsp if (commit_ref)
6128 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6129 818c7501 2019-07-11 stsp return err;
6130 818c7501 2019-07-11 stsp }
6131 818c7501 2019-07-11 stsp
6132 818c7501 2019-07-11 stsp const struct got_error *
6133 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6134 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6135 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6136 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6137 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6138 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6139 818c7501 2019-07-11 stsp {
6140 0ebf8283 2019-07-24 stsp const struct got_error *err;
6141 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6142 0ebf8283 2019-07-24 stsp
6143 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6144 0ebf8283 2019-07-24 stsp if (err)
6145 0ebf8283 2019-07-24 stsp return err;
6146 0ebf8283 2019-07-24 stsp
6147 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6148 0ebf8283 2019-07-24 stsp if (err)
6149 0ebf8283 2019-07-24 stsp goto done;
6150 0ebf8283 2019-07-24 stsp
6151 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6152 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6153 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6154 0ebf8283 2019-07-24 stsp done:
6155 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6156 0ebf8283 2019-07-24 stsp return err;
6157 0ebf8283 2019-07-24 stsp }
6158 0ebf8283 2019-07-24 stsp
6159 0ebf8283 2019-07-24 stsp const struct got_error *
6160 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6161 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6162 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6163 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6164 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6165 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6166 0ebf8283 2019-07-24 stsp {
6167 0ebf8283 2019-07-24 stsp const struct got_error *err;
6168 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6169 0ebf8283 2019-07-24 stsp
6170 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6171 0ebf8283 2019-07-24 stsp if (err)
6172 0ebf8283 2019-07-24 stsp return err;
6173 0ebf8283 2019-07-24 stsp
6174 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6175 0ebf8283 2019-07-24 stsp if (err)
6176 0ebf8283 2019-07-24 stsp goto done;
6177 0ebf8283 2019-07-24 stsp
6178 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6179 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6180 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6181 0ebf8283 2019-07-24 stsp done:
6182 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6183 0ebf8283 2019-07-24 stsp return err;
6184 0ebf8283 2019-07-24 stsp }
6185 0ebf8283 2019-07-24 stsp
6186 0ebf8283 2019-07-24 stsp static const struct got_error *
6187 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6188 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6189 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6190 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6191 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6192 0ebf8283 2019-07-24 stsp {
6193 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6194 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6195 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6196 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6197 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6198 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6199 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6200 818c7501 2019-07-11 stsp
6201 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6202 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6203 a0e95631 2019-07-12 stsp
6204 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6205 818c7501 2019-07-11 stsp
6206 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6207 a0e95631 2019-07-12 stsp if (err)
6208 3e3a69f1 2019-07-25 stsp return err;
6209 a0e95631 2019-07-12 stsp
6210 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6211 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6212 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6213 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6214 01757395 2019-07-12 stsp /*
6215 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6216 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6217 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6218 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6219 01757395 2019-07-12 stsp */
6220 01757395 2019-07-12 stsp if (merged_paths) {
6221 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6222 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6223 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6224 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6225 f2a9dc41 2019-12-13 tracey 0);
6226 01757395 2019-07-12 stsp if (err)
6227 01757395 2019-07-12 stsp goto done;
6228 01757395 2019-07-12 stsp }
6229 01757395 2019-07-12 stsp } else {
6230 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6231 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6232 01757395 2019-07-12 stsp if (err)
6233 01757395 2019-07-12 stsp goto done;
6234 01757395 2019-07-12 stsp }
6235 a0e95631 2019-07-12 stsp
6236 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6237 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6238 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6239 a0e95631 2019-07-12 stsp if (err)
6240 a0e95631 2019-07-12 stsp goto done;
6241 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6242 a0e95631 2019-07-12 stsp goto done;
6243 ff0d2220 2019-07-11 stsp }
6244 818c7501 2019-07-11 stsp
6245 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6246 edd02c5e 2019-07-11 stsp if (err)
6247 edd02c5e 2019-07-11 stsp goto done;
6248 edd02c5e 2019-07-11 stsp
6249 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6250 818c7501 2019-07-11 stsp if (err)
6251 818c7501 2019-07-11 stsp goto done;
6252 818c7501 2019-07-11 stsp
6253 5943eee2 2019-08-13 stsp if (new_logmsg) {
6254 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6255 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6256 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6257 5943eee2 2019-08-13 stsp goto done;
6258 5943eee2 2019-08-13 stsp }
6259 5943eee2 2019-08-13 stsp } else {
6260 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6261 5943eee2 2019-08-13 stsp if (err)
6262 5943eee2 2019-08-13 stsp goto done;
6263 5943eee2 2019-08-13 stsp }
6264 0ebf8283 2019-07-24 stsp
6265 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6266 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6267 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6268 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6269 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6270 a0e95631 2019-07-12 stsp if (err)
6271 a0e95631 2019-07-12 stsp goto done;
6272 a0e95631 2019-07-12 stsp
6273 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6274 a0e95631 2019-07-12 stsp if (err)
6275 a0e95631 2019-07-12 stsp goto done;
6276 a0e95631 2019-07-12 stsp
6277 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6278 a0e95631 2019-07-12 stsp if (err)
6279 a0e95631 2019-07-12 stsp goto done;
6280 a0e95631 2019-07-12 stsp
6281 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6282 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6283 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6284 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6285 a0e95631 2019-07-12 stsp err = sync_err;
6286 818c7501 2019-07-11 stsp done:
6287 a0e95631 2019-07-12 stsp free(fileindex_path);
6288 a0e95631 2019-07-12 stsp free(head_commit_id);
6289 a0e95631 2019-07-12 stsp if (head_ref)
6290 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6291 818c7501 2019-07-11 stsp if (err) {
6292 818c7501 2019-07-11 stsp free(*new_commit_id);
6293 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6294 818c7501 2019-07-11 stsp }
6295 818c7501 2019-07-11 stsp return err;
6296 818c7501 2019-07-11 stsp }
6297 818c7501 2019-07-11 stsp
6298 818c7501 2019-07-11 stsp const struct got_error *
6299 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6300 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6301 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6302 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6303 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6304 0ebf8283 2019-07-24 stsp {
6305 0ebf8283 2019-07-24 stsp const struct got_error *err;
6306 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6307 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6308 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6309 0ebf8283 2019-07-24 stsp
6310 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6311 0ebf8283 2019-07-24 stsp if (err)
6312 0ebf8283 2019-07-24 stsp return err;
6313 0ebf8283 2019-07-24 stsp
6314 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6315 0ebf8283 2019-07-24 stsp if (err)
6316 0ebf8283 2019-07-24 stsp goto done;
6317 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6318 0ebf8283 2019-07-24 stsp if (err)
6319 0ebf8283 2019-07-24 stsp goto done;
6320 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6321 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6322 0ebf8283 2019-07-24 stsp goto done;
6323 0ebf8283 2019-07-24 stsp }
6324 0ebf8283 2019-07-24 stsp
6325 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6326 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6327 0ebf8283 2019-07-24 stsp done:
6328 0ebf8283 2019-07-24 stsp if (commit_ref)
6329 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6330 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6331 0ebf8283 2019-07-24 stsp free(commit_id);
6332 0ebf8283 2019-07-24 stsp return err;
6333 0ebf8283 2019-07-24 stsp }
6334 0ebf8283 2019-07-24 stsp
6335 0ebf8283 2019-07-24 stsp const struct got_error *
6336 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6337 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6338 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6339 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6340 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6341 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6342 0ebf8283 2019-07-24 stsp {
6343 0ebf8283 2019-07-24 stsp const struct got_error *err;
6344 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6345 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6346 0ebf8283 2019-07-24 stsp
6347 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6348 0ebf8283 2019-07-24 stsp if (err)
6349 0ebf8283 2019-07-24 stsp return err;
6350 0ebf8283 2019-07-24 stsp
6351 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6352 0ebf8283 2019-07-24 stsp if (err)
6353 0ebf8283 2019-07-24 stsp goto done;
6354 0ebf8283 2019-07-24 stsp
6355 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6356 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6357 0ebf8283 2019-07-24 stsp done:
6358 0ebf8283 2019-07-24 stsp if (commit_ref)
6359 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6360 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6361 0ebf8283 2019-07-24 stsp return err;
6362 0ebf8283 2019-07-24 stsp }
6363 0ebf8283 2019-07-24 stsp
6364 0ebf8283 2019-07-24 stsp const struct got_error *
6365 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6366 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6367 818c7501 2019-07-11 stsp {
6368 3e3a69f1 2019-07-25 stsp if (fileindex)
6369 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6370 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6371 69844fba 2019-07-11 stsp }
6372 69844fba 2019-07-11 stsp
6373 69844fba 2019-07-11 stsp static const struct got_error *
6374 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6375 69844fba 2019-07-11 stsp {
6376 69844fba 2019-07-11 stsp const struct got_error *err;
6377 69844fba 2019-07-11 stsp struct got_reference *ref;
6378 69844fba 2019-07-11 stsp
6379 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6380 69844fba 2019-07-11 stsp if (err) {
6381 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6382 69844fba 2019-07-11 stsp return NULL;
6383 69844fba 2019-07-11 stsp return err;
6384 69844fba 2019-07-11 stsp }
6385 69844fba 2019-07-11 stsp
6386 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6387 69844fba 2019-07-11 stsp got_ref_close(ref);
6388 69844fba 2019-07-11 stsp return err;
6389 818c7501 2019-07-11 stsp }
6390 818c7501 2019-07-11 stsp
6391 69844fba 2019-07-11 stsp static const struct got_error *
6392 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6393 69844fba 2019-07-11 stsp {
6394 69844fba 2019-07-11 stsp const struct got_error *err;
6395 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6396 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6397 69844fba 2019-07-11 stsp
6398 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6399 69844fba 2019-07-11 stsp if (err)
6400 69844fba 2019-07-11 stsp goto done;
6401 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6402 69844fba 2019-07-11 stsp if (err)
6403 69844fba 2019-07-11 stsp goto done;
6404 69844fba 2019-07-11 stsp
6405 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6406 69844fba 2019-07-11 stsp if (err)
6407 69844fba 2019-07-11 stsp goto done;
6408 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6409 69844fba 2019-07-11 stsp if (err)
6410 69844fba 2019-07-11 stsp goto done;
6411 69844fba 2019-07-11 stsp
6412 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6413 69844fba 2019-07-11 stsp if (err)
6414 69844fba 2019-07-11 stsp goto done;
6415 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6416 69844fba 2019-07-11 stsp if (err)
6417 69844fba 2019-07-11 stsp goto done;
6418 69844fba 2019-07-11 stsp
6419 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6420 69844fba 2019-07-11 stsp if (err)
6421 69844fba 2019-07-11 stsp goto done;
6422 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6423 69844fba 2019-07-11 stsp if (err)
6424 69844fba 2019-07-11 stsp goto done;
6425 69844fba 2019-07-11 stsp
6426 69844fba 2019-07-11 stsp done:
6427 69844fba 2019-07-11 stsp free(tmp_branch_name);
6428 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6429 69844fba 2019-07-11 stsp free(branch_ref_name);
6430 69844fba 2019-07-11 stsp free(commit_ref_name);
6431 69844fba 2019-07-11 stsp return err;
6432 69844fba 2019-07-11 stsp }
6433 69844fba 2019-07-11 stsp
6434 818c7501 2019-07-11 stsp const struct got_error *
6435 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6436 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6437 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6438 818c7501 2019-07-11 stsp struct got_repository *repo)
6439 818c7501 2019-07-11 stsp {
6440 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6441 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6442 818c7501 2019-07-11 stsp
6443 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6444 818c7501 2019-07-11 stsp if (err)
6445 818c7501 2019-07-11 stsp return err;
6446 818c7501 2019-07-11 stsp
6447 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6448 818c7501 2019-07-11 stsp if (err)
6449 818c7501 2019-07-11 stsp goto done;
6450 818c7501 2019-07-11 stsp
6451 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6452 818c7501 2019-07-11 stsp if (err)
6453 818c7501 2019-07-11 stsp goto done;
6454 818c7501 2019-07-11 stsp
6455 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6456 818c7501 2019-07-11 stsp if (err)
6457 818c7501 2019-07-11 stsp goto done;
6458 818c7501 2019-07-11 stsp
6459 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6460 818c7501 2019-07-11 stsp done:
6461 3e3a69f1 2019-07-25 stsp if (fileindex)
6462 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6463 818c7501 2019-07-11 stsp free(new_head_commit_id);
6464 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6465 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6466 818c7501 2019-07-11 stsp err = unlockerr;
6467 818c7501 2019-07-11 stsp return err;
6468 818c7501 2019-07-11 stsp }
6469 818c7501 2019-07-11 stsp
6470 818c7501 2019-07-11 stsp const struct got_error *
6471 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6472 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6473 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6474 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6475 818c7501 2019-07-11 stsp {
6476 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6477 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6478 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6479 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6480 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6481 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6482 818c7501 2019-07-11 stsp
6483 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6484 818c7501 2019-07-11 stsp if (err)
6485 818c7501 2019-07-11 stsp return err;
6486 818c7501 2019-07-11 stsp
6487 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6488 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6489 818c7501 2019-07-11 stsp if (err)
6490 818c7501 2019-07-11 stsp goto done;
6491 818c7501 2019-07-11 stsp
6492 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6493 818c7501 2019-07-11 stsp if (err)
6494 818c7501 2019-07-11 stsp goto done;
6495 818c7501 2019-07-11 stsp
6496 818c7501 2019-07-11 stsp /*
6497 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6498 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6499 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6500 818c7501 2019-07-11 stsp */
6501 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6502 818c7501 2019-07-11 stsp if (err)
6503 818c7501 2019-07-11 stsp goto done;
6504 818c7501 2019-07-11 stsp
6505 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6506 818c7501 2019-07-11 stsp if (err)
6507 818c7501 2019-07-11 stsp goto done;
6508 818c7501 2019-07-11 stsp
6509 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6510 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6511 ca355955 2019-07-12 stsp if (err)
6512 ca355955 2019-07-12 stsp goto done;
6513 ca355955 2019-07-12 stsp
6514 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6515 ca355955 2019-07-12 stsp if (err)
6516 ca355955 2019-07-12 stsp goto done;
6517 ca355955 2019-07-12 stsp
6518 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6519 818c7501 2019-07-11 stsp if (err)
6520 818c7501 2019-07-11 stsp goto done;
6521 818c7501 2019-07-11 stsp
6522 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6523 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6524 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6525 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6526 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6527 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6528 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6529 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6530 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6531 55bd499d 2019-07-12 stsp if (err)
6532 1f1abb7e 2019-08-08 stsp goto sync;
6533 55bd499d 2019-07-12 stsp
6534 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6535 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6536 ca355955 2019-07-12 stsp sync:
6537 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6538 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6539 ca355955 2019-07-12 stsp err = sync_err;
6540 818c7501 2019-07-11 stsp done:
6541 818c7501 2019-07-11 stsp got_ref_close(resolved);
6542 a3a2faf2 2019-07-12 stsp free(tree_id);
6543 818c7501 2019-07-11 stsp free(commit_id);
6544 0ebf8283 2019-07-24 stsp if (fileindex)
6545 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6546 0ebf8283 2019-07-24 stsp free(fileindex_path);
6547 0ebf8283 2019-07-24 stsp
6548 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6549 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6550 0ebf8283 2019-07-24 stsp err = unlockerr;
6551 0ebf8283 2019-07-24 stsp return err;
6552 0ebf8283 2019-07-24 stsp }
6553 0ebf8283 2019-07-24 stsp
6554 0ebf8283 2019-07-24 stsp const struct got_error *
6555 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6556 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6557 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6558 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6559 0ebf8283 2019-07-24 stsp {
6560 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6561 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6562 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6563 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6564 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6565 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6566 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6567 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6570 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6571 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6572 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6573 0ebf8283 2019-07-24 stsp
6574 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6575 0ebf8283 2019-07-24 stsp if (err)
6576 0ebf8283 2019-07-24 stsp return err;
6577 0ebf8283 2019-07-24 stsp
6578 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6579 0ebf8283 2019-07-24 stsp if (err)
6580 0ebf8283 2019-07-24 stsp goto done;
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6583 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6584 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6585 0ebf8283 2019-07-24 stsp &ok_arg);
6586 0ebf8283 2019-07-24 stsp if (err)
6587 0ebf8283 2019-07-24 stsp goto done;
6588 0ebf8283 2019-07-24 stsp
6589 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6590 0ebf8283 2019-07-24 stsp if (err)
6591 0ebf8283 2019-07-24 stsp goto done;
6592 0ebf8283 2019-07-24 stsp
6593 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6594 0ebf8283 2019-07-24 stsp if (err)
6595 0ebf8283 2019-07-24 stsp goto done;
6596 0ebf8283 2019-07-24 stsp
6597 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6598 0ebf8283 2019-07-24 stsp worktree);
6599 0ebf8283 2019-07-24 stsp if (err)
6600 0ebf8283 2019-07-24 stsp goto done;
6601 0ebf8283 2019-07-24 stsp
6602 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6603 0ebf8283 2019-07-24 stsp 0);
6604 0ebf8283 2019-07-24 stsp if (err)
6605 0ebf8283 2019-07-24 stsp goto done;
6606 0ebf8283 2019-07-24 stsp
6607 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6608 0ebf8283 2019-07-24 stsp if (err)
6609 0ebf8283 2019-07-24 stsp goto done;
6610 0ebf8283 2019-07-24 stsp
6611 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6612 0ebf8283 2019-07-24 stsp if (err)
6613 0ebf8283 2019-07-24 stsp goto done;
6614 0ebf8283 2019-07-24 stsp
6615 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6616 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6617 0ebf8283 2019-07-24 stsp if (err)
6618 0ebf8283 2019-07-24 stsp goto done;
6619 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6620 0ebf8283 2019-07-24 stsp if (err)
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6623 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6624 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6625 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp }
6627 0ebf8283 2019-07-24 stsp
6628 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6629 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6630 0ebf8283 2019-07-24 stsp if (err)
6631 0ebf8283 2019-07-24 stsp goto done;
6632 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6633 0ebf8283 2019-07-24 stsp if (err)
6634 0ebf8283 2019-07-24 stsp goto done;
6635 0ebf8283 2019-07-24 stsp
6636 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6637 0ebf8283 2019-07-24 stsp if (err)
6638 0ebf8283 2019-07-24 stsp goto done;
6639 0ebf8283 2019-07-24 stsp done:
6640 0ebf8283 2019-07-24 stsp free(fileindex_path);
6641 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6642 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6643 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6644 0ebf8283 2019-07-24 stsp if (wt_branch)
6645 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6646 0ebf8283 2019-07-24 stsp if (err) {
6647 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6648 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6649 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6650 0ebf8283 2019-07-24 stsp }
6651 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6652 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6653 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6654 0ebf8283 2019-07-24 stsp }
6655 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6656 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6657 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6658 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6659 3e3a69f1 2019-07-25 stsp }
6660 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6661 0ebf8283 2019-07-24 stsp }
6662 0ebf8283 2019-07-24 stsp return err;
6663 0ebf8283 2019-07-24 stsp }
6664 0ebf8283 2019-07-24 stsp
6665 0ebf8283 2019-07-24 stsp const struct got_error *
6666 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6667 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6668 0ebf8283 2019-07-24 stsp {
6669 3e3a69f1 2019-07-25 stsp if (fileindex)
6670 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6671 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6672 0ebf8283 2019-07-24 stsp }
6673 0ebf8283 2019-07-24 stsp
6674 0ebf8283 2019-07-24 stsp const struct got_error *
6675 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6676 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6677 0ebf8283 2019-07-24 stsp {
6678 0ebf8283 2019-07-24 stsp const struct got_error *err;
6679 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6680 0ebf8283 2019-07-24 stsp
6681 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6682 0ebf8283 2019-07-24 stsp if (err)
6683 0ebf8283 2019-07-24 stsp return err;
6684 0ebf8283 2019-07-24 stsp
6685 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6686 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6687 0ebf8283 2019-07-24 stsp return NULL;
6688 0ebf8283 2019-07-24 stsp }
6689 0ebf8283 2019-07-24 stsp
6690 0ebf8283 2019-07-24 stsp const struct got_error *
6691 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6692 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6693 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6694 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6695 0ebf8283 2019-07-24 stsp {
6696 0ebf8283 2019-07-24 stsp const struct got_error *err;
6697 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6698 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6699 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6700 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6701 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6702 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6705 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6706 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6707 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6708 0ebf8283 2019-07-24 stsp
6709 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6710 3e3a69f1 2019-07-25 stsp if (err)
6711 3e3a69f1 2019-07-25 stsp return err;
6712 3e3a69f1 2019-07-25 stsp
6713 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6714 3e3a69f1 2019-07-25 stsp if (err)
6715 f032f1f7 2019-08-04 stsp goto done;
6716 f032f1f7 2019-08-04 stsp
6717 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6718 f032f1f7 2019-08-04 stsp &have_staged_files);
6719 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6720 f032f1f7 2019-08-04 stsp goto done;
6721 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6722 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6723 3e3a69f1 2019-07-25 stsp goto done;
6724 f032f1f7 2019-08-04 stsp }
6725 3e3a69f1 2019-07-25 stsp
6726 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6727 0ebf8283 2019-07-24 stsp if (err)
6728 f032f1f7 2019-08-04 stsp goto done;
6729 0ebf8283 2019-07-24 stsp
6730 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6731 0ebf8283 2019-07-24 stsp if (err)
6732 0ebf8283 2019-07-24 stsp goto done;
6733 0ebf8283 2019-07-24 stsp
6734 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6735 0ebf8283 2019-07-24 stsp if (err)
6736 0ebf8283 2019-07-24 stsp goto done;
6737 0ebf8283 2019-07-24 stsp
6738 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6739 0ebf8283 2019-07-24 stsp worktree);
6740 0ebf8283 2019-07-24 stsp if (err)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp
6743 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6744 0ebf8283 2019-07-24 stsp if (err)
6745 0ebf8283 2019-07-24 stsp goto done;
6746 0ebf8283 2019-07-24 stsp
6747 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6748 0ebf8283 2019-07-24 stsp if (err)
6749 0ebf8283 2019-07-24 stsp goto done;
6750 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6751 0ebf8283 2019-07-24 stsp if (err)
6752 0ebf8283 2019-07-24 stsp goto done;
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6755 0ebf8283 2019-07-24 stsp if (err)
6756 0ebf8283 2019-07-24 stsp goto done;
6757 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6758 0ebf8283 2019-07-24 stsp if (err)
6759 0ebf8283 2019-07-24 stsp goto done;
6760 0ebf8283 2019-07-24 stsp
6761 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6762 0ebf8283 2019-07-24 stsp if (err)
6763 0ebf8283 2019-07-24 stsp goto done;
6764 0ebf8283 2019-07-24 stsp done:
6765 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6766 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6767 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6768 0ebf8283 2019-07-24 stsp if (commit_ref)
6769 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6770 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6771 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6772 0ebf8283 2019-07-24 stsp if (err) {
6773 0ebf8283 2019-07-24 stsp free(*commit_id);
6774 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6775 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6776 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6777 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6778 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6779 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6780 0ebf8283 2019-07-24 stsp }
6781 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6782 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6783 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6784 3e3a69f1 2019-07-25 stsp }
6785 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6786 0ebf8283 2019-07-24 stsp }
6787 0ebf8283 2019-07-24 stsp return err;
6788 0ebf8283 2019-07-24 stsp }
6789 0ebf8283 2019-07-24 stsp
6790 0ebf8283 2019-07-24 stsp static const struct got_error *
6791 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6792 0ebf8283 2019-07-24 stsp {
6793 0ebf8283 2019-07-24 stsp const struct got_error *err;
6794 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6795 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6796 0ebf8283 2019-07-24 stsp
6797 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6798 0ebf8283 2019-07-24 stsp if (err)
6799 0ebf8283 2019-07-24 stsp goto done;
6800 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6801 0ebf8283 2019-07-24 stsp if (err)
6802 0ebf8283 2019-07-24 stsp goto done;
6803 0ebf8283 2019-07-24 stsp
6804 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6805 0ebf8283 2019-07-24 stsp worktree);
6806 0ebf8283 2019-07-24 stsp if (err)
6807 0ebf8283 2019-07-24 stsp goto done;
6808 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6809 0ebf8283 2019-07-24 stsp if (err)
6810 0ebf8283 2019-07-24 stsp goto done;
6811 0ebf8283 2019-07-24 stsp
6812 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6813 0ebf8283 2019-07-24 stsp if (err)
6814 0ebf8283 2019-07-24 stsp goto done;
6815 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6816 0ebf8283 2019-07-24 stsp if (err)
6817 0ebf8283 2019-07-24 stsp goto done;
6818 0ebf8283 2019-07-24 stsp
6819 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6820 0ebf8283 2019-07-24 stsp if (err)
6821 0ebf8283 2019-07-24 stsp goto done;
6822 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6823 0ebf8283 2019-07-24 stsp if (err)
6824 0ebf8283 2019-07-24 stsp goto done;
6825 0ebf8283 2019-07-24 stsp done:
6826 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6827 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6828 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6829 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6830 0ebf8283 2019-07-24 stsp return err;
6831 0ebf8283 2019-07-24 stsp }
6832 0ebf8283 2019-07-24 stsp
6833 0ebf8283 2019-07-24 stsp const struct got_error *
6834 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6835 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6836 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6837 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6838 0ebf8283 2019-07-24 stsp {
6839 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6840 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6841 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6842 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6843 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6844 0ebf8283 2019-07-24 stsp
6845 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6846 0ebf8283 2019-07-24 stsp if (err)
6847 0ebf8283 2019-07-24 stsp return err;
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6850 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6851 0ebf8283 2019-07-24 stsp if (err)
6852 0ebf8283 2019-07-24 stsp goto done;
6853 0ebf8283 2019-07-24 stsp
6854 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6855 0ebf8283 2019-07-24 stsp if (err)
6856 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp
6858 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6859 0ebf8283 2019-07-24 stsp if (err)
6860 0ebf8283 2019-07-24 stsp goto done;
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6863 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6864 0ebf8283 2019-07-24 stsp if (err)
6865 0ebf8283 2019-07-24 stsp goto done;
6866 0ebf8283 2019-07-24 stsp
6867 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6868 0ebf8283 2019-07-24 stsp if (err)
6869 0ebf8283 2019-07-24 stsp goto done;
6870 0ebf8283 2019-07-24 stsp
6871 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6872 0ebf8283 2019-07-24 stsp if (err)
6873 0ebf8283 2019-07-24 stsp goto done;
6874 0ebf8283 2019-07-24 stsp
6875 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6876 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6877 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6878 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6879 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6880 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6881 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6882 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6883 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6884 0ebf8283 2019-07-24 stsp if (err)
6885 1f1abb7e 2019-08-08 stsp goto sync;
6886 0ebf8283 2019-07-24 stsp
6887 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6888 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6889 0ebf8283 2019-07-24 stsp sync:
6890 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6891 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6892 0ebf8283 2019-07-24 stsp err = sync_err;
6893 0ebf8283 2019-07-24 stsp done:
6894 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6895 0ebf8283 2019-07-24 stsp free(tree_id);
6896 818c7501 2019-07-11 stsp free(fileindex_path);
6897 818c7501 2019-07-11 stsp
6898 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6899 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6900 818c7501 2019-07-11 stsp err = unlockerr;
6901 818c7501 2019-07-11 stsp return err;
6902 818c7501 2019-07-11 stsp }
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp const struct got_error *
6905 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6906 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6907 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6908 0ebf8283 2019-07-24 stsp {
6909 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6910 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6911 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6912 0ebf8283 2019-07-24 stsp
6913 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6914 0ebf8283 2019-07-24 stsp if (err)
6915 0ebf8283 2019-07-24 stsp return err;
6916 0ebf8283 2019-07-24 stsp
6917 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6918 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6919 0ebf8283 2019-07-24 stsp if (err)
6920 0ebf8283 2019-07-24 stsp goto done;
6921 0ebf8283 2019-07-24 stsp
6922 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6927 0ebf8283 2019-07-24 stsp if (err)
6928 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp
6930 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6931 0ebf8283 2019-07-24 stsp if (err)
6932 0ebf8283 2019-07-24 stsp goto done;
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6935 0ebf8283 2019-07-24 stsp done:
6936 3e3a69f1 2019-07-25 stsp if (fileindex)
6937 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6938 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6939 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6940 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6941 0ebf8283 2019-07-24 stsp err = unlockerr;
6942 0ebf8283 2019-07-24 stsp return err;
6943 0ebf8283 2019-07-24 stsp }
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp const struct got_error *
6946 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6947 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6948 0ebf8283 2019-07-24 stsp {
6949 0ebf8283 2019-07-24 stsp const struct got_error *err;
6950 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6951 0ebf8283 2019-07-24 stsp
6952 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp return err;
6955 0ebf8283 2019-07-24 stsp
6956 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6957 0ebf8283 2019-07-24 stsp if (err)
6958 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp
6960 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6961 0ebf8283 2019-07-24 stsp done:
6962 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6963 2822a352 2019-10-15 stsp return err;
6964 2822a352 2019-10-15 stsp }
6965 2822a352 2019-10-15 stsp
6966 2822a352 2019-10-15 stsp const struct got_error *
6967 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6968 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6969 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6970 2822a352 2019-10-15 stsp struct got_repository *repo)
6971 2822a352 2019-10-15 stsp {
6972 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6973 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6974 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6975 2822a352 2019-10-15 stsp
6976 2822a352 2019-10-15 stsp *fileindex = NULL;
6977 2822a352 2019-10-15 stsp *branch_ref = NULL;
6978 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6979 2822a352 2019-10-15 stsp
6980 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6981 2822a352 2019-10-15 stsp if (err)
6982 2822a352 2019-10-15 stsp return err;
6983 2822a352 2019-10-15 stsp
6984 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6985 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6986 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6987 2822a352 2019-10-15 stsp "update -b or different branch name required");
6988 2822a352 2019-10-15 stsp goto done;
6989 2822a352 2019-10-15 stsp }
6990 2822a352 2019-10-15 stsp
6991 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6992 2822a352 2019-10-15 stsp if (err)
6993 2822a352 2019-10-15 stsp goto done;
6994 2822a352 2019-10-15 stsp
6995 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6996 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6997 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6998 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6999 2822a352 2019-10-15 stsp &ok_arg);
7000 2822a352 2019-10-15 stsp if (err)
7001 2822a352 2019-10-15 stsp goto done;
7002 2822a352 2019-10-15 stsp
7003 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7004 2822a352 2019-10-15 stsp if (err)
7005 2822a352 2019-10-15 stsp goto done;
7006 2822a352 2019-10-15 stsp
7007 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7008 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7009 2822a352 2019-10-15 stsp done:
7010 2822a352 2019-10-15 stsp if (err) {
7011 2822a352 2019-10-15 stsp if (*branch_ref) {
7012 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7013 2822a352 2019-10-15 stsp *branch_ref = NULL;
7014 2822a352 2019-10-15 stsp }
7015 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7016 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7017 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7018 2822a352 2019-10-15 stsp }
7019 2822a352 2019-10-15 stsp if (*fileindex) {
7020 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7021 2822a352 2019-10-15 stsp *fileindex = NULL;
7022 2822a352 2019-10-15 stsp }
7023 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7024 2822a352 2019-10-15 stsp }
7025 0cb83759 2019-08-03 stsp return err;
7026 0cb83759 2019-08-03 stsp }
7027 0cb83759 2019-08-03 stsp
7028 2822a352 2019-10-15 stsp const struct got_error *
7029 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7030 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7031 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7032 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7033 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7034 2822a352 2019-10-15 stsp {
7035 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7036 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7037 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7038 2822a352 2019-10-15 stsp
7039 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7040 2822a352 2019-10-15 stsp if (err)
7041 2822a352 2019-10-15 stsp goto done;
7042 2822a352 2019-10-15 stsp
7043 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7044 2822a352 2019-10-15 stsp if (err)
7045 2822a352 2019-10-15 stsp goto done;
7046 2822a352 2019-10-15 stsp
7047 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7048 2822a352 2019-10-15 stsp worktree->path_prefix);
7049 2822a352 2019-10-15 stsp if (err)
7050 2822a352 2019-10-15 stsp goto done;
7051 2822a352 2019-10-15 stsp
7052 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7053 2822a352 2019-10-15 stsp if (err)
7054 2822a352 2019-10-15 stsp goto done;
7055 2822a352 2019-10-15 stsp
7056 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7057 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7058 2822a352 2019-10-15 stsp if (err)
7059 2822a352 2019-10-15 stsp goto sync;
7060 2822a352 2019-10-15 stsp
7061 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7062 2822a352 2019-10-15 stsp if (err)
7063 2822a352 2019-10-15 stsp goto sync;
7064 2822a352 2019-10-15 stsp
7065 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7066 2822a352 2019-10-15 stsp sync:
7067 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7068 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7069 2822a352 2019-10-15 stsp err = sync_err;
7070 2822a352 2019-10-15 stsp
7071 2822a352 2019-10-15 stsp done:
7072 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7073 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7074 2822a352 2019-10-15 stsp err = unlockerr;
7075 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7076 2822a352 2019-10-15 stsp
7077 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7078 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7079 2822a352 2019-10-15 stsp err = unlockerr;
7080 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7081 2822a352 2019-10-15 stsp
7082 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7083 2822a352 2019-10-15 stsp free(fileindex_path);
7084 2822a352 2019-10-15 stsp free(tree_id);
7085 2822a352 2019-10-15 stsp
7086 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7087 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7088 2822a352 2019-10-15 stsp err = unlockerr;
7089 2822a352 2019-10-15 stsp return err;
7090 2822a352 2019-10-15 stsp }
7091 2822a352 2019-10-15 stsp
7092 2822a352 2019-10-15 stsp const struct got_error *
7093 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7094 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7095 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7096 2822a352 2019-10-15 stsp {
7097 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7098 8b692cd0 2019-10-21 stsp
7099 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7100 8b692cd0 2019-10-21 stsp
7101 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7102 8b692cd0 2019-10-21 stsp
7103 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7104 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7105 8b692cd0 2019-10-21 stsp err = unlockerr;
7106 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7107 8b692cd0 2019-10-21 stsp
7108 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7109 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7110 8b692cd0 2019-10-21 stsp err = unlockerr;
7111 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7112 8b692cd0 2019-10-21 stsp
7113 8b692cd0 2019-10-21 stsp return err;
7114 2822a352 2019-10-15 stsp }
7115 2822a352 2019-10-15 stsp
7116 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7117 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7118 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7119 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7120 2db2652d 2019-08-07 stsp struct got_repository *repo;
7121 2db2652d 2019-08-07 stsp int have_changes;
7122 2db2652d 2019-08-07 stsp };
7123 2db2652d 2019-08-07 stsp
7124 2db2652d 2019-08-07 stsp const struct got_error *
7125 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7126 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7127 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7128 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7129 735ef5ac 2019-08-03 stsp {
7130 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7131 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7132 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7133 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7134 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7135 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7136 8b13ce36 2019-08-08 stsp
7137 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7138 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7139 8b13ce36 2019-08-08 stsp return NULL;
7140 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7141 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7142 735ef5ac 2019-08-03 stsp
7143 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7144 735ef5ac 2019-08-03 stsp if (ie == NULL)
7145 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7146 735ef5ac 2019-08-03 stsp
7147 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7148 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7149 735ef5ac 2019-08-03 stsp relpath) == -1)
7150 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7151 735ef5ac 2019-08-03 stsp
7152 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7153 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7154 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7155 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7156 735ef5ac 2019-08-03 stsp }
7157 735ef5ac 2019-08-03 stsp
7158 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7159 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7160 3aa5969e 2019-08-06 stsp goto done;
7161 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7162 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7163 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7164 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7165 735ef5ac 2019-08-03 stsp goto done;
7166 3aa5969e 2019-08-06 stsp }
7167 735ef5ac 2019-08-03 stsp
7168 2db2652d 2019-08-07 stsp a->have_changes = 1;
7169 2db2652d 2019-08-07 stsp
7170 735ef5ac 2019-08-03 stsp p = in_repo_path;
7171 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7172 735ef5ac 2019-08-03 stsp p++;
7173 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7174 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7175 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7176 735ef5ac 2019-08-03 stsp done:
7177 735ef5ac 2019-08-03 stsp free(in_repo_path);
7178 dc424a06 2019-08-07 stsp return err;
7179 dc424a06 2019-08-07 stsp }
7180 dc424a06 2019-08-07 stsp
7181 2db2652d 2019-08-07 stsp struct stage_path_arg {
7182 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7183 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7184 2db2652d 2019-08-07 stsp struct got_repository *repo;
7185 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7186 2db2652d 2019-08-07 stsp void *status_arg;
7187 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7188 2db2652d 2019-08-07 stsp void *patch_arg;
7189 7b5dc508 2019-10-28 stsp int staged_something;
7190 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7191 2db2652d 2019-08-07 stsp };
7192 2db2652d 2019-08-07 stsp
7193 2db2652d 2019-08-07 stsp static const struct got_error *
7194 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7195 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7196 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7197 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7198 0cb83759 2019-08-03 stsp {
7199 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7200 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7201 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7202 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7203 0cb83759 2019-08-03 stsp uint32_t stage;
7204 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7205 0aeb8099 2020-07-23 stsp struct stat sb;
7206 8b13ce36 2019-08-08 stsp
7207 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7208 8b13ce36 2019-08-08 stsp return NULL;
7209 0cb83759 2019-08-03 stsp
7210 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7211 d3e7c587 2019-08-03 stsp if (ie == NULL)
7212 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7213 0cb83759 2019-08-03 stsp
7214 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7215 2db2652d 2019-08-07 stsp relpath)== -1)
7216 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7217 0cb83759 2019-08-03 stsp
7218 0cb83759 2019-08-03 stsp switch (status) {
7219 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7220 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7221 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7222 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7223 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7224 0aeb8099 2020-07-23 stsp break;
7225 0aeb8099 2020-07-23 stsp }
7226 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7227 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7228 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7229 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7230 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7231 dc424a06 2019-08-07 stsp if (err)
7232 dc424a06 2019-08-07 stsp break;
7233 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7234 dc424a06 2019-08-07 stsp break;
7235 dc424a06 2019-08-07 stsp } else {
7236 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7237 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7238 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7239 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7240 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7241 dc424a06 2019-08-07 stsp break;
7242 dc424a06 2019-08-07 stsp }
7243 dc424a06 2019-08-07 stsp }
7244 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7245 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7246 0cb83759 2019-08-03 stsp if (err)
7247 dc424a06 2019-08-07 stsp break;
7248 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7249 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7250 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7251 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7252 0cb83759 2019-08-03 stsp else
7253 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7254 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7255 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7256 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7257 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7258 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7259 35213c7c 2020-07-23 stsp ssize_t target_len;
7260 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7261 35213c7c 2020-07-23 stsp sizeof(target_path));
7262 35213c7c 2020-07-23 stsp if (target_len == -1) {
7263 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7264 35213c7c 2020-07-23 stsp ondisk_path);
7265 35213c7c 2020-07-23 stsp break;
7266 35213c7c 2020-07-23 stsp }
7267 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7268 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7269 35213c7c 2020-07-23 stsp a->worktree->root_path);
7270 35213c7c 2020-07-23 stsp if (err)
7271 35213c7c 2020-07-23 stsp break;
7272 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7273 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7274 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7275 35213c7c 2020-07-23 stsp break;
7276 35213c7c 2020-07-23 stsp }
7277 35213c7c 2020-07-23 stsp }
7278 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7279 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7280 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7281 35213c7c 2020-07-23 stsp else
7282 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7283 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7284 0aeb8099 2020-07-23 stsp } else {
7285 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7286 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7287 0aeb8099 2020-07-23 stsp }
7288 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7289 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7290 dc424a06 2019-08-07 stsp break;
7291 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7292 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7293 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7294 0cb83759 2019-08-03 stsp break;
7295 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7296 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7297 d3e7c587 2019-08-03 stsp break;
7298 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7299 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7300 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7301 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7302 dc424a06 2019-08-07 stsp if (err)
7303 dc424a06 2019-08-07 stsp break;
7304 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7305 88f33a19 2019-08-08 stsp break;
7306 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7307 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7308 dc424a06 2019-08-07 stsp break;
7309 88f33a19 2019-08-08 stsp }
7310 dc424a06 2019-08-07 stsp }
7311 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7312 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7313 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7314 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7315 dc424a06 2019-08-07 stsp break;
7316 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7317 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7318 12463d8b 2019-12-13 stsp de_name);
7319 0cb83759 2019-08-03 stsp break;
7320 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7321 d3e7c587 2019-08-03 stsp break;
7322 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7323 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7324 ebf48fd5 2019-08-03 stsp break;
7325 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7326 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7327 2a06fe5f 2019-08-24 stsp break;
7328 0cb83759 2019-08-03 stsp default:
7329 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7330 537ac44b 2019-08-03 stsp break;
7331 0cb83759 2019-08-03 stsp }
7332 dc424a06 2019-08-07 stsp
7333 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7334 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7335 dc424a06 2019-08-07 stsp free(path_content);
7336 2db2652d 2019-08-07 stsp free(ondisk_path);
7337 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7338 0ebf8283 2019-07-24 stsp return err;
7339 0ebf8283 2019-07-24 stsp }
7340 0cb83759 2019-08-03 stsp
7341 0cb83759 2019-08-03 stsp const struct got_error *
7342 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7343 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7344 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7345 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7346 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7347 0cb83759 2019-08-03 stsp {
7348 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7349 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7350 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7351 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7352 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7353 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7354 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7355 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7356 0cb83759 2019-08-03 stsp
7357 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7358 0cb83759 2019-08-03 stsp if (err)
7359 0cb83759 2019-08-03 stsp return err;
7360 0cb83759 2019-08-03 stsp
7361 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7362 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7363 735ef5ac 2019-08-03 stsp if (err)
7364 735ef5ac 2019-08-03 stsp goto done;
7365 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7366 735ef5ac 2019-08-03 stsp if (err)
7367 735ef5ac 2019-08-03 stsp goto done;
7368 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7369 0cb83759 2019-08-03 stsp if (err)
7370 0cb83759 2019-08-03 stsp goto done;
7371 0cb83759 2019-08-03 stsp
7372 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7373 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7374 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7375 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7376 2db2652d 2019-08-07 stsp oka.repo = repo;
7377 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7378 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7379 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7380 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7381 735ef5ac 2019-08-03 stsp if (err)
7382 735ef5ac 2019-08-03 stsp goto done;
7383 735ef5ac 2019-08-03 stsp }
7384 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7385 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7386 2db2652d 2019-08-07 stsp goto done;
7387 2db2652d 2019-08-07 stsp }
7388 735ef5ac 2019-08-03 stsp
7389 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7390 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7391 2db2652d 2019-08-07 stsp spa.repo = repo;
7392 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7393 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7394 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7395 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7396 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7397 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7398 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7399 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7400 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7401 42005733 2019-08-03 stsp if (err)
7402 2db2652d 2019-08-07 stsp goto done;
7403 7b5dc508 2019-10-28 stsp }
7404 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7405 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7406 7b5dc508 2019-10-28 stsp goto done;
7407 0cb83759 2019-08-03 stsp }
7408 0cb83759 2019-08-03 stsp
7409 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7410 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7411 0cb83759 2019-08-03 stsp err = sync_err;
7412 0cb83759 2019-08-03 stsp done:
7413 735ef5ac 2019-08-03 stsp if (head_ref)
7414 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7415 735ef5ac 2019-08-03 stsp free(head_commit_id);
7416 0cb83759 2019-08-03 stsp free(fileindex_path);
7417 0cb83759 2019-08-03 stsp if (fileindex)
7418 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7419 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7420 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7421 0cb83759 2019-08-03 stsp err = unlockerr;
7422 0cb83759 2019-08-03 stsp return err;
7423 0cb83759 2019-08-03 stsp }
7424 ad493afc 2019-08-03 stsp
7425 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7426 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7427 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7428 ad493afc 2019-08-03 stsp struct got_repository *repo;
7429 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7430 ad493afc 2019-08-03 stsp void *progress_arg;
7431 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7432 2e1f37b0 2019-08-08 stsp void *patch_arg;
7433 ad493afc 2019-08-03 stsp };
7434 2e1f37b0 2019-08-08 stsp
7435 2e1f37b0 2019-08-08 stsp static const struct got_error *
7436 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7437 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7438 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7439 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7440 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7441 2e1f37b0 2019-08-08 stsp {
7442 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7443 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7444 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7445 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7446 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7447 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7448 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7449 2e1f37b0 2019-08-08 stsp
7450 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7451 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7452 2e1f37b0 2019-08-08 stsp
7453 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7454 2e1f37b0 2019-08-08 stsp if (err)
7455 2e1f37b0 2019-08-08 stsp return err;
7456 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7457 2e1f37b0 2019-08-08 stsp if (err)
7458 2e1f37b0 2019-08-08 stsp goto done;
7459 2e1f37b0 2019-08-08 stsp
7460 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7461 2e1f37b0 2019-08-08 stsp if (err)
7462 2e1f37b0 2019-08-08 stsp goto done;
7463 2e1f37b0 2019-08-08 stsp
7464 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7465 2e1f37b0 2019-08-08 stsp if (err)
7466 2e1f37b0 2019-08-08 stsp goto done;
7467 2e1f37b0 2019-08-08 stsp
7468 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7469 2e1f37b0 2019-08-08 stsp if (err)
7470 2e1f37b0 2019-08-08 stsp goto done;
7471 2e1f37b0 2019-08-08 stsp
7472 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7473 2e1f37b0 2019-08-08 stsp if (err)
7474 2e1f37b0 2019-08-08 stsp goto done;
7475 ad493afc 2019-08-03 stsp
7476 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7477 2e1f37b0 2019-08-08 stsp if (err)
7478 2e1f37b0 2019-08-08 stsp goto done;
7479 2e1f37b0 2019-08-08 stsp
7480 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7481 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7482 2e1f37b0 2019-08-08 stsp if (err)
7483 2e1f37b0 2019-08-08 stsp goto done;
7484 2e1f37b0 2019-08-08 stsp
7485 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7486 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7487 2e1f37b0 2019-08-08 stsp if (err)
7488 2e1f37b0 2019-08-08 stsp goto done;
7489 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7490 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7491 2e1f37b0 2019-08-08 stsp if (err)
7492 2e1f37b0 2019-08-08 stsp goto done;
7493 2e1f37b0 2019-08-08 stsp
7494 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7495 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7496 2e1f37b0 2019-08-08 stsp goto done;
7497 2e1f37b0 2019-08-08 stsp }
7498 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7499 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7500 2e1f37b0 2019-08-08 stsp goto done;
7501 2e1f37b0 2019-08-08 stsp }
7502 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7503 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7504 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7505 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7506 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7507 fe621944 2020-11-10 stsp nchanges++;
7508 fe621944 2020-11-10 stsp }
7509 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7510 2e1f37b0 2019-08-08 stsp int choice;
7511 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7512 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7513 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7514 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7515 2e1f37b0 2019-08-08 stsp if (err)
7516 2e1f37b0 2019-08-08 stsp goto done;
7517 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7518 2e1f37b0 2019-08-08 stsp have_content = 1;
7519 19e4b907 2019-08-08 stsp else
7520 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7521 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7522 2e1f37b0 2019-08-08 stsp break;
7523 2e1f37b0 2019-08-08 stsp }
7524 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7525 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7526 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7527 2e1f37b0 2019-08-08 stsp done:
7528 2e1f37b0 2019-08-08 stsp free(label1);
7529 2e1f37b0 2019-08-08 stsp if (blob)
7530 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7531 2e1f37b0 2019-08-08 stsp if (staged_blob)
7532 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7533 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7534 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7535 fe621944 2020-11-10 stsp err = free_err;
7536 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7537 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7538 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7539 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7540 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7541 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7542 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7543 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7544 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7545 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7546 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7547 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7548 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7549 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7550 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7551 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7552 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7553 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7554 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7555 2e1f37b0 2019-08-08 stsp }
7556 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7557 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7558 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7559 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7560 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7561 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7562 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7563 2e1f37b0 2019-08-08 stsp }
7564 2e1f37b0 2019-08-08 stsp free(path1);
7565 2e1f37b0 2019-08-08 stsp free(path2);
7566 fda8017d 2020-07-23 stsp return err;
7567 fda8017d 2020-07-23 stsp }
7568 fda8017d 2020-07-23 stsp
7569 fda8017d 2020-07-23 stsp static const struct got_error *
7570 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7571 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7572 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7573 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7574 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7575 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7576 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7577 fda8017d 2020-07-23 stsp {
7578 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7579 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7580 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7581 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7582 fda8017d 2020-07-23 stsp FILE *f = NULL;
7583 fda8017d 2020-07-23 stsp struct stat sb;
7584 fda8017d 2020-07-23 stsp
7585 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7586 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7587 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7588 fda8017d 2020-07-23 stsp if (err)
7589 fda8017d 2020-07-23 stsp return err;
7590 fda8017d 2020-07-23 stsp
7591 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7592 fda8017d 2020-07-23 stsp return NULL;
7593 fda8017d 2020-07-23 stsp
7594 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7595 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7596 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7597 fda8017d 2020-07-23 stsp if (err)
7598 fda8017d 2020-07-23 stsp goto done;
7599 fda8017d 2020-07-23 stsp }
7600 fda8017d 2020-07-23 stsp
7601 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7602 fda8017d 2020-07-23 stsp if (f == NULL) {
7603 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7604 fda8017d 2020-07-23 stsp path_unstaged_content);
7605 fda8017d 2020-07-23 stsp goto done;
7606 fda8017d 2020-07-23 stsp }
7607 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7608 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7609 fda8017d 2020-07-23 stsp goto done;
7610 fda8017d 2020-07-23 stsp }
7611 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7612 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7613 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7614 fda8017d 2020-07-23 stsp size_t r;
7615 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7616 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7617 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7618 fda8017d 2020-07-23 stsp goto done;
7619 fda8017d 2020-07-23 stsp }
7620 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7621 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7622 fda8017d 2020-07-23 stsp goto done;
7623 fda8017d 2020-07-23 stsp }
7624 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7625 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7626 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7627 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7628 fda8017d 2020-07-23 stsp progress_arg);
7629 fda8017d 2020-07-23 stsp } else {
7630 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7631 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7632 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7633 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7634 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7635 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7636 fda8017d 2020-07-23 stsp }
7637 fda8017d 2020-07-23 stsp if (err)
7638 fda8017d 2020-07-23 stsp goto done;
7639 fda8017d 2020-07-23 stsp
7640 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7641 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7642 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7643 2ac8aa02 2020-11-09 stsp } else {
7644 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7645 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7646 2ac8aa02 2020-11-09 stsp }
7647 fda8017d 2020-07-23 stsp done:
7648 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7649 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7650 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7651 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7652 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7653 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7654 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7655 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7656 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7657 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7658 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7659 2e1f37b0 2019-08-08 stsp return err;
7660 2e1f37b0 2019-08-08 stsp }
7661 2e1f37b0 2019-08-08 stsp
7662 ad493afc 2019-08-03 stsp static const struct got_error *
7663 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7664 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7665 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7666 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7667 ad493afc 2019-08-03 stsp {
7668 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7669 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7670 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7671 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7672 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7673 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7674 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7675 9bc94a15 2019-08-03 stsp struct stat sb;
7676 ad493afc 2019-08-03 stsp
7677 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7678 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7679 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7680 2e1f37b0 2019-08-08 stsp return NULL;
7681 2e1f37b0 2019-08-08 stsp
7682 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7683 ad493afc 2019-08-03 stsp if (ie == NULL)
7684 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7685 9bc94a15 2019-08-03 stsp
7686 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7687 9bc94a15 2019-08-03 stsp == -1)
7688 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7689 ad493afc 2019-08-03 stsp
7690 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7691 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7692 f69721c3 2019-10-21 stsp if (err)
7693 f69721c3 2019-10-21 stsp goto done;
7694 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7695 f69721c3 2019-10-21 stsp id_str) == -1) {
7696 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7697 f69721c3 2019-10-21 stsp goto done;
7698 f69721c3 2019-10-21 stsp }
7699 f69721c3 2019-10-21 stsp
7700 ad493afc 2019-08-03 stsp switch (staged_status) {
7701 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7702 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7703 ad493afc 2019-08-03 stsp blob_id, 8192);
7704 ad493afc 2019-08-03 stsp if (err)
7705 ad493afc 2019-08-03 stsp break;
7706 ad493afc 2019-08-03 stsp /* fall through */
7707 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7708 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7709 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7710 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7711 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7712 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7713 2e1f37b0 2019-08-08 stsp if (err)
7714 2e1f37b0 2019-08-08 stsp break;
7715 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7716 2e1f37b0 2019-08-08 stsp break;
7717 2e1f37b0 2019-08-08 stsp } else {
7718 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7719 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7720 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7721 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7722 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7723 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7724 2e1f37b0 2019-08-08 stsp }
7725 2e1f37b0 2019-08-08 stsp }
7726 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7727 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7728 ad493afc 2019-08-03 stsp if (err)
7729 ad493afc 2019-08-03 stsp break;
7730 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7731 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7732 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7733 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7734 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7735 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7736 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7737 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7738 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7739 ea7786be 2020-07-23 stsp break;
7740 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7741 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7742 36bf999c 2020-07-23 stsp char *staged_target;
7743 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7744 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7745 36bf999c 2020-07-23 stsp if (err)
7746 36bf999c 2020-07-23 stsp goto done;
7747 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7748 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7749 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7750 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7751 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7752 36bf999c 2020-07-23 stsp free(staged_target);
7753 dfe9fba0 2020-07-23 stsp } else {
7754 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7755 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7756 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7757 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7758 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7759 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7760 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7761 dfe9fba0 2020-07-23 stsp }
7762 ea7786be 2020-07-23 stsp break;
7763 ea7786be 2020-07-23 stsp default:
7764 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7765 ea7786be 2020-07-23 stsp break;
7766 ea7786be 2020-07-23 stsp }
7767 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7768 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7769 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7770 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7771 2ac8aa02 2020-11-09 stsp }
7772 ad493afc 2019-08-03 stsp break;
7773 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7774 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7775 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7776 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7777 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7778 2e1f37b0 2019-08-08 stsp if (err)
7779 2e1f37b0 2019-08-08 stsp break;
7780 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7781 2e1f37b0 2019-08-08 stsp break;
7782 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7783 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7784 2e1f37b0 2019-08-08 stsp break;
7785 2e1f37b0 2019-08-08 stsp }
7786 2e1f37b0 2019-08-08 stsp }
7787 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7788 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7789 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7790 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7791 9bc94a15 2019-08-03 stsp if (err)
7792 9bc94a15 2019-08-03 stsp break;
7793 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7794 ad493afc 2019-08-03 stsp break;
7795 ad493afc 2019-08-03 stsp }
7796 f69721c3 2019-10-21 stsp done:
7797 ad493afc 2019-08-03 stsp free(ondisk_path);
7798 ad493afc 2019-08-03 stsp if (blob_base)
7799 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7800 ad493afc 2019-08-03 stsp if (blob_staged)
7801 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7802 f69721c3 2019-10-21 stsp free(id_str);
7803 f69721c3 2019-10-21 stsp free(label_orig);
7804 ad493afc 2019-08-03 stsp return err;
7805 ad493afc 2019-08-03 stsp }
7806 ad493afc 2019-08-03 stsp
7807 ad493afc 2019-08-03 stsp const struct got_error *
7808 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7809 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7810 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7811 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7812 ad493afc 2019-08-03 stsp struct got_repository *repo)
7813 ad493afc 2019-08-03 stsp {
7814 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7815 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7816 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7817 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7818 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7819 ad493afc 2019-08-03 stsp
7820 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7821 ad493afc 2019-08-03 stsp if (err)
7822 ad493afc 2019-08-03 stsp return err;
7823 ad493afc 2019-08-03 stsp
7824 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7825 ad493afc 2019-08-03 stsp if (err)
7826 ad493afc 2019-08-03 stsp goto done;
7827 ad493afc 2019-08-03 stsp
7828 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7829 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7830 ad493afc 2019-08-03 stsp upa.repo = repo;
7831 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7832 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7833 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7834 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7835 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7836 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7837 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7838 ad493afc 2019-08-03 stsp if (err)
7839 ad493afc 2019-08-03 stsp goto done;
7840 ad493afc 2019-08-03 stsp }
7841 ad493afc 2019-08-03 stsp
7842 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7843 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7844 ad493afc 2019-08-03 stsp err = sync_err;
7845 ad493afc 2019-08-03 stsp done:
7846 ad493afc 2019-08-03 stsp free(fileindex_path);
7847 ad493afc 2019-08-03 stsp if (fileindex)
7848 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7849 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7850 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7851 ad493afc 2019-08-03 stsp err = unlockerr;
7852 ad493afc 2019-08-03 stsp return err;
7853 ad493afc 2019-08-03 stsp }
7854 b2118c49 2020-07-28 stsp
7855 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7856 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7857 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7858 b2118c49 2020-07-28 stsp void *info_arg;
7859 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7860 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7861 b2118c49 2020-07-28 stsp void *cancel_arg;
7862 b2118c49 2020-07-28 stsp };
7863 b2118c49 2020-07-28 stsp
7864 b2118c49 2020-07-28 stsp static const struct got_error *
7865 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7866 b2118c49 2020-07-28 stsp {
7867 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7868 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7869 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7870 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7871 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7872 b2118c49 2020-07-28 stsp int stage;
7873 b2118c49 2020-07-28 stsp
7874 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7875 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7876 b2118c49 2020-07-28 stsp
7877 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7878 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7879 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7880 b2118c49 2020-07-28 stsp break;
7881 b2118c49 2020-07-28 stsp }
7882 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7883 b2118c49 2020-07-28 stsp return NULL;
7884 b2118c49 2020-07-28 stsp
7885 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7886 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7887 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7888 b2118c49 2020-07-28 stsp }
7889 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7890 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7891 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7892 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7893 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7894 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7895 b2118c49 2020-07-28 stsp }
7896 b2118c49 2020-07-28 stsp
7897 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7898 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7899 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7900 b2118c49 2020-07-28 stsp }
7901 b2118c49 2020-07-28 stsp
7902 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7903 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7904 b2118c49 2020-07-28 stsp }
7905 b2118c49 2020-07-28 stsp
7906 b2118c49 2020-07-28 stsp const struct got_error *
7907 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7908 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7909 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7910 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7911 b2118c49 2020-07-28 stsp
7912 b2118c49 2020-07-28 stsp {
7913 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7914 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7915 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7916 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7917 b2118c49 2020-07-28 stsp
7918 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7919 b2118c49 2020-07-28 stsp if (err)
7920 b2118c49 2020-07-28 stsp return err;
7921 b2118c49 2020-07-28 stsp
7922 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7923 b2118c49 2020-07-28 stsp if (err)
7924 b2118c49 2020-07-28 stsp goto done;
7925 b2118c49 2020-07-28 stsp
7926 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7927 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7928 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7929 b2118c49 2020-07-28 stsp arg.paths = paths;
7930 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7931 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7932 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7933 b2118c49 2020-07-28 stsp &arg);
7934 b2118c49 2020-07-28 stsp done:
7935 b2118c49 2020-07-28 stsp free(fileindex_path);
7936 b2118c49 2020-07-28 stsp if (fileindex)
7937 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7938 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7939 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7940 b2118c49 2020-07-28 stsp err = unlockerr;
7941 b2118c49 2020-07-28 stsp return err;
7942 b2118c49 2020-07-28 stsp }