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