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