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