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