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