Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 9d31a1d8 2018-03-11 stsp
59 9d31a1d8 2018-03-11 stsp #ifndef MIN
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 9d31a1d8 2018-03-11 stsp #endif
62 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 86c3caaf 2018-03-09 stsp
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp {
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 99724ed4 2018-03-10 stsp char *path;
71 99724ed4 2018-03-10 stsp
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 99724ed4 2018-03-10 stsp free(path);
77 507dc3bb 2018-12-29 stsp return err;
78 507dc3bb 2018-12-29 stsp }
79 507dc3bb 2018-12-29 stsp
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp {
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp }
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
95 507dc3bb 2018-12-29 stsp if (err)
96 507dc3bb 2018-12-29 stsp goto done;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp done:
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
117 99724ed4 2018-03-10 stsp }
118 99724ed4 2018-03-10 stsp
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
121 09fe317a 2018-03-11 stsp {
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
125 09fe317a 2018-03-11 stsp ssize_t n;
126 09fe317a 2018-03-11 stsp struct stat sb;
127 09fe317a 2018-03-11 stsp
128 09fe317a 2018-03-11 stsp *content = NULL;
129 09fe317a 2018-03-11 stsp
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 f02eaa22 2019-03-11 stsp else
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
142 ef99fdb1 2018-03-11 stsp goto done;
143 ef99fdb1 2018-03-11 stsp }
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
147 09fe317a 2018-03-11 stsp goto done;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
152 d10c9b58 2019-02-19 stsp goto done;
153 d10c9b58 2019-02-19 stsp }
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
157 09fe317a 2018-03-11 stsp goto done;
158 09fe317a 2018-03-11 stsp }
159 09fe317a 2018-03-11 stsp
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp goto done;
169 09fe317a 2018-03-11 stsp }
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp done:
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
176 09fe317a 2018-03-11 stsp if (err) {
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
179 09fe317a 2018-03-11 stsp }
180 09fe317a 2018-03-11 stsp return err;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
185 024e9686 2019-05-14 stsp {
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
188 024e9686 2019-05-14 stsp
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
193 024e9686 2019-05-14 stsp } else {
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
197 024e9686 2019-05-14 stsp }
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
201 024e9686 2019-05-14 stsp }
202 024e9686 2019-05-14 stsp
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
217 65596e15 2018-12-24 stsp
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
220 0c48fee2 2019-03-11 stsp goto done;
221 0c48fee2 2019-03-11 stsp }
222 0c48fee2 2019-03-11 stsp
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
224 65596e15 2018-12-24 stsp if (err)
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
227 65596e15 2018-12-24 stsp if (err)
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
231 86c3caaf 2018-03-09 stsp
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
235 0bb8a95e 2018-03-12 stsp }
236 577ec78f 2018-03-11 stsp
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
240 86c3caaf 2018-03-09 stsp goto done;
241 86c3caaf 2018-03-09 stsp }
242 86c3caaf 2018-03-09 stsp
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 86c3caaf 2018-03-09 stsp goto done;
247 86c3caaf 2018-03-09 stsp }
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
250 86c3caaf 2018-03-09 stsp goto done;
251 86c3caaf 2018-03-09 stsp }
252 86c3caaf 2018-03-09 stsp
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 056e7441 2018-03-11 stsp if (err)
256 056e7441 2018-03-11 stsp goto done;
257 056e7441 2018-03-11 stsp
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 65596e15 2018-12-24 stsp
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
270 65596e15 2018-12-24 stsp if (err)
271 65596e15 2018-12-24 stsp goto done;
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 99724ed4 2018-03-10 stsp if (err)
274 86c3caaf 2018-03-09 stsp goto done;
275 86c3caaf 2018-03-09 stsp
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 99724ed4 2018-03-10 stsp if (err)
280 86c3caaf 2018-03-09 stsp goto done;
281 86c3caaf 2018-03-09 stsp
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp if (err)
286 ec22038e 2019-03-10 stsp goto done;
287 ec22038e 2019-03-10 stsp
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
297 ec22038e 2019-03-10 stsp goto done;
298 ec22038e 2019-03-10 stsp }
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 577ec78f 2018-03-11 stsp if (err)
301 577ec78f 2018-03-11 stsp goto done;
302 577ec78f 2018-03-11 stsp
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
306 1451e70d 2018-03-10 stsp goto done;
307 1451e70d 2018-03-10 stsp }
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 99724ed4 2018-03-10 stsp if (err)
310 1451e70d 2018-03-10 stsp goto done;
311 1451e70d 2018-03-10 stsp
312 86c3caaf 2018-03-09 stsp done:
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
324 86c3caaf 2018-03-09 stsp {
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
365 6d9d28c3 2018-03-11 stsp goto done;
366 6d9d28c3 2018-03-11 stsp }
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp }
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
378 6d9d28c3 2018-03-11 stsp
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
382 6d9d28c3 2018-03-11 stsp goto done;
383 6d9d28c3 2018-03-11 stsp }
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
386 6d9d28c3 2018-03-11 stsp if (err)
387 6d9d28c3 2018-03-11 stsp goto done;
388 eaccb85f 2018-12-25 stsp
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
391 93a30277 2018-12-24 stsp if (err)
392 93a30277 2018-12-24 stsp goto done;
393 93a30277 2018-12-24 stsp
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
396 c442a90d 2019-03-10 stsp if (err)
397 c442a90d 2019-03-10 stsp goto done;
398 c442a90d 2019-03-10 stsp
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 eaccb85f 2018-12-25 stsp if (err)
401 c442a90d 2019-03-10 stsp goto done;
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
405 eaccb85f 2018-12-25 stsp goto done;
406 c442a90d 2019-03-10 stsp }
407 eaccb85f 2018-12-25 stsp
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 eaccb85f 2018-12-25 stsp if (err)
410 eaccb85f 2018-12-25 stsp goto done;
411 eaccb85f 2018-12-25 stsp
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
414 f5baf295 2018-03-11 stsp if (err)
415 6d9d28c3 2018-03-11 stsp goto done;
416 6d9d28c3 2018-03-11 stsp
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
419 6d9d28c3 2018-03-11 stsp done:
420 eaccb85f 2018-12-25 stsp if (repo)
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
427 6d9d28c3 2018-03-11 stsp if (err) {
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
429 6d9d28c3 2018-03-11 stsp close(fd);
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
433 6d9d28c3 2018-03-11 stsp } else
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
435 6d9d28c3 2018-03-11 stsp
436 6d9d28c3 2018-03-11 stsp return err;
437 86c3caaf 2018-03-09 stsp }
438 247140b2 2019-02-05 stsp
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
441 247140b2 2019-02-05 stsp {
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
443 86c3caaf 2018-03-09 stsp
444 247140b2 2019-02-05 stsp do {
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
454 247140b2 2019-02-05 stsp
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
456 247140b2 2019-02-05 stsp }
457 247140b2 2019-02-05 stsp
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
460 86c3caaf 2018-03-09 stsp {
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
473 86c3caaf 2018-03-09 stsp }
474 86c3caaf 2018-03-09 stsp
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
477 c7f4312f 2019-02-05 stsp {
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
479 c7f4312f 2019-02-05 stsp }
480 c7f4312f 2019-02-05 stsp
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
483 86c3caaf 2018-03-09 stsp {
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
485 49520a32 2018-12-29 stsp }
486 49520a32 2018-12-29 stsp
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
489 49520a32 2018-12-29 stsp {
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
491 e5dc7198 2018-12-29 stsp }
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
496 e5dc7198 2018-12-29 stsp {
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
498 e5dc7198 2018-12-29 stsp
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
502 e5dc7198 2018-12-29 stsp }
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
507 86c3caaf 2018-03-09 stsp }
508 86c3caaf 2018-03-09 stsp
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
511 86c3caaf 2018-03-09 stsp {
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
513 024e9686 2019-05-14 stsp }
514 024e9686 2019-05-14 stsp
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
518 024e9686 2019-05-14 stsp {
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
521 024e9686 2019-05-14 stsp
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp }
534 024e9686 2019-05-14 stsp
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
536 024e9686 2019-05-14 stsp if (err)
537 024e9686 2019-05-14 stsp goto done;
538 024e9686 2019-05-14 stsp
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
541 024e9686 2019-05-14 stsp done:
542 024e9686 2019-05-14 stsp free(path_got);
543 024e9686 2019-05-14 stsp if (err)
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
546 507dc3bb 2018-12-29 stsp }
547 507dc3bb 2018-12-29 stsp
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
550 507dc3bb 2018-12-29 stsp {
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
552 86c3caaf 2018-03-09 stsp }
553 86c3caaf 2018-03-09 stsp
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
557 507dc3bb 2018-12-29 stsp {
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
571 507dc3bb 2018-12-29 stsp if (err)
572 507dc3bb 2018-12-29 stsp return err;
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp }
578 507dc3bb 2018-12-29 stsp
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
581 507dc3bb 2018-12-29 stsp if (err)
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 507dc3bb 2018-12-29 stsp if (err)
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp done:
594 507dc3bb 2018-12-29 stsp if (obj)
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
599 507dc3bb 2018-12-29 stsp }
600 507dc3bb 2018-12-29 stsp
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
603 86c3caaf 2018-03-09 stsp {
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
609 21908da4 2019-01-13 stsp }
610 21908da4 2019-01-13 stsp
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
613 4a1ddfc2 2019-01-12 stsp {
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
616 4a1ddfc2 2019-01-12 stsp
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
619 4a1ddfc2 2019-01-12 stsp
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
629 ddcd8544 2019-03-11 stsp }
630 ddcd8544 2019-03-11 stsp }
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
633 68c76935 2019-02-19 stsp }
634 68c76935 2019-02-19 stsp
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
637 68c76935 2019-02-19 stsp {
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp *same = 1;
644 68c76935 2019-02-19 stsp
645 230a42bd 2019-05-11 jcs for (;;) {
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp }
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
658 68c76935 2019-02-19 stsp *same = 0;
659 68c76935 2019-02-19 stsp break;
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
662 68c76935 2019-02-19 stsp *same = 0;
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 68c76935 2019-02-19 stsp *same = 0;
667 68c76935 2019-02-19 stsp break;
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp } else {
670 68c76935 2019-02-19 stsp *same = 0;
671 68c76935 2019-02-19 stsp break;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp }
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
680 68c76935 2019-02-19 stsp {
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
685 68c76935 2019-02-19 stsp
686 68c76935 2019-02-19 stsp *same = 1;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
696 68c76935 2019-02-19 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp if (size1 != size2) {
701 68c76935 2019-02-19 stsp *same = 0;
702 68c76935 2019-02-19 stsp return NULL;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
712 68c76935 2019-02-19 stsp goto done;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
716 68c76935 2019-02-19 stsp done:
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
721 68c76935 2019-02-19 stsp
722 6353ad76 2019-02-08 stsp return err;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp
725 6353ad76 2019-02-08 stsp /*
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
729 6353ad76 2019-02-08 stsp */
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
737 6353ad76 2019-02-08 stsp {
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 6353ad76 2019-02-08 stsp
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
747 234035bc 2019-06-01 stsp
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
751 af54ae4a 2019-02-19 stsp
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
754 af54ae4a 2019-02-19 stsp
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 6353ad76 2019-02-08 stsp if (err)
757 6353ad76 2019-02-08 stsp goto done;
758 6353ad76 2019-02-08 stsp
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
763 af54ae4a 2019-02-19 stsp goto done;
764 af54ae4a 2019-02-19 stsp }
765 af54ae4a 2019-02-19 stsp
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 6353ad76 2019-02-08 stsp if (err)
768 6353ad76 2019-02-08 stsp goto done;
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
772 1430b4e0 2019-03-27 stsp if (err)
773 1430b4e0 2019-03-27 stsp goto done;
774 1430b4e0 2019-03-27 stsp } else {
775 1430b4e0 2019-03-27 stsp /*
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
779 1430b4e0 2019-03-27 stsp */
780 1430b4e0 2019-03-27 stsp }
781 6353ad76 2019-02-08 stsp
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 6353ad76 2019-02-08 stsp if (err)
785 6353ad76 2019-02-08 stsp goto done;
786 6353ad76 2019-02-08 stsp
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 1ee397ad 2019-07-12 stsp if (err)
790 1ee397ad 2019-07-12 stsp goto done;
791 6353ad76 2019-02-08 stsp
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
794 816dc654 2019-02-16 stsp goto done;
795 68c76935 2019-02-19 stsp }
796 68c76935 2019-02-19 stsp
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
801 68c76935 2019-02-19 stsp if (err)
802 68c76935 2019-02-19 stsp goto done;
803 816dc654 2019-02-16 stsp }
804 6353ad76 2019-02-08 stsp
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
807 70a0c8ec 2019-02-20 stsp goto done;
808 70a0c8ec 2019-02-20 stsp }
809 70a0c8ec 2019-02-20 stsp
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp }
815 6353ad76 2019-02-08 stsp done:
816 fdcb7daf 2019-12-15 stsp if (err) {
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
819 fdcb7daf 2019-12-15 stsp }
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
829 af57b12a 2020-07-23 stsp }
830 af57b12a 2020-07-23 stsp return err;
831 af57b12a 2020-07-23 stsp }
832 af57b12a 2020-07-23 stsp
833 af57b12a 2020-07-23 stsp static const struct got_error *
834 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
835 af57b12a 2020-07-23 stsp size_t target_len)
836 af57b12a 2020-07-23 stsp {
837 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
838 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
839 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
840 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
842 af57b12a 2020-07-23 stsp ondisk_path);
843 af57b12a 2020-07-23 stsp return NULL;
844 af57b12a 2020-07-23 stsp }
845 af57b12a 2020-07-23 stsp
846 af57b12a 2020-07-23 stsp /*
847 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
849 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
850 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
851 993e2a1b 2020-07-23 stsp *
852 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
853 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
854 993e2a1b 2020-07-23 stsp *
855 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
856 993e2a1b 2020-07-23 stsp * has deleted this symlink.
857 11cc08c1 2020-07-23 stsp */
858 11cc08c1 2020-07-23 stsp static const struct got_error *
859 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
860 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
862 11cc08c1 2020-07-23 stsp {
863 11cc08c1 2020-07-23 stsp const struct got_error *err;
864 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 11cc08c1 2020-07-23 stsp FILE *f = NULL;
866 11cc08c1 2020-07-23 stsp
867 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
868 11cc08c1 2020-07-23 stsp if (err)
869 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
872 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
874 11cc08c1 2020-07-23 stsp goto done;
875 11cc08c1 2020-07-23 stsp }
876 11cc08c1 2020-07-23 stsp
877 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
878 11cc08c1 2020-07-23 stsp if (err)
879 11cc08c1 2020-07-23 stsp goto done;
880 11cc08c1 2020-07-23 stsp
881 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
883 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
884 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
885 11cc08c1 2020-07-23 stsp "target paths were found:\n"
886 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
889 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
890 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
891 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
892 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
893 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
894 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
896 11cc08c1 2020-07-23 stsp goto done;
897 11cc08c1 2020-07-23 stsp }
898 11cc08c1 2020-07-23 stsp
899 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
900 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
901 11cc08c1 2020-07-23 stsp goto done;
902 11cc08c1 2020-07-23 stsp }
903 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
904 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
905 11cc08c1 2020-07-23 stsp goto done;
906 11cc08c1 2020-07-23 stsp }
907 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
909 11cc08c1 2020-07-23 stsp goto done;
910 11cc08c1 2020-07-23 stsp }
911 11cc08c1 2020-07-23 stsp done:
912 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
913 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
914 11cc08c1 2020-07-23 stsp free(path);
915 11cc08c1 2020-07-23 stsp free(id_str);
916 11cc08c1 2020-07-23 stsp free(label_deriv);
917 11cc08c1 2020-07-23 stsp return err;
918 11cc08c1 2020-07-23 stsp }
919 d219f183 2020-07-23 stsp
920 d219f183 2020-07-23 stsp /* forward declaration */
921 d219f183 2020-07-23 stsp static const struct got_error *
922 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
924 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
925 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
926 11cc08c1 2020-07-23 stsp
927 11cc08c1 2020-07-23 stsp /*
928 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
929 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
930 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
931 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
932 af57b12a 2020-07-23 stsp */
933 af57b12a 2020-07-23 stsp static const struct got_error *
934 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
935 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
936 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
937 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
938 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
940 af57b12a 2020-07-23 stsp {
941 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
942 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
943 af57b12a 2020-07-23 stsp struct stat sb;
944 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
945 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
946 11cc08c1 2020-07-23 stsp int have_local_change = 0;
947 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
948 af57b12a 2020-07-23 stsp
949 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
950 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
951 af57b12a 2020-07-23 stsp
952 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
953 d219f183 2020-07-23 stsp /*
954 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
955 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
956 d219f183 2020-07-23 stsp * a merge conflict.
957 d219f183 2020-07-23 stsp */
958 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
959 d219f183 2020-07-23 stsp int local_changes_subsumed;
960 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
961 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
962 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
963 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
964 d219f183 2020-07-23 stsp }
965 d219f183 2020-07-23 stsp
966 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
967 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
968 af57b12a 2020-07-23 stsp }
969 af57b12a 2020-07-23 stsp
970 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
971 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
972 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
973 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
974 af57b12a 2020-07-23 stsp ondisk_path);
975 af57b12a 2020-07-23 stsp goto done;
976 af57b12a 2020-07-23 stsp }
977 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
978 af57b12a 2020-07-23 stsp
979 526a746f 2020-07-23 stsp if (blob_orig) {
980 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
981 526a746f 2020-07-23 stsp if (err)
982 526a746f 2020-07-23 stsp goto done;
983 526a746f 2020-07-23 stsp }
984 af57b12a 2020-07-23 stsp
985 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
986 af57b12a 2020-07-23 stsp if (err)
987 af57b12a 2020-07-23 stsp goto done;
988 af57b12a 2020-07-23 stsp
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 11cc08c1 2020-07-23 stsp have_local_change = 1;
993 11cc08c1 2020-07-23 stsp
994 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
995 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
996 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
997 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
999 11cc08c1 2020-07-23 stsp
1000 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1001 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1002 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1003 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1004 11cc08c1 2020-07-23 stsp path);
1005 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1006 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1008 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1009 11cc08c1 2020-07-23 stsp path);
1010 11cc08c1 2020-07-23 stsp } else {
1011 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1012 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1013 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1014 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1015 11cc08c1 2020-07-23 stsp if (err)
1016 11cc08c1 2020-07-23 stsp goto done;
1017 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1018 11cc08c1 2020-07-23 stsp path);
1019 11cc08c1 2020-07-23 stsp }
1020 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1021 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1022 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1023 af57b12a 2020-07-23 stsp strlen(deriv_target));
1024 af57b12a 2020-07-23 stsp if (err)
1025 af57b12a 2020-07-23 stsp goto done;
1026 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1028 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1031 11cc08c1 2020-07-23 stsp if (err)
1032 11cc08c1 2020-07-23 stsp goto done;
1033 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 11cc08c1 2020-07-23 stsp path);
1035 6353ad76 2019-02-08 stsp }
1036 11cc08c1 2020-07-23 stsp
1037 af57b12a 2020-07-23 stsp done:
1038 af57b12a 2020-07-23 stsp free(ancestor_target);
1039 af57b12a 2020-07-23 stsp free(deriv_target);
1040 14c901f1 2019-08-08 stsp return err;
1041 14c901f1 2019-08-08 stsp }
1042 14c901f1 2019-08-08 stsp
1043 14c901f1 2019-08-08 stsp /*
1044 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1046 14c901f1 2019-08-08 stsp * acts as the second derived version.
1047 14c901f1 2019-08-08 stsp */
1048 14c901f1 2019-08-08 stsp static const struct got_error *
1049 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1051 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1052 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1053 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1055 14c901f1 2019-08-08 stsp {
1056 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1057 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1058 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1060 14c901f1 2019-08-08 stsp
1061 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1062 14c901f1 2019-08-08 stsp
1063 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1064 14c901f1 2019-08-08 stsp if (parent == NULL)
1065 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1066 14c901f1 2019-08-08 stsp
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1070 14c901f1 2019-08-08 stsp base_path = NULL;
1071 14c901f1 2019-08-08 stsp goto done;
1072 14c901f1 2019-08-08 stsp }
1073 14c901f1 2019-08-08 stsp
1074 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1075 14c901f1 2019-08-08 stsp if (err)
1076 14c901f1 2019-08-08 stsp goto done;
1077 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 14c901f1 2019-08-08 stsp blob_deriv);
1079 14c901f1 2019-08-08 stsp if (err)
1080 14c901f1 2019-08-08 stsp goto done;
1081 14c901f1 2019-08-08 stsp
1082 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1083 14c901f1 2019-08-08 stsp if (err)
1084 14c901f1 2019-08-08 stsp goto done;
1085 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1086 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1088 14c901f1 2019-08-08 stsp goto done;
1089 14c901f1 2019-08-08 stsp }
1090 14c901f1 2019-08-08 stsp
1091 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1094 14c901f1 2019-08-08 stsp done:
1095 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1097 14c901f1 2019-08-08 stsp free(base_path);
1098 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1099 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1100 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1101 14c901f1 2019-08-08 stsp }
1102 6353ad76 2019-02-08 stsp free(id_str);
1103 818c7501 2019-07-11 stsp free(label_deriv);
1104 4a1ddfc2 2019-01-12 stsp return err;
1105 4a1ddfc2 2019-01-12 stsp }
1106 4a1ddfc2 2019-01-12 stsp
1107 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1108 054041d0 2020-06-24 stsp create_fileindex_entry(struct got_fileindex *fileindex,
1109 054041d0 2020-06-24 stsp struct got_object_id *base_commit_id, const char *ondisk_path,
1110 054041d0 2020-06-24 stsp const char *path, struct got_object_id *blob_id)
1111 13d9040b 2019-03-27 stsp {
1112 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1113 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1114 13d9040b 2019-03-27 stsp
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1116 054041d0 2020-06-24 stsp if (err)
1117 054041d0 2020-06-24 stsp return err;
1118 054041d0 2020-06-24 stsp
1119 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1121 054041d0 2020-06-24 stsp if (err)
1122 054041d0 2020-06-24 stsp goto done;
1123 054041d0 2020-06-24 stsp
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1125 054041d0 2020-06-24 stsp done:
1126 054041d0 2020-06-24 stsp if (err)
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 13d9040b 2019-03-27 stsp return err;
1129 1ebedb77 2019-10-19 stsp }
1130 1ebedb77 2019-10-19 stsp
1131 1ebedb77 2019-10-19 stsp static mode_t
1132 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1133 1ebedb77 2019-10-19 stsp {
1134 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1135 1ebedb77 2019-10-19 stsp
1136 1ebedb77 2019-10-19 stsp if (executable) {
1137 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1142 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1143 1ebedb77 2019-10-19 stsp }
1144 1ebedb77 2019-10-19 stsp
1145 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1146 13d9040b 2019-03-27 stsp }
1147 13d9040b 2019-03-27 stsp
1148 8ba819a3 2020-07-23 stsp /* forward declaration */
1149 13d9040b 2019-03-27 stsp static const struct got_error *
1150 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1152 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1153 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1154 bd6aa359 2020-07-23 stsp struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 8ba819a3 2020-07-23 stsp
1157 5a1fbc73 2020-07-23 stsp /*
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1160 5a1fbc73 2020-07-23 stsp */
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1163 5a1fbc73 2020-07-23 stsp size_t target_len)
1164 5a1fbc73 2020-07-23 stsp {
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1168 5a1fbc73 2020-07-23 stsp int fd;
1169 5a1fbc73 2020-07-23 stsp
1170 5a1fbc73 2020-07-23 stsp /*
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1177 5a1fbc73 2020-07-23 stsp */
1178 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 5a1fbc73 2020-07-23 stsp }
1192 5a1fbc73 2020-07-23 stsp
1193 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1194 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1195 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1196 5a1fbc73 2020-07-23 stsp return err;
1197 5a1fbc73 2020-07-23 stsp }
1198 5a1fbc73 2020-07-23 stsp
1199 5a1fbc73 2020-07-23 stsp static const struct got_error *
1200 8ba819a3 2020-07-23 stsp install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1201 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1202 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1203 8ba819a3 2020-07-23 stsp int reverting_versioned_file, struct got_repository *repo,
1204 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1205 9d31a1d8 2018-03-11 stsp {
1206 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1207 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1208 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1209 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1210 906c123b 2020-07-23 stsp char *path_got = NULL;
1211 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1213 8ba819a3 2020-07-23 stsp
1214 8ba819a3 2020-07-23 stsp /*
1215 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1216 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1217 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1218 8ba819a3 2020-07-23 stsp * in the blob object.
1219 8ba819a3 2020-07-23 stsp */
1220 8ba819a3 2020-07-23 stsp do {
1221 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1222 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1223 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1224 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1225 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1226 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1227 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1228 bd6aa359 2020-07-23 stsp 1, repo, progress_cb, progress_arg);
1229 8ba819a3 2020-07-23 stsp }
1230 8ba819a3 2020-07-23 stsp if (len > 0) {
1231 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1232 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1233 8ba819a3 2020-07-23 stsp len - hdrlen);
1234 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1235 8ba819a3 2020-07-23 stsp hdrlen = 0;
1236 8ba819a3 2020-07-23 stsp }
1237 8ba819a3 2020-07-23 stsp } while (len != 0);
1238 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1239 8ba819a3 2020-07-23 stsp
1240 8ba819a3 2020-07-23 stsp /*
1241 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1242 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1243 8ba819a3 2020-07-23 stsp */
1244 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1245 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1246 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1247 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1248 ef68ca6f 2020-07-23 stsp goto done;
1249 ef68ca6f 2020-07-23 stsp }
1250 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1251 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1252 8ba819a3 2020-07-23 stsp goto done;
1253 8ba819a3 2020-07-23 stsp }
1254 8ba819a3 2020-07-23 stsp }
1255 8ba819a3 2020-07-23 stsp
1256 8ba819a3 2020-07-23 stsp /*
1257 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1258 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1259 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1260 8ba819a3 2020-07-23 stsp */
1261 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1262 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1263 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1264 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1265 b15bc87b 2020-07-23 stsp goto done;
1266 b15bc87b 2020-07-23 stsp }
1267 8ba819a3 2020-07-23 stsp }
1268 8ba819a3 2020-07-23 stsp
1269 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1270 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1271 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1272 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1273 8ba819a3 2020-07-23 stsp /* install as a regular file */
1274 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1275 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1276 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1277 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1278 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1279 8ba819a3 2020-07-23 stsp goto done;
1280 8ba819a3 2020-07-23 stsp }
1281 8ba819a3 2020-07-23 stsp
1282 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1283 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1284 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1285 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1286 906c123b 2020-07-23 stsp goto done;
1287 906c123b 2020-07-23 stsp }
1288 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1289 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1290 906c123b 2020-07-23 stsp /* install as a regular file */
1291 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1292 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1293 906c123b 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1294 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1295 906c123b 2020-07-23 stsp repo, progress_cb, progress_arg);
1296 906c123b 2020-07-23 stsp goto done;
1297 906c123b 2020-07-23 stsp }
1298 906c123b 2020-07-23 stsp
1299 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1300 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1301 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1302 5a1fbc73 2020-07-23 stsp target_path, target_len);
1303 5a1fbc73 2020-07-23 stsp if (err)
1304 f35fa46a 2020-07-23 stsp goto done;
1305 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1306 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1307 5a1fbc73 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1308 f35fa46a 2020-07-23 stsp }
1309 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1310 f35fa46a 2020-07-23 stsp }
1311 f35fa46a 2020-07-23 stsp
1312 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1313 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1314 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1315 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1316 f35fa46a 2020-07-23 stsp ondisk_path);
1317 f35fa46a 2020-07-23 stsp goto done;
1318 f35fa46a 2020-07-23 stsp }
1319 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1320 f35fa46a 2020-07-23 stsp if (err)
1321 f35fa46a 2020-07-23 stsp goto done;
1322 f35fa46a 2020-07-23 stsp /*
1323 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1324 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1325 f35fa46a 2020-07-23 stsp */
1326 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1327 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1328 f35fa46a 2020-07-23 stsp goto done;
1329 f35fa46a 2020-07-23 stsp }
1330 f35fa46a 2020-07-23 stsp }
1331 f35fa46a 2020-07-23 stsp
1332 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1333 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1334 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1335 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1336 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1337 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1338 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1339 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1340 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1341 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1342 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1343 8ba819a3 2020-07-23 stsp } else {
1344 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1345 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1346 8ba819a3 2020-07-23 stsp }
1347 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1348 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1349 8ba819a3 2020-07-23 stsp done:
1350 8ba819a3 2020-07-23 stsp free(resolved_path);
1351 8ba819a3 2020-07-23 stsp free(abspath);
1352 906c123b 2020-07-23 stsp free(path_got);
1353 8ba819a3 2020-07-23 stsp return err;
1354 8ba819a3 2020-07-23 stsp }
1355 8ba819a3 2020-07-23 stsp
1356 8ba819a3 2020-07-23 stsp static const struct got_error *
1357 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1358 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1359 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1360 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1361 bd6aa359 2020-07-23 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1362 bd6aa359 2020-07-23 stsp void *progress_arg)
1363 8ba819a3 2020-07-23 stsp {
1364 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1365 507dc3bb 2018-12-29 stsp int fd = -1;
1366 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1367 507dc3bb 2018-12-29 stsp int update = 0;
1368 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1369 8ba819a3 2020-07-23 stsp
1370 8ba819a3 2020-07-23 stsp if (S_ISLNK(te_mode))
1371 8ba819a3 2020-07-23 stsp return install_symlink(worktree, ondisk_path, path, te_mode,
1372 8ba819a3 2020-07-23 stsp st_mode, blob, restoring_missing_file,
1373 8ba819a3 2020-07-23 stsp reverting_versioned_file, repo, progress_cb, progress_arg);
1374 9d31a1d8 2018-03-11 stsp
1375 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1376 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1377 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1378 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1379 21908da4 2019-01-13 stsp char *parent = dirname(path);
1380 21908da4 2019-01-13 stsp if (parent == NULL)
1381 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1382 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1383 21908da4 2019-01-13 stsp if (err)
1384 21908da4 2019-01-13 stsp return err;
1385 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1386 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1387 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1388 21908da4 2019-01-13 stsp if (fd == -1)
1389 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1390 230a42bd 2019-05-11 jcs ondisk_path);
1391 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1392 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1393 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1394 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1395 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1396 507dc3bb 2018-12-29 stsp goto done;
1397 d70b8e30 2018-12-27 stsp } else {
1398 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1399 507dc3bb 2018-12-29 stsp ondisk_path);
1400 507dc3bb 2018-12-29 stsp if (err)
1401 507dc3bb 2018-12-29 stsp goto done;
1402 507dc3bb 2018-12-29 stsp update = 1;
1403 9d31a1d8 2018-03-11 stsp }
1404 507dc3bb 2018-12-29 stsp } else
1405 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1406 9d31a1d8 2018-03-11 stsp }
1407 9d31a1d8 2018-03-11 stsp
1408 bd6aa359 2020-07-23 stsp if (progress_cb) {
1409 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1410 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1411 bd6aa359 2020-07-23 stsp path);
1412 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1413 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1414 bd6aa359 2020-07-23 stsp path);
1415 bd6aa359 2020-07-23 stsp else
1416 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1417 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1418 bd6aa359 2020-07-23 stsp if (err)
1419 bd6aa359 2020-07-23 stsp goto done;
1420 bd6aa359 2020-07-23 stsp }
1421 d7b62c98 2018-12-27 stsp
1422 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1423 9d31a1d8 2018-03-11 stsp do {
1424 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1425 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1426 9d31a1d8 2018-03-11 stsp if (err)
1427 9d31a1d8 2018-03-11 stsp break;
1428 9d31a1d8 2018-03-11 stsp if (len > 0) {
1429 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1430 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1431 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1433 b87c6f83 2018-12-24 stsp goto done;
1434 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1435 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1436 b87c6f83 2018-12-24 stsp goto done;
1437 9d31a1d8 2018-03-11 stsp }
1438 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1439 9d31a1d8 2018-03-11 stsp }
1440 9d31a1d8 2018-03-11 stsp } while (len != 0);
1441 9d31a1d8 2018-03-11 stsp
1442 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1444 816dc654 2019-02-16 stsp goto done;
1445 816dc654 2019-02-16 stsp }
1446 9d31a1d8 2018-03-11 stsp
1447 507dc3bb 2018-12-29 stsp if (update) {
1448 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1449 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1450 230a42bd 2019-05-11 jcs ondisk_path);
1451 2a57020b 2019-02-20 stsp unlink(tmppath);
1452 68ed9ba5 2019-02-10 stsp goto done;
1453 68ed9ba5 2019-02-10 stsp }
1454 68ed9ba5 2019-02-10 stsp }
1455 ba8a0d4d 2019-02-10 stsp
1456 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1457 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1458 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1459 1ebedb77 2019-10-19 stsp goto done;
1460 507dc3bb 2018-12-29 stsp }
1461 507dc3bb 2018-12-29 stsp
1462 9d31a1d8 2018-03-11 stsp done:
1463 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1464 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1465 507dc3bb 2018-12-29 stsp free(tmppath);
1466 6353ad76 2019-02-08 stsp return err;
1467 6353ad76 2019-02-08 stsp }
1468 6353ad76 2019-02-08 stsp
1469 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1470 6353ad76 2019-02-08 stsp static const struct got_error *
1471 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1472 7154f6ce 2019-03-27 stsp {
1473 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1474 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1475 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1476 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1477 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1478 7154f6ce 2019-03-27 stsp };
1479 7154f6ce 2019-03-27 stsp int i = 0;
1480 7154f6ce 2019-03-27 stsp char *line;
1481 7154f6ce 2019-03-27 stsp size_t len;
1482 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1483 7154f6ce 2019-03-27 stsp
1484 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1485 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1486 7154f6ce 2019-03-27 stsp if (line == NULL) {
1487 7154f6ce 2019-03-27 stsp if (feof(f))
1488 7154f6ce 2019-03-27 stsp break;
1489 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1490 7154f6ce 2019-03-27 stsp break;
1491 7154f6ce 2019-03-27 stsp }
1492 7154f6ce 2019-03-27 stsp
1493 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1494 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1495 19332e6d 2019-05-13 stsp == 0)
1496 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1497 7154f6ce 2019-03-27 stsp else
1498 7154f6ce 2019-03-27 stsp i++;
1499 7154f6ce 2019-03-27 stsp }
1500 7154f6ce 2019-03-27 stsp }
1501 7154f6ce 2019-03-27 stsp
1502 7154f6ce 2019-03-27 stsp return err;
1503 e2b1e152 2019-07-13 stsp }
1504 e2b1e152 2019-07-13 stsp
1505 e2b1e152 2019-07-13 stsp static int
1506 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1507 1ebedb77 2019-10-19 stsp {
1508 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1509 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1510 1ebedb77 2019-10-19 stsp }
1511 1ebedb77 2019-10-19 stsp
1512 1ebedb77 2019-10-19 stsp static int
1513 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1514 e2b1e152 2019-07-13 stsp {
1515 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1516 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1517 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1518 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1519 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1520 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1521 c363b2c1 2019-08-03 stsp }
1522 c363b2c1 2019-08-03 stsp
1523 c363b2c1 2019-08-03 stsp static unsigned char
1524 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1525 c363b2c1 2019-08-03 stsp {
1526 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1527 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1528 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1529 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1530 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1531 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1532 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1533 c363b2c1 2019-08-03 stsp default:
1534 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1535 a919d5c4 2020-07-23 stsp }
1536 a919d5c4 2020-07-23 stsp }
1537 a919d5c4 2020-07-23 stsp
1538 a919d5c4 2020-07-23 stsp static const struct got_error *
1539 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1540 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1541 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1542 a919d5c4 2020-07-23 stsp {
1543 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1544 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1545 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1546 a919d5c4 2020-07-23 stsp ssize_t elen;
1547 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1548 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1550 a919d5c4 2020-07-23 stsp
1551 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1552 a919d5c4 2020-07-23 stsp
1553 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1554 a919d5c4 2020-07-23 stsp do {
1555 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1556 a919d5c4 2020-07-23 stsp if (err)
1557 a919d5c4 2020-07-23 stsp return err;
1558 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1559 a919d5c4 2020-07-23 stsp /*
1560 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1561 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1562 a919d5c4 2020-07-23 stsp */
1563 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1564 a919d5c4 2020-07-23 stsp }
1565 a919d5c4 2020-07-23 stsp if (len > 0) {
1566 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1567 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1568 a919d5c4 2020-07-23 stsp len - hdrlen);
1569 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1570 a919d5c4 2020-07-23 stsp hdrlen = 0;
1571 a919d5c4 2020-07-23 stsp }
1572 a919d5c4 2020-07-23 stsp } while (len != 0);
1573 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1574 a919d5c4 2020-07-23 stsp
1575 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1576 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1577 a919d5c4 2020-07-23 stsp if (elen == -1)
1578 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1579 a919d5c4 2020-07-23 stsp } else {
1580 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1581 a919d5c4 2020-07-23 stsp if (elen == -1)
1582 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1583 c363b2c1 2019-08-03 stsp }
1584 a919d5c4 2020-07-23 stsp
1585 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1586 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1587 a919d5c4 2020-07-23 stsp
1588 a919d5c4 2020-07-23 stsp return NULL;
1589 7154f6ce 2019-03-27 stsp }
1590 7154f6ce 2019-03-27 stsp
1591 7154f6ce 2019-03-27 stsp static const struct got_error *
1592 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1593 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1594 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1595 6353ad76 2019-02-08 stsp {
1596 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1597 6353ad76 2019-02-08 stsp struct got_object_id id;
1598 6353ad76 2019-02-08 stsp size_t hdrlen;
1599 1338848f 2019-12-13 stsp int fd = -1;
1600 6353ad76 2019-02-08 stsp FILE *f = NULL;
1601 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1602 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1603 6353ad76 2019-02-08 stsp size_t flen, blen;
1604 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1605 6353ad76 2019-02-08 stsp
1606 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1607 6353ad76 2019-02-08 stsp
1608 7f91a133 2019-12-13 stsp /*
1609 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1610 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1611 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1612 7f91a133 2019-12-13 stsp */
1613 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1614 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1615 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1616 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1617 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1618 882ef1b9 2019-12-13 stsp else
1619 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1620 882ef1b9 2019-12-13 stsp goto done;
1621 882ef1b9 2019-12-13 stsp }
1622 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1623 3d35a492 2019-12-13 stsp goto done;
1624 3d35a492 2019-12-13 stsp }
1625 7f91a133 2019-12-13 stsp } else {
1626 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1627 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1628 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1629 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1630 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1631 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1632 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1633 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1634 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1635 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1636 3d35a492 2019-12-13 stsp else
1637 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1638 3d35a492 2019-12-13 stsp goto done;
1639 3d35a492 2019-12-13 stsp }
1640 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1641 1338848f 2019-12-13 stsp goto done;
1642 a378724f 2019-02-10 stsp }
1643 a378724f 2019-02-10 stsp }
1644 6353ad76 2019-02-08 stsp
1645 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1646 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1647 1338848f 2019-12-13 stsp goto done;
1648 3f148bc6 2019-07-27 stsp }
1649 efdd40df 2019-07-27 stsp
1650 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1651 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1652 1338848f 2019-12-13 stsp goto done;
1653 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1654 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1655 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1656 1338848f 2019-12-13 stsp goto done;
1657 d00136be 2019-03-26 stsp }
1658 b8f41171 2019-02-10 stsp
1659 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1660 1338848f 2019-12-13 stsp goto done;
1661 6353ad76 2019-02-08 stsp
1662 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1663 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1664 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1665 c363b2c1 2019-08-03 stsp else
1666 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1667 c363b2c1 2019-08-03 stsp
1668 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1669 6353ad76 2019-02-08 stsp if (err)
1670 1338848f 2019-12-13 stsp goto done;
1671 6353ad76 2019-02-08 stsp
1672 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1673 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1674 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1675 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1676 a919d5c4 2020-07-23 stsp goto done;
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1679 a919d5c4 2020-07-23 stsp de_name, blob);
1680 a919d5c4 2020-07-23 stsp goto done;
1681 a919d5c4 2020-07-23 stsp }
1682 a919d5c4 2020-07-23 stsp
1683 a919d5c4 2020-07-23 stsp
1684 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1685 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1686 ab0d4361 2019-12-13 stsp if (fd == -1) {
1687 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1688 ab0d4361 2019-12-13 stsp goto done;
1689 ab0d4361 2019-12-13 stsp }
1690 3d35a492 2019-12-13 stsp }
1691 3d35a492 2019-12-13 stsp
1692 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1693 6353ad76 2019-02-08 stsp if (f == NULL) {
1694 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1695 6353ad76 2019-02-08 stsp goto done;
1696 6353ad76 2019-02-08 stsp }
1697 1338848f 2019-12-13 stsp fd = -1;
1698 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1699 656b1f76 2019-05-11 jcs for (;;) {
1700 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1701 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1702 6353ad76 2019-02-08 stsp if (err)
1703 7154f6ce 2019-03-27 stsp goto done;
1704 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1705 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1706 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1707 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1708 7154f6ce 2019-03-27 stsp goto done;
1709 80c5c120 2019-02-19 stsp }
1710 6353ad76 2019-02-08 stsp if (blen == 0) {
1711 6353ad76 2019-02-08 stsp if (flen != 0)
1712 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1713 6353ad76 2019-02-08 stsp break;
1714 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1715 6353ad76 2019-02-08 stsp if (blen != 0)
1716 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1717 6353ad76 2019-02-08 stsp break;
1718 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1719 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1720 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1721 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1722 6353ad76 2019-02-08 stsp break;
1723 6353ad76 2019-02-08 stsp }
1724 6353ad76 2019-02-08 stsp } else {
1725 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1726 6353ad76 2019-02-08 stsp break;
1727 6353ad76 2019-02-08 stsp }
1728 6353ad76 2019-02-08 stsp hdrlen = 0;
1729 6353ad76 2019-02-08 stsp }
1730 7154f6ce 2019-03-27 stsp
1731 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1732 7154f6ce 2019-03-27 stsp rewind(f);
1733 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1734 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1735 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1736 6353ad76 2019-02-08 stsp done:
1737 6353ad76 2019-02-08 stsp if (blob)
1738 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1739 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1740 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1741 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1742 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1743 9d31a1d8 2018-03-11 stsp return err;
1744 e2b1e152 2019-07-13 stsp }
1745 e2b1e152 2019-07-13 stsp
1746 e2b1e152 2019-07-13 stsp /*
1747 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1748 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1749 e2b1e152 2019-07-13 stsp */
1750 e2b1e152 2019-07-13 stsp static const struct got_error *
1751 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1752 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1753 e2b1e152 2019-07-13 stsp {
1754 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1755 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1756 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1757 e2b1e152 2019-07-13 stsp
1758 e2b1e152 2019-07-13 stsp return NULL;
1759 9d31a1d8 2018-03-11 stsp }
1760 9d31a1d8 2018-03-11 stsp
1761 9d31a1d8 2018-03-11 stsp static const struct got_error *
1762 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1763 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1764 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1765 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1766 0584f854 2019-04-06 stsp void *progress_arg)
1767 9d31a1d8 2018-03-11 stsp {
1768 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1769 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1770 6353ad76 2019-02-08 stsp char *ondisk_path;
1771 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1772 b8f41171 2019-02-10 stsp struct stat sb;
1773 9d31a1d8 2018-03-11 stsp
1774 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1776 6353ad76 2019-02-08 stsp
1777 abb4604f 2019-07-27 stsp if (ie) {
1778 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1779 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1780 a76c42e6 2019-08-03 stsp goto done;
1781 a76c42e6 2019-08-03 stsp }
1782 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1783 7f91a133 2019-12-13 stsp repo);
1784 abb4604f 2019-07-27 stsp if (err)
1785 abb4604f 2019-07-27 stsp goto done;
1786 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1787 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1788 abb4604f 2019-07-27 stsp } else
1789 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1790 6353ad76 2019-02-08 stsp
1791 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1792 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1793 b8f41171 2019-02-10 stsp goto done;
1794 b8f41171 2019-02-10 stsp }
1795 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1796 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1797 5036ab18 2020-04-18 stsp path);
1798 5036ab18 2020-04-18 stsp goto done;
1799 5036ab18 2020-04-18 stsp }
1800 b8f41171 2019-02-10 stsp
1801 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1802 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1803 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1804 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1805 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1806 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1807 e2b1e152 2019-07-13 stsp if (err)
1808 e2b1e152 2019-07-13 stsp goto done;
1809 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1810 b8f41171 2019-02-10 stsp path);
1811 6353ad76 2019-02-08 stsp goto done;
1812 a378724f 2019-02-10 stsp }
1813 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1814 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1815 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1816 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1817 b8f41171 2019-02-10 stsp goto done;
1818 e2b1e152 2019-07-13 stsp }
1819 9d31a1d8 2018-03-11 stsp }
1820 9d31a1d8 2018-03-11 stsp
1821 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1822 8da9e5f4 2019-01-12 stsp if (err)
1823 6353ad76 2019-02-08 stsp goto done;
1824 512f0d0e 2019-01-02 stsp
1825 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1826 234035bc 2019-06-01 stsp int update_timestamps;
1827 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1828 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1829 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1830 234035bc 2019-06-01 stsp struct got_object_id id2;
1831 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1832 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1833 234035bc 2019-06-01 stsp if (err)
1834 f69721c3 2019-10-21 stsp goto done;
1835 f69721c3 2019-10-21 stsp }
1836 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1837 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1838 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1839 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1840 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1841 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1842 f69721c3 2019-10-21 stsp goto done;
1843 f69721c3 2019-10-21 stsp }
1844 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1845 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1846 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1847 234035bc 2019-06-01 stsp goto done;
1848 f69721c3 2019-10-21 stsp }
1849 234035bc 2019-06-01 stsp }
1850 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1851 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1852 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1853 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1854 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1855 993e2a1b 2020-07-23 stsp } else {
1856 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1857 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1858 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1859 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1860 993e2a1b 2020-07-23 stsp }
1861 f69721c3 2019-10-21 stsp free(label_orig);
1862 234035bc 2019-06-01 stsp if (blob2)
1863 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1864 909d120e 2019-09-22 stsp if (err)
1865 909d120e 2019-09-22 stsp goto done;
1866 234035bc 2019-06-01 stsp /*
1867 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1868 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1869 234035bc 2019-06-01 stsp * unmodified files again.
1870 234035bc 2019-06-01 stsp */
1871 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1872 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1873 234035bc 2019-06-01 stsp update_timestamps);
1874 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1875 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1876 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1877 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1878 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1879 1ee397ad 2019-07-12 stsp if (err)
1880 1ee397ad 2019-07-12 stsp goto done;
1881 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1882 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1883 13d9040b 2019-03-27 stsp if (err)
1884 13d9040b 2019-03-27 stsp goto done;
1885 13d9040b 2019-03-27 stsp } else {
1886 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1887 bd6aa359 2020-07-23 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1888 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1889 13d9040b 2019-03-27 stsp if (err)
1890 13d9040b 2019-03-27 stsp goto done;
1891 054041d0 2020-06-24 stsp if (ie) {
1892 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1893 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1894 054041d0 2020-06-24 stsp } else {
1895 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1896 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1897 054041d0 2020-06-24 stsp &blob->id);
1898 054041d0 2020-06-24 stsp }
1899 13d9040b 2019-03-27 stsp if (err)
1900 13d9040b 2019-03-27 stsp goto done;
1901 13d9040b 2019-03-27 stsp }
1902 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1903 6353ad76 2019-02-08 stsp done:
1904 6353ad76 2019-02-08 stsp free(ondisk_path);
1905 8da9e5f4 2019-01-12 stsp return err;
1906 90285c3b 2019-01-08 stsp }
1907 90285c3b 2019-01-08 stsp
1908 90285c3b 2019-01-08 stsp static const struct got_error *
1909 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1910 90285c3b 2019-01-08 stsp {
1911 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1912 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1913 90285c3b 2019-01-08 stsp
1914 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1915 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1916 90285c3b 2019-01-08 stsp
1917 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1918 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1919 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1920 c97665ae 2019-01-13 stsp } else {
1921 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1922 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1923 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1924 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1925 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1926 230a42bd 2019-05-11 jcs parent);
1927 90285c3b 2019-01-08 stsp break;
1928 90285c3b 2019-01-08 stsp }
1929 90285c3b 2019-01-08 stsp parent = dirname(parent);
1930 90285c3b 2019-01-08 stsp }
1931 90285c3b 2019-01-08 stsp }
1932 90285c3b 2019-01-08 stsp free(ondisk_path);
1933 90285c3b 2019-01-08 stsp return err;
1934 512f0d0e 2019-01-02 stsp }
1935 512f0d0e 2019-01-02 stsp
1936 708d8e67 2019-03-27 stsp static const struct got_error *
1937 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1938 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1939 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1940 708d8e67 2019-03-27 stsp {
1941 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1942 708d8e67 2019-03-27 stsp unsigned char status;
1943 708d8e67 2019-03-27 stsp struct stat sb;
1944 708d8e67 2019-03-27 stsp char *ondisk_path;
1945 708d8e67 2019-03-27 stsp
1946 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1947 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1948 a76c42e6 2019-08-03 stsp
1949 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1950 708d8e67 2019-03-27 stsp == -1)
1951 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1952 708d8e67 2019-03-27 stsp
1953 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1954 708d8e67 2019-03-27 stsp if (err)
1955 5a58a424 2020-06-23 stsp goto done;
1956 708d8e67 2019-03-27 stsp
1957 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1958 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
1959 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1960 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
1961 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
1962 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
1963 993e2a1b 2020-07-23 stsp goto done;
1964 993e2a1b 2020-07-23 stsp }
1965 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1966 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
1967 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
1968 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
1969 993e2a1b 2020-07-23 stsp if (err)
1970 993e2a1b 2020-07-23 stsp goto done;
1971 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1972 993e2a1b 2020-07-23 stsp ie->path);
1973 993e2a1b 2020-07-23 stsp goto done;
1974 993e2a1b 2020-07-23 stsp }
1975 993e2a1b 2020-07-23 stsp
1976 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1977 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1978 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1979 1ee397ad 2019-07-12 stsp if (err)
1980 5a58a424 2020-06-23 stsp goto done;
1981 fc6346c4 2019-03-27 stsp /*
1982 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1983 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1984 fc6346c4 2019-03-27 stsp */
1985 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1986 fc6346c4 2019-03-27 stsp 0);
1987 fc6346c4 2019-03-27 stsp } else {
1988 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1989 1ee397ad 2019-07-12 stsp if (err)
1990 5a58a424 2020-06-23 stsp goto done;
1991 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1992 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1993 fc6346c4 2019-03-27 stsp if (err)
1994 5a58a424 2020-06-23 stsp goto done;
1995 fc6346c4 2019-03-27 stsp }
1996 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1997 708d8e67 2019-03-27 stsp }
1998 5a58a424 2020-06-23 stsp done:
1999 5a58a424 2020-06-23 stsp free(ondisk_path);
2000 fc6346c4 2019-03-27 stsp return err;
2001 708d8e67 2019-03-27 stsp }
2002 708d8e67 2019-03-27 stsp
2003 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2004 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2005 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2006 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2007 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2008 8da9e5f4 2019-01-12 stsp void *progress_arg;
2009 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2010 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2011 8da9e5f4 2019-01-12 stsp };
2012 8da9e5f4 2019-01-12 stsp
2013 512f0d0e 2019-01-02 stsp static const struct got_error *
2014 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2015 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2016 512f0d0e 2019-01-02 stsp {
2017 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2018 0584f854 2019-04-06 stsp
2019 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2020 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2021 512f0d0e 2019-01-02 stsp
2022 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2023 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2024 8da9e5f4 2019-01-12 stsp }
2025 512f0d0e 2019-01-02 stsp
2026 8da9e5f4 2019-01-12 stsp static const struct got_error *
2027 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2028 8da9e5f4 2019-01-12 stsp {
2029 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2030 7a9df742 2019-01-08 stsp
2031 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2032 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2033 0584f854 2019-04-06 stsp
2034 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2035 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2036 9d31a1d8 2018-03-11 stsp }
2037 9d31a1d8 2018-03-11 stsp
2038 9d31a1d8 2018-03-11 stsp static const struct got_error *
2039 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2040 9d31a1d8 2018-03-11 stsp {
2041 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2042 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2043 8da9e5f4 2019-01-12 stsp char *path;
2044 9d31a1d8 2018-03-11 stsp
2045 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2046 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2047 0584f854 2019-04-06 stsp
2048 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2049 63c5ca5d 2019-08-24 stsp return NULL;
2050 63c5ca5d 2019-08-24 stsp
2051 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2052 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2053 8da9e5f4 2019-01-12 stsp == -1)
2054 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2055 9d31a1d8 2018-03-11 stsp
2056 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2057 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2058 8da9e5f4 2019-01-12 stsp else
2059 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2060 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2061 9d31a1d8 2018-03-11 stsp
2062 8da9e5f4 2019-01-12 stsp free(path);
2063 0cd1c46a 2019-03-11 stsp return err;
2064 0cd1c46a 2019-03-11 stsp }
2065 0cd1c46a 2019-03-11 stsp
2066 818c7501 2019-07-11 stsp static const struct got_error *
2067 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2068 0cd1c46a 2019-03-11 stsp {
2069 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2070 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2071 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2072 0cd1c46a 2019-03-11 stsp
2073 517bab73 2019-03-11 stsp *refname = NULL;
2074 517bab73 2019-03-11 stsp
2075 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2076 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2077 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2078 0cd1c46a 2019-03-11 stsp
2079 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2080 0647c563 2019-03-11 stsp == -1) {
2081 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2082 0647c563 2019-03-11 stsp *refname = NULL;
2083 0cd1c46a 2019-03-11 stsp }
2084 517bab73 2019-03-11 stsp free(uuidstr);
2085 517bab73 2019-03-11 stsp return err;
2086 517bab73 2019-03-11 stsp }
2087 517bab73 2019-03-11 stsp
2088 818c7501 2019-07-11 stsp const struct got_error *
2089 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2090 818c7501 2019-07-11 stsp {
2091 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2092 818c7501 2019-07-11 stsp }
2093 818c7501 2019-07-11 stsp
2094 818c7501 2019-07-11 stsp static const struct got_error *
2095 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2096 818c7501 2019-07-11 stsp {
2097 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2098 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2099 818c7501 2019-07-11 stsp }
2100 818c7501 2019-07-11 stsp
2101 818c7501 2019-07-11 stsp static const struct got_error *
2102 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2103 818c7501 2019-07-11 stsp {
2104 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2105 818c7501 2019-07-11 stsp }
2106 818c7501 2019-07-11 stsp
2107 818c7501 2019-07-11 stsp static const struct got_error *
2108 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2109 818c7501 2019-07-11 stsp {
2110 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2111 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2112 818c7501 2019-07-11 stsp }
2113 818c7501 2019-07-11 stsp
2114 818c7501 2019-07-11 stsp static const struct got_error *
2115 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2116 818c7501 2019-07-11 stsp {
2117 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2118 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2119 0ebf8283 2019-07-24 stsp }
2120 0ebf8283 2019-07-24 stsp
2121 0ebf8283 2019-07-24 stsp static const struct got_error *
2122 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2123 0ebf8283 2019-07-24 stsp {
2124 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2125 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2126 0ebf8283 2019-07-24 stsp }
2127 0ebf8283 2019-07-24 stsp
2128 0ebf8283 2019-07-24 stsp static const struct got_error *
2129 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2130 0ebf8283 2019-07-24 stsp {
2131 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2132 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2133 0ebf8283 2019-07-24 stsp }
2134 0ebf8283 2019-07-24 stsp
2135 0ebf8283 2019-07-24 stsp static const struct got_error *
2136 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2137 0ebf8283 2019-07-24 stsp {
2138 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2139 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2140 818c7501 2019-07-11 stsp }
2141 818c7501 2019-07-11 stsp
2142 0ebf8283 2019-07-24 stsp static const struct got_error *
2143 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2144 0ebf8283 2019-07-24 stsp {
2145 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2146 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2147 0ebf8283 2019-07-24 stsp }
2148 818c7501 2019-07-11 stsp
2149 0ebf8283 2019-07-24 stsp const struct got_error *
2150 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2151 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2152 0ebf8283 2019-07-24 stsp {
2153 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2154 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2155 0ebf8283 2019-07-24 stsp *path = NULL;
2156 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2157 0ebf8283 2019-07-24 stsp }
2158 0ebf8283 2019-07-24 stsp return NULL;
2159 0ebf8283 2019-07-24 stsp }
2160 0ebf8283 2019-07-24 stsp
2161 517bab73 2019-03-11 stsp /*
2162 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2163 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2164 517bab73 2019-03-11 stsp */
2165 517bab73 2019-03-11 stsp static const struct got_error *
2166 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2167 517bab73 2019-03-11 stsp {
2168 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2169 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2170 517bab73 2019-03-11 stsp char *refname;
2171 517bab73 2019-03-11 stsp
2172 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2173 517bab73 2019-03-11 stsp if (err)
2174 517bab73 2019-03-11 stsp return err;
2175 0cd1c46a 2019-03-11 stsp
2176 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2177 0cd1c46a 2019-03-11 stsp if (err)
2178 0cd1c46a 2019-03-11 stsp goto done;
2179 0cd1c46a 2019-03-11 stsp
2180 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2181 0cd1c46a 2019-03-11 stsp done:
2182 0cd1c46a 2019-03-11 stsp free(refname);
2183 0cd1c46a 2019-03-11 stsp if (ref)
2184 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2185 3e3a69f1 2019-07-25 stsp return err;
2186 3e3a69f1 2019-07-25 stsp }
2187 3e3a69f1 2019-07-25 stsp
2188 3e3a69f1 2019-07-25 stsp static const struct got_error *
2189 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2190 3e3a69f1 2019-07-25 stsp {
2191 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2192 3e3a69f1 2019-07-25 stsp
2193 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2194 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2195 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2196 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2197 3e3a69f1 2019-07-25 stsp }
2198 9d31a1d8 2018-03-11 stsp return err;
2199 9d31a1d8 2018-03-11 stsp }
2200 9d31a1d8 2018-03-11 stsp
2201 3e3a69f1 2019-07-25 stsp
2202 ebf99748 2019-05-09 stsp static const struct got_error *
2203 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2204 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2205 ebf99748 2019-05-09 stsp {
2206 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2207 ebf99748 2019-05-09 stsp FILE *index = NULL;
2208 0cd1c46a 2019-03-11 stsp
2209 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2210 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2211 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2212 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2213 ebf99748 2019-05-09 stsp
2214 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2215 3e3a69f1 2019-07-25 stsp if (err)
2216 ebf99748 2019-05-09 stsp goto done;
2217 ebf99748 2019-05-09 stsp
2218 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2219 ebf99748 2019-05-09 stsp if (index == NULL) {
2220 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2221 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2222 ebf99748 2019-05-09 stsp } else {
2223 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2224 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2225 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2226 ebf99748 2019-05-09 stsp }
2227 ebf99748 2019-05-09 stsp done:
2228 ebf99748 2019-05-09 stsp if (err) {
2229 ebf99748 2019-05-09 stsp free(*fileindex_path);
2230 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2231 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2232 ebf99748 2019-05-09 stsp *fileindex = NULL;
2233 ebf99748 2019-05-09 stsp }
2234 ebf99748 2019-05-09 stsp return err;
2235 ebf99748 2019-05-09 stsp }
2236 c932eeeb 2019-05-22 stsp
2237 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2238 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2239 c932eeeb 2019-05-22 stsp const char *path;
2240 c932eeeb 2019-05-22 stsp size_t path_len;
2241 c932eeeb 2019-05-22 stsp const char *entry_name;
2242 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2243 a484d721 2019-06-10 stsp void *progress_arg;
2244 c932eeeb 2019-05-22 stsp };
2245 ebf99748 2019-05-09 stsp
2246 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2247 c932eeeb 2019-05-22 stsp static const struct got_error *
2248 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2249 c932eeeb 2019-05-22 stsp {
2250 1ee397ad 2019-07-12 stsp const struct got_error *err;
2251 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2252 c932eeeb 2019-05-22 stsp
2253 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2254 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2255 c932eeeb 2019-05-22 stsp return NULL;
2256 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2257 102ff934 2019-06-11 stsp return NULL;
2258 102ff934 2019-06-11 stsp
2259 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2260 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2261 c932eeeb 2019-05-22 stsp return NULL;
2262 c932eeeb 2019-05-22 stsp
2263 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2264 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2265 edd02c5e 2019-07-11 stsp ie->path);
2266 1ee397ad 2019-07-12 stsp if (err)
2267 1ee397ad 2019-07-12 stsp return err;
2268 1ee397ad 2019-07-12 stsp }
2269 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2270 c932eeeb 2019-05-22 stsp return NULL;
2271 9c6338c4 2019-06-02 stsp }
2272 9c6338c4 2019-06-02 stsp
2273 9c6338c4 2019-06-02 stsp static const struct got_error *
2274 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2275 9c6338c4 2019-06-02 stsp {
2276 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2277 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2278 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2279 867630bb 2020-01-17 stsp struct timespec timeout;
2280 9c6338c4 2019-06-02 stsp
2281 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2282 9c6338c4 2019-06-02 stsp fileindex_path);
2283 9c6338c4 2019-06-02 stsp if (err)
2284 9c6338c4 2019-06-02 stsp goto done;
2285 9c6338c4 2019-06-02 stsp
2286 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2287 9c6338c4 2019-06-02 stsp if (err)
2288 9c6338c4 2019-06-02 stsp goto done;
2289 9c6338c4 2019-06-02 stsp
2290 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2291 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2292 9c6338c4 2019-06-02 stsp fileindex_path);
2293 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2294 9c6338c4 2019-06-02 stsp }
2295 867630bb 2020-01-17 stsp
2296 867630bb 2020-01-17 stsp /*
2297 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2298 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2299 867630bb 2020-01-17 stsp * was recorded in the file index.
2300 867630bb 2020-01-17 stsp */
2301 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2302 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2303 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2304 9c6338c4 2019-06-02 stsp done:
2305 9c6338c4 2019-06-02 stsp if (new_index)
2306 9c6338c4 2019-06-02 stsp fclose(new_index);
2307 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2308 9c6338c4 2019-06-02 stsp return err;
2309 c932eeeb 2019-05-22 stsp }
2310 6ced4176 2019-07-12 stsp
2311 6ced4176 2019-07-12 stsp static const struct got_error *
2312 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2313 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2314 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2315 6ced4176 2019-07-12 stsp {
2316 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2317 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2318 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2319 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2320 6ced4176 2019-07-12 stsp
2321 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2322 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2323 6ced4176 2019-07-12 stsp *tree_id = NULL;
2324 6ced4176 2019-07-12 stsp
2325 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2326 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2327 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2328 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2329 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2330 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2331 6ced4176 2019-07-12 stsp goto done;
2332 6ced4176 2019-07-12 stsp }
2333 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2334 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2335 6ced4176 2019-07-12 stsp if (err)
2336 6ced4176 2019-07-12 stsp goto done;
2337 6ced4176 2019-07-12 stsp return NULL;
2338 6ced4176 2019-07-12 stsp }
2339 6ced4176 2019-07-12 stsp
2340 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2341 6ced4176 2019-07-12 stsp
2342 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2343 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2344 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2345 6ced4176 2019-07-12 stsp goto done;
2346 6ced4176 2019-07-12 stsp }
2347 6ced4176 2019-07-12 stsp
2348 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2349 6ced4176 2019-07-12 stsp in_repo_path);
2350 6ced4176 2019-07-12 stsp if (err)
2351 6ced4176 2019-07-12 stsp goto done;
2352 6ced4176 2019-07-12 stsp
2353 6ced4176 2019-07-12 stsp free(in_repo_path);
2354 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2355 6ced4176 2019-07-12 stsp
2356 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2357 6ced4176 2019-07-12 stsp if (err)
2358 6ced4176 2019-07-12 stsp goto done;
2359 c932eeeb 2019-05-22 stsp
2360 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2361 6ced4176 2019-07-12 stsp /* Check out a single file. */
2362 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2363 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2364 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2365 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2366 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2367 6ced4176 2019-07-12 stsp goto done;
2368 6ced4176 2019-07-12 stsp }
2369 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2370 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2371 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2372 6ced4176 2019-07-12 stsp goto done;
2373 6ced4176 2019-07-12 stsp }
2374 6ced4176 2019-07-12 stsp } else {
2375 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2376 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2377 6ced4176 2019-07-12 stsp if (err)
2378 6ced4176 2019-07-12 stsp return err;
2379 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2380 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2381 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2382 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2383 6ced4176 2019-07-12 stsp goto done;
2384 6ced4176 2019-07-12 stsp }
2385 6ced4176 2019-07-12 stsp }
2386 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2387 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2388 6ced4176 2019-07-12 stsp } else {
2389 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2390 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2391 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2392 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2393 6ced4176 2019-07-12 stsp goto done;
2394 6ced4176 2019-07-12 stsp }
2395 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2396 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2397 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2398 6ced4176 2019-07-12 stsp goto done;
2399 6ced4176 2019-07-12 stsp }
2400 6ced4176 2019-07-12 stsp }
2401 6ced4176 2019-07-12 stsp done:
2402 6ced4176 2019-07-12 stsp free(id);
2403 6ced4176 2019-07-12 stsp free(in_repo_path);
2404 6ced4176 2019-07-12 stsp if (err) {
2405 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2406 6ced4176 2019-07-12 stsp free(*tree_relpath);
2407 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2408 6ced4176 2019-07-12 stsp free(*tree_id);
2409 6ced4176 2019-07-12 stsp *tree_id = NULL;
2410 6ced4176 2019-07-12 stsp }
2411 07ed472d 2019-07-12 stsp return err;
2412 07ed472d 2019-07-12 stsp }
2413 07ed472d 2019-07-12 stsp
2414 07ed472d 2019-07-12 stsp static const struct got_error *
2415 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2416 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2417 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2418 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2419 07ed472d 2019-07-12 stsp {
2420 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2421 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2422 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2423 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2424 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2425 07ed472d 2019-07-12 stsp
2426 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2427 7f47418f 2019-12-20 stsp if (err) {
2428 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2429 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2430 7f47418f 2019-12-20 stsp goto done;
2431 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2432 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2433 7f47418f 2019-12-20 stsp if (err)
2434 7f47418f 2019-12-20 stsp return err;
2435 7f47418f 2019-12-20 stsp }
2436 07ed472d 2019-07-12 stsp
2437 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2438 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2439 07ed472d 2019-07-12 stsp if (err)
2440 07ed472d 2019-07-12 stsp goto done;
2441 07ed472d 2019-07-12 stsp
2442 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2443 07ed472d 2019-07-12 stsp if (err)
2444 07ed472d 2019-07-12 stsp goto done;
2445 07ed472d 2019-07-12 stsp
2446 07ed472d 2019-07-12 stsp if (entry_name &&
2447 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2448 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2449 07ed472d 2019-07-12 stsp goto done;
2450 07ed472d 2019-07-12 stsp }
2451 07ed472d 2019-07-12 stsp
2452 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2453 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2454 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2455 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2456 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2457 07ed472d 2019-07-12 stsp arg.repo = repo;
2458 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2459 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2460 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2461 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2462 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2463 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2464 07ed472d 2019-07-12 stsp done:
2465 07ed472d 2019-07-12 stsp if (tree)
2466 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2467 07ed472d 2019-07-12 stsp if (commit)
2468 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2469 6ced4176 2019-07-12 stsp return err;
2470 6ced4176 2019-07-12 stsp }
2471 6ced4176 2019-07-12 stsp
2472 9d31a1d8 2018-03-11 stsp const struct got_error *
2473 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2474 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2475 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2476 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2477 9d31a1d8 2018-03-11 stsp {
2478 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2480 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2481 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2482 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2483 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2484 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2485 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2486 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2487 f2ea84fa 2019-07-27 stsp int entry_type;
2488 f2ea84fa 2019-07-27 stsp char *relpath;
2489 f2ea84fa 2019-07-27 stsp char *entry_name;
2490 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2491 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2492 9d31a1d8 2018-03-11 stsp
2493 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2494 f2ea84fa 2019-07-27 stsp
2495 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2496 9d31a1d8 2018-03-11 stsp if (err)
2497 9d31a1d8 2018-03-11 stsp return err;
2498 93a30277 2018-12-24 stsp
2499 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2500 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2501 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2502 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2503 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2504 f2ea84fa 2019-07-27 stsp goto done;
2505 f2ea84fa 2019-07-27 stsp }
2506 6ced4176 2019-07-12 stsp
2507 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2508 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2509 f2ea84fa 2019-07-27 stsp if (err) {
2510 f2ea84fa 2019-07-27 stsp free(tpd);
2511 6ced4176 2019-07-12 stsp goto done;
2512 6ced4176 2019-07-12 stsp }
2513 f2ea84fa 2019-07-27 stsp
2514 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2515 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2516 f2ea84fa 2019-07-27 stsp if (err) {
2517 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2518 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2519 f2ea84fa 2019-07-27 stsp free(tpd);
2520 f2ea84fa 2019-07-27 stsp goto done;
2521 f2ea84fa 2019-07-27 stsp }
2522 f2ea84fa 2019-07-27 stsp } else
2523 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2524 f2ea84fa 2019-07-27 stsp
2525 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2526 6ced4176 2019-07-12 stsp }
2527 6ced4176 2019-07-12 stsp
2528 51514078 2018-12-25 stsp /*
2529 51514078 2018-12-25 stsp * Read the file index.
2530 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2531 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2532 51514078 2018-12-25 stsp */
2533 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2534 8da9e5f4 2019-01-12 stsp if (err)
2535 c4cdcb68 2019-04-03 stsp goto done;
2536 c4cdcb68 2019-04-03 stsp
2537 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2538 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2539 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2540 f2ea84fa 2019-07-27 stsp
2541 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2542 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2543 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2544 f2ea84fa 2019-07-27 stsp if (err)
2545 f2ea84fa 2019-07-27 stsp break;
2546 f2ea84fa 2019-07-27 stsp
2547 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2548 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2549 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2550 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2551 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2552 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2553 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2554 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2555 f2ea84fa 2019-07-27 stsp if (err)
2556 f2ea84fa 2019-07-27 stsp break;
2557 f2ea84fa 2019-07-27 stsp
2558 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2559 711d9cd9 2019-07-12 stsp }
2560 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2561 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2562 af12c6b9 2019-06-04 stsp err = sync_err;
2563 9d31a1d8 2018-03-11 stsp done:
2564 9c6338c4 2019-06-02 stsp free(fileindex_path);
2565 edfa7d7f 2018-09-11 stsp if (tree)
2566 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2567 9d31a1d8 2018-03-11 stsp if (commit)
2568 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2569 5ade8233 2019-07-12 stsp if (fileindex)
2570 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2571 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2572 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2573 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2574 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2575 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2576 f2ea84fa 2019-07-27 stsp free(tpd);
2577 f2ea84fa 2019-07-27 stsp }
2578 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2579 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2580 9d31a1d8 2018-03-11 stsp err = unlockerr;
2581 f8d1f275 2019-02-04 stsp return err;
2582 f8d1f275 2019-02-04 stsp }
2583 f8d1f275 2019-02-04 stsp
2584 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2585 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2586 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2587 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2588 234035bc 2019-06-01 stsp void *progress_arg;
2589 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2590 234035bc 2019-06-01 stsp void *cancel_arg;
2591 f69721c3 2019-10-21 stsp const char *label_orig;
2592 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2593 234035bc 2019-06-01 stsp };
2594 234035bc 2019-06-01 stsp
2595 234035bc 2019-06-01 stsp static const struct got_error *
2596 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2597 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2598 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2599 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2600 234035bc 2019-06-01 stsp {
2601 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2602 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2603 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2604 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2605 234035bc 2019-06-01 stsp struct stat sb;
2606 234035bc 2019-06-01 stsp unsigned char status;
2607 234035bc 2019-06-01 stsp int local_changes_subsumed;
2608 234035bc 2019-06-01 stsp
2609 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2610 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2611 d6c87207 2019-08-02 stsp strlen(path2));
2612 1ee397ad 2019-07-12 stsp if (ie == NULL)
2613 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2614 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2615 234035bc 2019-06-01 stsp
2616 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2617 234035bc 2019-06-01 stsp path2) == -1)
2618 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2619 234035bc 2019-06-01 stsp
2620 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2621 7f91a133 2019-12-13 stsp repo);
2622 234035bc 2019-06-01 stsp if (err)
2623 234035bc 2019-06-01 stsp goto done;
2624 234035bc 2019-06-01 stsp
2625 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2626 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2627 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2628 234035bc 2019-06-01 stsp goto done;
2629 234035bc 2019-06-01 stsp }
2630 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2631 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2632 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2633 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2634 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2635 234035bc 2019-06-01 stsp goto done;
2636 234035bc 2019-06-01 stsp }
2637 234035bc 2019-06-01 stsp
2638 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2639 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2640 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2641 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2642 af57b12a 2020-07-23 stsp a->progress_arg);
2643 af57b12a 2020-07-23 stsp } else {
2644 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2645 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2646 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2647 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2648 af57b12a 2020-07-23 stsp }
2649 234035bc 2019-06-01 stsp } else if (blob1) {
2650 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2651 d6c87207 2019-08-02 stsp strlen(path1));
2652 1ee397ad 2019-07-12 stsp if (ie == NULL)
2653 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2654 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2655 2b92fad7 2019-06-02 stsp
2656 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2657 2b92fad7 2019-06-02 stsp path1) == -1)
2658 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2659 2b92fad7 2019-06-02 stsp
2660 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2661 7f91a133 2019-12-13 stsp repo);
2662 2b92fad7 2019-06-02 stsp if (err)
2663 2b92fad7 2019-06-02 stsp goto done;
2664 2b92fad7 2019-06-02 stsp
2665 2b92fad7 2019-06-02 stsp switch (status) {
2666 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2667 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2668 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2669 1ee397ad 2019-07-12 stsp if (err)
2670 1ee397ad 2019-07-12 stsp goto done;
2671 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2672 2b92fad7 2019-06-02 stsp if (err)
2673 2b92fad7 2019-06-02 stsp goto done;
2674 2b92fad7 2019-06-02 stsp if (ie)
2675 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2676 2b92fad7 2019-06-02 stsp break;
2677 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2678 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2679 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2680 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2681 1ee397ad 2019-07-12 stsp if (err)
2682 1ee397ad 2019-07-12 stsp goto done;
2683 2b92fad7 2019-06-02 stsp if (ie)
2684 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2685 2b92fad7 2019-06-02 stsp break;
2686 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2687 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2688 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2689 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2690 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2691 1ee397ad 2019-07-12 stsp if (err)
2692 1ee397ad 2019-07-12 stsp goto done;
2693 2b92fad7 2019-06-02 stsp break;
2694 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2695 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2696 1ee397ad 2019-07-12 stsp if (err)
2697 1ee397ad 2019-07-12 stsp goto done;
2698 2b92fad7 2019-06-02 stsp break;
2699 2b92fad7 2019-06-02 stsp default:
2700 2b92fad7 2019-06-02 stsp break;
2701 2b92fad7 2019-06-02 stsp }
2702 234035bc 2019-06-01 stsp } else if (blob2) {
2703 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2704 234035bc 2019-06-01 stsp path2) == -1)
2705 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2706 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2707 d6c87207 2019-08-02 stsp strlen(path2));
2708 234035bc 2019-06-01 stsp if (ie) {
2709 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2710 7f91a133 2019-12-13 stsp -1, NULL, repo);
2711 234035bc 2019-06-01 stsp if (err)
2712 234035bc 2019-06-01 stsp goto done;
2713 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2714 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2715 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2716 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2717 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2718 1ee397ad 2019-07-12 stsp status, path2);
2719 234035bc 2019-06-01 stsp goto done;
2720 234035bc 2019-06-01 stsp }
2721 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2722 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2723 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2724 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2725 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2726 526a746f 2020-07-23 stsp if (err)
2727 526a746f 2020-07-23 stsp goto done;
2728 526a746f 2020-07-23 stsp } else {
2729 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2730 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2731 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2732 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2733 526a746f 2020-07-23 stsp a->progress_arg);
2734 526a746f 2020-07-23 stsp if (err)
2735 526a746f 2020-07-23 stsp goto done;
2736 526a746f 2020-07-23 stsp }
2737 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2738 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2739 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2740 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2741 234035bc 2019-06-01 stsp if (err)
2742 234035bc 2019-06-01 stsp goto done;
2743 234035bc 2019-06-01 stsp }
2744 234035bc 2019-06-01 stsp } else {
2745 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2746 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2747 960094b4 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2748 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2749 234035bc 2019-06-01 stsp if (err)
2750 234035bc 2019-06-01 stsp goto done;
2751 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2752 234035bc 2019-06-01 stsp if (err)
2753 234035bc 2019-06-01 stsp goto done;
2754 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2755 3969253a 2020-03-07 stsp NULL, NULL, 1);
2756 3969253a 2020-03-07 stsp if (err) {
2757 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2758 3969253a 2020-03-07 stsp goto done;
2759 3969253a 2020-03-07 stsp }
2760 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2761 2b92fad7 2019-06-02 stsp if (err) {
2762 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2763 2b92fad7 2019-06-02 stsp goto done;
2764 2b92fad7 2019-06-02 stsp }
2765 234035bc 2019-06-01 stsp }
2766 234035bc 2019-06-01 stsp }
2767 234035bc 2019-06-01 stsp done:
2768 234035bc 2019-06-01 stsp free(ondisk_path);
2769 234035bc 2019-06-01 stsp return err;
2770 234035bc 2019-06-01 stsp }
2771 234035bc 2019-06-01 stsp
2772 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2773 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2774 234035bc 2019-06-01 stsp struct got_repository *repo;
2775 234035bc 2019-06-01 stsp };
2776 234035bc 2019-06-01 stsp
2777 234035bc 2019-06-01 stsp static const struct got_error *
2778 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2779 234035bc 2019-06-01 stsp {
2780 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2781 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2782 234035bc 2019-06-01 stsp unsigned char status;
2783 234035bc 2019-06-01 stsp struct stat sb;
2784 234035bc 2019-06-01 stsp char *ondisk_path;
2785 234035bc 2019-06-01 stsp
2786 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2787 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2788 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2789 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2790 234035bc 2019-06-01 stsp
2791 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2792 234035bc 2019-06-01 stsp == -1)
2793 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2794 234035bc 2019-06-01 stsp
2795 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2796 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2797 234035bc 2019-06-01 stsp if (err)
2798 234035bc 2019-06-01 stsp return err;
2799 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2800 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2801 234035bc 2019-06-01 stsp
2802 234035bc 2019-06-01 stsp return NULL;
2803 234035bc 2019-06-01 stsp }
2804 234035bc 2019-06-01 stsp
2805 818c7501 2019-07-11 stsp static const struct got_error *
2806 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2807 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2808 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2809 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2811 234035bc 2019-06-01 stsp {
2812 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2813 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2814 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2815 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2816 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2817 234035bc 2019-06-01 stsp
2818 03415a1a 2019-06-02 stsp if (commit_id1) {
2819 f69721c3 2019-10-21 stsp char *id_str;
2820 f69721c3 2019-10-21 stsp
2821 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2822 03415a1a 2019-06-02 stsp worktree->path_prefix);
2823 03415a1a 2019-06-02 stsp if (err)
2824 03415a1a 2019-06-02 stsp goto done;
2825 03415a1a 2019-06-02 stsp
2826 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2827 03415a1a 2019-06-02 stsp if (err)
2828 03415a1a 2019-06-02 stsp goto done;
2829 f69721c3 2019-10-21 stsp
2830 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2831 f69721c3 2019-10-21 stsp if (err)
2832 f69721c3 2019-10-21 stsp goto done;
2833 f69721c3 2019-10-21 stsp
2834 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2835 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2836 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2837 f69721c3 2019-10-21 stsp free(id_str);
2838 f69721c3 2019-10-21 stsp goto done;
2839 f69721c3 2019-10-21 stsp }
2840 f69721c3 2019-10-21 stsp free(id_str);
2841 03415a1a 2019-06-02 stsp }
2842 234035bc 2019-06-01 stsp
2843 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2844 234035bc 2019-06-01 stsp worktree->path_prefix);
2845 234035bc 2019-06-01 stsp if (err)
2846 234035bc 2019-06-01 stsp goto done;
2847 234035bc 2019-06-01 stsp
2848 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2849 234035bc 2019-06-01 stsp if (err)
2850 234035bc 2019-06-01 stsp goto done;
2851 234035bc 2019-06-01 stsp
2852 234035bc 2019-06-01 stsp arg.worktree = worktree;
2853 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2854 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2855 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2856 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2857 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2858 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2859 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2860 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2861 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2862 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2863 af12c6b9 2019-06-04 stsp err = sync_err;
2864 234035bc 2019-06-01 stsp done:
2865 234035bc 2019-06-01 stsp if (tree1)
2866 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2867 234035bc 2019-06-01 stsp if (tree2)
2868 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2869 f69721c3 2019-10-21 stsp free(label_orig);
2870 818c7501 2019-07-11 stsp return err;
2871 818c7501 2019-07-11 stsp }
2872 234035bc 2019-06-01 stsp
2873 818c7501 2019-07-11 stsp const struct got_error *
2874 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2875 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2876 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2877 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2878 818c7501 2019-07-11 stsp {
2879 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2880 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2881 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2882 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2883 818c7501 2019-07-11 stsp
2884 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2885 818c7501 2019-07-11 stsp if (err)
2886 818c7501 2019-07-11 stsp return err;
2887 818c7501 2019-07-11 stsp
2888 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2889 818c7501 2019-07-11 stsp if (err)
2890 818c7501 2019-07-11 stsp goto done;
2891 818c7501 2019-07-11 stsp
2892 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2893 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2894 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2895 818c7501 2019-07-11 stsp &mok_arg);
2896 818c7501 2019-07-11 stsp if (err)
2897 818c7501 2019-07-11 stsp goto done;
2898 818c7501 2019-07-11 stsp
2899 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2900 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2901 818c7501 2019-07-11 stsp done:
2902 818c7501 2019-07-11 stsp if (fileindex)
2903 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2904 818c7501 2019-07-11 stsp free(fileindex_path);
2905 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2906 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2907 234035bc 2019-06-01 stsp err = unlockerr;
2908 234035bc 2019-06-01 stsp return err;
2909 234035bc 2019-06-01 stsp }
2910 234035bc 2019-06-01 stsp
2911 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2912 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2913 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2914 927df6b7 2019-02-10 stsp const char *status_path;
2915 927df6b7 2019-02-10 stsp size_t status_path_len;
2916 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2917 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2918 f8d1f275 2019-02-04 stsp void *status_arg;
2919 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2920 f8d1f275 2019-02-04 stsp void *cancel_arg;
2921 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2922 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2923 f2a9dc41 2019-12-13 tracey int report_unchanged;
2924 3143d852 2020-06-25 stsp int no_ignores;
2925 f8d1f275 2019-02-04 stsp };
2926 88d0e355 2019-08-03 stsp
2927 f8d1f275 2019-02-04 stsp static const struct got_error *
2928 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2929 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2930 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2931 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2932 927df6b7 2019-02-10 stsp {
2933 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2934 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2935 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2936 927df6b7 2019-02-10 stsp struct stat sb;
2937 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2938 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2939 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2940 927df6b7 2019-02-10 stsp
2941 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2942 98eaaa12 2019-08-03 stsp if (err)
2943 98eaaa12 2019-08-03 stsp return err;
2944 98eaaa12 2019-08-03 stsp
2945 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2946 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2947 98eaaa12 2019-08-03 stsp return NULL;
2948 98eaaa12 2019-08-03 stsp
2949 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2950 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2951 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2952 98eaaa12 2019-08-03 stsp }
2953 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2954 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2955 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2956 927df6b7 2019-02-10 stsp }
2957 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2958 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2959 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2960 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2961 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2962 98eaaa12 2019-08-03 stsp }
2963 98eaaa12 2019-08-03 stsp
2964 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2965 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2966 927df6b7 2019-02-10 stsp }
2967 927df6b7 2019-02-10 stsp
2968 927df6b7 2019-02-10 stsp static const struct got_error *
2969 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2970 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2971 f8d1f275 2019-02-04 stsp {
2972 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2973 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2974 f8d1f275 2019-02-04 stsp char *abspath;
2975 f8d1f275 2019-02-04 stsp
2976 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2977 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2978 0584f854 2019-04-06 stsp
2979 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2980 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2981 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2982 927df6b7 2019-02-10 stsp return NULL;
2983 927df6b7 2019-02-10 stsp
2984 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2985 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2986 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2987 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2988 f8d1f275 2019-02-04 stsp } else {
2989 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2990 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2991 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2992 f8d1f275 2019-02-04 stsp }
2993 f8d1f275 2019-02-04 stsp
2994 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2995 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2996 f8d1f275 2019-02-04 stsp free(abspath);
2997 9d31a1d8 2018-03-11 stsp return err;
2998 9d31a1d8 2018-03-11 stsp }
2999 f8d1f275 2019-02-04 stsp
3000 f8d1f275 2019-02-04 stsp static const struct got_error *
3001 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3002 f8d1f275 2019-02-04 stsp {
3003 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3004 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3005 2ec1f75b 2019-03-26 stsp unsigned char status;
3006 927df6b7 2019-02-10 stsp
3007 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3008 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3009 0584f854 2019-04-06 stsp
3010 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3011 927df6b7 2019-02-10 stsp return NULL;
3012 927df6b7 2019-02-10 stsp
3013 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3014 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3015 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3016 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3017 2ec1f75b 2019-03-26 stsp else
3018 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3019 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3020 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3021 6841da00 2019-08-08 stsp }
3022 6841da00 2019-08-08 stsp
3023 6841da00 2019-08-08 stsp void
3024 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3025 6841da00 2019-08-08 stsp {
3026 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3027 6841da00 2019-08-08 stsp
3028 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3029 6841da00 2019-08-08 stsp free((char *)pe->path);
3030 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3031 6841da00 2019-08-08 stsp }
3032 6841da00 2019-08-08 stsp
3033 6841da00 2019-08-08 stsp void
3034 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3035 6841da00 2019-08-08 stsp {
3036 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3037 6841da00 2019-08-08 stsp
3038 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3039 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3040 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3041 6841da00 2019-08-08 stsp free((char *)pe->path);
3042 6841da00 2019-08-08 stsp }
3043 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3044 6841da00 2019-08-08 stsp }
3045 6841da00 2019-08-08 stsp
3046 6841da00 2019-08-08 stsp static const struct got_error *
3047 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3048 6841da00 2019-08-08 stsp {
3049 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3050 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3051 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3052 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3053 6841da00 2019-08-08 stsp size_t linesize = 0;
3054 6841da00 2019-08-08 stsp ssize_t linelen;
3055 6841da00 2019-08-08 stsp
3056 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3057 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3058 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3059 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3060 6841da00 2019-08-08 stsp
3061 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3062 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3063 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3064 bd8de430 2019-10-04 stsp
3065 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3066 bd8de430 2019-10-04 stsp if (line[0] == '#')
3067 bd8de430 2019-10-04 stsp continue;
3068 bd8de430 2019-10-04 stsp
3069 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3070 bd8de430 2019-10-04 stsp if (line[0] == '!')
3071 bd8de430 2019-10-04 stsp continue;
3072 bd8de430 2019-10-04 stsp
3073 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3074 6841da00 2019-08-08 stsp line) == -1) {
3075 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3076 6841da00 2019-08-08 stsp goto done;
3077 6841da00 2019-08-08 stsp }
3078 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3079 6841da00 2019-08-08 stsp if (err)
3080 6841da00 2019-08-08 stsp goto done;
3081 6841da00 2019-08-08 stsp }
3082 6841da00 2019-08-08 stsp if (ferror(f)) {
3083 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3084 6841da00 2019-08-08 stsp goto done;
3085 6841da00 2019-08-08 stsp }
3086 6841da00 2019-08-08 stsp
3087 6841da00 2019-08-08 stsp dirpath = strdup(path);
3088 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3089 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3090 6841da00 2019-08-08 stsp goto done;
3091 6841da00 2019-08-08 stsp }
3092 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3093 6841da00 2019-08-08 stsp done:
3094 6841da00 2019-08-08 stsp free(line);
3095 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3096 6841da00 2019-08-08 stsp free(dirpath);
3097 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3098 6841da00 2019-08-08 stsp }
3099 6841da00 2019-08-08 stsp return err;
3100 6841da00 2019-08-08 stsp }
3101 6841da00 2019-08-08 stsp
3102 6841da00 2019-08-08 stsp int
3103 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3104 6841da00 2019-08-08 stsp {
3105 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3106 bd8de430 2019-10-04 stsp
3107 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3108 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3109 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3110 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3111 bd8de430 2019-10-04 stsp
3112 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3113 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3114 6841da00 2019-08-08 stsp
3115 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3116 bd8de430 2019-10-04 stsp continue;
3117 bd8de430 2019-10-04 stsp pattern += 3;
3118 bd8de430 2019-10-04 stsp p = path;
3119 bd8de430 2019-10-04 stsp while (*p) {
3120 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3121 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3122 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3123 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3124 bd8de430 2019-10-04 stsp p++;
3125 bd8de430 2019-10-04 stsp while (*p == '/')
3126 bd8de430 2019-10-04 stsp p++;
3127 bd8de430 2019-10-04 stsp continue;
3128 bd8de430 2019-10-04 stsp }
3129 bd8de430 2019-10-04 stsp return 1;
3130 bd8de430 2019-10-04 stsp }
3131 bd8de430 2019-10-04 stsp }
3132 bd8de430 2019-10-04 stsp }
3133 bd8de430 2019-10-04 stsp
3134 6841da00 2019-08-08 stsp /*
3135 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3136 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3137 6841da00 2019-08-08 stsp * ignores backwards.
3138 6841da00 2019-08-08 stsp */
3139 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3140 6841da00 2019-08-08 stsp while (pe) {
3141 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3142 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3143 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3144 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3145 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3146 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3147 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3148 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3149 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3150 6841da00 2019-08-08 stsp continue;
3151 6841da00 2019-08-08 stsp return 1;
3152 6841da00 2019-08-08 stsp }
3153 6841da00 2019-08-08 stsp }
3154 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3155 6841da00 2019-08-08 stsp }
3156 6841da00 2019-08-08 stsp
3157 6841da00 2019-08-08 stsp return 0;
3158 6841da00 2019-08-08 stsp }
3159 6841da00 2019-08-08 stsp
3160 6841da00 2019-08-08 stsp static const struct got_error *
3161 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3162 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3163 6841da00 2019-08-08 stsp {
3164 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3165 6841da00 2019-08-08 stsp char *ignorespath;
3166 886cec17 2019-12-15 stsp int fd = -1;
3167 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3168 6841da00 2019-08-08 stsp
3169 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3170 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3171 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3172 6841da00 2019-08-08 stsp
3173 886cec17 2019-12-15 stsp if (dirfd != -1) {
3174 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3175 886cec17 2019-12-15 stsp if (fd == -1) {
3176 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3177 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3178 886cec17 2019-12-15 stsp ignorespath);
3179 886cec17 2019-12-15 stsp } else {
3180 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3181 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3182 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3183 886cec17 2019-12-15 stsp ignorespath);
3184 886cec17 2019-12-15 stsp else {
3185 886cec17 2019-12-15 stsp fd = -1;
3186 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3187 886cec17 2019-12-15 stsp }
3188 886cec17 2019-12-15 stsp }
3189 886cec17 2019-12-15 stsp } else {
3190 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3191 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3192 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3193 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3194 886cec17 2019-12-15 stsp ignorespath);
3195 886cec17 2019-12-15 stsp } else
3196 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3197 886cec17 2019-12-15 stsp }
3198 6841da00 2019-08-08 stsp
3199 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3200 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3201 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3202 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3203 6841da00 2019-08-08 stsp free(ignorespath);
3204 6841da00 2019-08-08 stsp return err;
3205 f8d1f275 2019-02-04 stsp }
3206 f8d1f275 2019-02-04 stsp
3207 f8d1f275 2019-02-04 stsp static const struct got_error *
3208 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3209 f8d1f275 2019-02-04 stsp {
3210 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3211 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3212 f8d1f275 2019-02-04 stsp char *path = NULL;
3213 f8d1f275 2019-02-04 stsp
3214 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3215 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3216 0584f854 2019-04-06 stsp
3217 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3218 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3219 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3220 f8d1f275 2019-02-04 stsp } else {
3221 f8d1f275 2019-02-04 stsp path = de->d_name;
3222 f8d1f275 2019-02-04 stsp }
3223 f8d1f275 2019-02-04 stsp
3224 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3225 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3226 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3227 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3228 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3229 f8d1f275 2019-02-04 stsp if (parent_path[0])
3230 f8d1f275 2019-02-04 stsp free(path);
3231 b72f483a 2019-02-05 stsp return err;
3232 f8d1f275 2019-02-04 stsp }
3233 f8d1f275 2019-02-04 stsp
3234 347d1d3e 2019-07-12 stsp static const struct got_error *
3235 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3236 3143d852 2020-06-25 stsp {
3237 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3238 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3239 3143d852 2020-06-25 stsp
3240 3143d852 2020-06-25 stsp if (a->no_ignores)
3241 3143d852 2020-06-25 stsp return NULL;
3242 3143d852 2020-06-25 stsp
3243 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3244 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3245 3143d852 2020-06-25 stsp if (err)
3246 3143d852 2020-06-25 stsp return err;
3247 3143d852 2020-06-25 stsp
3248 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3249 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3250 3143d852 2020-06-25 stsp
3251 3143d852 2020-06-25 stsp return err;
3252 3143d852 2020-06-25 stsp }
3253 3143d852 2020-06-25 stsp
3254 3143d852 2020-06-25 stsp static const struct got_error *
3255 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3256 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3257 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3258 abb4604f 2019-07-27 stsp {
3259 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3260 abb4604f 2019-07-27 stsp struct stat sb;
3261 abb4604f 2019-07-27 stsp
3262 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3263 abb4604f 2019-07-27 stsp if (ie)
3264 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3265 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3266 abb4604f 2019-07-27 stsp
3267 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3268 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3269 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3270 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3271 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3272 abb4604f 2019-07-27 stsp return NULL;
3273 abb4604f 2019-07-27 stsp }
3274 abb4604f 2019-07-27 stsp
3275 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3276 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3277 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3278 abb4604f 2019-07-27 stsp
3279 abb4604f 2019-07-27 stsp return NULL;
3280 3143d852 2020-06-25 stsp }
3281 3143d852 2020-06-25 stsp
3282 3143d852 2020-06-25 stsp static const struct got_error *
3283 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3284 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3285 3143d852 2020-06-25 stsp {
3286 3143d852 2020-06-25 stsp const struct got_error *err;
3287 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3288 3143d852 2020-06-25 stsp
3289 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3290 3143d852 2020-06-25 stsp ".cvsignore");
3291 3143d852 2020-06-25 stsp if (err)
3292 3143d852 2020-06-25 stsp return err;
3293 3143d852 2020-06-25 stsp
3294 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3295 3143d852 2020-06-25 stsp ".gitignore");
3296 3143d852 2020-06-25 stsp if (err)
3297 3143d852 2020-06-25 stsp return err;
3298 3143d852 2020-06-25 stsp
3299 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3300 3143d852 2020-06-25 stsp if (err) {
3301 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3302 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3303 3143d852 2020-06-25 stsp return err;
3304 3143d852 2020-06-25 stsp }
3305 3143d852 2020-06-25 stsp for (;;) {
3306 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3307 3143d852 2020-06-25 stsp ".cvsignore");
3308 3143d852 2020-06-25 stsp if (err)
3309 3143d852 2020-06-25 stsp break;
3310 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3311 3143d852 2020-06-25 stsp ".gitignore");
3312 3143d852 2020-06-25 stsp if (err)
3313 3143d852 2020-06-25 stsp break;
3314 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3315 3143d852 2020-06-25 stsp if (err) {
3316 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3317 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3318 3143d852 2020-06-25 stsp break;
3319 3143d852 2020-06-25 stsp }
3320 b737c85a 2020-06-26 stsp free(parent_path);
3321 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3322 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3323 3143d852 2020-06-25 stsp }
3324 3143d852 2020-06-25 stsp
3325 b737c85a 2020-06-26 stsp free(parent_path);
3326 b737c85a 2020-06-26 stsp free(next_parent_path);
3327 3143d852 2020-06-25 stsp return err;
3328 abb4604f 2019-07-27 stsp }
3329 abb4604f 2019-07-27 stsp
3330 abb4604f 2019-07-27 stsp static const struct got_error *
3331 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3332 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3333 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3334 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3335 f2a9dc41 2019-12-13 tracey int report_unchanged)
3336 f8d1f275 2019-02-04 stsp {
3337 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3338 6fc93f37 2019-12-13 stsp int fd = -1;
3339 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3340 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3341 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3342 3143d852 2020-06-25 stsp
3343 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3344 f8d1f275 2019-02-04 stsp
3345 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3346 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3347 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3348 8dc303cc 2019-07-27 stsp
3349 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3350 6fc93f37 2019-12-13 stsp if (fd == -1) {
3351 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3352 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3353 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3354 abb4604f 2019-07-27 stsp else
3355 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3356 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3357 f2a9dc41 2019-12-13 tracey report_unchanged);
3358 abb4604f 2019-07-27 stsp } else {
3359 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3360 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3361 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3362 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3363 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3364 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3365 abb4604f 2019-07-27 stsp arg.status_path = path;
3366 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3367 abb4604f 2019-07-27 stsp arg.repo = repo;
3368 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3369 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3370 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3371 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3372 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3373 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3374 022fae89 2019-12-06 tracey if (!no_ignores) {
3375 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3376 3143d852 2020-06-25 stsp worktree->root_path, path);
3377 3143d852 2020-06-25 stsp if (err)
3378 3143d852 2020-06-25 stsp goto done;
3379 022fae89 2019-12-06 tracey }
3380 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3381 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3382 927df6b7 2019-02-10 stsp }
3383 3143d852 2020-06-25 stsp done:
3384 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3385 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3386 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3387 927df6b7 2019-02-10 stsp free(ondisk_path);
3388 347d1d3e 2019-07-12 stsp return err;
3389 347d1d3e 2019-07-12 stsp }
3390 347d1d3e 2019-07-12 stsp
3391 347d1d3e 2019-07-12 stsp const struct got_error *
3392 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3393 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3394 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3395 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3396 347d1d3e 2019-07-12 stsp {
3397 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3398 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3399 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3400 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3401 347d1d3e 2019-07-12 stsp
3402 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3403 347d1d3e 2019-07-12 stsp if (err)
3404 347d1d3e 2019-07-12 stsp return err;
3405 347d1d3e 2019-07-12 stsp
3406 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3407 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3408 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3409 72ea6654 2019-07-27 stsp if (err)
3410 72ea6654 2019-07-27 stsp break;
3411 72ea6654 2019-07-27 stsp }
3412 f8d1f275 2019-02-04 stsp free(fileindex_path);
3413 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3414 f8d1f275 2019-02-04 stsp return err;
3415 f8d1f275 2019-02-04 stsp }
3416 6c7ab921 2019-03-18 stsp
3417 6c7ab921 2019-03-18 stsp const struct got_error *
3418 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3419 6c7ab921 2019-03-18 stsp const char *arg)
3420 6c7ab921 2019-03-18 stsp {
3421 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3422 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3423 6c7ab921 2019-03-18 stsp size_t len;
3424 00bb5ea0 2020-07-23 stsp struct stat sb;
3425 6c7ab921 2019-03-18 stsp
3426 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3427 6c7ab921 2019-03-18 stsp
3428 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3429 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3430 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3431 00bb5ea0 2020-07-23 stsp
3432 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3433 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3434 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3435 00bb5ea0 2020-07-23 stsp goto done;
3436 00bb5ea0 2020-07-23 stsp }
3437 00bb5ea0 2020-07-23 stsp }
3438 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3439 00bb5ea0 2020-07-23 stsp /*
3440 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3441 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3442 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3443 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3444 00bb5ea0 2020-07-23 stsp */
3445 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3446 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3447 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3448 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3449 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3450 00bb5ea0 2020-07-23 stsp goto done;
3451 00bb5ea0 2020-07-23 stsp }
3452 00bb5ea0 2020-07-23 stsp
3453 00bb5ea0 2020-07-23 stsp }
3454 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3455 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3456 00bb5ea0 2020-07-23 stsp if (err)
3457 d0710d08 2019-07-22 stsp goto done;
3458 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3459 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3460 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3461 00bb5ea0 2020-07-23 stsp goto done;
3462 00bb5ea0 2020-07-23 stsp }
3463 00bb5ea0 2020-07-23 stsp } else {
3464 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3465 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3466 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3467 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3468 00bb5ea0 2020-07-23 stsp goto done;
3469 00bb5ea0 2020-07-23 stsp }
3470 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3471 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3472 00bb5ea0 2020-07-23 stsp goto done;
3473 00bb5ea0 2020-07-23 stsp }
3474 d0710d08 2019-07-22 stsp }
3475 d0710d08 2019-07-22 stsp }
3476 6c7ab921 2019-03-18 stsp
3477 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3478 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3479 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3480 6c7ab921 2019-03-18 stsp goto done;
3481 6c7ab921 2019-03-18 stsp }
3482 6c7ab921 2019-03-18 stsp
3483 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3484 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3485 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3486 f6d88e1a 2019-05-29 stsp if (err)
3487 f6d88e1a 2019-05-29 stsp goto done;
3488 f6d88e1a 2019-05-29 stsp } else {
3489 f6d88e1a 2019-05-29 stsp path = strdup("");
3490 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3491 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3492 f6d88e1a 2019-05-29 stsp goto done;
3493 f6d88e1a 2019-05-29 stsp }
3494 6c7ab921 2019-03-18 stsp }
3495 6c7ab921 2019-03-18 stsp
3496 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3497 6c7ab921 2019-03-18 stsp len = strlen(path);
3498 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3499 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3500 6c7ab921 2019-03-18 stsp len--;
3501 6c7ab921 2019-03-18 stsp }
3502 6c7ab921 2019-03-18 stsp done:
3503 6c7ab921 2019-03-18 stsp free(resolved);
3504 d0710d08 2019-07-22 stsp free(cwd);
3505 6c7ab921 2019-03-18 stsp if (err == NULL)
3506 6c7ab921 2019-03-18 stsp *wt_path = path;
3507 6c7ab921 2019-03-18 stsp else
3508 6c7ab921 2019-03-18 stsp free(path);
3509 d00136be 2019-03-26 stsp return err;
3510 d00136be 2019-03-26 stsp }
3511 d00136be 2019-03-26 stsp
3512 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3513 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3514 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3515 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3516 4e68cba3 2019-11-23 stsp void *progress_arg;
3517 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3518 4e68cba3 2019-11-23 stsp };
3519 4e68cba3 2019-11-23 stsp
3520 4e68cba3 2019-11-23 stsp static const struct got_error *
3521 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3522 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3523 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3524 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3525 07f5b47a 2019-06-02 stsp {
3526 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3527 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3528 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3529 6d022e97 2019-08-04 stsp struct stat sb;
3530 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3531 07f5b47a 2019-06-02 stsp
3532 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3533 dbb83fbd 2019-12-12 stsp relpath) == -1)
3534 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3535 dbb83fbd 2019-12-12 stsp
3536 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3537 6d022e97 2019-08-04 stsp if (ie) {
3538 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3539 12463d8b 2019-12-13 stsp de_name, a->repo);
3540 6d022e97 2019-08-04 stsp if (err)
3541 dbb83fbd 2019-12-12 stsp goto done;
3542 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3543 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3544 dbb83fbd 2019-12-12 stsp goto done;
3545 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3546 dbb83fbd 2019-12-12 stsp if (err)
3547 dbb83fbd 2019-12-12 stsp goto done;
3548 6d022e97 2019-08-04 stsp }
3549 07f5b47a 2019-06-02 stsp
3550 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3551 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3552 dbb83fbd 2019-12-12 stsp goto done;
3553 4e68cba3 2019-11-23 stsp }
3554 07f5b47a 2019-06-02 stsp
3555 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3556 4e68cba3 2019-11-23 stsp if (err)
3557 4e68cba3 2019-11-23 stsp goto done;
3558 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3559 3969253a 2020-03-07 stsp if (err) {
3560 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3561 3969253a 2020-03-07 stsp goto done;
3562 3969253a 2020-03-07 stsp }
3563 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3564 07f5b47a 2019-06-02 stsp if (err) {
3565 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3566 4e68cba3 2019-11-23 stsp goto done;
3567 07f5b47a 2019-06-02 stsp }
3568 4e68cba3 2019-11-23 stsp done:
3569 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3570 dbb83fbd 2019-12-12 stsp if (err)
3571 dbb83fbd 2019-12-12 stsp return err;
3572 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3573 dbb83fbd 2019-12-12 stsp return NULL;
3574 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3575 07f5b47a 2019-06-02 stsp }
3576 07f5b47a 2019-06-02 stsp
3577 d00136be 2019-03-26 stsp const struct got_error *
3578 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3579 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3580 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3581 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3582 d00136be 2019-03-26 stsp {
3583 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3584 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3585 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3586 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3587 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3588 d00136be 2019-03-26 stsp
3589 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3590 d00136be 2019-03-26 stsp if (err)
3591 d00136be 2019-03-26 stsp return err;
3592 d00136be 2019-03-26 stsp
3593 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3594 d00136be 2019-03-26 stsp if (err)
3595 d00136be 2019-03-26 stsp goto done;
3596 d00136be 2019-03-26 stsp
3597 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3598 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3599 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3600 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3601 4e68cba3 2019-11-23 stsp saa.repo = repo;
3602 4e68cba3 2019-11-23 stsp
3603 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3604 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3605 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3606 1dd54920 2019-05-11 stsp if (err)
3607 af12c6b9 2019-06-04 stsp break;
3608 1dd54920 2019-05-11 stsp }
3609 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3610 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3611 af12c6b9 2019-06-04 stsp err = sync_err;
3612 d00136be 2019-03-26 stsp done:
3613 fb399478 2019-07-12 stsp free(fileindex_path);
3614 d00136be 2019-03-26 stsp if (fileindex)
3615 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3616 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3617 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3618 d00136be 2019-03-26 stsp err = unlockerr;
3619 6c7ab921 2019-03-18 stsp return err;
3620 6c7ab921 2019-03-18 stsp }
3621 17ed4618 2019-06-02 stsp
3622 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3623 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3624 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3625 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3626 f2a9dc41 2019-12-13 tracey void *progress_arg;
3627 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3628 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3629 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3630 f2a9dc41 2019-12-13 tracey };
3631 f2a9dc41 2019-12-13 tracey
3632 f2a9dc41 2019-12-13 tracey static const struct got_error *
3633 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3634 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3635 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3636 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3637 17ed4618 2019-06-02 stsp {
3638 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3639 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3640 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3641 17ed4618 2019-06-02 stsp struct stat sb;
3642 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3643 17ed4618 2019-06-02 stsp
3644 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3645 17ed4618 2019-06-02 stsp if (ie == NULL)
3646 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3647 2ec1f75b 2019-03-26 stsp
3648 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3649 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3650 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3651 9acbc4fa 2019-08-03 stsp return NULL;
3652 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3653 9acbc4fa 2019-08-03 stsp }
3654 9acbc4fa 2019-08-03 stsp
3655 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3656 f2a9dc41 2019-12-13 tracey relpath) == -1)
3657 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3658 f2a9dc41 2019-12-13 tracey
3659 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3660 12463d8b 2019-12-13 stsp a->repo);
3661 17ed4618 2019-06-02 stsp if (err)
3662 f2a9dc41 2019-12-13 tracey goto done;
3663 17ed4618 2019-06-02 stsp
3664 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3665 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3666 f2a9dc41 2019-12-13 tracey goto done;
3667 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3668 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3669 f2a9dc41 2019-12-13 tracey goto done;
3670 f2a9dc41 2019-12-13 tracey }
3671 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3672 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3673 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3674 f2a9dc41 2019-12-13 tracey goto done;
3675 f2a9dc41 2019-12-13 tracey }
3676 17ed4618 2019-06-02 stsp }
3677 17ed4618 2019-06-02 stsp
3678 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3679 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3680 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3681 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3682 12463d8b 2019-12-13 stsp ondisk_path);
3683 12463d8b 2019-12-13 stsp goto done;
3684 12463d8b 2019-12-13 stsp }
3685 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3686 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3687 12463d8b 2019-12-13 stsp goto done;
3688 15341bfd 2020-03-05 tracey }
3689 15341bfd 2020-03-05 tracey
3690 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3691 15341bfd 2020-03-05 tracey
3692 15341bfd 2020-03-05 tracey if (parent == NULL) {
3693 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3694 15341bfd 2020-03-05 tracey goto done;
3695 15341bfd 2020-03-05 tracey }
3696 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3697 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3698 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3699 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3700 15341bfd 2020-03-05 tracey parent);
3701 15341bfd 2020-03-05 tracey break;
3702 15341bfd 2020-03-05 tracey }
3703 15341bfd 2020-03-05 tracey parent = dirname(parent);
3704 15341bfd 2020-03-05 tracey if (parent == NULL) {
3705 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3706 15341bfd 2020-03-05 tracey goto done;
3707 15341bfd 2020-03-05 tracey }
3708 12463d8b 2019-12-13 stsp }
3709 f2a9dc41 2019-12-13 tracey }
3710 17ed4618 2019-06-02 stsp
3711 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3712 f2a9dc41 2019-12-13 tracey done:
3713 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3714 f2a9dc41 2019-12-13 tracey if (err)
3715 f2a9dc41 2019-12-13 tracey return err;
3716 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3717 f2a9dc41 2019-12-13 tracey return NULL;
3718 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3719 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3720 17ed4618 2019-06-02 stsp }
3721 17ed4618 2019-06-02 stsp
3722 2ec1f75b 2019-03-26 stsp const struct got_error *
3723 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3724 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3725 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3726 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3727 2ec1f75b 2019-03-26 stsp {
3728 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3729 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3730 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3731 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3732 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3733 2ec1f75b 2019-03-26 stsp
3734 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3735 2ec1f75b 2019-03-26 stsp if (err)
3736 2ec1f75b 2019-03-26 stsp return err;
3737 2ec1f75b 2019-03-26 stsp
3738 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3739 2ec1f75b 2019-03-26 stsp if (err)
3740 2ec1f75b 2019-03-26 stsp goto done;
3741 f2a9dc41 2019-12-13 tracey
3742 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3743 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3744 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3745 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3746 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3747 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3748 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3749 2ec1f75b 2019-03-26 stsp
3750 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3751 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3752 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3753 17ed4618 2019-06-02 stsp if (err)
3754 af12c6b9 2019-06-04 stsp break;
3755 2ec1f75b 2019-03-26 stsp }
3756 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3757 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3758 af12c6b9 2019-06-04 stsp err = sync_err;
3759 2ec1f75b 2019-03-26 stsp done:
3760 fb399478 2019-07-12 stsp free(fileindex_path);
3761 2ec1f75b 2019-03-26 stsp if (fileindex)
3762 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3763 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3764 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3765 2ec1f75b 2019-03-26 stsp err = unlockerr;
3766 33aa809d 2019-08-08 stsp return err;
3767 33aa809d 2019-08-08 stsp }
3768 33aa809d 2019-08-08 stsp
3769 33aa809d 2019-08-08 stsp static const struct got_error *
3770 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3771 33aa809d 2019-08-08 stsp {
3772 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3773 33aa809d 2019-08-08 stsp char *line = NULL;
3774 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3775 33aa809d 2019-08-08 stsp ssize_t linelen;
3776 33aa809d 2019-08-08 stsp
3777 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3778 33aa809d 2019-08-08 stsp if (linelen == -1) {
3779 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3780 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3781 33aa809d 2019-08-08 stsp goto done;
3782 33aa809d 2019-08-08 stsp }
3783 33aa809d 2019-08-08 stsp return NULL;
3784 33aa809d 2019-08-08 stsp }
3785 33aa809d 2019-08-08 stsp if (outfile) {
3786 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3787 33aa809d 2019-08-08 stsp if (n != linelen) {
3788 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3789 33aa809d 2019-08-08 stsp goto done;
3790 33aa809d 2019-08-08 stsp }
3791 33aa809d 2019-08-08 stsp }
3792 33aa809d 2019-08-08 stsp if (rejectfile) {
3793 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3794 33aa809d 2019-08-08 stsp if (n != linelen)
3795 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3796 33aa809d 2019-08-08 stsp }
3797 33aa809d 2019-08-08 stsp done:
3798 33aa809d 2019-08-08 stsp free(line);
3799 2ec1f75b 2019-03-26 stsp return err;
3800 2ec1f75b 2019-03-26 stsp }
3801 1f1abb7e 2019-08-08 stsp
3802 33aa809d 2019-08-08 stsp static const struct got_error *
3803 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3804 33aa809d 2019-08-08 stsp {
3805 33aa809d 2019-08-08 stsp char *line = NULL;
3806 33aa809d 2019-08-08 stsp size_t linesize = 0;
3807 33aa809d 2019-08-08 stsp ssize_t linelen;
3808 33aa809d 2019-08-08 stsp
3809 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3810 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3811 500467ff 2019-09-25 hiltjo if (ferror(f))
3812 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3813 500467ff 2019-09-25 hiltjo return NULL;
3814 500467ff 2019-09-25 hiltjo }
3815 33aa809d 2019-08-08 stsp free(line);
3816 33aa809d 2019-08-08 stsp return NULL;
3817 33aa809d 2019-08-08 stsp }
3818 33aa809d 2019-08-08 stsp
3819 33aa809d 2019-08-08 stsp static const struct got_error *
3820 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3821 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3822 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3823 33aa809d 2019-08-08 stsp {
3824 33aa809d 2019-08-08 stsp const struct got_error *err;
3825 33aa809d 2019-08-08 stsp
3826 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3827 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3828 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3829 33aa809d 2019-08-08 stsp if (err)
3830 33aa809d 2019-08-08 stsp return err;
3831 33aa809d 2019-08-08 stsp (*line_cur1)++;
3832 33aa809d 2019-08-08 stsp }
3833 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3834 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3835 33aa809d 2019-08-08 stsp if (rejectfile)
3836 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3837 33aa809d 2019-08-08 stsp else
3838 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3839 33aa809d 2019-08-08 stsp if (err)
3840 33aa809d 2019-08-08 stsp return err;
3841 33aa809d 2019-08-08 stsp (*line_cur2)++;
3842 33aa809d 2019-08-08 stsp }
3843 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3844 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3845 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3846 33aa809d 2019-08-08 stsp if (err)
3847 33aa809d 2019-08-08 stsp return err;
3848 33aa809d 2019-08-08 stsp (*line_cur2)++;
3849 33aa809d 2019-08-08 stsp }
3850 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3851 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3852 33aa809d 2019-08-08 stsp if (rejectfile)
3853 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3854 33aa809d 2019-08-08 stsp else
3855 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3856 33aa809d 2019-08-08 stsp if (err)
3857 33aa809d 2019-08-08 stsp return err;
3858 33aa809d 2019-08-08 stsp (*line_cur1)++;
3859 33aa809d 2019-08-08 stsp }
3860 f1e81a05 2019-08-10 stsp
3861 f1e81a05 2019-08-10 stsp return NULL;
3862 f1e81a05 2019-08-10 stsp }
3863 f1e81a05 2019-08-10 stsp
3864 f1e81a05 2019-08-10 stsp static const struct got_error *
3865 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3866 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3867 f1e81a05 2019-08-10 stsp {
3868 f1e81a05 2019-08-10 stsp const struct got_error *err;
3869 f1e81a05 2019-08-10 stsp
3870 f1e81a05 2019-08-10 stsp if (outfile) {
3871 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3872 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3873 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3874 f1e81a05 2019-08-10 stsp if (err)
3875 f1e81a05 2019-08-10 stsp return err;
3876 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3877 f1e81a05 2019-08-10 stsp }
3878 f1e81a05 2019-08-10 stsp }
3879 f1e81a05 2019-08-10 stsp if (rejectfile) {
3880 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3881 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3882 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3883 f1e81a05 2019-08-10 stsp if (err)
3884 f1e81a05 2019-08-10 stsp return err;
3885 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3886 f1e81a05 2019-08-10 stsp }
3887 33aa809d 2019-08-08 stsp }
3888 33aa809d 2019-08-08 stsp
3889 33aa809d 2019-08-08 stsp return NULL;
3890 33aa809d 2019-08-08 stsp }
3891 33aa809d 2019-08-08 stsp
3892 33aa809d 2019-08-08 stsp static const struct got_error *
3893 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3894 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3895 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3896 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3897 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3898 33aa809d 2019-08-08 stsp {
3899 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3900 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3901 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3902 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3903 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3904 33aa809d 2019-08-08 stsp long pos1, pos2;
3905 33aa809d 2019-08-08 stsp FILE *hunkfile;
3906 33aa809d 2019-08-08 stsp
3907 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3908 33aa809d 2019-08-08 stsp
3909 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3910 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3911 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3912 33aa809d 2019-08-08 stsp
3913 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3914 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3915 33aa809d 2019-08-08 stsp
3916 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3917 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3918 33aa809d 2019-08-08 stsp
3919 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3920 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3921 33aa809d 2019-08-08 stsp goto done;
3922 33aa809d 2019-08-08 stsp }
3923 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3924 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3925 33aa809d 2019-08-08 stsp goto done;
3926 33aa809d 2019-08-08 stsp }
3927 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3928 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3929 33aa809d 2019-08-08 stsp goto done;
3930 33aa809d 2019-08-08 stsp }
3931 33aa809d 2019-08-08 stsp
3932 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3933 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3934 33aa809d 2019-08-08 stsp if (err)
3935 33aa809d 2019-08-08 stsp goto done;
3936 33aa809d 2019-08-08 stsp
3937 33aa809d 2019-08-08 stsp switch (*choice) {
3938 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3939 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3940 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3941 33aa809d 2019-08-08 stsp break;
3942 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3943 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3944 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3945 33aa809d 2019-08-08 stsp break;
3946 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3947 33aa809d 2019-08-08 stsp break;
3948 33aa809d 2019-08-08 stsp default:
3949 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3950 33aa809d 2019-08-08 stsp break;
3951 33aa809d 2019-08-08 stsp }
3952 33aa809d 2019-08-08 stsp done:
3953 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3954 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3955 33aa809d 2019-08-08 stsp return err;
3956 33aa809d 2019-08-08 stsp }
3957 33aa809d 2019-08-08 stsp
3958 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3959 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3960 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3961 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3962 1f1abb7e 2019-08-08 stsp void *progress_arg;
3963 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3964 33aa809d 2019-08-08 stsp void *patch_arg;
3965 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3966 1f1abb7e 2019-08-08 stsp };
3967 a129376b 2019-03-28 stsp
3968 e20a8b6f 2019-06-04 stsp static const struct got_error *
3969 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3970 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3971 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3972 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3973 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3974 33aa809d 2019-08-08 stsp {
3975 33aa809d 2019-08-08 stsp const struct got_error *err;
3976 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3977 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3978 1ebedb77 2019-10-19 stsp int fd2 = -1;
3979 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3980 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3981 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3982 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3983 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3984 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3985 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3986 33aa809d 2019-08-08 stsp int n = 0;
3987 33aa809d 2019-08-08 stsp
3988 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3989 33aa809d 2019-08-08 stsp
3990 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3991 33aa809d 2019-08-08 stsp if (err)
3992 33aa809d 2019-08-08 stsp return err;
3993 33aa809d 2019-08-08 stsp
3994 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3995 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3996 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3997 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3998 12463d8b 2019-12-13 stsp goto done;
3999 12463d8b 2019-12-13 stsp }
4000 12463d8b 2019-12-13 stsp } else {
4001 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4002 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4003 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4004 12463d8b 2019-12-13 stsp goto done;
4005 12463d8b 2019-12-13 stsp }
4006 1ebedb77 2019-10-19 stsp }
4007 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4008 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4009 1ebedb77 2019-10-19 stsp goto done;
4010 1ebedb77 2019-10-19 stsp }
4011 1ebedb77 2019-10-19 stsp
4012 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4013 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4014 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4015 33aa809d 2019-08-08 stsp goto done;
4016 33aa809d 2019-08-08 stsp }
4017 1ebedb77 2019-10-19 stsp fd2 = -1;
4018 33aa809d 2019-08-08 stsp
4019 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4020 33aa809d 2019-08-08 stsp if (err)
4021 33aa809d 2019-08-08 stsp goto done;
4022 33aa809d 2019-08-08 stsp
4023 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4024 33aa809d 2019-08-08 stsp if (err)
4025 33aa809d 2019-08-08 stsp goto done;
4026 33aa809d 2019-08-08 stsp
4027 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4028 33aa809d 2019-08-08 stsp if (err)
4029 33aa809d 2019-08-08 stsp goto done;
4030 33aa809d 2019-08-08 stsp
4031 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4032 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4033 33aa809d 2019-08-08 stsp goto done;
4034 33aa809d 2019-08-08 stsp }
4035 33aa809d 2019-08-08 stsp
4036 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4037 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4038 33aa809d 2019-08-08 stsp if (err)
4039 33aa809d 2019-08-08 stsp goto done;
4040 33aa809d 2019-08-08 stsp
4041 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4042 33aa809d 2019-08-08 stsp if (err)
4043 33aa809d 2019-08-08 stsp goto done;
4044 33aa809d 2019-08-08 stsp
4045 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4046 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4047 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4048 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4049 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4050 33aa809d 2019-08-08 stsp int choice;
4051 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4052 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4053 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4054 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4055 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4056 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4057 33aa809d 2019-08-08 stsp if (err)
4058 33aa809d 2019-08-08 stsp goto done;
4059 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4060 33aa809d 2019-08-08 stsp have_content = 1;
4061 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4062 33aa809d 2019-08-08 stsp break;
4063 33aa809d 2019-08-08 stsp }
4064 1ebedb77 2019-10-19 stsp if (have_content) {
4065 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4066 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4067 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4068 1ebedb77 2019-10-19 stsp if (err)
4069 1ebedb77 2019-10-19 stsp goto done;
4070 1ebedb77 2019-10-19 stsp
4071 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4072 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4073 1ebedb77 2019-10-19 stsp goto done;
4074 1ebedb77 2019-10-19 stsp }
4075 1ebedb77 2019-10-19 stsp }
4076 33aa809d 2019-08-08 stsp done:
4077 33aa809d 2019-08-08 stsp free(id_str);
4078 33aa809d 2019-08-08 stsp if (blob)
4079 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4080 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4081 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4082 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4083 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4084 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4085 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4086 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4087 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4088 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4089 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4090 33aa809d 2019-08-08 stsp if (err || !have_content) {
4091 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4092 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4093 33aa809d 2019-08-08 stsp free(*path_outfile);
4094 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4095 33aa809d 2019-08-08 stsp }
4096 33aa809d 2019-08-08 stsp free(args);
4097 33aa809d 2019-08-08 stsp if (ds) {
4098 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4099 33aa809d 2019-08-08 stsp free(ds);
4100 33aa809d 2019-08-08 stsp }
4101 33aa809d 2019-08-08 stsp if (changes)
4102 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4103 33aa809d 2019-08-08 stsp free(path1);
4104 33aa809d 2019-08-08 stsp return err;
4105 33aa809d 2019-08-08 stsp }
4106 33aa809d 2019-08-08 stsp
4107 33aa809d 2019-08-08 stsp static const struct got_error *
4108 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4109 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4110 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4111 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4112 a129376b 2019-03-28 stsp {
4113 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4114 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4115 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4116 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4117 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4118 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4119 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4120 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4121 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4122 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4123 a129376b 2019-03-28 stsp
4124 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4125 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4126 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4127 d3bcc3d1 2019-08-08 stsp return NULL;
4128 3d69ad8d 2019-08-17 semarie
4129 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4130 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4131 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4132 d3bcc3d1 2019-08-08 stsp
4133 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4134 65084dad 2019-08-08 stsp if (ie == NULL)
4135 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4136 a129376b 2019-03-28 stsp
4137 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4138 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4139 a129376b 2019-03-28 stsp if (err) {
4140 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4141 a129376b 2019-03-28 stsp goto done;
4142 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4143 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4144 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4145 e20a8b6f 2019-06-04 stsp goto done;
4146 e20a8b6f 2019-06-04 stsp }
4147 a129376b 2019-03-28 stsp }
4148 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4149 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4150 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4151 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4152 a129376b 2019-03-28 stsp goto done;
4153 a129376b 2019-03-28 stsp }
4154 a129376b 2019-03-28 stsp } else {
4155 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4156 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4157 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4158 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4159 a129376b 2019-03-28 stsp goto done;
4160 a129376b 2019-03-28 stsp }
4161 a129376b 2019-03-28 stsp } else {
4162 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4163 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4164 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4165 a129376b 2019-03-28 stsp goto done;
4166 a129376b 2019-03-28 stsp }
4167 a129376b 2019-03-28 stsp }
4168 a129376b 2019-03-28 stsp }
4169 a129376b 2019-03-28 stsp
4170 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4171 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4172 a9fa2909 2019-07-27 stsp if (err) {
4173 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4174 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4175 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4176 a9fa2909 2019-07-27 stsp goto done;
4177 a9fa2909 2019-07-27 stsp } else {
4178 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4179 a9fa2909 2019-07-27 stsp if (err)
4180 a9fa2909 2019-07-27 stsp goto done;
4181 a9fa2909 2019-07-27 stsp
4182 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4183 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4184 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4185 a9fa2909 2019-07-27 stsp goto done;
4186 a9fa2909 2019-07-27 stsp }
4187 a9fa2909 2019-07-27 stsp
4188 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4189 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4190 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4191 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4192 a9fa2909 2019-07-27 stsp goto done;
4193 a9fa2909 2019-07-27 stsp }
4194 a129376b 2019-03-28 stsp }
4195 a129376b 2019-03-28 stsp
4196 a129376b 2019-03-28 stsp switch (status) {
4197 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4198 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4199 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4200 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4201 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4202 33aa809d 2019-08-08 stsp if (err)
4203 33aa809d 2019-08-08 stsp goto done;
4204 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4205 33aa809d 2019-08-08 stsp break;
4206 33aa809d 2019-08-08 stsp }
4207 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4208 1f1abb7e 2019-08-08 stsp ie->path);
4209 1ee397ad 2019-07-12 stsp if (err)
4210 1ee397ad 2019-07-12 stsp goto done;
4211 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4212 a129376b 2019-03-28 stsp break;
4213 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4214 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4215 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4216 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4217 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4218 33aa809d 2019-08-08 stsp if (err)
4219 33aa809d 2019-08-08 stsp goto done;
4220 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4221 33aa809d 2019-08-08 stsp break;
4222 33aa809d 2019-08-08 stsp }
4223 33aa809d 2019-08-08 stsp /* fall through */
4224 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4225 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4226 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4227 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4228 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4229 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4230 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4231 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4232 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4233 24278f30 2019-08-03 stsp } else
4234 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4235 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4236 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4237 a129376b 2019-03-28 stsp if (err)
4238 65084dad 2019-08-08 stsp goto done;
4239 65084dad 2019-08-08 stsp
4240 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4241 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4242 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4243 a129376b 2019-03-28 stsp goto done;
4244 65084dad 2019-08-08 stsp }
4245 65084dad 2019-08-08 stsp
4246 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4247 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4248 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4249 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4250 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4251 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4252 33aa809d 2019-08-08 stsp break;
4253 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4254 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4255 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4256 33aa809d 2019-08-08 stsp goto done;
4257 33aa809d 2019-08-08 stsp }
4258 33aa809d 2019-08-08 stsp } else {
4259 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
4260 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4261 bd6aa359 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4262 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
4263 a129376b 2019-03-28 stsp if (err)
4264 a129376b 2019-03-28 stsp goto done;
4265 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4266 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4267 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4268 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4269 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4270 33aa809d 2019-08-08 stsp if (err)
4271 33aa809d 2019-08-08 stsp goto done;
4272 33aa809d 2019-08-08 stsp }
4273 a129376b 2019-03-28 stsp }
4274 a129376b 2019-03-28 stsp break;
4275 e20a8b6f 2019-06-04 stsp }
4276 a129376b 2019-03-28 stsp default:
4277 1f1abb7e 2019-08-08 stsp break;
4278 a129376b 2019-03-28 stsp }
4279 a129376b 2019-03-28 stsp done:
4280 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4281 33aa809d 2019-08-08 stsp free(path_content);
4282 e20a8b6f 2019-06-04 stsp free(parent_path);
4283 a129376b 2019-03-28 stsp free(tree_path);
4284 a129376b 2019-03-28 stsp if (blob)
4285 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4286 a129376b 2019-03-28 stsp if (tree)
4287 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4288 a129376b 2019-03-28 stsp free(tree_id);
4289 e20a8b6f 2019-06-04 stsp return err;
4290 e20a8b6f 2019-06-04 stsp }
4291 e20a8b6f 2019-06-04 stsp
4292 e20a8b6f 2019-06-04 stsp const struct got_error *
4293 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4294 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4295 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4296 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4297 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4298 e20a8b6f 2019-06-04 stsp {
4299 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4300 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4301 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4302 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4303 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4304 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4305 e20a8b6f 2019-06-04 stsp
4306 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4307 e20a8b6f 2019-06-04 stsp if (err)
4308 e20a8b6f 2019-06-04 stsp return err;
4309 e20a8b6f 2019-06-04 stsp
4310 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4311 e20a8b6f 2019-06-04 stsp if (err)
4312 e20a8b6f 2019-06-04 stsp goto done;
4313 e20a8b6f 2019-06-04 stsp
4314 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4315 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4316 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4317 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4318 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4319 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4320 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4321 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4322 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4323 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4324 e20a8b6f 2019-06-04 stsp if (err)
4325 af12c6b9 2019-06-04 stsp break;
4326 e20a8b6f 2019-06-04 stsp }
4327 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4328 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4329 e20a8b6f 2019-06-04 stsp err = sync_err;
4330 af12c6b9 2019-06-04 stsp done:
4331 fb399478 2019-07-12 stsp free(fileindex_path);
4332 a129376b 2019-03-28 stsp if (fileindex)
4333 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4334 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4335 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4336 a129376b 2019-03-28 stsp err = unlockerr;
4337 c4296144 2019-05-09 stsp return err;
4338 c4296144 2019-05-09 stsp }
4339 c4296144 2019-05-09 stsp
4340 cf066bf8 2019-05-09 stsp static void
4341 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4342 cf066bf8 2019-05-09 stsp {
4343 24519714 2019-05-09 stsp free(ct->path);
4344 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4345 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4346 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4347 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4348 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4349 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4350 cf066bf8 2019-05-09 stsp free(ct);
4351 cf066bf8 2019-05-09 stsp }
4352 24519714 2019-05-09 stsp
4353 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4354 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4355 24519714 2019-05-09 stsp struct got_repository *repo;
4356 24519714 2019-05-09 stsp struct got_worktree *worktree;
4357 5f8a88c6 2019-08-03 stsp int have_staged_files;
4358 24519714 2019-05-09 stsp };
4359 cf066bf8 2019-05-09 stsp
4360 c4296144 2019-05-09 stsp static const struct got_error *
4361 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4362 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4363 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4364 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4365 c4296144 2019-05-09 stsp {
4366 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4367 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4368 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4369 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4370 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4371 768aea60 2019-05-09 stsp struct stat sb;
4372 c4296144 2019-05-09 stsp
4373 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4374 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4375 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4376 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4377 5f8a88c6 2019-08-03 stsp return NULL;
4378 5f8a88c6 2019-08-03 stsp } else {
4379 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4380 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4381 c4296144 2019-05-09 stsp
4382 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4383 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4384 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4385 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4386 5f8a88c6 2019-08-03 stsp return NULL;
4387 5f8a88c6 2019-08-03 stsp }
4388 0b5cc0d6 2019-05-09 stsp
4389 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4390 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4391 036813ee 2019-05-09 stsp goto done;
4392 036813ee 2019-05-09 stsp }
4393 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4394 036813ee 2019-05-09 stsp parent_path = strdup("");
4395 69960a46 2019-05-09 stsp if (parent_path == NULL)
4396 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4397 69960a46 2019-05-09 stsp } else {
4398 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4399 69960a46 2019-05-09 stsp if (err)
4400 69960a46 2019-05-09 stsp return err;
4401 69960a46 2019-05-09 stsp }
4402 c4296144 2019-05-09 stsp
4403 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4404 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4405 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4406 c4296144 2019-05-09 stsp goto done;
4407 768aea60 2019-05-09 stsp }
4408 768aea60 2019-05-09 stsp
4409 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4410 768aea60 2019-05-09 stsp relpath) == -1) {
4411 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4412 768aea60 2019-05-09 stsp goto done;
4413 768aea60 2019-05-09 stsp }
4414 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4415 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4416 768aea60 2019-05-09 stsp } else {
4417 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4418 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4419 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4420 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4421 12463d8b 2019-12-13 stsp ct->ondisk_path);
4422 12463d8b 2019-12-13 stsp goto done;
4423 12463d8b 2019-12-13 stsp }
4424 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4425 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4426 768aea60 2019-05-09 stsp goto done;
4427 768aea60 2019-05-09 stsp }
4428 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4429 c4296144 2019-05-09 stsp }
4430 c4296144 2019-05-09 stsp
4431 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4432 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4433 44d03001 2019-05-09 stsp relpath) == -1) {
4434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4435 44d03001 2019-05-09 stsp goto done;
4436 44d03001 2019-05-09 stsp }
4437 44d03001 2019-05-09 stsp
4438 cf066bf8 2019-05-09 stsp ct->status = status;
4439 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4440 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4441 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4442 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4443 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4444 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4445 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4446 b416585c 2019-05-13 stsp goto done;
4447 b416585c 2019-05-13 stsp }
4448 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4449 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4450 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4451 f0b75401 2019-08-03 stsp goto done;
4452 f0b75401 2019-08-03 stsp }
4453 f0b75401 2019-08-03 stsp }
4454 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4455 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4456 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4457 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4458 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4459 036813ee 2019-05-09 stsp goto done;
4460 036813ee 2019-05-09 stsp }
4461 ca2503ea 2019-05-09 stsp }
4462 24519714 2019-05-09 stsp ct->path = strdup(path);
4463 24519714 2019-05-09 stsp if (ct->path == NULL) {
4464 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4465 24519714 2019-05-09 stsp goto done;
4466 24519714 2019-05-09 stsp }
4467 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4468 c4296144 2019-05-09 stsp done:
4469 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4470 ed175427 2019-05-09 stsp free_commitable(ct);
4471 24519714 2019-05-09 stsp free(parent_path);
4472 036813ee 2019-05-09 stsp free(path);
4473 a129376b 2019-03-28 stsp return err;
4474 a129376b 2019-03-28 stsp }
4475 c4296144 2019-05-09 stsp
4476 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4477 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4478 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4479 036813ee 2019-05-09 stsp struct got_repository *);
4480 ed175427 2019-05-09 stsp
4481 ed175427 2019-05-09 stsp static const struct got_error *
4482 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4483 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4484 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4485 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4486 afa376bf 2019-05-09 stsp struct got_repository *repo)
4487 ed175427 2019-05-09 stsp {
4488 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4489 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4490 036813ee 2019-05-09 stsp char *subpath;
4491 ed175427 2019-05-09 stsp
4492 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4493 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4494 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4495 ed175427 2019-05-09 stsp
4496 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4497 ed175427 2019-05-09 stsp if (err)
4498 ed175427 2019-05-09 stsp return err;
4499 ed175427 2019-05-09 stsp
4500 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4501 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4502 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4503 036813ee 2019-05-09 stsp free(subpath);
4504 036813ee 2019-05-09 stsp return err;
4505 036813ee 2019-05-09 stsp }
4506 ed175427 2019-05-09 stsp
4507 036813ee 2019-05-09 stsp static const struct got_error *
4508 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4509 036813ee 2019-05-09 stsp {
4510 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4511 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4512 ed175427 2019-05-09 stsp
4513 036813ee 2019-05-09 stsp *match = 0;
4514 ed175427 2019-05-09 stsp
4515 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4516 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4517 0f63689d 2019-05-10 stsp return NULL;
4518 036813ee 2019-05-09 stsp }
4519 0b5cc0d6 2019-05-09 stsp
4520 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4521 0f63689d 2019-05-10 stsp if (err)
4522 0f63689d 2019-05-10 stsp return err;
4523 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4524 036813ee 2019-05-09 stsp free(ct_parent_path);
4525 036813ee 2019-05-09 stsp return err;
4526 036813ee 2019-05-09 stsp }
4527 0b5cc0d6 2019-05-09 stsp
4528 768aea60 2019-05-09 stsp static mode_t
4529 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4530 768aea60 2019-05-09 stsp {
4531 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4532 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4533 3d9a4ec4 2020-07-23 stsp
4534 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4535 768aea60 2019-05-09 stsp }
4536 768aea60 2019-05-09 stsp
4537 036813ee 2019-05-09 stsp static const struct got_error *
4538 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4539 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4540 036813ee 2019-05-09 stsp {
4541 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4542 ca2503ea 2019-05-09 stsp
4543 036813ee 2019-05-09 stsp *new_te = NULL;
4544 0b5cc0d6 2019-05-09 stsp
4545 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4546 036813ee 2019-05-09 stsp if (err)
4547 036813ee 2019-05-09 stsp goto done;
4548 0b5cc0d6 2019-05-09 stsp
4549 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4550 036813ee 2019-05-09 stsp
4551 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4552 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4553 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4554 5f8a88c6 2019-08-03 stsp else
4555 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4556 036813ee 2019-05-09 stsp done:
4557 036813ee 2019-05-09 stsp if (err && *new_te) {
4558 56e0773d 2019-11-28 stsp free(*new_te);
4559 036813ee 2019-05-09 stsp *new_te = NULL;
4560 036813ee 2019-05-09 stsp }
4561 036813ee 2019-05-09 stsp return err;
4562 036813ee 2019-05-09 stsp }
4563 036813ee 2019-05-09 stsp
4564 036813ee 2019-05-09 stsp static const struct got_error *
4565 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4566 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4567 036813ee 2019-05-09 stsp {
4568 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4569 036813ee 2019-05-09 stsp char *ct_name;
4570 036813ee 2019-05-09 stsp
4571 036813ee 2019-05-09 stsp *new_te = NULL;
4572 036813ee 2019-05-09 stsp
4573 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4574 036813ee 2019-05-09 stsp if (*new_te == NULL)
4575 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4576 036813ee 2019-05-09 stsp
4577 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4578 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4579 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4580 036813ee 2019-05-09 stsp goto done;
4581 036813ee 2019-05-09 stsp }
4582 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4583 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4584 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4585 036813ee 2019-05-09 stsp goto done;
4586 036813ee 2019-05-09 stsp }
4587 036813ee 2019-05-09 stsp
4588 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4589 036813ee 2019-05-09 stsp
4590 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4591 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4592 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4593 5f8a88c6 2019-08-03 stsp else
4594 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4595 036813ee 2019-05-09 stsp done:
4596 036813ee 2019-05-09 stsp if (err && *new_te) {
4597 56e0773d 2019-11-28 stsp free(*new_te);
4598 036813ee 2019-05-09 stsp *new_te = NULL;
4599 036813ee 2019-05-09 stsp }
4600 036813ee 2019-05-09 stsp return err;
4601 036813ee 2019-05-09 stsp }
4602 036813ee 2019-05-09 stsp
4603 036813ee 2019-05-09 stsp static const struct got_error *
4604 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4605 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4606 036813ee 2019-05-09 stsp {
4607 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4608 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4609 036813ee 2019-05-09 stsp
4610 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4611 036813ee 2019-05-09 stsp if (err)
4612 036813ee 2019-05-09 stsp return err;
4613 036813ee 2019-05-09 stsp if (new_pe == NULL)
4614 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4615 036813ee 2019-05-09 stsp return NULL;
4616 afa376bf 2019-05-09 stsp }
4617 afa376bf 2019-05-09 stsp
4618 afa376bf 2019-05-09 stsp static const struct got_error *
4619 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4620 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4621 afa376bf 2019-05-09 stsp {
4622 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4623 5f8a88c6 2019-08-03 stsp unsigned char status;
4624 5f8a88c6 2019-08-03 stsp
4625 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4626 afa376bf 2019-05-09 stsp ct_path++;
4627 5f8a88c6 2019-08-03 stsp
4628 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4629 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4630 5f8a88c6 2019-08-03 stsp else
4631 5f8a88c6 2019-08-03 stsp status = ct->status;
4632 5f8a88c6 2019-08-03 stsp
4633 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4634 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4635 036813ee 2019-05-09 stsp }
4636 036813ee 2019-05-09 stsp
4637 036813ee 2019-05-09 stsp static const struct got_error *
4638 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4639 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4640 44d03001 2019-05-09 stsp {
4641 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4642 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4643 44d03001 2019-05-09 stsp char *te_path;
4644 44d03001 2019-05-09 stsp
4645 44d03001 2019-05-09 stsp *modified = 0;
4646 44d03001 2019-05-09 stsp
4647 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4648 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4649 44d03001 2019-05-09 stsp te->name) == -1)
4650 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4651 44d03001 2019-05-09 stsp
4652 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4653 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4654 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4655 44d03001 2019-05-09 stsp strlen(te_path));
4656 44d03001 2019-05-09 stsp if (*modified)
4657 44d03001 2019-05-09 stsp break;
4658 44d03001 2019-05-09 stsp }
4659 44d03001 2019-05-09 stsp
4660 44d03001 2019-05-09 stsp free(te_path);
4661 44d03001 2019-05-09 stsp return err;
4662 44d03001 2019-05-09 stsp }
4663 44d03001 2019-05-09 stsp
4664 44d03001 2019-05-09 stsp static const struct got_error *
4665 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4666 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4667 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4668 036813ee 2019-05-09 stsp {
4669 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4670 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4671 036813ee 2019-05-09 stsp
4672 036813ee 2019-05-09 stsp *ctp = NULL;
4673 036813ee 2019-05-09 stsp
4674 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4675 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4676 036813ee 2019-05-09 stsp char *ct_name = NULL;
4677 036813ee 2019-05-09 stsp int path_matches;
4678 036813ee 2019-05-09 stsp
4679 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4680 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4681 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4682 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4683 5f8a88c6 2019-08-03 stsp continue;
4684 5f8a88c6 2019-08-03 stsp } else {
4685 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4686 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4687 5f8a88c6 2019-08-03 stsp continue;
4688 5f8a88c6 2019-08-03 stsp }
4689 036813ee 2019-05-09 stsp
4690 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4691 036813ee 2019-05-09 stsp continue;
4692 036813ee 2019-05-09 stsp
4693 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4694 036813ee 2019-05-09 stsp if (err)
4695 036813ee 2019-05-09 stsp return err;
4696 036813ee 2019-05-09 stsp if (!path_matches)
4697 036813ee 2019-05-09 stsp continue;
4698 036813ee 2019-05-09 stsp
4699 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4700 036813ee 2019-05-09 stsp if (ct_name == NULL)
4701 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4702 036813ee 2019-05-09 stsp
4703 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4704 036813ee 2019-05-09 stsp continue;
4705 036813ee 2019-05-09 stsp
4706 036813ee 2019-05-09 stsp *ctp = ct;
4707 036813ee 2019-05-09 stsp break;
4708 036813ee 2019-05-09 stsp }
4709 2b496619 2019-07-10 stsp
4710 2b496619 2019-07-10 stsp return err;
4711 2b496619 2019-07-10 stsp }
4712 2b496619 2019-07-10 stsp
4713 2b496619 2019-07-10 stsp static const struct got_error *
4714 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4715 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4716 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4717 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4718 2b496619 2019-07-10 stsp struct got_repository *repo)
4719 2b496619 2019-07-10 stsp {
4720 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4721 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4722 2b496619 2019-07-10 stsp char *subtree_path;
4723 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4724 ba580f68 2020-03-22 stsp int nentries;
4725 2b496619 2019-07-10 stsp
4726 2b496619 2019-07-10 stsp *new_tep = NULL;
4727 2b496619 2019-07-10 stsp
4728 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4729 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4730 2b496619 2019-07-10 stsp child_path) == -1)
4731 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4732 036813ee 2019-05-09 stsp
4733 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4734 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4735 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4736 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4737 56e0773d 2019-11-28 stsp
4738 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4739 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4740 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4741 2b496619 2019-07-10 stsp goto done;
4742 2b496619 2019-07-10 stsp }
4743 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4744 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4745 2b496619 2019-07-10 stsp if (err) {
4746 56e0773d 2019-11-28 stsp free(new_te);
4747 2b496619 2019-07-10 stsp goto done;
4748 2b496619 2019-07-10 stsp }
4749 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4750 2b496619 2019-07-10 stsp done:
4751 56e0773d 2019-11-28 stsp free(id);
4752 2b496619 2019-07-10 stsp free(subtree_path);
4753 2b496619 2019-07-10 stsp if (err == NULL)
4754 2b496619 2019-07-10 stsp *new_tep = new_te;
4755 036813ee 2019-05-09 stsp return err;
4756 036813ee 2019-05-09 stsp }
4757 036813ee 2019-05-09 stsp
4758 036813ee 2019-05-09 stsp static const struct got_error *
4759 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4760 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4761 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4762 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4763 036813ee 2019-05-09 stsp struct got_repository *repo)
4764 036813ee 2019-05-09 stsp {
4765 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4766 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4767 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4768 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4769 036813ee 2019-05-09 stsp
4770 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4771 ba580f68 2020-03-22 stsp *nentries = 0;
4772 036813ee 2019-05-09 stsp
4773 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4774 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4775 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4776 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4777 036813ee 2019-05-09 stsp
4778 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4779 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4780 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4781 036813ee 2019-05-09 stsp continue;
4782 036813ee 2019-05-09 stsp
4783 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4784 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4785 036813ee 2019-05-09 stsp continue;
4786 036813ee 2019-05-09 stsp
4787 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4788 036813ee 2019-05-09 stsp pe->path);
4789 036813ee 2019-05-09 stsp if (err)
4790 036813ee 2019-05-09 stsp goto done;
4791 036813ee 2019-05-09 stsp
4792 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4793 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4794 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4795 036813ee 2019-05-09 stsp if (err)
4796 036813ee 2019-05-09 stsp goto done;
4797 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4798 afa376bf 2019-05-09 stsp if (err)
4799 afa376bf 2019-05-09 stsp goto done;
4800 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4801 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4802 2b496619 2019-07-10 stsp if (err)
4803 2f51b5b3 2019-05-09 stsp goto done;
4804 ba580f68 2020-03-22 stsp (*nentries)++;
4805 2b496619 2019-07-10 stsp } else {
4806 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4807 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4808 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4809 2b496619 2019-07-10 stsp == NULL) {
4810 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4811 2b496619 2019-07-10 stsp child_path, path_base_tree,
4812 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4813 2b496619 2019-07-10 stsp repo);
4814 2b496619 2019-07-10 stsp if (err)
4815 2b496619 2019-07-10 stsp goto done;
4816 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4817 2b496619 2019-07-10 stsp if (err)
4818 2b496619 2019-07-10 stsp goto done;
4819 ba580f68 2020-03-22 stsp (*nentries)++;
4820 9ba0479c 2019-05-10 stsp }
4821 ed175427 2019-05-09 stsp }
4822 2f51b5b3 2019-05-09 stsp }
4823 2f51b5b3 2019-05-09 stsp
4824 2f51b5b3 2019-05-09 stsp if (base_tree) {
4825 56e0773d 2019-11-28 stsp int i, nbase_entries;
4826 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4827 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4828 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4829 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4830 63c5ca5d 2019-08-24 stsp
4831 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4832 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4833 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4834 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4835 63c5ca5d 2019-08-24 stsp if (err)
4836 63c5ca5d 2019-08-24 stsp goto done;
4837 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4838 63c5ca5d 2019-08-24 stsp if (err)
4839 63c5ca5d 2019-08-24 stsp goto done;
4840 ba580f68 2020-03-22 stsp (*nentries)++;
4841 63c5ca5d 2019-08-24 stsp continue;
4842 63c5ca5d 2019-08-24 stsp }
4843 2f51b5b3 2019-05-09 stsp
4844 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4845 44d03001 2019-05-09 stsp int modified;
4846 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4847 036813ee 2019-05-09 stsp if (err)
4848 036813ee 2019-05-09 stsp goto done;
4849 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4850 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4851 2f51b5b3 2019-05-09 stsp if (err)
4852 2f51b5b3 2019-05-09 stsp goto done;
4853 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4854 44d03001 2019-05-09 stsp if (modified) {
4855 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4856 ba580f68 2020-03-22 stsp int nsubentries;
4857 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4858 ba580f68 2020-03-22 stsp &nsubentries, te,
4859 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4860 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4861 44d03001 2019-05-09 stsp if (err)
4862 44d03001 2019-05-09 stsp goto done;
4863 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4864 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4865 ba580f68 2020-03-22 stsp free(new_id);
4866 ba580f68 2020-03-22 stsp continue;
4867 ba580f68 2020-03-22 stsp }
4868 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4869 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4870 56e0773d 2019-11-28 stsp free(new_id);
4871 44d03001 2019-05-09 stsp }
4872 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4873 036813ee 2019-05-09 stsp if (err)
4874 036813ee 2019-05-09 stsp goto done;
4875 ba580f68 2020-03-22 stsp (*nentries)++;
4876 2f51b5b3 2019-05-09 stsp continue;
4877 036813ee 2019-05-09 stsp }
4878 2f51b5b3 2019-05-09 stsp
4879 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4880 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4881 f66c734c 2019-09-22 stsp if (err)
4882 f66c734c 2019-09-22 stsp goto done;
4883 2f51b5b3 2019-05-09 stsp if (ct) {
4884 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4885 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4886 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4887 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4888 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4889 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4890 2f51b5b3 2019-05-09 stsp if (err)
4891 2f51b5b3 2019-05-09 stsp goto done;
4892 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4893 2f51b5b3 2019-05-09 stsp if (err)
4894 2f51b5b3 2019-05-09 stsp goto done;
4895 ba580f68 2020-03-22 stsp (*nentries)++;
4896 2f51b5b3 2019-05-09 stsp }
4897 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4898 b416585c 2019-05-13 stsp status_arg);
4899 afa376bf 2019-05-09 stsp if (err)
4900 afa376bf 2019-05-09 stsp goto done;
4901 2f51b5b3 2019-05-09 stsp } else {
4902 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4903 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4904 2f51b5b3 2019-05-09 stsp if (err)
4905 2f51b5b3 2019-05-09 stsp goto done;
4906 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4907 2f51b5b3 2019-05-09 stsp if (err)
4908 2f51b5b3 2019-05-09 stsp goto done;
4909 ba580f68 2020-03-22 stsp (*nentries)++;
4910 2f51b5b3 2019-05-09 stsp }
4911 ca2503ea 2019-05-09 stsp }
4912 ed175427 2019-05-09 stsp }
4913 0b5cc0d6 2019-05-09 stsp
4914 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4915 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4916 ed175427 2019-05-09 stsp done:
4917 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4918 ed175427 2019-05-09 stsp return err;
4919 ed175427 2019-05-09 stsp }
4920 ed175427 2019-05-09 stsp
4921 ebf99748 2019-05-09 stsp static const struct got_error *
4922 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4923 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4924 72fd46fa 2019-09-06 stsp int have_staged_files)
4925 ebf99748 2019-05-09 stsp {
4926 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4927 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4928 ebf99748 2019-05-09 stsp
4929 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4930 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4931 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4932 ebf99748 2019-05-09 stsp
4933 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4934 ebf99748 2019-05-09 stsp if (ie) {
4935 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4936 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4937 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4938 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4939 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4940 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4941 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4942 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4943 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4944 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4945 72fd46fa 2019-09-06 stsp !have_staged_files);
4946 ebf99748 2019-05-09 stsp } else
4947 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4948 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4949 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4950 72fd46fa 2019-09-06 stsp !have_staged_files);
4951 ebf99748 2019-05-09 stsp } else {
4952 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
4953 ebf99748 2019-05-09 stsp if (err)
4954 af12c6b9 2019-06-04 stsp break;
4955 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
4956 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4957 3969253a 2020-03-07 stsp if (err) {
4958 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4959 3969253a 2020-03-07 stsp break;
4960 3969253a 2020-03-07 stsp }
4961 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
4962 3969253a 2020-03-07 stsp if (err) {
4963 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4964 af12c6b9 2019-06-04 stsp break;
4965 3969253a 2020-03-07 stsp }
4966 ebf99748 2019-05-09 stsp }
4967 ebf99748 2019-05-09 stsp }
4968 d56d26ce 2019-05-10 stsp return err;
4969 d56d26ce 2019-05-10 stsp }
4970 735ef5ac 2019-08-03 stsp
4971 d56d26ce 2019-05-10 stsp
4972 d56d26ce 2019-05-10 stsp static const struct got_error *
4973 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4974 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4975 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4976 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4977 735ef5ac 2019-08-03 stsp int ood_errcode)
4978 d56d26ce 2019-05-10 stsp {
4979 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4980 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4981 1a36436d 2019-06-10 stsp
4982 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4983 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4984 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4985 1a36436d 2019-06-10 stsp return NULL;
4986 9bead371 2019-07-28 stsp /*
4987 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4988 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4989 9bead371 2019-07-28 stsp */
4990 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4991 735ef5ac 2019-08-03 stsp in_repo_path);
4992 9bead371 2019-07-28 stsp if (err) {
4993 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4994 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4995 1a36436d 2019-06-10 stsp goto done;
4996 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4997 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4998 1a36436d 2019-06-10 stsp } else {
4999 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5000 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5001 735ef5ac 2019-08-03 stsp in_repo_path);
5002 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5003 1a36436d 2019-06-10 stsp goto done;
5004 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5005 1a36436d 2019-06-10 stsp }
5006 1a36436d 2019-06-10 stsp done:
5007 1a36436d 2019-06-10 stsp free(id);
5008 1a36436d 2019-06-10 stsp return err;
5009 ebf99748 2019-05-09 stsp }
5010 ebf99748 2019-05-09 stsp
5011 c4296144 2019-05-09 stsp const struct got_error *
5012 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5013 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5014 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5015 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5016 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5017 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5018 afa376bf 2019-05-09 stsp struct got_repository *repo)
5019 c4296144 2019-05-09 stsp {
5020 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5021 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5022 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5023 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5024 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5025 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5026 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5027 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5028 ba580f68 2020-03-22 stsp int nentries;
5029 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5030 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5031 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5032 c4296144 2019-05-09 stsp
5033 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5034 c4296144 2019-05-09 stsp
5035 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5036 675c7539 2019-05-09 stsp
5037 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5038 588edf97 2019-05-10 stsp if (err)
5039 588edf97 2019-05-10 stsp goto done;
5040 de18fc63 2019-05-09 stsp
5041 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5042 588edf97 2019-05-10 stsp if (err)
5043 588edf97 2019-05-10 stsp goto done;
5044 588edf97 2019-05-10 stsp
5045 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5046 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5047 33ad4cbe 2019-05-12 jcs if (err)
5048 33ad4cbe 2019-05-12 jcs goto done;
5049 33ad4cbe 2019-05-12 jcs }
5050 c4296144 2019-05-09 stsp
5051 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5052 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5053 33ad4cbe 2019-05-12 jcs goto done;
5054 33ad4cbe 2019-05-12 jcs }
5055 33ad4cbe 2019-05-12 jcs
5056 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5057 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5058 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5059 cf066bf8 2019-05-09 stsp char *ondisk_path;
5060 cf066bf8 2019-05-09 stsp
5061 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5062 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5063 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5064 5f8a88c6 2019-08-03 stsp continue;
5065 5f8a88c6 2019-08-03 stsp
5066 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5067 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5068 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5069 cf066bf8 2019-05-09 stsp continue;
5070 cf066bf8 2019-05-09 stsp
5071 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5072 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5073 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5074 cf066bf8 2019-05-09 stsp goto done;
5075 cf066bf8 2019-05-09 stsp }
5076 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5077 bd6aa359 2020-07-23 stsp if (err) {
5078 bd6aa359 2020-07-23 stsp free(ondisk_path);
5079 cf066bf8 2019-05-09 stsp goto done;
5080 bd6aa359 2020-07-23 stsp }
5081 bd6aa359 2020-07-23 stsp
5082 bd6aa359 2020-07-23 stsp /*
5083 bd6aa359 2020-07-23 stsp * When comitting a symlink we convert "bad" symlinks (those
5084 bd6aa359 2020-07-23 stsp * which point outside the work tree or into .got) to regular
5085 bd6aa359 2020-07-23 stsp * files. This way, the post-commit work tree state matches
5086 bd6aa359 2020-07-23 stsp * a fresh checkout of the tree which was committed.
5087 bd6aa359 2020-07-23 stsp */
5088 bd6aa359 2020-07-23 stsp if (S_ISLNK(get_ct_file_mode(ct))) {
5089 bd6aa359 2020-07-23 stsp struct got_blob_object *blob;
5090 bd6aa359 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5091 bd6aa359 2020-07-23 stsp PATH_MAX);
5092 bd6aa359 2020-07-23 stsp if (err) {
5093 bd6aa359 2020-07-23 stsp free(ondisk_path);
5094 bd6aa359 2020-07-23 stsp goto done;
5095 bd6aa359 2020-07-23 stsp }
5096 bd6aa359 2020-07-23 stsp err = install_symlink(worktree, ondisk_path, ct->path,
5097 bd6aa359 2020-07-23 stsp get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5098 bd6aa359 2020-07-23 stsp 0, 0, repo, NULL, NULL);
5099 bd6aa359 2020-07-23 stsp got_object_blob_close(blob);
5100 bd6aa359 2020-07-23 stsp if (err) {
5101 bd6aa359 2020-07-23 stsp free(ondisk_path);
5102 bd6aa359 2020-07-23 stsp goto done;
5103 bd6aa359 2020-07-23 stsp }
5104 bd6aa359 2020-07-23 stsp }
5105 bd6aa359 2020-07-23 stsp free(ondisk_path);
5106 cf066bf8 2019-05-09 stsp }
5107 036813ee 2019-05-09 stsp
5108 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5109 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5110 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5111 036813ee 2019-05-09 stsp if (err)
5112 036813ee 2019-05-09 stsp goto done;
5113 036813ee 2019-05-09 stsp
5114 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5115 de18fc63 2019-05-09 stsp if (err)
5116 de18fc63 2019-05-09 stsp goto done;
5117 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5118 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5119 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5120 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5121 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5122 33ad4cbe 2019-05-12 jcs free(logmsg);
5123 9d40349a 2019-05-09 stsp if (err)
5124 9d40349a 2019-05-09 stsp goto done;
5125 9d40349a 2019-05-09 stsp
5126 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5127 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5128 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5129 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5130 09f5bd90 2019-05-09 stsp goto done;
5131 09f5bd90 2019-05-09 stsp }
5132 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5133 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5134 09f5bd90 2019-05-09 stsp if (err)
5135 09f5bd90 2019-05-09 stsp goto done;
5136 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5137 ebf99748 2019-05-09 stsp if (err)
5138 ebf99748 2019-05-09 stsp goto done;
5139 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5140 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5141 09f5bd90 2019-05-09 stsp goto done;
5142 09f5bd90 2019-05-09 stsp }
5143 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5144 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5145 f2c16586 2019-05-09 stsp if (err)
5146 f2c16586 2019-05-09 stsp goto done;
5147 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5148 f2c16586 2019-05-09 stsp if (err)
5149 f2c16586 2019-05-09 stsp goto done;
5150 f2c16586 2019-05-09 stsp
5151 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5152 09f5bd90 2019-05-09 stsp if (err)
5153 09f5bd90 2019-05-09 stsp goto done;
5154 09f5bd90 2019-05-09 stsp
5155 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5156 ebf99748 2019-05-09 stsp if (err)
5157 ebf99748 2019-05-09 stsp goto done;
5158 c4296144 2019-05-09 stsp done:
5159 588edf97 2019-05-10 stsp if (head_tree)
5160 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5161 588edf97 2019-05-10 stsp if (head_commit)
5162 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5163 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5164 2f17228e 2019-05-12 stsp if (head_ref2) {
5165 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5166 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5167 2f17228e 2019-05-12 stsp err = unlockerr;
5168 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5169 39cd0ff6 2019-07-12 stsp }
5170 39cd0ff6 2019-07-12 stsp return err;
5171 39cd0ff6 2019-07-12 stsp }
5172 5c1e53bc 2019-07-28 stsp
5173 5c1e53bc 2019-07-28 stsp static const struct got_error *
5174 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5175 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5176 5c1e53bc 2019-07-28 stsp {
5177 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5178 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5179 39cd0ff6 2019-07-12 stsp
5180 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5181 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5182 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5183 5c1e53bc 2019-07-28 stsp
5184 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5185 5c1e53bc 2019-07-28 stsp ct_path++;
5186 5c1e53bc 2019-07-28 stsp
5187 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5188 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5189 5c1e53bc 2019-07-28 stsp break;
5190 5c1e53bc 2019-07-28 stsp }
5191 5c1e53bc 2019-07-28 stsp
5192 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5193 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5194 5c1e53bc 2019-07-28 stsp
5195 5c1e53bc 2019-07-28 stsp return NULL;
5196 5c1e53bc 2019-07-28 stsp }
5197 5c1e53bc 2019-07-28 stsp
5198 f0b75401 2019-08-03 stsp static const struct got_error *
5199 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5200 f0b75401 2019-08-03 stsp {
5201 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5202 f0b75401 2019-08-03 stsp
5203 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5204 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5205 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5206 f0b75401 2019-08-03 stsp }
5207 f0b75401 2019-08-03 stsp
5208 f0b75401 2019-08-03 stsp return NULL;
5209 f0b75401 2019-08-03 stsp }
5210 f0b75401 2019-08-03 stsp
5211 5f8a88c6 2019-08-03 stsp static const struct got_error *
5212 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5213 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5214 5f8a88c6 2019-08-03 stsp {
5215 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5216 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5217 5f8a88c6 2019-08-03 stsp
5218 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5219 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5220 5f8a88c6 2019-08-03 stsp continue;
5221 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5222 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5223 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5224 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5225 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5226 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5227 5f8a88c6 2019-08-03 stsp }
5228 5f8a88c6 2019-08-03 stsp
5229 5f8a88c6 2019-08-03 stsp return NULL;
5230 5f8a88c6 2019-08-03 stsp }
5231 5f8a88c6 2019-08-03 stsp
5232 39cd0ff6 2019-07-12 stsp const struct got_error *
5233 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5234 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5235 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5236 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5237 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5238 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5239 39cd0ff6 2019-07-12 stsp {
5240 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5241 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5242 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5243 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5244 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5245 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5246 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5247 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5248 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5249 39cd0ff6 2019-07-12 stsp
5250 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5251 39cd0ff6 2019-07-12 stsp
5252 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5253 39cd0ff6 2019-07-12 stsp
5254 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5255 39cd0ff6 2019-07-12 stsp if (err)
5256 39cd0ff6 2019-07-12 stsp goto done;
5257 39cd0ff6 2019-07-12 stsp
5258 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5259 39cd0ff6 2019-07-12 stsp if (err)
5260 39cd0ff6 2019-07-12 stsp goto done;
5261 39cd0ff6 2019-07-12 stsp
5262 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5263 39cd0ff6 2019-07-12 stsp if (err)
5264 39cd0ff6 2019-07-12 stsp goto done;
5265 39cd0ff6 2019-07-12 stsp
5266 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5267 39cd0ff6 2019-07-12 stsp if (err)
5268 39cd0ff6 2019-07-12 stsp goto done;
5269 39cd0ff6 2019-07-12 stsp
5270 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5271 5f8a88c6 2019-08-03 stsp &have_staged_files);
5272 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5273 5f8a88c6 2019-08-03 stsp goto done;
5274 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5275 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5276 5f8a88c6 2019-08-03 stsp if (err)
5277 5f8a88c6 2019-08-03 stsp goto done;
5278 5f8a88c6 2019-08-03 stsp }
5279 5f8a88c6 2019-08-03 stsp
5280 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5281 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5282 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5283 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5284 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5285 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5286 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5287 5c1e53bc 2019-07-28 stsp if (err)
5288 5c1e53bc 2019-07-28 stsp goto done;
5289 5c1e53bc 2019-07-28 stsp }
5290 39cd0ff6 2019-07-12 stsp
5291 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5292 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5293 39cd0ff6 2019-07-12 stsp goto done;
5294 5c1e53bc 2019-07-28 stsp }
5295 5c1e53bc 2019-07-28 stsp
5296 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5297 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5298 5c1e53bc 2019-07-28 stsp if (err)
5299 5c1e53bc 2019-07-28 stsp goto done;
5300 39cd0ff6 2019-07-12 stsp }
5301 f0b75401 2019-08-03 stsp
5302 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5303 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5304 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5305 f0b75401 2019-08-03 stsp
5306 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5307 f0b75401 2019-08-03 stsp ct_path++;
5308 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5309 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5310 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5311 b50cabdf 2019-07-12 stsp if (err)
5312 b50cabdf 2019-07-12 stsp goto done;
5313 f0b75401 2019-08-03 stsp
5314 b50cabdf 2019-07-12 stsp }
5315 b50cabdf 2019-07-12 stsp
5316 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5317 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5318 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5319 39cd0ff6 2019-07-12 stsp if (err)
5320 39cd0ff6 2019-07-12 stsp goto done;
5321 39cd0ff6 2019-07-12 stsp
5322 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5323 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5324 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5325 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5326 39cd0ff6 2019-07-12 stsp err = sync_err;
5327 39cd0ff6 2019-07-12 stsp done:
5328 39cd0ff6 2019-07-12 stsp if (fileindex)
5329 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5330 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5331 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5332 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5333 39cd0ff6 2019-07-12 stsp err = unlockerr;
5334 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5335 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5336 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5337 39cd0ff6 2019-07-12 stsp }
5338 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5339 c4296144 2019-05-09 stsp return err;
5340 8656d6c4 2019-05-20 stsp }
5341 8656d6c4 2019-05-20 stsp
5342 8656d6c4 2019-05-20 stsp const char *
5343 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5344 8656d6c4 2019-05-20 stsp {
5345 8656d6c4 2019-05-20 stsp return ct->path;
5346 8656d6c4 2019-05-20 stsp }
5347 8656d6c4 2019-05-20 stsp
5348 8656d6c4 2019-05-20 stsp unsigned int
5349 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5350 8656d6c4 2019-05-20 stsp {
5351 8656d6c4 2019-05-20 stsp return ct->status;
5352 818c7501 2019-07-11 stsp }
5353 818c7501 2019-07-11 stsp
5354 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5355 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5356 818c7501 2019-07-11 stsp struct got_repository *repo;
5357 818c7501 2019-07-11 stsp };
5358 818c7501 2019-07-11 stsp
5359 818c7501 2019-07-11 stsp static const struct got_error *
5360 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5361 818c7501 2019-07-11 stsp {
5362 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5363 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5364 818c7501 2019-07-11 stsp unsigned char status;
5365 818c7501 2019-07-11 stsp struct stat sb;
5366 818c7501 2019-07-11 stsp char *ondisk_path;
5367 818c7501 2019-07-11 stsp
5368 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5369 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5370 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5371 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5372 818c7501 2019-07-11 stsp
5373 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5374 818c7501 2019-07-11 stsp == -1)
5375 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5376 818c7501 2019-07-11 stsp
5377 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5378 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5379 818c7501 2019-07-11 stsp free(ondisk_path);
5380 818c7501 2019-07-11 stsp if (err)
5381 818c7501 2019-07-11 stsp return err;
5382 818c7501 2019-07-11 stsp
5383 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5384 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5385 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5386 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5387 818c7501 2019-07-11 stsp
5388 818c7501 2019-07-11 stsp return NULL;
5389 818c7501 2019-07-11 stsp }
5390 818c7501 2019-07-11 stsp
5391 818c7501 2019-07-11 stsp const struct got_error *
5392 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5393 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5394 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5395 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5396 818c7501 2019-07-11 stsp {
5397 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5398 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5399 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5400 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5401 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5402 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5403 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5404 818c7501 2019-07-11 stsp
5405 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5406 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5407 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5408 818c7501 2019-07-11 stsp
5409 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5410 818c7501 2019-07-11 stsp if (err)
5411 818c7501 2019-07-11 stsp return err;
5412 818c7501 2019-07-11 stsp
5413 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5414 818c7501 2019-07-11 stsp if (err)
5415 818c7501 2019-07-11 stsp goto done;
5416 818c7501 2019-07-11 stsp
5417 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5418 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5419 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5420 818c7501 2019-07-11 stsp &ok_arg);
5421 818c7501 2019-07-11 stsp if (err)
5422 818c7501 2019-07-11 stsp goto done;
5423 818c7501 2019-07-11 stsp
5424 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5425 818c7501 2019-07-11 stsp if (err)
5426 818c7501 2019-07-11 stsp goto done;
5427 818c7501 2019-07-11 stsp
5428 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5429 818c7501 2019-07-11 stsp if (err)
5430 818c7501 2019-07-11 stsp goto done;
5431 818c7501 2019-07-11 stsp
5432 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5433 818c7501 2019-07-11 stsp if (err)
5434 818c7501 2019-07-11 stsp goto done;
5435 818c7501 2019-07-11 stsp
5436 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5437 818c7501 2019-07-11 stsp 0);
5438 e51d7b55 2020-01-04 stsp if (err)
5439 e51d7b55 2020-01-04 stsp goto done;
5440 e51d7b55 2020-01-04 stsp
5441 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5442 818c7501 2019-07-11 stsp if (err)
5443 818c7501 2019-07-11 stsp goto done;
5444 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5445 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5446 e51d7b55 2020-01-04 stsp goto done;
5447 e51d7b55 2020-01-04 stsp }
5448 818c7501 2019-07-11 stsp
5449 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5450 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5451 818c7501 2019-07-11 stsp if (err)
5452 818c7501 2019-07-11 stsp goto done;
5453 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5454 818c7501 2019-07-11 stsp if (err)
5455 818c7501 2019-07-11 stsp goto done;
5456 818c7501 2019-07-11 stsp
5457 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5458 818c7501 2019-07-11 stsp
5459 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5460 818c7501 2019-07-11 stsp if (err)
5461 818c7501 2019-07-11 stsp goto done;
5462 818c7501 2019-07-11 stsp
5463 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5464 818c7501 2019-07-11 stsp if (err)
5465 818c7501 2019-07-11 stsp goto done;
5466 818c7501 2019-07-11 stsp
5467 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5468 818c7501 2019-07-11 stsp worktree->base_commit_id);
5469 818c7501 2019-07-11 stsp if (err)
5470 818c7501 2019-07-11 stsp goto done;
5471 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5472 818c7501 2019-07-11 stsp if (err)
5473 818c7501 2019-07-11 stsp goto done;
5474 818c7501 2019-07-11 stsp
5475 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5476 818c7501 2019-07-11 stsp if (err)
5477 818c7501 2019-07-11 stsp goto done;
5478 818c7501 2019-07-11 stsp done:
5479 818c7501 2019-07-11 stsp free(fileindex_path);
5480 818c7501 2019-07-11 stsp free(tmp_branch_name);
5481 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5482 818c7501 2019-07-11 stsp free(branch_ref_name);
5483 818c7501 2019-07-11 stsp if (branch_ref)
5484 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5485 818c7501 2019-07-11 stsp if (wt_branch)
5486 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5487 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5488 818c7501 2019-07-11 stsp if (err) {
5489 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5490 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5491 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5492 818c7501 2019-07-11 stsp }
5493 818c7501 2019-07-11 stsp if (*tmp_branch) {
5494 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5495 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5496 818c7501 2019-07-11 stsp }
5497 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5498 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5499 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5500 3e3a69f1 2019-07-25 stsp }
5501 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5502 818c7501 2019-07-11 stsp }
5503 818c7501 2019-07-11 stsp return err;
5504 818c7501 2019-07-11 stsp }
5505 818c7501 2019-07-11 stsp
5506 818c7501 2019-07-11 stsp const struct got_error *
5507 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5508 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5509 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5510 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5511 818c7501 2019-07-11 stsp {
5512 818c7501 2019-07-11 stsp const struct got_error *err;
5513 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5514 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5515 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5516 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5517 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5518 818c7501 2019-07-11 stsp
5519 818c7501 2019-07-11 stsp *commit_id = NULL;
5520 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5521 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5522 3e3a69f1 2019-07-25 stsp *branch = NULL;
5523 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5524 3e3a69f1 2019-07-25 stsp
5525 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5526 3e3a69f1 2019-07-25 stsp if (err)
5527 3e3a69f1 2019-07-25 stsp return err;
5528 818c7501 2019-07-11 stsp
5529 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5530 3e3a69f1 2019-07-25 stsp if (err)
5531 f032f1f7 2019-08-04 stsp goto done;
5532 f032f1f7 2019-08-04 stsp
5533 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5534 f032f1f7 2019-08-04 stsp &have_staged_files);
5535 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5536 f032f1f7 2019-08-04 stsp goto done;
5537 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5538 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5539 3e3a69f1 2019-07-25 stsp goto done;
5540 f032f1f7 2019-08-04 stsp }
5541 3e3a69f1 2019-07-25 stsp
5542 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5543 818c7501 2019-07-11 stsp if (err)
5544 f032f1f7 2019-08-04 stsp goto done;
5545 818c7501 2019-07-11 stsp
5546 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5547 818c7501 2019-07-11 stsp if (err)
5548 818c7501 2019-07-11 stsp goto done;
5549 818c7501 2019-07-11 stsp
5550 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5551 818c7501 2019-07-11 stsp if (err)
5552 818c7501 2019-07-11 stsp goto done;
5553 818c7501 2019-07-11 stsp
5554 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5555 818c7501 2019-07-11 stsp if (err)
5556 818c7501 2019-07-11 stsp goto done;
5557 818c7501 2019-07-11 stsp
5558 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5559 818c7501 2019-07-11 stsp if (err)
5560 818c7501 2019-07-11 stsp goto done;
5561 818c7501 2019-07-11 stsp
5562 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5563 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5564 818c7501 2019-07-11 stsp if (err)
5565 818c7501 2019-07-11 stsp goto done;
5566 818c7501 2019-07-11 stsp
5567 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5568 818c7501 2019-07-11 stsp if (err)
5569 818c7501 2019-07-11 stsp goto done;
5570 818c7501 2019-07-11 stsp
5571 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5572 818c7501 2019-07-11 stsp if (err)
5573 818c7501 2019-07-11 stsp goto done;
5574 818c7501 2019-07-11 stsp
5575 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5576 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5577 818c7501 2019-07-11 stsp if (err)
5578 818c7501 2019-07-11 stsp goto done;
5579 818c7501 2019-07-11 stsp
5580 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5581 818c7501 2019-07-11 stsp if (err)
5582 818c7501 2019-07-11 stsp goto done;
5583 818c7501 2019-07-11 stsp done:
5584 818c7501 2019-07-11 stsp free(commit_ref_name);
5585 818c7501 2019-07-11 stsp free(branch_ref_name);
5586 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5587 818c7501 2019-07-11 stsp if (commit_ref)
5588 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5589 818c7501 2019-07-11 stsp if (branch_ref)
5590 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5591 818c7501 2019-07-11 stsp if (err) {
5592 818c7501 2019-07-11 stsp free(*commit_id);
5593 818c7501 2019-07-11 stsp *commit_id = NULL;
5594 818c7501 2019-07-11 stsp if (*tmp_branch) {
5595 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5596 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5597 818c7501 2019-07-11 stsp }
5598 818c7501 2019-07-11 stsp if (*new_base_branch) {
5599 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5600 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5601 818c7501 2019-07-11 stsp }
5602 818c7501 2019-07-11 stsp if (*branch) {
5603 818c7501 2019-07-11 stsp got_ref_close(*branch);
5604 818c7501 2019-07-11 stsp *branch = NULL;
5605 818c7501 2019-07-11 stsp }
5606 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5607 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5608 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5609 3e3a69f1 2019-07-25 stsp }
5610 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5611 818c7501 2019-07-11 stsp }
5612 818c7501 2019-07-11 stsp return err;
5613 c4296144 2019-05-09 stsp }
5614 818c7501 2019-07-11 stsp
5615 818c7501 2019-07-11 stsp const struct got_error *
5616 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5617 818c7501 2019-07-11 stsp {
5618 818c7501 2019-07-11 stsp const struct got_error *err;
5619 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5620 818c7501 2019-07-11 stsp
5621 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5622 818c7501 2019-07-11 stsp if (err)
5623 818c7501 2019-07-11 stsp return err;
5624 818c7501 2019-07-11 stsp
5625 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5626 818c7501 2019-07-11 stsp free(tmp_branch_name);
5627 818c7501 2019-07-11 stsp return NULL;
5628 818c7501 2019-07-11 stsp }
5629 818c7501 2019-07-11 stsp
5630 818c7501 2019-07-11 stsp static const struct got_error *
5631 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5632 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5633 818c7501 2019-07-11 stsp {
5634 0ebf8283 2019-07-24 stsp *logmsg = arg;
5635 818c7501 2019-07-11 stsp return NULL;
5636 818c7501 2019-07-11 stsp }
5637 818c7501 2019-07-11 stsp
5638 818c7501 2019-07-11 stsp static const struct got_error *
5639 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5640 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5641 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5642 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5643 818c7501 2019-07-11 stsp {
5644 818c7501 2019-07-11 stsp return NULL;
5645 01757395 2019-07-12 stsp }
5646 01757395 2019-07-12 stsp
5647 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5648 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5649 01757395 2019-07-12 stsp void *progress_arg;
5650 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5651 01757395 2019-07-12 stsp };
5652 01757395 2019-07-12 stsp
5653 01757395 2019-07-12 stsp static const struct got_error *
5654 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5655 01757395 2019-07-12 stsp {
5656 01757395 2019-07-12 stsp const struct got_error *err;
5657 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5658 01757395 2019-07-12 stsp char *p;
5659 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5660 01757395 2019-07-12 stsp
5661 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5662 01757395 2019-07-12 stsp if (err)
5663 01757395 2019-07-12 stsp return err;
5664 01757395 2019-07-12 stsp
5665 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5666 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5667 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5668 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5669 01757395 2019-07-12 stsp return NULL;
5670 01757395 2019-07-12 stsp
5671 01757395 2019-07-12 stsp p = strdup(path);
5672 01757395 2019-07-12 stsp if (p == NULL)
5673 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5674 01757395 2019-07-12 stsp
5675 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5676 01757395 2019-07-12 stsp if (err || new == NULL)
5677 01757395 2019-07-12 stsp free(p);
5678 01757395 2019-07-12 stsp return err;
5679 01757395 2019-07-12 stsp }
5680 01757395 2019-07-12 stsp
5681 01757395 2019-07-12 stsp void
5682 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5683 01757395 2019-07-12 stsp {
5684 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5685 01757395 2019-07-12 stsp
5686 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5687 01757395 2019-07-12 stsp free((char *)pe->path);
5688 01757395 2019-07-12 stsp
5689 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5690 818c7501 2019-07-11 stsp }
5691 818c7501 2019-07-11 stsp
5692 0ebf8283 2019-07-24 stsp static const struct got_error *
5693 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5694 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5695 818c7501 2019-07-11 stsp {
5696 818c7501 2019-07-11 stsp const struct got_error *err;
5697 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5698 818c7501 2019-07-11 stsp
5699 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5700 818c7501 2019-07-11 stsp if (err) {
5701 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5702 818c7501 2019-07-11 stsp goto done;
5703 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5704 818c7501 2019-07-11 stsp if (err)
5705 818c7501 2019-07-11 stsp goto done;
5706 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5707 818c7501 2019-07-11 stsp if (err)
5708 818c7501 2019-07-11 stsp goto done;
5709 de05890f 2020-03-05 stsp } else if (is_rebase) {
5710 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5711 818c7501 2019-07-11 stsp int cmp;
5712 818c7501 2019-07-11 stsp
5713 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5714 818c7501 2019-07-11 stsp if (err)
5715 818c7501 2019-07-11 stsp goto done;
5716 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5717 818c7501 2019-07-11 stsp free(stored_id);
5718 818c7501 2019-07-11 stsp if (cmp != 0) {
5719 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5720 818c7501 2019-07-11 stsp goto done;
5721 818c7501 2019-07-11 stsp }
5722 818c7501 2019-07-11 stsp }
5723 0ebf8283 2019-07-24 stsp done:
5724 0ebf8283 2019-07-24 stsp if (commit_ref)
5725 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5726 0ebf8283 2019-07-24 stsp return err;
5727 0ebf8283 2019-07-24 stsp }
5728 0ebf8283 2019-07-24 stsp
5729 0ebf8283 2019-07-24 stsp static const struct got_error *
5730 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5731 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5732 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5733 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5734 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5735 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5736 0ebf8283 2019-07-24 stsp {
5737 0ebf8283 2019-07-24 stsp const struct got_error *err;
5738 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5739 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5740 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5741 818c7501 2019-07-11 stsp
5742 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5743 0ebf8283 2019-07-24 stsp
5744 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5745 0ebf8283 2019-07-24 stsp if (err)
5746 0ebf8283 2019-07-24 stsp return err;
5747 0ebf8283 2019-07-24 stsp
5748 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5749 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5750 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5751 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5752 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5753 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5754 818c7501 2019-07-11 stsp if (commit_ref)
5755 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5756 818c7501 2019-07-11 stsp return err;
5757 818c7501 2019-07-11 stsp }
5758 818c7501 2019-07-11 stsp
5759 818c7501 2019-07-11 stsp const struct got_error *
5760 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5761 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5762 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5763 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5764 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5765 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5766 818c7501 2019-07-11 stsp {
5767 0ebf8283 2019-07-24 stsp const struct got_error *err;
5768 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5769 0ebf8283 2019-07-24 stsp
5770 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5771 0ebf8283 2019-07-24 stsp if (err)
5772 0ebf8283 2019-07-24 stsp return err;
5773 0ebf8283 2019-07-24 stsp
5774 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5775 0ebf8283 2019-07-24 stsp if (err)
5776 0ebf8283 2019-07-24 stsp goto done;
5777 0ebf8283 2019-07-24 stsp
5778 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5779 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5780 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5781 0ebf8283 2019-07-24 stsp done:
5782 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5783 0ebf8283 2019-07-24 stsp return err;
5784 0ebf8283 2019-07-24 stsp }
5785 0ebf8283 2019-07-24 stsp
5786 0ebf8283 2019-07-24 stsp const struct got_error *
5787 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5788 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5789 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5790 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5791 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5792 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5793 0ebf8283 2019-07-24 stsp {
5794 0ebf8283 2019-07-24 stsp const struct got_error *err;
5795 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5796 0ebf8283 2019-07-24 stsp
5797 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5798 0ebf8283 2019-07-24 stsp if (err)
5799 0ebf8283 2019-07-24 stsp return err;
5800 0ebf8283 2019-07-24 stsp
5801 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5802 0ebf8283 2019-07-24 stsp if (err)
5803 0ebf8283 2019-07-24 stsp goto done;
5804 0ebf8283 2019-07-24 stsp
5805 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5806 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5807 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5808 0ebf8283 2019-07-24 stsp done:
5809 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5810 0ebf8283 2019-07-24 stsp return err;
5811 0ebf8283 2019-07-24 stsp }
5812 0ebf8283 2019-07-24 stsp
5813 0ebf8283 2019-07-24 stsp static const struct got_error *
5814 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5815 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5816 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5817 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5818 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5819 0ebf8283 2019-07-24 stsp {
5820 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5821 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5822 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5823 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5824 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5825 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5826 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5827 818c7501 2019-07-11 stsp
5828 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5829 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5830 a0e95631 2019-07-12 stsp
5831 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5832 818c7501 2019-07-11 stsp
5833 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5834 a0e95631 2019-07-12 stsp if (err)
5835 3e3a69f1 2019-07-25 stsp return err;
5836 a0e95631 2019-07-12 stsp
5837 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5838 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5839 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5840 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5841 01757395 2019-07-12 stsp /*
5842 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5843 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5844 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5845 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5846 01757395 2019-07-12 stsp */
5847 01757395 2019-07-12 stsp if (merged_paths) {
5848 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5849 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5850 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5851 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5852 f2a9dc41 2019-12-13 tracey 0);
5853 01757395 2019-07-12 stsp if (err)
5854 01757395 2019-07-12 stsp goto done;
5855 01757395 2019-07-12 stsp }
5856 01757395 2019-07-12 stsp } else {
5857 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5858 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5859 01757395 2019-07-12 stsp if (err)
5860 01757395 2019-07-12 stsp goto done;
5861 01757395 2019-07-12 stsp }
5862 a0e95631 2019-07-12 stsp
5863 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5864 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5865 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5866 a0e95631 2019-07-12 stsp if (err)
5867 a0e95631 2019-07-12 stsp goto done;
5868 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5869 a0e95631 2019-07-12 stsp goto done;
5870 ff0d2220 2019-07-11 stsp }
5871 818c7501 2019-07-11 stsp
5872 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5873 edd02c5e 2019-07-11 stsp if (err)
5874 edd02c5e 2019-07-11 stsp goto done;
5875 edd02c5e 2019-07-11 stsp
5876 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5877 818c7501 2019-07-11 stsp if (err)
5878 818c7501 2019-07-11 stsp goto done;
5879 818c7501 2019-07-11 stsp
5880 5943eee2 2019-08-13 stsp if (new_logmsg) {
5881 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5882 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5883 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5884 5943eee2 2019-08-13 stsp goto done;
5885 5943eee2 2019-08-13 stsp }
5886 5943eee2 2019-08-13 stsp } else {
5887 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5888 5943eee2 2019-08-13 stsp if (err)
5889 5943eee2 2019-08-13 stsp goto done;
5890 5943eee2 2019-08-13 stsp }
5891 0ebf8283 2019-07-24 stsp
5892 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5893 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5894 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5895 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5896 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5897 a0e95631 2019-07-12 stsp if (err)
5898 a0e95631 2019-07-12 stsp goto done;
5899 a0e95631 2019-07-12 stsp
5900 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5901 a0e95631 2019-07-12 stsp if (err)
5902 a0e95631 2019-07-12 stsp goto done;
5903 a0e95631 2019-07-12 stsp
5904 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5905 a0e95631 2019-07-12 stsp if (err)
5906 a0e95631 2019-07-12 stsp goto done;
5907 a0e95631 2019-07-12 stsp
5908 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5909 72fd46fa 2019-09-06 stsp fileindex, 0);
5910 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5911 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5912 a0e95631 2019-07-12 stsp err = sync_err;
5913 818c7501 2019-07-11 stsp done:
5914 a0e95631 2019-07-12 stsp free(fileindex_path);
5915 a0e95631 2019-07-12 stsp free(head_commit_id);
5916 a0e95631 2019-07-12 stsp if (head_ref)
5917 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5918 818c7501 2019-07-11 stsp if (err) {
5919 818c7501 2019-07-11 stsp free(*new_commit_id);
5920 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5921 818c7501 2019-07-11 stsp }
5922 818c7501 2019-07-11 stsp return err;
5923 818c7501 2019-07-11 stsp }
5924 818c7501 2019-07-11 stsp
5925 818c7501 2019-07-11 stsp const struct got_error *
5926 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5927 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5928 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5929 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5930 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5931 0ebf8283 2019-07-24 stsp {
5932 0ebf8283 2019-07-24 stsp const struct got_error *err;
5933 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5934 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5935 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5936 0ebf8283 2019-07-24 stsp
5937 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5938 0ebf8283 2019-07-24 stsp if (err)
5939 0ebf8283 2019-07-24 stsp return err;
5940 0ebf8283 2019-07-24 stsp
5941 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5942 0ebf8283 2019-07-24 stsp if (err)
5943 0ebf8283 2019-07-24 stsp goto done;
5944 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5945 0ebf8283 2019-07-24 stsp if (err)
5946 0ebf8283 2019-07-24 stsp goto done;
5947 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5948 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5949 0ebf8283 2019-07-24 stsp goto done;
5950 0ebf8283 2019-07-24 stsp }
5951 0ebf8283 2019-07-24 stsp
5952 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5953 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5954 0ebf8283 2019-07-24 stsp done:
5955 0ebf8283 2019-07-24 stsp if (commit_ref)
5956 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5957 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5958 0ebf8283 2019-07-24 stsp free(commit_id);
5959 0ebf8283 2019-07-24 stsp return err;
5960 0ebf8283 2019-07-24 stsp }
5961 0ebf8283 2019-07-24 stsp
5962 0ebf8283 2019-07-24 stsp const struct got_error *
5963 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5964 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5965 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5966 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5967 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5968 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5969 0ebf8283 2019-07-24 stsp {
5970 0ebf8283 2019-07-24 stsp const struct got_error *err;
5971 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5972 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5973 0ebf8283 2019-07-24 stsp
5974 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5975 0ebf8283 2019-07-24 stsp if (err)
5976 0ebf8283 2019-07-24 stsp return err;
5977 0ebf8283 2019-07-24 stsp
5978 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5979 0ebf8283 2019-07-24 stsp if (err)
5980 0ebf8283 2019-07-24 stsp goto done;
5981 0ebf8283 2019-07-24 stsp
5982 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5983 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5984 0ebf8283 2019-07-24 stsp done:
5985 0ebf8283 2019-07-24 stsp if (commit_ref)
5986 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5987 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5988 0ebf8283 2019-07-24 stsp return err;
5989 0ebf8283 2019-07-24 stsp }
5990 0ebf8283 2019-07-24 stsp
5991 0ebf8283 2019-07-24 stsp const struct got_error *
5992 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5993 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5994 818c7501 2019-07-11 stsp {
5995 3e3a69f1 2019-07-25 stsp if (fileindex)
5996 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5997 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
5998 69844fba 2019-07-11 stsp }
5999 69844fba 2019-07-11 stsp
6000 69844fba 2019-07-11 stsp static const struct got_error *
6001 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6002 69844fba 2019-07-11 stsp {
6003 69844fba 2019-07-11 stsp const struct got_error *err;
6004 69844fba 2019-07-11 stsp struct got_reference *ref;
6005 69844fba 2019-07-11 stsp
6006 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6007 69844fba 2019-07-11 stsp if (err) {
6008 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6009 69844fba 2019-07-11 stsp return NULL;
6010 69844fba 2019-07-11 stsp return err;
6011 69844fba 2019-07-11 stsp }
6012 69844fba 2019-07-11 stsp
6013 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6014 69844fba 2019-07-11 stsp got_ref_close(ref);
6015 69844fba 2019-07-11 stsp return err;
6016 818c7501 2019-07-11 stsp }
6017 818c7501 2019-07-11 stsp
6018 69844fba 2019-07-11 stsp static const struct got_error *
6019 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6020 69844fba 2019-07-11 stsp {
6021 69844fba 2019-07-11 stsp const struct got_error *err;
6022 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6023 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6024 69844fba 2019-07-11 stsp
6025 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6026 69844fba 2019-07-11 stsp if (err)
6027 69844fba 2019-07-11 stsp goto done;
6028 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6029 69844fba 2019-07-11 stsp if (err)
6030 69844fba 2019-07-11 stsp goto done;
6031 69844fba 2019-07-11 stsp
6032 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6033 69844fba 2019-07-11 stsp if (err)
6034 69844fba 2019-07-11 stsp goto done;
6035 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6036 69844fba 2019-07-11 stsp if (err)
6037 69844fba 2019-07-11 stsp goto done;
6038 69844fba 2019-07-11 stsp
6039 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6040 69844fba 2019-07-11 stsp if (err)
6041 69844fba 2019-07-11 stsp goto done;
6042 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6043 69844fba 2019-07-11 stsp if (err)
6044 69844fba 2019-07-11 stsp goto done;
6045 69844fba 2019-07-11 stsp
6046 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6047 69844fba 2019-07-11 stsp if (err)
6048 69844fba 2019-07-11 stsp goto done;
6049 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6050 69844fba 2019-07-11 stsp if (err)
6051 69844fba 2019-07-11 stsp goto done;
6052 69844fba 2019-07-11 stsp
6053 69844fba 2019-07-11 stsp done:
6054 69844fba 2019-07-11 stsp free(tmp_branch_name);
6055 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6056 69844fba 2019-07-11 stsp free(branch_ref_name);
6057 69844fba 2019-07-11 stsp free(commit_ref_name);
6058 69844fba 2019-07-11 stsp return err;
6059 69844fba 2019-07-11 stsp }
6060 69844fba 2019-07-11 stsp
6061 818c7501 2019-07-11 stsp const struct got_error *
6062 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6063 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6064 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6065 818c7501 2019-07-11 stsp struct got_repository *repo)
6066 818c7501 2019-07-11 stsp {
6067 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6068 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6069 818c7501 2019-07-11 stsp
6070 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6071 818c7501 2019-07-11 stsp if (err)
6072 818c7501 2019-07-11 stsp return err;
6073 818c7501 2019-07-11 stsp
6074 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6075 818c7501 2019-07-11 stsp if (err)
6076 818c7501 2019-07-11 stsp goto done;
6077 818c7501 2019-07-11 stsp
6078 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6079 818c7501 2019-07-11 stsp if (err)
6080 818c7501 2019-07-11 stsp goto done;
6081 818c7501 2019-07-11 stsp
6082 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6083 818c7501 2019-07-11 stsp if (err)
6084 818c7501 2019-07-11 stsp goto done;
6085 818c7501 2019-07-11 stsp
6086 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6087 818c7501 2019-07-11 stsp done:
6088 3e3a69f1 2019-07-25 stsp if (fileindex)
6089 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6090 818c7501 2019-07-11 stsp free(new_head_commit_id);
6091 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6092 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6093 818c7501 2019-07-11 stsp err = unlockerr;
6094 818c7501 2019-07-11 stsp return err;
6095 818c7501 2019-07-11 stsp }
6096 818c7501 2019-07-11 stsp
6097 818c7501 2019-07-11 stsp const struct got_error *
6098 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6099 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6100 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6101 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6102 818c7501 2019-07-11 stsp {
6103 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6104 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6105 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6106 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6107 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6108 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6109 818c7501 2019-07-11 stsp
6110 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6111 818c7501 2019-07-11 stsp if (err)
6112 818c7501 2019-07-11 stsp return err;
6113 818c7501 2019-07-11 stsp
6114 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6115 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6116 818c7501 2019-07-11 stsp if (err)
6117 818c7501 2019-07-11 stsp goto done;
6118 818c7501 2019-07-11 stsp
6119 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6120 818c7501 2019-07-11 stsp if (err)
6121 818c7501 2019-07-11 stsp goto done;
6122 818c7501 2019-07-11 stsp
6123 818c7501 2019-07-11 stsp /*
6124 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6125 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6126 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6127 818c7501 2019-07-11 stsp */
6128 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6129 818c7501 2019-07-11 stsp if (err)
6130 818c7501 2019-07-11 stsp goto done;
6131 818c7501 2019-07-11 stsp
6132 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6133 818c7501 2019-07-11 stsp if (err)
6134 818c7501 2019-07-11 stsp goto done;
6135 818c7501 2019-07-11 stsp
6136 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6137 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6138 ca355955 2019-07-12 stsp if (err)
6139 ca355955 2019-07-12 stsp goto done;
6140 ca355955 2019-07-12 stsp
6141 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6142 ca355955 2019-07-12 stsp if (err)
6143 ca355955 2019-07-12 stsp goto done;
6144 ca355955 2019-07-12 stsp
6145 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6146 818c7501 2019-07-11 stsp if (err)
6147 818c7501 2019-07-11 stsp goto done;
6148 818c7501 2019-07-11 stsp
6149 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6150 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6151 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6152 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6153 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6154 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6155 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6156 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6157 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6158 55bd499d 2019-07-12 stsp if (err)
6159 1f1abb7e 2019-08-08 stsp goto sync;
6160 55bd499d 2019-07-12 stsp
6161 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6162 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6163 ca355955 2019-07-12 stsp sync:
6164 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6165 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6166 ca355955 2019-07-12 stsp err = sync_err;
6167 818c7501 2019-07-11 stsp done:
6168 818c7501 2019-07-11 stsp got_ref_close(resolved);
6169 a3a2faf2 2019-07-12 stsp free(tree_id);
6170 818c7501 2019-07-11 stsp free(commit_id);
6171 0ebf8283 2019-07-24 stsp if (fileindex)
6172 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6173 0ebf8283 2019-07-24 stsp free(fileindex_path);
6174 0ebf8283 2019-07-24 stsp
6175 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6176 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6177 0ebf8283 2019-07-24 stsp err = unlockerr;
6178 0ebf8283 2019-07-24 stsp return err;
6179 0ebf8283 2019-07-24 stsp }
6180 0ebf8283 2019-07-24 stsp
6181 0ebf8283 2019-07-24 stsp const struct got_error *
6182 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6183 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6184 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6185 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6186 0ebf8283 2019-07-24 stsp {
6187 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6188 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6189 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6190 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6191 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6192 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6193 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6194 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6195 0ebf8283 2019-07-24 stsp
6196 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6197 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6198 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6199 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6200 0ebf8283 2019-07-24 stsp
6201 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6202 0ebf8283 2019-07-24 stsp if (err)
6203 0ebf8283 2019-07-24 stsp return err;
6204 0ebf8283 2019-07-24 stsp
6205 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6206 0ebf8283 2019-07-24 stsp if (err)
6207 0ebf8283 2019-07-24 stsp goto done;
6208 0ebf8283 2019-07-24 stsp
6209 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6210 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6211 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6212 0ebf8283 2019-07-24 stsp &ok_arg);
6213 0ebf8283 2019-07-24 stsp if (err)
6214 0ebf8283 2019-07-24 stsp goto done;
6215 0ebf8283 2019-07-24 stsp
6216 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6217 0ebf8283 2019-07-24 stsp if (err)
6218 0ebf8283 2019-07-24 stsp goto done;
6219 0ebf8283 2019-07-24 stsp
6220 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6221 0ebf8283 2019-07-24 stsp if (err)
6222 0ebf8283 2019-07-24 stsp goto done;
6223 0ebf8283 2019-07-24 stsp
6224 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6225 0ebf8283 2019-07-24 stsp worktree);
6226 0ebf8283 2019-07-24 stsp if (err)
6227 0ebf8283 2019-07-24 stsp goto done;
6228 0ebf8283 2019-07-24 stsp
6229 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6230 0ebf8283 2019-07-24 stsp 0);
6231 0ebf8283 2019-07-24 stsp if (err)
6232 0ebf8283 2019-07-24 stsp goto done;
6233 0ebf8283 2019-07-24 stsp
6234 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6235 0ebf8283 2019-07-24 stsp if (err)
6236 0ebf8283 2019-07-24 stsp goto done;
6237 0ebf8283 2019-07-24 stsp
6238 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6239 0ebf8283 2019-07-24 stsp if (err)
6240 0ebf8283 2019-07-24 stsp goto done;
6241 0ebf8283 2019-07-24 stsp
6242 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6243 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6244 0ebf8283 2019-07-24 stsp if (err)
6245 0ebf8283 2019-07-24 stsp goto done;
6246 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6247 0ebf8283 2019-07-24 stsp if (err)
6248 0ebf8283 2019-07-24 stsp goto done;
6249 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6250 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6251 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6252 0ebf8283 2019-07-24 stsp goto done;
6253 0ebf8283 2019-07-24 stsp }
6254 0ebf8283 2019-07-24 stsp
6255 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6256 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6257 0ebf8283 2019-07-24 stsp if (err)
6258 0ebf8283 2019-07-24 stsp goto done;
6259 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6260 0ebf8283 2019-07-24 stsp if (err)
6261 0ebf8283 2019-07-24 stsp goto done;
6262 0ebf8283 2019-07-24 stsp
6263 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6264 0ebf8283 2019-07-24 stsp if (err)
6265 0ebf8283 2019-07-24 stsp goto done;
6266 0ebf8283 2019-07-24 stsp done:
6267 0ebf8283 2019-07-24 stsp free(fileindex_path);
6268 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6269 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6270 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6271 0ebf8283 2019-07-24 stsp if (wt_branch)
6272 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6273 0ebf8283 2019-07-24 stsp if (err) {
6274 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6275 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6276 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6277 0ebf8283 2019-07-24 stsp }
6278 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6279 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6280 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6281 0ebf8283 2019-07-24 stsp }
6282 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6283 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6284 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6285 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6286 3e3a69f1 2019-07-25 stsp }
6287 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6288 0ebf8283 2019-07-24 stsp }
6289 0ebf8283 2019-07-24 stsp return err;
6290 0ebf8283 2019-07-24 stsp }
6291 0ebf8283 2019-07-24 stsp
6292 0ebf8283 2019-07-24 stsp const struct got_error *
6293 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6294 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6295 0ebf8283 2019-07-24 stsp {
6296 3e3a69f1 2019-07-25 stsp if (fileindex)
6297 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6298 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6299 0ebf8283 2019-07-24 stsp }
6300 0ebf8283 2019-07-24 stsp
6301 0ebf8283 2019-07-24 stsp const struct got_error *
6302 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6303 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6304 0ebf8283 2019-07-24 stsp {
6305 0ebf8283 2019-07-24 stsp const struct got_error *err;
6306 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6307 0ebf8283 2019-07-24 stsp
6308 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6309 0ebf8283 2019-07-24 stsp if (err)
6310 0ebf8283 2019-07-24 stsp return err;
6311 0ebf8283 2019-07-24 stsp
6312 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6313 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6314 0ebf8283 2019-07-24 stsp return NULL;
6315 0ebf8283 2019-07-24 stsp }
6316 0ebf8283 2019-07-24 stsp
6317 0ebf8283 2019-07-24 stsp const struct got_error *
6318 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6319 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6320 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6321 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6322 0ebf8283 2019-07-24 stsp {
6323 0ebf8283 2019-07-24 stsp const struct got_error *err;
6324 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6325 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6326 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6327 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6328 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6329 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6330 0ebf8283 2019-07-24 stsp
6331 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6332 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6333 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6334 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6335 0ebf8283 2019-07-24 stsp
6336 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6337 3e3a69f1 2019-07-25 stsp if (err)
6338 3e3a69f1 2019-07-25 stsp return err;
6339 3e3a69f1 2019-07-25 stsp
6340 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6341 3e3a69f1 2019-07-25 stsp if (err)
6342 f032f1f7 2019-08-04 stsp goto done;
6343 f032f1f7 2019-08-04 stsp
6344 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6345 f032f1f7 2019-08-04 stsp &have_staged_files);
6346 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6347 f032f1f7 2019-08-04 stsp goto done;
6348 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6349 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6350 3e3a69f1 2019-07-25 stsp goto done;
6351 f032f1f7 2019-08-04 stsp }
6352 3e3a69f1 2019-07-25 stsp
6353 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6354 0ebf8283 2019-07-24 stsp if (err)
6355 f032f1f7 2019-08-04 stsp goto done;
6356 0ebf8283 2019-07-24 stsp
6357 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6358 0ebf8283 2019-07-24 stsp if (err)
6359 0ebf8283 2019-07-24 stsp goto done;
6360 0ebf8283 2019-07-24 stsp
6361 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6362 0ebf8283 2019-07-24 stsp if (err)
6363 0ebf8283 2019-07-24 stsp goto done;
6364 0ebf8283 2019-07-24 stsp
6365 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6366 0ebf8283 2019-07-24 stsp worktree);
6367 0ebf8283 2019-07-24 stsp if (err)
6368 0ebf8283 2019-07-24 stsp goto done;
6369 0ebf8283 2019-07-24 stsp
6370 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6371 0ebf8283 2019-07-24 stsp if (err)
6372 0ebf8283 2019-07-24 stsp goto done;
6373 0ebf8283 2019-07-24 stsp
6374 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6375 0ebf8283 2019-07-24 stsp if (err)
6376 0ebf8283 2019-07-24 stsp goto done;
6377 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6378 0ebf8283 2019-07-24 stsp if (err)
6379 0ebf8283 2019-07-24 stsp goto done;
6380 0ebf8283 2019-07-24 stsp
6381 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6382 0ebf8283 2019-07-24 stsp if (err)
6383 0ebf8283 2019-07-24 stsp goto done;
6384 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6385 0ebf8283 2019-07-24 stsp if (err)
6386 0ebf8283 2019-07-24 stsp goto done;
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6389 0ebf8283 2019-07-24 stsp if (err)
6390 0ebf8283 2019-07-24 stsp goto done;
6391 0ebf8283 2019-07-24 stsp done:
6392 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6393 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6394 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6395 0ebf8283 2019-07-24 stsp if (commit_ref)
6396 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6397 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6398 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6399 0ebf8283 2019-07-24 stsp if (err) {
6400 0ebf8283 2019-07-24 stsp free(*commit_id);
6401 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6402 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6403 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6404 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6405 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6406 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6407 0ebf8283 2019-07-24 stsp }
6408 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6409 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6411 3e3a69f1 2019-07-25 stsp }
6412 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6413 0ebf8283 2019-07-24 stsp }
6414 0ebf8283 2019-07-24 stsp return err;
6415 0ebf8283 2019-07-24 stsp }
6416 0ebf8283 2019-07-24 stsp
6417 0ebf8283 2019-07-24 stsp static const struct got_error *
6418 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6419 0ebf8283 2019-07-24 stsp {
6420 0ebf8283 2019-07-24 stsp const struct got_error *err;
6421 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6422 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6423 0ebf8283 2019-07-24 stsp
6424 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6425 0ebf8283 2019-07-24 stsp if (err)
6426 0ebf8283 2019-07-24 stsp goto done;
6427 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6428 0ebf8283 2019-07-24 stsp if (err)
6429 0ebf8283 2019-07-24 stsp goto done;
6430 0ebf8283 2019-07-24 stsp
6431 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6432 0ebf8283 2019-07-24 stsp worktree);
6433 0ebf8283 2019-07-24 stsp if (err)
6434 0ebf8283 2019-07-24 stsp goto done;
6435 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6436 0ebf8283 2019-07-24 stsp if (err)
6437 0ebf8283 2019-07-24 stsp goto done;
6438 0ebf8283 2019-07-24 stsp
6439 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6440 0ebf8283 2019-07-24 stsp if (err)
6441 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6443 0ebf8283 2019-07-24 stsp if (err)
6444 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp
6446 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6447 0ebf8283 2019-07-24 stsp if (err)
6448 0ebf8283 2019-07-24 stsp goto done;
6449 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6450 0ebf8283 2019-07-24 stsp if (err)
6451 0ebf8283 2019-07-24 stsp goto done;
6452 0ebf8283 2019-07-24 stsp done:
6453 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6454 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6455 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6456 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6457 0ebf8283 2019-07-24 stsp return err;
6458 0ebf8283 2019-07-24 stsp }
6459 0ebf8283 2019-07-24 stsp
6460 0ebf8283 2019-07-24 stsp const struct got_error *
6461 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6462 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6463 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6464 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6465 0ebf8283 2019-07-24 stsp {
6466 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6467 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6468 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6469 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6470 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6471 0ebf8283 2019-07-24 stsp
6472 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6473 0ebf8283 2019-07-24 stsp if (err)
6474 0ebf8283 2019-07-24 stsp return err;
6475 0ebf8283 2019-07-24 stsp
6476 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6477 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6478 0ebf8283 2019-07-24 stsp if (err)
6479 0ebf8283 2019-07-24 stsp goto done;
6480 0ebf8283 2019-07-24 stsp
6481 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6482 0ebf8283 2019-07-24 stsp if (err)
6483 0ebf8283 2019-07-24 stsp goto done;
6484 0ebf8283 2019-07-24 stsp
6485 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6486 0ebf8283 2019-07-24 stsp if (err)
6487 0ebf8283 2019-07-24 stsp goto done;
6488 0ebf8283 2019-07-24 stsp
6489 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6490 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6491 0ebf8283 2019-07-24 stsp if (err)
6492 0ebf8283 2019-07-24 stsp goto done;
6493 0ebf8283 2019-07-24 stsp
6494 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6495 0ebf8283 2019-07-24 stsp if (err)
6496 0ebf8283 2019-07-24 stsp goto done;
6497 0ebf8283 2019-07-24 stsp
6498 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6499 0ebf8283 2019-07-24 stsp if (err)
6500 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp
6502 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6503 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6504 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6505 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6506 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6507 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6508 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6509 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6510 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6511 0ebf8283 2019-07-24 stsp if (err)
6512 1f1abb7e 2019-08-08 stsp goto sync;
6513 0ebf8283 2019-07-24 stsp
6514 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6515 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6516 0ebf8283 2019-07-24 stsp sync:
6517 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6518 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6519 0ebf8283 2019-07-24 stsp err = sync_err;
6520 0ebf8283 2019-07-24 stsp done:
6521 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6522 0ebf8283 2019-07-24 stsp free(tree_id);
6523 818c7501 2019-07-11 stsp free(fileindex_path);
6524 818c7501 2019-07-11 stsp
6525 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6526 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6527 818c7501 2019-07-11 stsp err = unlockerr;
6528 818c7501 2019-07-11 stsp return err;
6529 818c7501 2019-07-11 stsp }
6530 0ebf8283 2019-07-24 stsp
6531 0ebf8283 2019-07-24 stsp const struct got_error *
6532 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6533 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6535 0ebf8283 2019-07-24 stsp {
6536 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6537 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6538 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6539 0ebf8283 2019-07-24 stsp
6540 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6541 0ebf8283 2019-07-24 stsp if (err)
6542 0ebf8283 2019-07-24 stsp return err;
6543 0ebf8283 2019-07-24 stsp
6544 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6545 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6546 0ebf8283 2019-07-24 stsp if (err)
6547 0ebf8283 2019-07-24 stsp goto done;
6548 0ebf8283 2019-07-24 stsp
6549 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6558 0ebf8283 2019-07-24 stsp if (err)
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6562 0ebf8283 2019-07-24 stsp done:
6563 3e3a69f1 2019-07-25 stsp if (fileindex)
6564 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6565 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6566 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6567 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6568 0ebf8283 2019-07-24 stsp err = unlockerr;
6569 0ebf8283 2019-07-24 stsp return err;
6570 0ebf8283 2019-07-24 stsp }
6571 0ebf8283 2019-07-24 stsp
6572 0ebf8283 2019-07-24 stsp const struct got_error *
6573 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6574 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6575 0ebf8283 2019-07-24 stsp {
6576 0ebf8283 2019-07-24 stsp const struct got_error *err;
6577 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6578 0ebf8283 2019-07-24 stsp
6579 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6580 0ebf8283 2019-07-24 stsp if (err)
6581 0ebf8283 2019-07-24 stsp return err;
6582 0ebf8283 2019-07-24 stsp
6583 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6584 0ebf8283 2019-07-24 stsp if (err)
6585 0ebf8283 2019-07-24 stsp goto done;
6586 0ebf8283 2019-07-24 stsp
6587 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6588 0ebf8283 2019-07-24 stsp done:
6589 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6590 2822a352 2019-10-15 stsp return err;
6591 2822a352 2019-10-15 stsp }
6592 2822a352 2019-10-15 stsp
6593 2822a352 2019-10-15 stsp const struct got_error *
6594 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6595 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6596 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6597 2822a352 2019-10-15 stsp struct got_repository *repo)
6598 2822a352 2019-10-15 stsp {
6599 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6600 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6601 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6602 2822a352 2019-10-15 stsp
6603 2822a352 2019-10-15 stsp *fileindex = NULL;
6604 2822a352 2019-10-15 stsp *branch_ref = NULL;
6605 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6606 2822a352 2019-10-15 stsp
6607 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6608 2822a352 2019-10-15 stsp if (err)
6609 2822a352 2019-10-15 stsp return err;
6610 2822a352 2019-10-15 stsp
6611 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6612 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6613 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6614 2822a352 2019-10-15 stsp "update -b or different branch name required");
6615 2822a352 2019-10-15 stsp goto done;
6616 2822a352 2019-10-15 stsp }
6617 2822a352 2019-10-15 stsp
6618 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 2822a352 2019-10-15 stsp if (err)
6620 2822a352 2019-10-15 stsp goto done;
6621 2822a352 2019-10-15 stsp
6622 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6623 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6624 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6625 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6626 2822a352 2019-10-15 stsp &ok_arg);
6627 2822a352 2019-10-15 stsp if (err)
6628 2822a352 2019-10-15 stsp goto done;
6629 2822a352 2019-10-15 stsp
6630 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6631 2822a352 2019-10-15 stsp if (err)
6632 2822a352 2019-10-15 stsp goto done;
6633 2822a352 2019-10-15 stsp
6634 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6635 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6636 2822a352 2019-10-15 stsp done:
6637 2822a352 2019-10-15 stsp if (err) {
6638 2822a352 2019-10-15 stsp if (*branch_ref) {
6639 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6640 2822a352 2019-10-15 stsp *branch_ref = NULL;
6641 2822a352 2019-10-15 stsp }
6642 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6643 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6644 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6645 2822a352 2019-10-15 stsp }
6646 2822a352 2019-10-15 stsp if (*fileindex) {
6647 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6648 2822a352 2019-10-15 stsp *fileindex = NULL;
6649 2822a352 2019-10-15 stsp }
6650 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6651 2822a352 2019-10-15 stsp }
6652 0cb83759 2019-08-03 stsp return err;
6653 0cb83759 2019-08-03 stsp }
6654 0cb83759 2019-08-03 stsp
6655 2822a352 2019-10-15 stsp const struct got_error *
6656 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6657 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6658 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6659 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6660 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6661 2822a352 2019-10-15 stsp {
6662 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6663 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6664 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6665 2822a352 2019-10-15 stsp
6666 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6667 2822a352 2019-10-15 stsp if (err)
6668 2822a352 2019-10-15 stsp goto done;
6669 2822a352 2019-10-15 stsp
6670 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6671 2822a352 2019-10-15 stsp if (err)
6672 2822a352 2019-10-15 stsp goto done;
6673 2822a352 2019-10-15 stsp
6674 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6675 2822a352 2019-10-15 stsp worktree->path_prefix);
6676 2822a352 2019-10-15 stsp if (err)
6677 2822a352 2019-10-15 stsp goto done;
6678 2822a352 2019-10-15 stsp
6679 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6680 2822a352 2019-10-15 stsp if (err)
6681 2822a352 2019-10-15 stsp goto done;
6682 2822a352 2019-10-15 stsp
6683 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6684 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6685 2822a352 2019-10-15 stsp if (err)
6686 2822a352 2019-10-15 stsp goto sync;
6687 2822a352 2019-10-15 stsp
6688 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6689 2822a352 2019-10-15 stsp if (err)
6690 2822a352 2019-10-15 stsp goto sync;
6691 2822a352 2019-10-15 stsp
6692 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6693 2822a352 2019-10-15 stsp sync:
6694 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6695 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6696 2822a352 2019-10-15 stsp err = sync_err;
6697 2822a352 2019-10-15 stsp
6698 2822a352 2019-10-15 stsp done:
6699 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6700 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6701 2822a352 2019-10-15 stsp err = unlockerr;
6702 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6703 2822a352 2019-10-15 stsp
6704 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6705 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6706 2822a352 2019-10-15 stsp err = unlockerr;
6707 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6708 2822a352 2019-10-15 stsp
6709 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6710 2822a352 2019-10-15 stsp free(fileindex_path);
6711 2822a352 2019-10-15 stsp free(tree_id);
6712 2822a352 2019-10-15 stsp
6713 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6714 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6715 2822a352 2019-10-15 stsp err = unlockerr;
6716 2822a352 2019-10-15 stsp return err;
6717 2822a352 2019-10-15 stsp }
6718 2822a352 2019-10-15 stsp
6719 2822a352 2019-10-15 stsp const struct got_error *
6720 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6721 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6722 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6723 2822a352 2019-10-15 stsp {
6724 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6725 8b692cd0 2019-10-21 stsp
6726 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6727 8b692cd0 2019-10-21 stsp
6728 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6729 8b692cd0 2019-10-21 stsp
6730 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6731 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6732 8b692cd0 2019-10-21 stsp err = unlockerr;
6733 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6734 8b692cd0 2019-10-21 stsp
6735 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6736 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6737 8b692cd0 2019-10-21 stsp err = unlockerr;
6738 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6739 8b692cd0 2019-10-21 stsp
6740 8b692cd0 2019-10-21 stsp return err;
6741 2822a352 2019-10-15 stsp }
6742 2822a352 2019-10-15 stsp
6743 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6744 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6745 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6746 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6747 2db2652d 2019-08-07 stsp struct got_repository *repo;
6748 2db2652d 2019-08-07 stsp int have_changes;
6749 2db2652d 2019-08-07 stsp };
6750 2db2652d 2019-08-07 stsp
6751 2db2652d 2019-08-07 stsp const struct got_error *
6752 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6753 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6754 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6755 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6756 735ef5ac 2019-08-03 stsp {
6757 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6758 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6759 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6760 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6761 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6762 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6763 8b13ce36 2019-08-08 stsp
6764 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6765 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6766 8b13ce36 2019-08-08 stsp return NULL;
6767 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6768 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6769 735ef5ac 2019-08-03 stsp
6770 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6771 735ef5ac 2019-08-03 stsp if (ie == NULL)
6772 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6773 735ef5ac 2019-08-03 stsp
6774 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6775 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6776 735ef5ac 2019-08-03 stsp relpath) == -1)
6777 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6778 735ef5ac 2019-08-03 stsp
6779 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6780 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6781 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6782 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6783 735ef5ac 2019-08-03 stsp }
6784 735ef5ac 2019-08-03 stsp
6785 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6786 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6787 3aa5969e 2019-08-06 stsp goto done;
6788 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6789 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6790 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6791 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6792 735ef5ac 2019-08-03 stsp goto done;
6793 3aa5969e 2019-08-06 stsp }
6794 735ef5ac 2019-08-03 stsp
6795 2db2652d 2019-08-07 stsp a->have_changes = 1;
6796 2db2652d 2019-08-07 stsp
6797 735ef5ac 2019-08-03 stsp p = in_repo_path;
6798 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6799 735ef5ac 2019-08-03 stsp p++;
6800 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6801 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6802 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6803 735ef5ac 2019-08-03 stsp done:
6804 735ef5ac 2019-08-03 stsp free(in_repo_path);
6805 dc424a06 2019-08-07 stsp return err;
6806 dc424a06 2019-08-07 stsp }
6807 dc424a06 2019-08-07 stsp
6808 2db2652d 2019-08-07 stsp struct stage_path_arg {
6809 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6810 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6811 2db2652d 2019-08-07 stsp struct got_repository *repo;
6812 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6813 2db2652d 2019-08-07 stsp void *status_arg;
6814 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6815 2db2652d 2019-08-07 stsp void *patch_arg;
6816 7b5dc508 2019-10-28 stsp int staged_something;
6817 2db2652d 2019-08-07 stsp };
6818 2db2652d 2019-08-07 stsp
6819 2db2652d 2019-08-07 stsp static const struct got_error *
6820 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6821 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6822 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6823 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6824 0cb83759 2019-08-03 stsp {
6825 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6826 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6827 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6828 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6829 0cb83759 2019-08-03 stsp uint32_t stage;
6830 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6831 8b13ce36 2019-08-08 stsp
6832 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6833 8b13ce36 2019-08-08 stsp return NULL;
6834 0cb83759 2019-08-03 stsp
6835 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6836 d3e7c587 2019-08-03 stsp if (ie == NULL)
6837 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6838 0cb83759 2019-08-03 stsp
6839 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6840 2db2652d 2019-08-07 stsp relpath)== -1)
6841 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6842 0cb83759 2019-08-03 stsp
6843 0cb83759 2019-08-03 stsp switch (status) {
6844 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6845 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6846 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6847 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6848 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6849 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6850 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6851 dc424a06 2019-08-07 stsp if (err)
6852 dc424a06 2019-08-07 stsp break;
6853 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6854 dc424a06 2019-08-07 stsp break;
6855 dc424a06 2019-08-07 stsp } else {
6856 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6857 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6858 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6859 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6860 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6861 dc424a06 2019-08-07 stsp break;
6862 dc424a06 2019-08-07 stsp }
6863 dc424a06 2019-08-07 stsp }
6864 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6865 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6866 0cb83759 2019-08-03 stsp if (err)
6867 dc424a06 2019-08-07 stsp break;
6868 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6869 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6870 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6871 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6872 0cb83759 2019-08-03 stsp else
6873 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6874 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6875 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6876 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6877 dc424a06 2019-08-07 stsp break;
6878 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6879 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6880 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6881 0cb83759 2019-08-03 stsp break;
6882 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6883 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6884 d3e7c587 2019-08-03 stsp break;
6885 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6886 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6887 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6888 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6889 dc424a06 2019-08-07 stsp if (err)
6890 dc424a06 2019-08-07 stsp break;
6891 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6892 88f33a19 2019-08-08 stsp break;
6893 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6894 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6895 dc424a06 2019-08-07 stsp break;
6896 88f33a19 2019-08-08 stsp }
6897 dc424a06 2019-08-07 stsp }
6898 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6899 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6900 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6901 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6902 dc424a06 2019-08-07 stsp break;
6903 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6904 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6905 12463d8b 2019-12-13 stsp de_name);
6906 0cb83759 2019-08-03 stsp break;
6907 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6908 d3e7c587 2019-08-03 stsp break;
6909 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6910 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6911 ebf48fd5 2019-08-03 stsp break;
6912 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6913 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6914 2a06fe5f 2019-08-24 stsp break;
6915 0cb83759 2019-08-03 stsp default:
6916 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6917 537ac44b 2019-08-03 stsp break;
6918 0cb83759 2019-08-03 stsp }
6919 dc424a06 2019-08-07 stsp
6920 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6921 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6922 dc424a06 2019-08-07 stsp free(path_content);
6923 2db2652d 2019-08-07 stsp free(ondisk_path);
6924 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6925 0ebf8283 2019-07-24 stsp return err;
6926 0ebf8283 2019-07-24 stsp }
6927 0cb83759 2019-08-03 stsp
6928 0cb83759 2019-08-03 stsp const struct got_error *
6929 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6930 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6931 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6932 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6933 1e71573e 2019-08-03 stsp struct got_repository *repo)
6934 0cb83759 2019-08-03 stsp {
6935 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6936 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6937 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6938 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6939 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6940 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6941 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6942 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6943 0cb83759 2019-08-03 stsp
6944 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6945 0cb83759 2019-08-03 stsp if (err)
6946 0cb83759 2019-08-03 stsp return err;
6947 0cb83759 2019-08-03 stsp
6948 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6949 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6950 735ef5ac 2019-08-03 stsp if (err)
6951 735ef5ac 2019-08-03 stsp goto done;
6952 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6953 735ef5ac 2019-08-03 stsp if (err)
6954 735ef5ac 2019-08-03 stsp goto done;
6955 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6956 0cb83759 2019-08-03 stsp if (err)
6957 0cb83759 2019-08-03 stsp goto done;
6958 0cb83759 2019-08-03 stsp
6959 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6960 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6961 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6962 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6963 2db2652d 2019-08-07 stsp oka.repo = repo;
6964 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6965 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6966 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6967 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6968 735ef5ac 2019-08-03 stsp if (err)
6969 735ef5ac 2019-08-03 stsp goto done;
6970 735ef5ac 2019-08-03 stsp }
6971 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6972 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6973 2db2652d 2019-08-07 stsp goto done;
6974 2db2652d 2019-08-07 stsp }
6975 735ef5ac 2019-08-03 stsp
6976 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6977 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6978 2db2652d 2019-08-07 stsp spa.repo = repo;
6979 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6980 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6981 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6982 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6983 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6984 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6985 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6986 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6987 42005733 2019-08-03 stsp if (err)
6988 2db2652d 2019-08-07 stsp goto done;
6989 7b5dc508 2019-10-28 stsp }
6990 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6991 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6992 7b5dc508 2019-10-28 stsp goto done;
6993 0cb83759 2019-08-03 stsp }
6994 0cb83759 2019-08-03 stsp
6995 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6996 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6997 0cb83759 2019-08-03 stsp err = sync_err;
6998 0cb83759 2019-08-03 stsp done:
6999 735ef5ac 2019-08-03 stsp if (head_ref)
7000 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7001 735ef5ac 2019-08-03 stsp free(head_commit_id);
7002 0cb83759 2019-08-03 stsp free(fileindex_path);
7003 0cb83759 2019-08-03 stsp if (fileindex)
7004 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7005 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7006 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7007 0cb83759 2019-08-03 stsp err = unlockerr;
7008 0cb83759 2019-08-03 stsp return err;
7009 0cb83759 2019-08-03 stsp }
7010 ad493afc 2019-08-03 stsp
7011 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7012 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7013 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7014 ad493afc 2019-08-03 stsp struct got_repository *repo;
7015 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7016 ad493afc 2019-08-03 stsp void *progress_arg;
7017 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7018 2e1f37b0 2019-08-08 stsp void *patch_arg;
7019 ad493afc 2019-08-03 stsp };
7020 2e1f37b0 2019-08-08 stsp
7021 2e1f37b0 2019-08-08 stsp static const struct got_error *
7022 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7023 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7024 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7025 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7026 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7027 2e1f37b0 2019-08-08 stsp {
7028 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7029 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7030 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7031 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7032 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7033 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7034 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7035 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7036 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7037 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7038 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7039 2e1f37b0 2019-08-08 stsp
7040 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7041 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7042 2e1f37b0 2019-08-08 stsp
7043 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7044 2e1f37b0 2019-08-08 stsp if (err)
7045 2e1f37b0 2019-08-08 stsp return err;
7046 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7047 2e1f37b0 2019-08-08 stsp if (err)
7048 2e1f37b0 2019-08-08 stsp goto done;
7049 2e1f37b0 2019-08-08 stsp
7050 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7051 2e1f37b0 2019-08-08 stsp if (err)
7052 2e1f37b0 2019-08-08 stsp goto done;
7053 2e1f37b0 2019-08-08 stsp
7054 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7055 2e1f37b0 2019-08-08 stsp if (err)
7056 2e1f37b0 2019-08-08 stsp goto done;
7057 2e1f37b0 2019-08-08 stsp
7058 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7059 2e1f37b0 2019-08-08 stsp if (err)
7060 2e1f37b0 2019-08-08 stsp goto done;
7061 2e1f37b0 2019-08-08 stsp
7062 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7063 2e1f37b0 2019-08-08 stsp if (err)
7064 2e1f37b0 2019-08-08 stsp goto done;
7065 ad493afc 2019-08-03 stsp
7066 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7067 2e1f37b0 2019-08-08 stsp if (err)
7068 2e1f37b0 2019-08-08 stsp goto done;
7069 2e1f37b0 2019-08-08 stsp
7070 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7071 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7072 2e1f37b0 2019-08-08 stsp goto done;
7073 2e1f37b0 2019-08-08 stsp }
7074 2e1f37b0 2019-08-08 stsp
7075 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7076 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7077 2e1f37b0 2019-08-08 stsp goto done;
7078 2e1f37b0 2019-08-08 stsp }
7079 2e1f37b0 2019-08-08 stsp
7080 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7081 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7082 2e1f37b0 2019-08-08 stsp if (err)
7083 2e1f37b0 2019-08-08 stsp goto done;
7084 2e1f37b0 2019-08-08 stsp
7085 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7086 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7087 2e1f37b0 2019-08-08 stsp if (err)
7088 2e1f37b0 2019-08-08 stsp goto done;
7089 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7090 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7091 2e1f37b0 2019-08-08 stsp if (err)
7092 2e1f37b0 2019-08-08 stsp goto done;
7093 2e1f37b0 2019-08-08 stsp
7094 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7095 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7096 2e1f37b0 2019-08-08 stsp goto done;
7097 2e1f37b0 2019-08-08 stsp }
7098 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7099 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7100 2e1f37b0 2019-08-08 stsp goto done;
7101 2e1f37b0 2019-08-08 stsp }
7102 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7103 2e1f37b0 2019-08-08 stsp int choice;
7104 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7105 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7106 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7107 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7108 2e1f37b0 2019-08-08 stsp if (err)
7109 2e1f37b0 2019-08-08 stsp goto done;
7110 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7111 2e1f37b0 2019-08-08 stsp have_content = 1;
7112 19e4b907 2019-08-08 stsp else
7113 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7114 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7115 2e1f37b0 2019-08-08 stsp break;
7116 2e1f37b0 2019-08-08 stsp }
7117 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7118 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7119 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7120 2e1f37b0 2019-08-08 stsp done:
7121 2e1f37b0 2019-08-08 stsp free(label1);
7122 2e1f37b0 2019-08-08 stsp if (blob)
7123 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7124 2e1f37b0 2019-08-08 stsp if (staged_blob)
7125 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7126 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7127 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7128 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7129 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7130 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7131 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7132 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7133 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7134 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7135 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7136 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7137 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7138 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7139 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7140 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7141 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7142 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7143 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7144 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7145 2e1f37b0 2019-08-08 stsp }
7146 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7147 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7148 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7149 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7150 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7151 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7152 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7153 2e1f37b0 2019-08-08 stsp }
7154 2e1f37b0 2019-08-08 stsp free(args);
7155 2e1f37b0 2019-08-08 stsp if (ds) {
7156 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7157 2e1f37b0 2019-08-08 stsp free(ds);
7158 2e1f37b0 2019-08-08 stsp }
7159 2e1f37b0 2019-08-08 stsp if (changes)
7160 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7161 2e1f37b0 2019-08-08 stsp free(path1);
7162 2e1f37b0 2019-08-08 stsp free(path2);
7163 2e1f37b0 2019-08-08 stsp return err;
7164 2e1f37b0 2019-08-08 stsp }
7165 2e1f37b0 2019-08-08 stsp
7166 ad493afc 2019-08-03 stsp static const struct got_error *
7167 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7168 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7169 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7171 ad493afc 2019-08-03 stsp {
7172 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7173 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7174 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7175 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7176 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7177 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7178 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7179 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7180 9bc94a15 2019-08-03 stsp struct stat sb;
7181 ad493afc 2019-08-03 stsp
7182 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7183 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7184 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7185 2e1f37b0 2019-08-08 stsp return NULL;
7186 2e1f37b0 2019-08-08 stsp
7187 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7188 ad493afc 2019-08-03 stsp if (ie == NULL)
7189 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7190 9bc94a15 2019-08-03 stsp
7191 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7192 9bc94a15 2019-08-03 stsp == -1)
7193 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7194 ad493afc 2019-08-03 stsp
7195 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7196 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7197 f69721c3 2019-10-21 stsp if (err)
7198 f69721c3 2019-10-21 stsp goto done;
7199 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7200 f69721c3 2019-10-21 stsp id_str) == -1) {
7201 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7202 f69721c3 2019-10-21 stsp goto done;
7203 f69721c3 2019-10-21 stsp }
7204 f69721c3 2019-10-21 stsp
7205 ad493afc 2019-08-03 stsp switch (staged_status) {
7206 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7207 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7208 ad493afc 2019-08-03 stsp blob_id, 8192);
7209 ad493afc 2019-08-03 stsp if (err)
7210 ad493afc 2019-08-03 stsp break;
7211 ad493afc 2019-08-03 stsp /* fall through */
7212 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7213 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7214 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7215 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7216 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7217 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7218 2e1f37b0 2019-08-08 stsp if (err)
7219 2e1f37b0 2019-08-08 stsp break;
7220 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7221 2e1f37b0 2019-08-08 stsp break;
7222 2e1f37b0 2019-08-08 stsp } else {
7223 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7224 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7225 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7226 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7227 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7228 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7229 2e1f37b0 2019-08-08 stsp break;
7230 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7231 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7232 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7233 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7234 2e1f37b0 2019-08-08 stsp a->repo);
7235 2e1f37b0 2019-08-08 stsp if (err)
7236 2e1f37b0 2019-08-08 stsp break;
7237 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7238 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7239 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7240 2e1f37b0 2019-08-08 stsp }
7241 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7242 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7243 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7244 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7245 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7246 f69721c3 2019-10-21 stsp a->progress_arg);
7247 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7248 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7249 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7250 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7251 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7252 2e1f37b0 2019-08-08 stsp }
7253 2e1f37b0 2019-08-08 stsp }
7254 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7255 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7256 ad493afc 2019-08-03 stsp if (err)
7257 ad493afc 2019-08-03 stsp break;
7258 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7259 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7260 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7261 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7262 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7263 ad493afc 2019-08-03 stsp if (err == NULL)
7264 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7265 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7266 ad493afc 2019-08-03 stsp break;
7267 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7268 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7269 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7270 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7271 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7272 2e1f37b0 2019-08-08 stsp if (err)
7273 2e1f37b0 2019-08-08 stsp break;
7274 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7275 2e1f37b0 2019-08-08 stsp break;
7276 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7277 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7278 2e1f37b0 2019-08-08 stsp break;
7279 2e1f37b0 2019-08-08 stsp }
7280 2e1f37b0 2019-08-08 stsp }
7281 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7282 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7283 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7284 9bc94a15 2019-08-03 stsp if (err)
7285 9bc94a15 2019-08-03 stsp break;
7286 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7287 ad493afc 2019-08-03 stsp break;
7288 ad493afc 2019-08-03 stsp }
7289 f69721c3 2019-10-21 stsp done:
7290 ad493afc 2019-08-03 stsp free(ondisk_path);
7291 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7292 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7293 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7294 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7295 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7296 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7297 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7298 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7299 ad493afc 2019-08-03 stsp if (blob_base)
7300 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7301 ad493afc 2019-08-03 stsp if (blob_staged)
7302 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7303 f69721c3 2019-10-21 stsp free(id_str);
7304 f69721c3 2019-10-21 stsp free(label_orig);
7305 ad493afc 2019-08-03 stsp return err;
7306 ad493afc 2019-08-03 stsp }
7307 ad493afc 2019-08-03 stsp
7308 ad493afc 2019-08-03 stsp const struct got_error *
7309 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7310 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7311 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7312 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7313 ad493afc 2019-08-03 stsp struct got_repository *repo)
7314 ad493afc 2019-08-03 stsp {
7315 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7316 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7317 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7318 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7319 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7320 ad493afc 2019-08-03 stsp
7321 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7322 ad493afc 2019-08-03 stsp if (err)
7323 ad493afc 2019-08-03 stsp return err;
7324 ad493afc 2019-08-03 stsp
7325 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7326 ad493afc 2019-08-03 stsp if (err)
7327 ad493afc 2019-08-03 stsp goto done;
7328 ad493afc 2019-08-03 stsp
7329 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7330 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7331 ad493afc 2019-08-03 stsp upa.repo = repo;
7332 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7333 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7334 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7335 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7336 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7337 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7338 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7339 ad493afc 2019-08-03 stsp if (err)
7340 ad493afc 2019-08-03 stsp goto done;
7341 ad493afc 2019-08-03 stsp }
7342 ad493afc 2019-08-03 stsp
7343 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7344 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7345 ad493afc 2019-08-03 stsp err = sync_err;
7346 ad493afc 2019-08-03 stsp done:
7347 ad493afc 2019-08-03 stsp free(fileindex_path);
7348 ad493afc 2019-08-03 stsp if (fileindex)
7349 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7350 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7351 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7352 ad493afc 2019-08-03 stsp err = unlockerr;
7353 ad493afc 2019-08-03 stsp return err;
7354 ad493afc 2019-08-03 stsp }