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