Blame


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