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 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.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 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
36 86c3caaf 2018-03-09 stsp
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 86c3caaf 2018-03-09 stsp
45 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
52 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
53 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
54 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
55 9d31a1d8 2018-03-11 stsp
56 9d31a1d8 2018-03-11 stsp #ifndef MIN
57 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 9d31a1d8 2018-03-11 stsp #endif
59 86c3caaf 2018-03-09 stsp
60 99724ed4 2018-03-10 stsp static const struct got_error *
61 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
62 99724ed4 2018-03-10 stsp {
63 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
64 99724ed4 2018-03-10 stsp char *path;
65 99724ed4 2018-03-10 stsp int fd = -1;
66 99724ed4 2018-03-10 stsp
67 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
69 99724ed4 2018-03-10 stsp path = NULL;
70 99724ed4 2018-03-10 stsp goto done;
71 99724ed4 2018-03-10 stsp }
72 99724ed4 2018-03-10 stsp
73 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
75 99724ed4 2018-03-10 stsp if (fd == -1) {
76 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
77 99724ed4 2018-03-10 stsp goto done;
78 99724ed4 2018-03-10 stsp }
79 99724ed4 2018-03-10 stsp
80 99724ed4 2018-03-10 stsp if (content) {
81 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
82 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
83 638f9024 2019-05-13 stsp err = got_error_from_errno("dprintf");
84 99724ed4 2018-03-10 stsp goto done;
85 99724ed4 2018-03-10 stsp }
86 99724ed4 2018-03-10 stsp }
87 99724ed4 2018-03-10 stsp
88 99724ed4 2018-03-10 stsp done:
89 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
90 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
91 99724ed4 2018-03-10 stsp free(path);
92 507dc3bb 2018-12-29 stsp return err;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp static const struct got_error *
96 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
97 507dc3bb 2018-12-29 stsp {
98 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
99 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
100 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
101 507dc3bb 2018-12-29 stsp char *path = NULL;
102 507dc3bb 2018-12-29 stsp
103 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
105 507dc3bb 2018-12-29 stsp path = NULL;
106 507dc3bb 2018-12-29 stsp goto done;
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp
109 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
110 507dc3bb 2018-12-29 stsp if (err)
111 507dc3bb 2018-12-29 stsp goto done;
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp if (content) {
114 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
115 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
117 507dc3bb 2018-12-29 stsp goto done;
118 507dc3bb 2018-12-29 stsp }
119 507dc3bb 2018-12-29 stsp }
120 507dc3bb 2018-12-29 stsp
121 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
122 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
123 2a57020b 2019-02-20 stsp unlink(tmppath);
124 507dc3bb 2018-12-29 stsp goto done;
125 507dc3bb 2018-12-29 stsp }
126 507dc3bb 2018-12-29 stsp
127 507dc3bb 2018-12-29 stsp done:
128 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
129 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
130 230a42bd 2019-05-11 jcs free(tmppath);
131 99724ed4 2018-03-10 stsp return err;
132 99724ed4 2018-03-10 stsp }
133 99724ed4 2018-03-10 stsp
134 09fe317a 2018-03-11 stsp static const struct got_error *
135 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
136 09fe317a 2018-03-11 stsp {
137 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
138 09fe317a 2018-03-11 stsp char *path;
139 09fe317a 2018-03-11 stsp int fd = -1;
140 09fe317a 2018-03-11 stsp ssize_t n;
141 09fe317a 2018-03-11 stsp struct stat sb;
142 09fe317a 2018-03-11 stsp
143 09fe317a 2018-03-11 stsp *content = NULL;
144 09fe317a 2018-03-11 stsp
145 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
147 09fe317a 2018-03-11 stsp path = NULL;
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
152 09fe317a 2018-03-11 stsp if (fd == -1) {
153 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
154 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
155 f02eaa22 2019-03-11 stsp else
156 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
157 ef99fdb1 2018-03-11 stsp goto done;
158 ef99fdb1 2018-03-11 stsp }
159 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
162 09fe317a 2018-03-11 stsp goto done;
163 09fe317a 2018-03-11 stsp }
164 09fe317a 2018-03-11 stsp
165 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
166 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
167 d10c9b58 2019-02-19 stsp goto done;
168 d10c9b58 2019-02-19 stsp }
169 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
170 09fe317a 2018-03-11 stsp if (*content == NULL) {
171 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
172 09fe317a 2018-03-11 stsp goto done;
173 09fe317a 2018-03-11 stsp }
174 09fe317a 2018-03-11 stsp
175 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
176 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
177 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
178 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
179 09fe317a 2018-03-11 stsp goto done;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
182 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
183 09fe317a 2018-03-11 stsp goto done;
184 09fe317a 2018-03-11 stsp }
185 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
186 09fe317a 2018-03-11 stsp
187 09fe317a 2018-03-11 stsp done:
188 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
189 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
190 09fe317a 2018-03-11 stsp free(path);
191 09fe317a 2018-03-11 stsp if (err) {
192 09fe317a 2018-03-11 stsp free(*content);
193 09fe317a 2018-03-11 stsp *content = NULL;
194 09fe317a 2018-03-11 stsp }
195 09fe317a 2018-03-11 stsp return err;
196 09fe317a 2018-03-11 stsp }
197 09fe317a 2018-03-11 stsp
198 024e9686 2019-05-14 stsp static const struct got_error *
199 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
200 024e9686 2019-05-14 stsp {
201 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
202 024e9686 2019-05-14 stsp char *refstr = NULL;
203 024e9686 2019-05-14 stsp
204 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
205 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
206 024e9686 2019-05-14 stsp if (refstr == NULL)
207 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
208 024e9686 2019-05-14 stsp } else {
209 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
210 024e9686 2019-05-14 stsp if (refstr == NULL)
211 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
212 024e9686 2019-05-14 stsp }
213 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
214 024e9686 2019-05-14 stsp free(refstr);
215 024e9686 2019-05-14 stsp return err;
216 024e9686 2019-05-14 stsp }
217 024e9686 2019-05-14 stsp
218 86c3caaf 2018-03-09 stsp const struct got_error *
219 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
220 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
221 86c3caaf 2018-03-09 stsp {
222 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
223 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
224 ec22038e 2019-03-10 stsp uuid_t uuid;
225 ec22038e 2019-03-10 stsp uint32_t uuid_status;
226 65596e15 2018-12-24 stsp int obj_type;
227 7ac97322 2018-03-11 stsp char *path_got = NULL;
228 1451e70d 2018-03-10 stsp char *formatstr = NULL;
229 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
230 65596e15 2018-12-24 stsp char *basestr = NULL;
231 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
232 65596e15 2018-12-24 stsp
233 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
234 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
235 0c48fee2 2019-03-11 stsp goto done;
236 0c48fee2 2019-03-11 stsp }
237 0c48fee2 2019-03-11 stsp
238 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
239 65596e15 2018-12-24 stsp if (err)
240 65596e15 2018-12-24 stsp return err;
241 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
242 65596e15 2018-12-24 stsp if (err)
243 65596e15 2018-12-24 stsp return err;
244 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
245 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
246 86c3caaf 2018-03-09 stsp
247 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
248 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
249 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
250 0bb8a95e 2018-03-12 stsp }
251 577ec78f 2018-03-11 stsp
252 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
253 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
254 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
255 86c3caaf 2018-03-09 stsp goto done;
256 86c3caaf 2018-03-09 stsp }
257 86c3caaf 2018-03-09 stsp
258 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
259 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
260 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp }
263 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
264 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
265 86c3caaf 2018-03-09 stsp goto done;
266 86c3caaf 2018-03-09 stsp }
267 86c3caaf 2018-03-09 stsp
268 056e7441 2018-03-11 stsp /* Create an empty lock file. */
269 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
270 056e7441 2018-03-11 stsp if (err)
271 056e7441 2018-03-11 stsp goto done;
272 056e7441 2018-03-11 stsp
273 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
274 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
275 99724ed4 2018-03-10 stsp if (err)
276 86c3caaf 2018-03-09 stsp goto done;
277 86c3caaf 2018-03-09 stsp
278 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
279 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
280 65596e15 2018-12-24 stsp if (err)
281 65596e15 2018-12-24 stsp goto done;
282 65596e15 2018-12-24 stsp
283 65596e15 2018-12-24 stsp /* Record our base commit. */
284 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
285 65596e15 2018-12-24 stsp if (err)
286 65596e15 2018-12-24 stsp goto done;
287 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
288 99724ed4 2018-03-10 stsp if (err)
289 86c3caaf 2018-03-09 stsp goto done;
290 86c3caaf 2018-03-09 stsp
291 1451e70d 2018-03-10 stsp /* Store path to repository. */
292 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
293 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
294 99724ed4 2018-03-10 stsp if (err)
295 86c3caaf 2018-03-09 stsp goto done;
296 86c3caaf 2018-03-09 stsp
297 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
298 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
299 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
300 ec22038e 2019-03-10 stsp if (err)
301 ec22038e 2019-03-10 stsp goto done;
302 ec22038e 2019-03-10 stsp
303 ec22038e 2019-03-10 stsp /* Generate UUID. */
304 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
305 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
306 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
307 ec22038e 2019-03-10 stsp goto done;
308 ec22038e 2019-03-10 stsp }
309 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
310 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
311 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
312 ec22038e 2019-03-10 stsp goto done;
313 ec22038e 2019-03-10 stsp }
314 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
315 577ec78f 2018-03-11 stsp if (err)
316 577ec78f 2018-03-11 stsp goto done;
317 577ec78f 2018-03-11 stsp
318 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
319 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
320 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
321 1451e70d 2018-03-10 stsp goto done;
322 1451e70d 2018-03-10 stsp }
323 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
324 99724ed4 2018-03-10 stsp if (err)
325 1451e70d 2018-03-10 stsp goto done;
326 1451e70d 2018-03-10 stsp
327 86c3caaf 2018-03-09 stsp done:
328 65596e15 2018-12-24 stsp free(commit_id);
329 7ac97322 2018-03-11 stsp free(path_got);
330 1451e70d 2018-03-10 stsp free(formatstr);
331 0bb8a95e 2018-03-12 stsp free(absprefix);
332 65596e15 2018-12-24 stsp free(basestr);
333 ec22038e 2019-03-10 stsp free(uuidstr);
334 86c3caaf 2018-03-09 stsp return err;
335 86c3caaf 2018-03-09 stsp }
336 86c3caaf 2018-03-09 stsp
337 247140b2 2019-02-05 stsp static const struct got_error *
338 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
339 86c3caaf 2018-03-09 stsp {
340 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
341 7ac97322 2018-03-11 stsp char *path_got;
342 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
343 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
344 056e7441 2018-03-11 stsp char *path_lock = NULL;
345 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
346 6d9d28c3 2018-03-11 stsp int version, fd = -1;
347 6d9d28c3 2018-03-11 stsp const char *errstr;
348 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
349 c442a90d 2019-03-10 stsp uint32_t uuid_status;
350 6d9d28c3 2018-03-11 stsp
351 6d9d28c3 2018-03-11 stsp *worktree = NULL;
352 6d9d28c3 2018-03-11 stsp
353 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
355 7ac97322 2018-03-11 stsp path_got = NULL;
356 6d9d28c3 2018-03-11 stsp goto done;
357 6d9d28c3 2018-03-11 stsp }
358 6d9d28c3 2018-03-11 stsp
359 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
361 056e7441 2018-03-11 stsp path_lock = NULL;
362 6d9d28c3 2018-03-11 stsp goto done;
363 6d9d28c3 2018-03-11 stsp }
364 6d9d28c3 2018-03-11 stsp
365 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
366 6d9d28c3 2018-03-11 stsp if (fd == -1) {
367 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
368 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
373 6d9d28c3 2018-03-11 stsp if (err)
374 6d9d28c3 2018-03-11 stsp goto done;
375 6d9d28c3 2018-03-11 stsp
376 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
377 6d9d28c3 2018-03-11 stsp if (errstr) {
378 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
379 6d9d28c3 2018-03-11 stsp goto done;
380 6d9d28c3 2018-03-11 stsp }
381 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
382 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 6d9d28c3 2018-03-11 stsp
386 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
387 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
388 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
389 6d9d28c3 2018-03-11 stsp goto done;
390 6d9d28c3 2018-03-11 stsp }
391 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
392 6d9d28c3 2018-03-11 stsp
393 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
394 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
395 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
396 6d9d28c3 2018-03-11 stsp goto done;
397 6d9d28c3 2018-03-11 stsp }
398 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
399 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
400 6d9d28c3 2018-03-11 stsp if (err)
401 6d9d28c3 2018-03-11 stsp goto done;
402 eaccb85f 2018-12-25 stsp
403 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
404 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
405 93a30277 2018-12-24 stsp if (err)
406 93a30277 2018-12-24 stsp goto done;
407 93a30277 2018-12-24 stsp
408 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
409 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
410 c442a90d 2019-03-10 stsp if (err)
411 c442a90d 2019-03-10 stsp goto done;
412 c442a90d 2019-03-10 stsp
413 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
414 eaccb85f 2018-12-25 stsp if (err)
415 c442a90d 2019-03-10 stsp goto done;
416 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
417 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
418 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
419 eaccb85f 2018-12-25 stsp goto done;
420 c442a90d 2019-03-10 stsp }
421 eaccb85f 2018-12-25 stsp
422 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
423 eaccb85f 2018-12-25 stsp if (err)
424 eaccb85f 2018-12-25 stsp goto done;
425 eaccb85f 2018-12-25 stsp
426 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
427 eaccb85f 2018-12-25 stsp base_commit_id_str);
428 f5baf295 2018-03-11 stsp if (err)
429 6d9d28c3 2018-03-11 stsp goto done;
430 6d9d28c3 2018-03-11 stsp
431 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
432 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
433 6d9d28c3 2018-03-11 stsp done:
434 eaccb85f 2018-12-25 stsp if (repo)
435 eaccb85f 2018-12-25 stsp got_repo_close(repo);
436 7ac97322 2018-03-11 stsp free(path_got);
437 056e7441 2018-03-11 stsp free(path_lock);
438 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
439 c442a90d 2019-03-10 stsp free(uuidstr);
440 bd165944 2019-03-10 stsp free(formatstr);
441 6d9d28c3 2018-03-11 stsp if (err) {
442 6d9d28c3 2018-03-11 stsp if (fd != -1)
443 6d9d28c3 2018-03-11 stsp close(fd);
444 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
445 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
446 6d9d28c3 2018-03-11 stsp *worktree = NULL;
447 6d9d28c3 2018-03-11 stsp } else
448 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
449 6d9d28c3 2018-03-11 stsp
450 6d9d28c3 2018-03-11 stsp return err;
451 86c3caaf 2018-03-09 stsp }
452 247140b2 2019-02-05 stsp
453 247140b2 2019-02-05 stsp const struct got_error *
454 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
455 247140b2 2019-02-05 stsp {
456 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
457 86c3caaf 2018-03-09 stsp
458 247140b2 2019-02-05 stsp do {
459 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
460 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
461 247140b2 2019-02-05 stsp return err;
462 247140b2 2019-02-05 stsp if (*worktree)
463 247140b2 2019-02-05 stsp return NULL;
464 247140b2 2019-02-05 stsp path = dirname(path);
465 d1542a27 2019-02-05 stsp if (path == NULL)
466 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
467 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
468 247140b2 2019-02-05 stsp
469 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
470 247140b2 2019-02-05 stsp }
471 247140b2 2019-02-05 stsp
472 3a6ce05a 2019-02-11 stsp const struct got_error *
473 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
474 86c3caaf 2018-03-09 stsp {
475 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
476 c88eb298 2018-03-11 stsp free(worktree->root_path);
477 cde76477 2018-03-11 stsp free(worktree->repo_path);
478 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
479 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
480 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
481 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
482 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
483 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
484 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
485 6d9d28c3 2018-03-11 stsp free(worktree);
486 3a6ce05a 2019-02-11 stsp return err;
487 86c3caaf 2018-03-09 stsp }
488 86c3caaf 2018-03-09 stsp
489 2fbdb5ae 2018-12-29 stsp const char *
490 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
491 c7f4312f 2019-02-05 stsp {
492 c7f4312f 2019-02-05 stsp return worktree->root_path;
493 c7f4312f 2019-02-05 stsp }
494 c7f4312f 2019-02-05 stsp
495 c7f4312f 2019-02-05 stsp const char *
496 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
497 86c3caaf 2018-03-09 stsp {
498 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
499 49520a32 2018-12-29 stsp }
500 49520a32 2018-12-29 stsp
501 49520a32 2018-12-29 stsp const char *
502 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
503 49520a32 2018-12-29 stsp {
504 f609be2e 2018-12-29 stsp return worktree->path_prefix;
505 e5dc7198 2018-12-29 stsp }
506 e5dc7198 2018-12-29 stsp
507 e5dc7198 2018-12-29 stsp const struct got_error *
508 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 e5dc7198 2018-12-29 stsp const char *path_prefix)
510 e5dc7198 2018-12-29 stsp {
511 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
512 e5dc7198 2018-12-29 stsp
513 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
514 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
516 e5dc7198 2018-12-29 stsp }
517 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
518 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
519 e5dc7198 2018-12-29 stsp free(absprefix);
520 e5dc7198 2018-12-29 stsp return NULL;
521 86c3caaf 2018-03-09 stsp }
522 86c3caaf 2018-03-09 stsp
523 bc70eb79 2019-05-09 stsp const char *
524 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
525 86c3caaf 2018-03-09 stsp {
526 36a38700 2019-05-10 stsp return worktree->head_ref_name;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp const struct got_error *
530 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
531 024e9686 2019-05-14 stsp struct got_reference *head_ref)
532 024e9686 2019-05-14 stsp {
533 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
534 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
535 024e9686 2019-05-14 stsp
536 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
538 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
539 024e9686 2019-05-14 stsp path_got = NULL;
540 024e9686 2019-05-14 stsp goto done;
541 024e9686 2019-05-14 stsp }
542 024e9686 2019-05-14 stsp
543 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
544 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
545 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
546 024e9686 2019-05-14 stsp goto done;
547 024e9686 2019-05-14 stsp }
548 024e9686 2019-05-14 stsp
549 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
550 024e9686 2019-05-14 stsp if (err)
551 024e9686 2019-05-14 stsp goto done;
552 024e9686 2019-05-14 stsp
553 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
554 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
555 024e9686 2019-05-14 stsp done:
556 024e9686 2019-05-14 stsp free(path_got);
557 024e9686 2019-05-14 stsp if (err)
558 024e9686 2019-05-14 stsp free(head_ref_name);
559 024e9686 2019-05-14 stsp return err;
560 507dc3bb 2018-12-29 stsp }
561 507dc3bb 2018-12-29 stsp
562 b72f483a 2019-02-05 stsp struct got_object_id *
563 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
564 507dc3bb 2018-12-29 stsp {
565 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
566 86c3caaf 2018-03-09 stsp }
567 86c3caaf 2018-03-09 stsp
568 507dc3bb 2018-12-29 stsp const struct got_error *
569 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
571 507dc3bb 2018-12-29 stsp {
572 507dc3bb 2018-12-29 stsp const struct got_error *err;
573 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
574 507dc3bb 2018-12-29 stsp char *id_str = NULL;
575 507dc3bb 2018-12-29 stsp char *path_got = NULL;
576 507dc3bb 2018-12-29 stsp
577 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
580 507dc3bb 2018-12-29 stsp path_got = NULL;
581 507dc3bb 2018-12-29 stsp goto done;
582 507dc3bb 2018-12-29 stsp }
583 507dc3bb 2018-12-29 stsp
584 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
585 507dc3bb 2018-12-29 stsp if (err)
586 507dc3bb 2018-12-29 stsp return err;
587 507dc3bb 2018-12-29 stsp
588 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
590 507dc3bb 2018-12-29 stsp goto done;
591 507dc3bb 2018-12-29 stsp }
592 507dc3bb 2018-12-29 stsp
593 507dc3bb 2018-12-29 stsp /* Record our base commit. */
594 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
595 507dc3bb 2018-12-29 stsp if (err)
596 507dc3bb 2018-12-29 stsp goto done;
597 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 507dc3bb 2018-12-29 stsp if (err)
599 507dc3bb 2018-12-29 stsp goto done;
600 507dc3bb 2018-12-29 stsp
601 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
602 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
603 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
605 507dc3bb 2018-12-29 stsp goto done;
606 507dc3bb 2018-12-29 stsp }
607 507dc3bb 2018-12-29 stsp done:
608 507dc3bb 2018-12-29 stsp if (obj)
609 507dc3bb 2018-12-29 stsp got_object_close(obj);
610 507dc3bb 2018-12-29 stsp free(id_str);
611 507dc3bb 2018-12-29 stsp free(path_got);
612 507dc3bb 2018-12-29 stsp return err;
613 507dc3bb 2018-12-29 stsp }
614 507dc3bb 2018-12-29 stsp
615 9d31a1d8 2018-03-11 stsp static const struct got_error *
616 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
617 86c3caaf 2018-03-09 stsp {
618 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
619 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
620 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
621 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
622 86c3caaf 2018-03-09 stsp return NULL;
623 21908da4 2019-01-13 stsp }
624 21908da4 2019-01-13 stsp
625 21908da4 2019-01-13 stsp static const struct got_error *
626 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
627 4a1ddfc2 2019-01-12 stsp {
628 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
629 4a1ddfc2 2019-01-12 stsp char *abspath;
630 4a1ddfc2 2019-01-12 stsp
631 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
632 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
633 4a1ddfc2 2019-01-12 stsp
634 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
635 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
636 ddcd8544 2019-03-11 stsp struct stat sb;
637 ddcd8544 2019-03-11 stsp err = NULL;
638 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
640 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
641 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
642 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
643 ddcd8544 2019-03-11 stsp }
644 ddcd8544 2019-03-11 stsp }
645 4a1ddfc2 2019-01-12 stsp free(abspath);
646 68c76935 2019-02-19 stsp return err;
647 68c76935 2019-02-19 stsp }
648 68c76935 2019-02-19 stsp
649 68c76935 2019-02-19 stsp static const struct got_error *
650 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
651 68c76935 2019-02-19 stsp {
652 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
653 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
654 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
655 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
656 68c76935 2019-02-19 stsp
657 68c76935 2019-02-19 stsp *same = 1;
658 68c76935 2019-02-19 stsp
659 230a42bd 2019-05-11 jcs for (;;) {
660 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
661 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
662 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp }
665 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
666 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
667 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
668 68c76935 2019-02-19 stsp break;
669 68c76935 2019-02-19 stsp }
670 68c76935 2019-02-19 stsp if (flen1 == 0) {
671 68c76935 2019-02-19 stsp if (flen2 != 0)
672 68c76935 2019-02-19 stsp *same = 0;
673 68c76935 2019-02-19 stsp break;
674 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
675 68c76935 2019-02-19 stsp if (flen1 != 0)
676 68c76935 2019-02-19 stsp *same = 0;
677 68c76935 2019-02-19 stsp break;
678 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
679 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
680 68c76935 2019-02-19 stsp *same = 0;
681 68c76935 2019-02-19 stsp break;
682 68c76935 2019-02-19 stsp }
683 68c76935 2019-02-19 stsp } else {
684 68c76935 2019-02-19 stsp *same = 0;
685 68c76935 2019-02-19 stsp break;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp }
688 68c76935 2019-02-19 stsp
689 68c76935 2019-02-19 stsp return err;
690 68c76935 2019-02-19 stsp }
691 68c76935 2019-02-19 stsp
692 68c76935 2019-02-19 stsp static const struct got_error *
693 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
694 68c76935 2019-02-19 stsp {
695 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
696 68c76935 2019-02-19 stsp struct stat sb;
697 68c76935 2019-02-19 stsp size_t size1, size2;
698 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp *same = 1;
701 68c76935 2019-02-19 stsp
702 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
704 68c76935 2019-02-19 stsp goto done;
705 68c76935 2019-02-19 stsp }
706 68c76935 2019-02-19 stsp size1 = sb.st_size;
707 68c76935 2019-02-19 stsp
708 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
710 68c76935 2019-02-19 stsp goto done;
711 68c76935 2019-02-19 stsp }
712 68c76935 2019-02-19 stsp size2 = sb.st_size;
713 68c76935 2019-02-19 stsp
714 68c76935 2019-02-19 stsp if (size1 != size2) {
715 68c76935 2019-02-19 stsp *same = 0;
716 68c76935 2019-02-19 stsp return NULL;
717 68c76935 2019-02-19 stsp }
718 68c76935 2019-02-19 stsp
719 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
720 68c76935 2019-02-19 stsp if (f1 == NULL)
721 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
722 68c76935 2019-02-19 stsp
723 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
724 68c76935 2019-02-19 stsp if (f2 == NULL) {
725 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
726 68c76935 2019-02-19 stsp goto done;
727 68c76935 2019-02-19 stsp }
728 68c76935 2019-02-19 stsp
729 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
730 68c76935 2019-02-19 stsp done:
731 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
732 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
733 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
734 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
735 68c76935 2019-02-19 stsp
736 6353ad76 2019-02-08 stsp return err;
737 6353ad76 2019-02-08 stsp }
738 6353ad76 2019-02-08 stsp
739 6353ad76 2019-02-08 stsp /*
740 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
741 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
742 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
743 6353ad76 2019-02-08 stsp */
744 6353ad76 2019-02-08 stsp static const struct got_error *
745 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
746 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
747 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
748 b8f41171 2019-02-10 stsp struct got_repository *repo,
749 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
750 6353ad76 2019-02-08 stsp {
751 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
752 6353ad76 2019-02-08 stsp int merged_fd = -1;
753 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
754 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
755 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
756 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
757 6353ad76 2019-02-08 stsp char *id_str = NULL;
758 6353ad76 2019-02-08 stsp char *label1 = NULL;
759 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
760 af54ae4a 2019-02-19 stsp char *parent;
761 6353ad76 2019-02-08 stsp
762 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
763 af54ae4a 2019-02-19 stsp if (parent == NULL)
764 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
765 af54ae4a 2019-02-19 stsp
766 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
767 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
768 af54ae4a 2019-02-19 stsp
769 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
770 6353ad76 2019-02-08 stsp if (err)
771 af54ae4a 2019-02-19 stsp goto done;
772 af54ae4a 2019-02-19 stsp
773 af54ae4a 2019-02-19 stsp free(base_path);
774 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
775 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
776 af54ae4a 2019-02-19 stsp base_path = NULL;
777 af54ae4a 2019-02-19 stsp goto done;
778 af54ae4a 2019-02-19 stsp }
779 af54ae4a 2019-02-19 stsp
780 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
781 6353ad76 2019-02-08 stsp if (err)
782 6353ad76 2019-02-08 stsp goto done;
783 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
784 6353ad76 2019-02-08 stsp if (err)
785 6353ad76 2019-02-08 stsp goto done;
786 6353ad76 2019-02-08 stsp
787 af54ae4a 2019-02-19 stsp free(base_path);
788 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
789 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
790 af54ae4a 2019-02-19 stsp base_path = NULL;
791 af54ae4a 2019-02-19 stsp goto done;
792 af54ae4a 2019-02-19 stsp }
793 af54ae4a 2019-02-19 stsp
794 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
795 6353ad76 2019-02-08 stsp if (err)
796 6353ad76 2019-02-08 stsp goto done;
797 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie)) {
798 1430b4e0 2019-03-27 stsp struct got_object_id id2;
799 1430b4e0 2019-03-27 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
800 1430b4e0 2019-03-27 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
801 1430b4e0 2019-03-27 stsp if (err)
802 1430b4e0 2019-03-27 stsp goto done;
803 1430b4e0 2019-03-27 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
804 1430b4e0 2019-03-27 stsp if (err)
805 1430b4e0 2019-03-27 stsp goto done;
806 1430b4e0 2019-03-27 stsp } else {
807 1430b4e0 2019-03-27 stsp /*
808 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
809 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
810 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
811 1430b4e0 2019-03-27 stsp */
812 1430b4e0 2019-03-27 stsp }
813 6353ad76 2019-02-08 stsp
814 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
815 6353ad76 2019-02-08 stsp if (err)
816 6353ad76 2019-02-08 stsp goto done;
817 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
818 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
819 6353ad76 2019-02-08 stsp goto done;
820 6353ad76 2019-02-08 stsp }
821 6353ad76 2019-02-08 stsp
822 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
823 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
824 6353ad76 2019-02-08 stsp if (err)
825 6353ad76 2019-02-08 stsp goto done;
826 6353ad76 2019-02-08 stsp
827 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
828 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
829 6353ad76 2019-02-08 stsp
830 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
831 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
832 816dc654 2019-02-16 stsp goto done;
833 68c76935 2019-02-19 stsp }
834 68c76935 2019-02-19 stsp
835 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
836 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
837 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
838 68c76935 2019-02-19 stsp merged_path);
839 68c76935 2019-02-19 stsp if (err)
840 68c76935 2019-02-19 stsp goto done;
841 816dc654 2019-02-16 stsp }
842 6353ad76 2019-02-08 stsp
843 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
844 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
845 70a0c8ec 2019-02-20 stsp goto done;
846 70a0c8ec 2019-02-20 stsp }
847 70a0c8ec 2019-02-20 stsp
848 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
849 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
850 230a42bd 2019-05-11 jcs ondisk_path);
851 2a57020b 2019-02-20 stsp unlink(merged_path);
852 6353ad76 2019-02-08 stsp goto done;
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp
855 02c07007 2019-02-10 stsp /*
856 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
857 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
858 02c07007 2019-02-10 stsp */
859 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
860 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
861 6353ad76 2019-02-08 stsp done:
862 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
863 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
864 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
865 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
866 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
868 6353ad76 2019-02-08 stsp if (blob2)
869 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 6353ad76 2019-02-08 stsp if (blob1_path) {
873 6353ad76 2019-02-08 stsp unlink(blob1_path);
874 6353ad76 2019-02-08 stsp free(blob1_path);
875 6353ad76 2019-02-08 stsp }
876 6353ad76 2019-02-08 stsp if (blob2_path) {
877 6353ad76 2019-02-08 stsp unlink(blob2_path);
878 6353ad76 2019-02-08 stsp free(blob2_path);
879 6353ad76 2019-02-08 stsp }
880 6353ad76 2019-02-08 stsp free(id_str);
881 6353ad76 2019-02-08 stsp free(label1);
882 4a1ddfc2 2019-01-12 stsp return err;
883 4a1ddfc2 2019-01-12 stsp }
884 4a1ddfc2 2019-01-12 stsp
885 4a1ddfc2 2019-01-12 stsp static const struct got_error *
886 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
887 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
888 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
889 13d9040b 2019-03-27 stsp int update_timestamps)
890 13d9040b 2019-03-27 stsp {
891 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
892 13d9040b 2019-03-27 stsp
893 13d9040b 2019-03-27 stsp if (ie == NULL)
894 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
895 13d9040b 2019-03-27 stsp if (ie)
896 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
897 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
898 13d9040b 2019-03-27 stsp update_timestamps);
899 13d9040b 2019-03-27 stsp else {
900 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
901 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
902 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
903 13d9040b 2019-03-27 stsp if (!err)
904 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
905 13d9040b 2019-03-27 stsp }
906 13d9040b 2019-03-27 stsp return err;
907 13d9040b 2019-03-27 stsp }
908 13d9040b 2019-03-27 stsp
909 13d9040b 2019-03-27 stsp static const struct got_error *
910 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
911 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
912 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
913 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
914 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
915 9d31a1d8 2018-03-11 stsp {
916 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
917 507dc3bb 2018-12-29 stsp int fd = -1;
918 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
919 507dc3bb 2018-12-29 stsp int update = 0;
920 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
921 9d31a1d8 2018-03-11 stsp
922 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
923 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
924 9d31a1d8 2018-03-11 stsp if (fd == -1) {
925 21908da4 2019-01-13 stsp if (errno == ENOENT) {
926 21908da4 2019-01-13 stsp char *parent = dirname(path);
927 21908da4 2019-01-13 stsp if (parent == NULL)
928 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
929 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
930 21908da4 2019-01-13 stsp if (err)
931 21908da4 2019-01-13 stsp return err;
932 21908da4 2019-01-13 stsp fd = open(ondisk_path,
933 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
934 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
935 21908da4 2019-01-13 stsp if (fd == -1)
936 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
937 230a42bd 2019-05-11 jcs ondisk_path);
938 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
939 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
940 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
941 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
942 507dc3bb 2018-12-29 stsp goto done;
943 d70b8e30 2018-12-27 stsp } else {
944 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
945 507dc3bb 2018-12-29 stsp ondisk_path);
946 507dc3bb 2018-12-29 stsp if (err)
947 507dc3bb 2018-12-29 stsp goto done;
948 507dc3bb 2018-12-29 stsp update = 1;
949 9d31a1d8 2018-03-11 stsp }
950 507dc3bb 2018-12-29 stsp } else
951 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
952 9d31a1d8 2018-03-11 stsp }
953 9d31a1d8 2018-03-11 stsp
954 a378724f 2019-02-10 stsp if (restoring_missing_file)
955 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
956 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
957 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
958 a378724f 2019-02-10 stsp else
959 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
960 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
961 d7b62c98 2018-12-27 stsp
962 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
963 9d31a1d8 2018-03-11 stsp do {
964 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
965 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
966 9d31a1d8 2018-03-11 stsp if (err)
967 9d31a1d8 2018-03-11 stsp break;
968 9d31a1d8 2018-03-11 stsp if (len > 0) {
969 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
970 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
971 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
972 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
973 b87c6f83 2018-12-24 stsp goto done;
974 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
975 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
976 b87c6f83 2018-12-24 stsp goto done;
977 9d31a1d8 2018-03-11 stsp }
978 61d6eaa3 2018-12-24 stsp hdrlen = 0;
979 9d31a1d8 2018-03-11 stsp }
980 9d31a1d8 2018-03-11 stsp } while (len != 0);
981 9d31a1d8 2018-03-11 stsp
982 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
983 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
984 816dc654 2019-02-16 stsp goto done;
985 816dc654 2019-02-16 stsp }
986 9d31a1d8 2018-03-11 stsp
987 507dc3bb 2018-12-29 stsp if (update) {
988 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
989 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
990 230a42bd 2019-05-11 jcs ondisk_path);
991 2a57020b 2019-02-20 stsp unlink(tmppath);
992 68ed9ba5 2019-02-10 stsp goto done;
993 68ed9ba5 2019-02-10 stsp }
994 68ed9ba5 2019-02-10 stsp }
995 ba8a0d4d 2019-02-10 stsp
996 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
997 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
998 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
999 507dc3bb 2018-12-29 stsp goto done;
1000 507dc3bb 2018-12-29 stsp }
1001 ba8a0d4d 2019-02-10 stsp } else {
1002 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1003 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
1004 68ed9ba5 2019-02-10 stsp goto done;
1005 68ed9ba5 2019-02-10 stsp }
1006 507dc3bb 2018-12-29 stsp }
1007 507dc3bb 2018-12-29 stsp
1008 9d31a1d8 2018-03-11 stsp done:
1009 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1010 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1011 507dc3bb 2018-12-29 stsp free(tmppath);
1012 6353ad76 2019-02-08 stsp return err;
1013 6353ad76 2019-02-08 stsp }
1014 6353ad76 2019-02-08 stsp
1015 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1016 6353ad76 2019-02-08 stsp static const struct got_error *
1017 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1018 7154f6ce 2019-03-27 stsp {
1019 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1020 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1021 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1022 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1023 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1024 7154f6ce 2019-03-27 stsp };
1025 7154f6ce 2019-03-27 stsp int i = 0;
1026 7154f6ce 2019-03-27 stsp char *line;
1027 7154f6ce 2019-03-27 stsp size_t len;
1028 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1029 7154f6ce 2019-03-27 stsp
1030 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1031 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1032 7154f6ce 2019-03-27 stsp if (line == NULL) {
1033 7154f6ce 2019-03-27 stsp if (feof(f))
1034 7154f6ce 2019-03-27 stsp break;
1035 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1036 7154f6ce 2019-03-27 stsp break;
1037 7154f6ce 2019-03-27 stsp }
1038 7154f6ce 2019-03-27 stsp
1039 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1040 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1041 19332e6d 2019-05-13 stsp == 0)
1042 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1043 7154f6ce 2019-03-27 stsp else
1044 7154f6ce 2019-03-27 stsp i++;
1045 7154f6ce 2019-03-27 stsp }
1046 7154f6ce 2019-03-27 stsp }
1047 7154f6ce 2019-03-27 stsp
1048 7154f6ce 2019-03-27 stsp return err;
1049 7154f6ce 2019-03-27 stsp }
1050 7154f6ce 2019-03-27 stsp
1051 7154f6ce 2019-03-27 stsp static const struct got_error *
1052 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1053 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1054 b8f41171 2019-02-10 stsp struct got_repository *repo)
1055 6353ad76 2019-02-08 stsp {
1056 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1057 6353ad76 2019-02-08 stsp struct got_object_id id;
1058 6353ad76 2019-02-08 stsp size_t hdrlen;
1059 6353ad76 2019-02-08 stsp FILE *f = NULL;
1060 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1061 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1062 6353ad76 2019-02-08 stsp size_t flen, blen;
1063 6353ad76 2019-02-08 stsp
1064 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1065 6353ad76 2019-02-08 stsp
1066 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1067 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1068 b8f41171 2019-02-10 stsp if (ie) {
1069 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1070 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1071 2ec1f75b 2019-03-26 stsp else
1072 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1073 b8f41171 2019-02-10 stsp sb->st_mode =
1074 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1075 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1076 b8f41171 2019-02-10 stsp } else
1077 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1078 a378724f 2019-02-10 stsp return NULL;
1079 a378724f 2019-02-10 stsp }
1080 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1081 a378724f 2019-02-10 stsp }
1082 6353ad76 2019-02-08 stsp
1083 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1084 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1085 6353ad76 2019-02-08 stsp return NULL;
1086 6353ad76 2019-02-08 stsp }
1087 6353ad76 2019-02-08 stsp
1088 b8f41171 2019-02-10 stsp if (ie == NULL)
1089 d00136be 2019-03-26 stsp return NULL;
1090 d00136be 2019-03-26 stsp
1091 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1092 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1093 2ec1f75b 2019-03-26 stsp return NULL;
1094 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1095 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1096 b8f41171 2019-02-10 stsp return NULL;
1097 d00136be 2019-03-26 stsp }
1098 b8f41171 2019-02-10 stsp
1099 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1100 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1101 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1102 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1103 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1104 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1105 6353ad76 2019-02-08 stsp return NULL;
1106 6353ad76 2019-02-08 stsp
1107 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1108 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1109 6353ad76 2019-02-08 stsp if (err)
1110 6353ad76 2019-02-08 stsp return err;
1111 6353ad76 2019-02-08 stsp
1112 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1113 6353ad76 2019-02-08 stsp if (f == NULL) {
1114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1115 6353ad76 2019-02-08 stsp goto done;
1116 6353ad76 2019-02-08 stsp }
1117 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1118 656b1f76 2019-05-11 jcs for (;;) {
1119 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1120 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1121 6353ad76 2019-02-08 stsp if (err)
1122 7154f6ce 2019-03-27 stsp goto done;
1123 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1124 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1125 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1127 7154f6ce 2019-03-27 stsp goto done;
1128 80c5c120 2019-02-19 stsp }
1129 6353ad76 2019-02-08 stsp if (blen == 0) {
1130 6353ad76 2019-02-08 stsp if (flen != 0)
1131 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1132 6353ad76 2019-02-08 stsp break;
1133 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1134 6353ad76 2019-02-08 stsp if (blen != 0)
1135 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1136 6353ad76 2019-02-08 stsp break;
1137 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1138 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1139 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1140 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1141 6353ad76 2019-02-08 stsp break;
1142 6353ad76 2019-02-08 stsp }
1143 6353ad76 2019-02-08 stsp } else {
1144 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1145 6353ad76 2019-02-08 stsp break;
1146 6353ad76 2019-02-08 stsp }
1147 6353ad76 2019-02-08 stsp hdrlen = 0;
1148 6353ad76 2019-02-08 stsp }
1149 7154f6ce 2019-03-27 stsp
1150 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1151 7154f6ce 2019-03-27 stsp rewind(f);
1152 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1153 7154f6ce 2019-03-27 stsp }
1154 6353ad76 2019-02-08 stsp done:
1155 6353ad76 2019-02-08 stsp if (blob)
1156 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1157 6353ad76 2019-02-08 stsp if (f)
1158 6353ad76 2019-02-08 stsp fclose(f);
1159 9d31a1d8 2018-03-11 stsp return err;
1160 9d31a1d8 2018-03-11 stsp }
1161 9d31a1d8 2018-03-11 stsp
1162 9d31a1d8 2018-03-11 stsp static const struct got_error *
1163 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1164 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1165 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1166 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1167 0584f854 2019-04-06 stsp void *progress_arg)
1168 9d31a1d8 2018-03-11 stsp {
1169 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1170 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1171 6353ad76 2019-02-08 stsp char *ondisk_path;
1172 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1173 b8f41171 2019-02-10 stsp struct stat sb;
1174 9d31a1d8 2018-03-11 stsp
1175 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1176 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1177 6353ad76 2019-02-08 stsp
1178 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1179 b8f41171 2019-02-10 stsp if (err)
1180 b8f41171 2019-02-10 stsp goto done;
1181 6353ad76 2019-02-08 stsp
1182 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1183 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1184 b8f41171 2019-02-10 stsp goto done;
1185 b8f41171 2019-02-10 stsp }
1186 b8f41171 2019-02-10 stsp
1187 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1188 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1189 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1190 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1191 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1192 b8f41171 2019-02-10 stsp path);
1193 6353ad76 2019-02-08 stsp goto done;
1194 a378724f 2019-02-10 stsp }
1195 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1196 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1197 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1198 b8f41171 2019-02-10 stsp goto done;
1199 9d31a1d8 2018-03-11 stsp }
1200 9d31a1d8 2018-03-11 stsp
1201 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 8da9e5f4 2019-01-12 stsp if (err)
1203 6353ad76 2019-02-08 stsp goto done;
1204 512f0d0e 2019-01-02 stsp
1205 1430b4e0 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1206 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1207 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1208 b8f41171 2019-02-10 stsp progress_arg);
1209 13d9040b 2019-03-27 stsp else if (status == GOT_STATUS_DELETE) {
1210 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1211 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1212 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1213 13d9040b 2019-03-27 stsp if (err)
1214 13d9040b 2019-03-27 stsp goto done;
1215 13d9040b 2019-03-27 stsp } else {
1216 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1217 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1218 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1219 13d9040b 2019-03-27 stsp if (err)
1220 13d9040b 2019-03-27 stsp goto done;
1221 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1222 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1223 13d9040b 2019-03-27 stsp if (err)
1224 13d9040b 2019-03-27 stsp goto done;
1225 13d9040b 2019-03-27 stsp }
1226 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1227 6353ad76 2019-02-08 stsp done:
1228 6353ad76 2019-02-08 stsp free(ondisk_path);
1229 8da9e5f4 2019-01-12 stsp return err;
1230 90285c3b 2019-01-08 stsp }
1231 90285c3b 2019-01-08 stsp
1232 90285c3b 2019-01-08 stsp static const struct got_error *
1233 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1234 90285c3b 2019-01-08 stsp {
1235 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1236 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1237 90285c3b 2019-01-08 stsp
1238 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1239 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1240 90285c3b 2019-01-08 stsp
1241 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1242 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1243 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1244 c97665ae 2019-01-13 stsp } else {
1245 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1246 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1247 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1248 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1249 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1250 230a42bd 2019-05-11 jcs parent);
1251 90285c3b 2019-01-08 stsp break;
1252 90285c3b 2019-01-08 stsp }
1253 90285c3b 2019-01-08 stsp parent = dirname(parent);
1254 90285c3b 2019-01-08 stsp }
1255 90285c3b 2019-01-08 stsp }
1256 90285c3b 2019-01-08 stsp free(ondisk_path);
1257 90285c3b 2019-01-08 stsp return err;
1258 512f0d0e 2019-01-02 stsp }
1259 512f0d0e 2019-01-02 stsp
1260 708d8e67 2019-03-27 stsp static const struct got_error *
1261 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1262 708d8e67 2019-03-27 stsp struct got_fileindex_entry *ie, const char *parent_path,
1263 708d8e67 2019-03-27 stsp struct got_repository *repo,
1264 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1265 708d8e67 2019-03-27 stsp {
1266 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1267 708d8e67 2019-03-27 stsp unsigned char status;
1268 708d8e67 2019-03-27 stsp struct stat sb;
1269 708d8e67 2019-03-27 stsp char *ondisk_path;
1270 708d8e67 2019-03-27 stsp
1271 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1272 708d8e67 2019-03-27 stsp == -1)
1273 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1274 708d8e67 2019-03-27 stsp
1275 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1276 708d8e67 2019-03-27 stsp if (err)
1277 708d8e67 2019-03-27 stsp return err;
1278 708d8e67 2019-03-27 stsp
1279 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1280 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1281 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1282 fc6346c4 2019-03-27 stsp /*
1283 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1284 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1285 fc6346c4 2019-03-27 stsp */
1286 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1287 fc6346c4 2019-03-27 stsp 0);
1288 708d8e67 2019-03-27 stsp if (err)
1289 708d8e67 2019-03-27 stsp return err;
1290 fc6346c4 2019-03-27 stsp } else {
1291 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1292 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1293 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1294 fc6346c4 2019-03-27 stsp if (err)
1295 fc6346c4 2019-03-27 stsp return err;
1296 fc6346c4 2019-03-27 stsp }
1297 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1298 708d8e67 2019-03-27 stsp }
1299 708d8e67 2019-03-27 stsp
1300 fc6346c4 2019-03-27 stsp return err;
1301 708d8e67 2019-03-27 stsp }
1302 708d8e67 2019-03-27 stsp
1303 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1304 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1305 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1306 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1307 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1308 8da9e5f4 2019-01-12 stsp void *progress_arg;
1309 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1310 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1311 8da9e5f4 2019-01-12 stsp };
1312 8da9e5f4 2019-01-12 stsp
1313 512f0d0e 2019-01-02 stsp static const struct got_error *
1314 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1316 512f0d0e 2019-01-02 stsp {
1317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1318 0584f854 2019-04-06 stsp
1319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1321 512f0d0e 2019-01-02 stsp
1322 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1323 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1324 8da9e5f4 2019-01-12 stsp }
1325 512f0d0e 2019-01-02 stsp
1326 8da9e5f4 2019-01-12 stsp static const struct got_error *
1327 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1328 8da9e5f4 2019-01-12 stsp {
1329 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1330 7a9df742 2019-01-08 stsp
1331 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1333 0584f854 2019-04-06 stsp
1334 708d8e67 2019-03-27 stsp return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1335 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1336 9d31a1d8 2018-03-11 stsp }
1337 9d31a1d8 2018-03-11 stsp
1338 9d31a1d8 2018-03-11 stsp static const struct got_error *
1339 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1340 9d31a1d8 2018-03-11 stsp {
1341 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1342 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1343 8da9e5f4 2019-01-12 stsp char *path;
1344 9d31a1d8 2018-03-11 stsp
1345 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1347 0584f854 2019-04-06 stsp
1348 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1349 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1350 8da9e5f4 2019-01-12 stsp == -1)
1351 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1352 9d31a1d8 2018-03-11 stsp
1353 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1354 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1355 8da9e5f4 2019-01-12 stsp else
1356 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1358 9d31a1d8 2018-03-11 stsp
1359 8da9e5f4 2019-01-12 stsp free(path);
1360 0cd1c46a 2019-03-11 stsp return err;
1361 0cd1c46a 2019-03-11 stsp }
1362 0cd1c46a 2019-03-11 stsp
1363 517bab73 2019-03-11 stsp const struct got_error *
1364 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1365 0cd1c46a 2019-03-11 stsp {
1366 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1367 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1368 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1369 0cd1c46a 2019-03-11 stsp
1370 517bab73 2019-03-11 stsp *refname = NULL;
1371 517bab73 2019-03-11 stsp
1372 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1374 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1375 0cd1c46a 2019-03-11 stsp
1376 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1377 0647c563 2019-03-11 stsp == -1) {
1378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1379 0647c563 2019-03-11 stsp *refname = NULL;
1380 0cd1c46a 2019-03-11 stsp }
1381 517bab73 2019-03-11 stsp free(uuidstr);
1382 517bab73 2019-03-11 stsp return err;
1383 517bab73 2019-03-11 stsp }
1384 517bab73 2019-03-11 stsp
1385 517bab73 2019-03-11 stsp /*
1386 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1387 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1388 517bab73 2019-03-11 stsp */
1389 517bab73 2019-03-11 stsp static const struct got_error *
1390 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1391 517bab73 2019-03-11 stsp {
1392 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1393 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1394 517bab73 2019-03-11 stsp char *refname;
1395 517bab73 2019-03-11 stsp
1396 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1397 517bab73 2019-03-11 stsp if (err)
1398 517bab73 2019-03-11 stsp return err;
1399 0cd1c46a 2019-03-11 stsp
1400 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1401 0cd1c46a 2019-03-11 stsp if (err)
1402 0cd1c46a 2019-03-11 stsp goto done;
1403 0cd1c46a 2019-03-11 stsp
1404 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1405 0cd1c46a 2019-03-11 stsp done:
1406 0cd1c46a 2019-03-11 stsp free(refname);
1407 0cd1c46a 2019-03-11 stsp if (ref)
1408 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1409 9d31a1d8 2018-03-11 stsp return err;
1410 9d31a1d8 2018-03-11 stsp }
1411 9d31a1d8 2018-03-11 stsp
1412 ebf99748 2019-05-09 stsp static const struct got_error *
1413 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1414 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1415 ebf99748 2019-05-09 stsp {
1416 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1417 ebf99748 2019-05-09 stsp FILE *index = NULL;
1418 0cd1c46a 2019-03-11 stsp
1419 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1420 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1421 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1422 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1423 ebf99748 2019-05-09 stsp
1424 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1425 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1427 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1428 ebf99748 2019-05-09 stsp goto done;
1429 ebf99748 2019-05-09 stsp }
1430 ebf99748 2019-05-09 stsp
1431 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1432 ebf99748 2019-05-09 stsp if (index == NULL) {
1433 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1434 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1435 ebf99748 2019-05-09 stsp } else {
1436 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1437 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1439 ebf99748 2019-05-09 stsp }
1440 ebf99748 2019-05-09 stsp done:
1441 ebf99748 2019-05-09 stsp if (err) {
1442 ebf99748 2019-05-09 stsp free(*fileindex_path);
1443 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1444 ebf99748 2019-05-09 stsp free(*fileindex);
1445 ebf99748 2019-05-09 stsp *fileindex = NULL;
1446 ebf99748 2019-05-09 stsp }
1447 ebf99748 2019-05-09 stsp return err;
1448 ebf99748 2019-05-09 stsp }
1449 ebf99748 2019-05-09 stsp
1450 9d31a1d8 2018-03-11 stsp const struct got_error *
1451 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1452 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1453 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1454 9d31a1d8 2018-03-11 stsp {
1455 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1456 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1457 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1458 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1459 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1460 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1461 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
1462 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1463 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1464 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1465 9d31a1d8 2018-03-11 stsp
1466 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1467 9d31a1d8 2018-03-11 stsp if (err)
1468 9d31a1d8 2018-03-11 stsp return err;
1469 93a30277 2018-12-24 stsp
1470 51514078 2018-12-25 stsp /*
1471 51514078 2018-12-25 stsp * Read the file index.
1472 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1473 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1474 51514078 2018-12-25 stsp */
1475 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1476 ebf99748 2019-05-09 stsp if (err)
1477 ebf99748 2019-05-09 stsp goto done;
1478 51514078 2018-12-25 stsp
1479 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1480 b8bdcc21 2018-12-24 stsp fileindex_path);
1481 51664889 2018-03-12 stsp if (err)
1482 51664889 2018-03-12 stsp goto done;
1483 51664889 2018-03-12 stsp
1484 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1485 0cd1c46a 2019-03-11 stsp if (err)
1486 0cd1c46a 2019-03-11 stsp goto done;
1487 0cd1c46a 2019-03-11 stsp
1488 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1489 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1490 9d31a1d8 2018-03-11 stsp if (err)
1491 9d31a1d8 2018-03-11 stsp goto done;
1492 9d31a1d8 2018-03-11 stsp
1493 c4cdcb68 2019-04-03 stsp if (path[0]) {
1494 c4cdcb68 2019-04-03 stsp char *tree_path;
1495 c4cdcb68 2019-04-03 stsp int obj_type;
1496 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1497 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1499 c4cdcb68 2019-04-03 stsp goto done;
1500 c4cdcb68 2019-04-03 stsp }
1501 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1502 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1503 c4cdcb68 2019-04-03 stsp path) == -1) {
1504 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1505 c4cdcb68 2019-04-03 stsp goto done;
1506 c4cdcb68 2019-04-03 stsp }
1507 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1508 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1509 c4cdcb68 2019-04-03 stsp free(tree_path);
1510 c4cdcb68 2019-04-03 stsp if (err)
1511 c4cdcb68 2019-04-03 stsp goto done;
1512 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1513 c4cdcb68 2019-04-03 stsp if (err)
1514 c4cdcb68 2019-04-03 stsp goto done;
1515 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1516 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1517 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1518 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1519 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1521 c4cdcb68 2019-04-03 stsp goto done;
1522 c4cdcb68 2019-04-03 stsp }
1523 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1524 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1525 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1526 c4cdcb68 2019-04-03 stsp goto done;
1527 c4cdcb68 2019-04-03 stsp }
1528 c4cdcb68 2019-04-03 stsp } else {
1529 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1530 c4cdcb68 2019-04-03 stsp if (err)
1531 c4cdcb68 2019-04-03 stsp goto done;
1532 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1533 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1534 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1535 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1536 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1537 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1538 c4cdcb68 2019-04-03 stsp goto done;
1539 c4cdcb68 2019-04-03 stsp }
1540 c4cdcb68 2019-04-03 stsp }
1541 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1542 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1543 c4cdcb68 2019-04-03 stsp free(tree_path);
1544 c4cdcb68 2019-04-03 stsp if (err)
1545 c4cdcb68 2019-04-03 stsp goto done;
1546 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1547 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1548 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1549 c4cdcb68 2019-04-03 stsp goto done;
1550 c4cdcb68 2019-04-03 stsp }
1551 c4cdcb68 2019-04-03 stsp }
1552 c4cdcb68 2019-04-03 stsp } else {
1553 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1554 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1555 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1556 c4cdcb68 2019-04-03 stsp goto done;
1557 c4cdcb68 2019-04-03 stsp }
1558 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1559 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1560 c4cdcb68 2019-04-03 stsp if (err)
1561 c4cdcb68 2019-04-03 stsp goto done;
1562 c4cdcb68 2019-04-03 stsp }
1563 9d31a1d8 2018-03-11 stsp
1564 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1565 8da9e5f4 2019-01-12 stsp if (err)
1566 c4cdcb68 2019-04-03 stsp goto done;
1567 c4cdcb68 2019-04-03 stsp
1568 c4cdcb68 2019-04-03 stsp if (entry_name &&
1569 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1570 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1571 8da9e5f4 2019-01-12 stsp goto done;
1572 c4cdcb68 2019-04-03 stsp }
1573 9d31a1d8 2018-03-11 stsp
1574 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1575 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1576 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1577 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1578 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1579 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1580 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1581 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1582 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1583 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1584 c4cdcb68 2019-04-03 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1585 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1586 8da9e5f4 2019-01-12 stsp
1587 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1588 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1589 9d31a1d8 2018-03-11 stsp if (err)
1590 9d31a1d8 2018-03-11 stsp goto done;
1591 9d31a1d8 2018-03-11 stsp
1592 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1593 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1594 230a42bd 2019-05-11 jcs fileindex_path);
1595 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1596 9d31a1d8 2018-03-11 stsp goto done;
1597 9d31a1d8 2018-03-11 stsp }
1598 9d31a1d8 2018-03-11 stsp
1599 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1600 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1601 9d31a1d8 2018-03-11 stsp
1602 9d31a1d8 2018-03-11 stsp done:
1603 c4cdcb68 2019-04-03 stsp free(relpath);
1604 edfa7d7f 2018-09-11 stsp if (tree)
1605 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1606 9d31a1d8 2018-03-11 stsp if (commit)
1607 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1608 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1609 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1610 b8bdcc21 2018-12-24 stsp if (new_index)
1611 b8bdcc21 2018-12-24 stsp fclose(new_index);
1612 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1613 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1614 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1615 a143fb78 2018-12-25 stsp if (checkout_err)
1616 a143fb78 2018-12-25 stsp err = checkout_err;
1617 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1618 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1619 9d31a1d8 2018-03-11 stsp err = unlockerr;
1620 f8d1f275 2019-02-04 stsp return err;
1621 f8d1f275 2019-02-04 stsp }
1622 f8d1f275 2019-02-04 stsp
1623 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1624 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1625 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1626 927df6b7 2019-02-10 stsp const char *status_path;
1627 927df6b7 2019-02-10 stsp size_t status_path_len;
1628 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1629 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1630 f8d1f275 2019-02-04 stsp void *status_arg;
1631 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1632 f8d1f275 2019-02-04 stsp void *cancel_arg;
1633 f8d1f275 2019-02-04 stsp };
1634 f8d1f275 2019-02-04 stsp
1635 f8d1f275 2019-02-04 stsp static const struct got_error *
1636 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1637 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1638 927df6b7 2019-02-10 stsp struct got_repository *repo)
1639 927df6b7 2019-02-10 stsp {
1640 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1641 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1642 927df6b7 2019-02-10 stsp struct stat sb;
1643 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1644 927df6b7 2019-02-10 stsp
1645 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1646 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1647 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1648 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1649 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1650 016a88dd 2019-05-13 stsp &commit_id);
1651 927df6b7 2019-02-10 stsp }
1652 927df6b7 2019-02-10 stsp return err;
1653 927df6b7 2019-02-10 stsp }
1654 927df6b7 2019-02-10 stsp
1655 927df6b7 2019-02-10 stsp static const struct got_error *
1656 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1657 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1658 f8d1f275 2019-02-04 stsp {
1659 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1660 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1661 f8d1f275 2019-02-04 stsp char *abspath;
1662 f8d1f275 2019-02-04 stsp
1663 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1664 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1665 0584f854 2019-04-06 stsp
1666 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1667 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1668 927df6b7 2019-02-10 stsp return NULL;
1669 927df6b7 2019-02-10 stsp
1670 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1671 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1672 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1674 f8d1f275 2019-02-04 stsp } else {
1675 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1676 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1677 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1678 f8d1f275 2019-02-04 stsp }
1679 f8d1f275 2019-02-04 stsp
1680 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1681 927df6b7 2019-02-10 stsp a->repo);
1682 f8d1f275 2019-02-04 stsp free(abspath);
1683 9d31a1d8 2018-03-11 stsp return err;
1684 9d31a1d8 2018-03-11 stsp }
1685 f8d1f275 2019-02-04 stsp
1686 f8d1f275 2019-02-04 stsp static const struct got_error *
1687 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1688 f8d1f275 2019-02-04 stsp {
1689 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1690 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1691 2ec1f75b 2019-03-26 stsp unsigned char status;
1692 927df6b7 2019-02-10 stsp
1693 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1694 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1695 0584f854 2019-04-06 stsp
1696 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1697 927df6b7 2019-02-10 stsp return NULL;
1698 927df6b7 2019-02-10 stsp
1699 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1700 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1701 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1702 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1703 2ec1f75b 2019-03-26 stsp else
1704 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1705 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1706 016a88dd 2019-05-13 stsp &commit_id);
1707 f8d1f275 2019-02-04 stsp }
1708 f8d1f275 2019-02-04 stsp
1709 f8d1f275 2019-02-04 stsp static const struct got_error *
1710 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1711 f8d1f275 2019-02-04 stsp {
1712 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1713 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1714 f8d1f275 2019-02-04 stsp char *path = NULL;
1715 f8d1f275 2019-02-04 stsp
1716 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1717 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1718 0584f854 2019-04-06 stsp
1719 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1720 2c201a36 2019-02-10 stsp return NULL;
1721 2c201a36 2019-02-10 stsp
1722 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1723 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1724 f8d1f275 2019-02-04 stsp return NULL;
1725 f8d1f275 2019-02-04 stsp
1726 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1727 927df6b7 2019-02-10 stsp return NULL;
1728 927df6b7 2019-02-10 stsp
1729 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1730 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1731 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1732 f8d1f275 2019-02-04 stsp } else {
1733 f8d1f275 2019-02-04 stsp path = de->d_name;
1734 f8d1f275 2019-02-04 stsp }
1735 f8d1f275 2019-02-04 stsp
1736 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1737 016a88dd 2019-05-13 stsp NULL, NULL);
1738 f8d1f275 2019-02-04 stsp if (parent_path[0])
1739 f8d1f275 2019-02-04 stsp free(path);
1740 b72f483a 2019-02-05 stsp return err;
1741 f8d1f275 2019-02-04 stsp }
1742 f8d1f275 2019-02-04 stsp
1743 f8d1f275 2019-02-04 stsp const struct got_error *
1744 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1745 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1746 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1747 f8d1f275 2019-02-04 stsp {
1748 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1749 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1750 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1751 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1752 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1753 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1754 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1755 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1756 f8d1f275 2019-02-04 stsp
1757 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1758 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1759 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
1760 f8d1f275 2019-02-04 stsp goto done;
1761 f8d1f275 2019-02-04 stsp }
1762 f8d1f275 2019-02-04 stsp
1763 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1764 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1765 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1766 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1767 f8d1f275 2019-02-04 stsp goto done;
1768 f8d1f275 2019-02-04 stsp }
1769 f8d1f275 2019-02-04 stsp
1770 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1771 f8d1f275 2019-02-04 stsp if (index == NULL) {
1772 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1773 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
1774 f8d1f275 2019-02-04 stsp goto done;
1775 f8d1f275 2019-02-04 stsp }
1776 f8d1f275 2019-02-04 stsp } else {
1777 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1778 f8d1f275 2019-02-04 stsp fclose(index);
1779 f8d1f275 2019-02-04 stsp if (err)
1780 f8d1f275 2019-02-04 stsp goto done;
1781 f8d1f275 2019-02-04 stsp }
1782 f8d1f275 2019-02-04 stsp
1783 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1784 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1786 c513d110 2019-02-05 stsp goto done;
1787 c513d110 2019-02-05 stsp }
1788 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1789 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1790 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1791 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1792 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1793 927df6b7 2019-02-10 stsp if (ie == NULL) {
1794 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1795 927df6b7 2019-02-10 stsp goto done;
1796 927df6b7 2019-02-10 stsp }
1797 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1798 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1799 927df6b7 2019-02-10 stsp goto done;
1800 927df6b7 2019-02-10 stsp } else {
1801 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
1802 927df6b7 2019-02-10 stsp goto done;
1803 927df6b7 2019-02-10 stsp }
1804 927df6b7 2019-02-10 stsp }
1805 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1806 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1807 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1808 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1809 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1810 927df6b7 2019-02-10 stsp arg.status_path = path;
1811 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1812 f8d1f275 2019-02-04 stsp arg.repo = repo;
1813 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1814 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1815 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1816 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1817 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1818 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1819 f8d1f275 2019-02-04 stsp done:
1820 f8d1f275 2019-02-04 stsp if (workdir)
1821 f8d1f275 2019-02-04 stsp closedir(workdir);
1822 927df6b7 2019-02-10 stsp free(ondisk_path);
1823 f8d1f275 2019-02-04 stsp free(fileindex_path);
1824 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1825 f8d1f275 2019-02-04 stsp return err;
1826 f8d1f275 2019-02-04 stsp }
1827 6c7ab921 2019-03-18 stsp
1828 6c7ab921 2019-03-18 stsp const struct got_error *
1829 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1830 6c7ab921 2019-03-18 stsp const char *arg)
1831 6c7ab921 2019-03-18 stsp {
1832 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1833 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1834 6c7ab921 2019-03-18 stsp size_t len;
1835 6c7ab921 2019-03-18 stsp
1836 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1837 6c7ab921 2019-03-18 stsp
1838 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1839 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1840 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
1841 6c7ab921 2019-03-18 stsp
1842 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1843 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1844 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1845 6c7ab921 2019-03-18 stsp goto done;
1846 6c7ab921 2019-03-18 stsp }
1847 6c7ab921 2019-03-18 stsp
1848 c4cdcb68 2019-04-03 stsp path = strdup(resolved +
1849 c4cdcb68 2019-04-03 stsp strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1850 6c7ab921 2019-03-18 stsp if (path == NULL) {
1851 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1852 6c7ab921 2019-03-18 stsp goto done;
1853 6c7ab921 2019-03-18 stsp }
1854 6c7ab921 2019-03-18 stsp
1855 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1856 6c7ab921 2019-03-18 stsp len = strlen(path);
1857 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1858 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1859 6c7ab921 2019-03-18 stsp len--;
1860 6c7ab921 2019-03-18 stsp }
1861 6c7ab921 2019-03-18 stsp done:
1862 6c7ab921 2019-03-18 stsp free(resolved);
1863 6c7ab921 2019-03-18 stsp if (err == NULL)
1864 6c7ab921 2019-03-18 stsp *wt_path = path;
1865 6c7ab921 2019-03-18 stsp else
1866 6c7ab921 2019-03-18 stsp free(path);
1867 d00136be 2019-03-26 stsp return err;
1868 d00136be 2019-03-26 stsp }
1869 d00136be 2019-03-26 stsp
1870 d00136be 2019-03-26 stsp const struct got_error *
1871 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1872 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
1873 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
1874 031a5338 2019-03-26 stsp struct got_repository *repo)
1875 d00136be 2019-03-26 stsp {
1876 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1877 1dd54920 2019-05-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1878 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1879 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1880 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
1881 d00136be 2019-03-26 stsp
1882 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1883 d00136be 2019-03-26 stsp if (err)
1884 d00136be 2019-03-26 stsp return err;
1885 d00136be 2019-03-26 stsp
1886 d00136be 2019-03-26 stsp
1887 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1888 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1889 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
1890 d00136be 2019-03-26 stsp goto done;
1891 d00136be 2019-03-26 stsp }
1892 d00136be 2019-03-26 stsp
1893 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1894 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1895 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1896 d00136be 2019-03-26 stsp fileindex_path = NULL;
1897 d00136be 2019-03-26 stsp goto done;
1898 d00136be 2019-03-26 stsp }
1899 d00136be 2019-03-26 stsp
1900 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1901 d00136be 2019-03-26 stsp if (index == NULL) {
1902 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
1903 d00136be 2019-03-26 stsp goto done;
1904 d00136be 2019-03-26 stsp }
1905 d00136be 2019-03-26 stsp
1906 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1907 d00136be 2019-03-26 stsp if (err)
1908 d00136be 2019-03-26 stsp goto done;
1909 d00136be 2019-03-26 stsp
1910 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
1911 1dd54920 2019-05-11 stsp struct got_fileindex_entry *ie = NULL;
1912 1dd54920 2019-05-11 stsp char *relpath;
1913 5c99ca9f 2019-03-27 stsp
1914 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
1915 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
1916 1dd54920 2019-05-11 stsp if (err)
1917 1dd54920 2019-05-11 stsp goto done;
1918 5c99ca9f 2019-03-27 stsp
1919 1dd54920 2019-05-11 stsp /* Re-adding an existing entry is a no-op. */
1920 1dd54920 2019-05-11 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1921 1dd54920 2019-05-11 stsp continue;
1922 1dd54920 2019-05-11 stsp
1923 1dd54920 2019-05-11 stsp err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1924 1dd54920 2019-05-11 stsp NULL, NULL);
1925 1dd54920 2019-05-11 stsp free(relpath);
1926 1dd54920 2019-05-11 stsp if (err)
1927 1dd54920 2019-05-11 stsp goto done;
1928 1dd54920 2019-05-11 stsp
1929 1dd54920 2019-05-11 stsp err = got_fileindex_entry_add(fileindex, ie);
1930 1dd54920 2019-05-11 stsp if (err) {
1931 1dd54920 2019-05-11 stsp got_fileindex_entry_free(ie);
1932 1dd54920 2019-05-11 stsp goto done;
1933 1dd54920 2019-05-11 stsp }
1934 d00136be 2019-03-26 stsp
1935 1dd54920 2019-05-11 stsp err = report_file_status(ie, pe->path, status_cb, status_arg,
1936 1dd54920 2019-05-11 stsp repo);
1937 1dd54920 2019-05-11 stsp if (err)
1938 1dd54920 2019-05-11 stsp goto done;
1939 1dd54920 2019-05-11 stsp }
1940 1dd54920 2019-05-11 stsp
1941 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1942 d00136be 2019-03-26 stsp fileindex_path);
1943 d00136be 2019-03-26 stsp if (err)
1944 d00136be 2019-03-26 stsp goto done;
1945 d00136be 2019-03-26 stsp
1946 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1947 d00136be 2019-03-26 stsp if (err)
1948 d00136be 2019-03-26 stsp goto done;
1949 d00136be 2019-03-26 stsp
1950 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1952 230a42bd 2019-05-11 jcs fileindex_path);
1953 d00136be 2019-03-26 stsp goto done;
1954 d00136be 2019-03-26 stsp }
1955 d00136be 2019-03-26 stsp
1956 d00136be 2019-03-26 stsp free(new_fileindex_path);
1957 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
1958 031a5338 2019-03-26 stsp
1959 d00136be 2019-03-26 stsp done:
1960 d00136be 2019-03-26 stsp if (index) {
1961 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1962 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1963 d00136be 2019-03-26 stsp }
1964 d00136be 2019-03-26 stsp if (new_fileindex_path) {
1965 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1966 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
1967 230a42bd 2019-05-11 jcs new_fileindex_path);
1968 d00136be 2019-03-26 stsp free(new_fileindex_path);
1969 d00136be 2019-03-26 stsp }
1970 d00136be 2019-03-26 stsp if (fileindex)
1971 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
1972 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1973 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
1974 d00136be 2019-03-26 stsp err = unlockerr;
1975 6c7ab921 2019-03-18 stsp return err;
1976 6c7ab921 2019-03-18 stsp }
1977 2ec1f75b 2019-03-26 stsp
1978 2ec1f75b 2019-03-26 stsp const struct got_error *
1979 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
1980 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
1981 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
1982 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
1983 2ec1f75b 2019-03-26 stsp {
1984 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1985 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1986 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1987 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1988 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1989 2ec1f75b 2019-03-26 stsp unsigned char status;
1990 2ec1f75b 2019-03-26 stsp struct stat sb;
1991 2ec1f75b 2019-03-26 stsp
1992 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1993 2ec1f75b 2019-03-26 stsp if (err)
1994 2ec1f75b 2019-03-26 stsp return err;
1995 2ec1f75b 2019-03-26 stsp
1996 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1997 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1998 2ec1f75b 2019-03-26 stsp if (err)
1999 2ec1f75b 2019-03-26 stsp goto done;
2000 2ec1f75b 2019-03-26 stsp
2001 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2002 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2003 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2004 2ec1f75b 2019-03-26 stsp goto done;
2005 2ec1f75b 2019-03-26 stsp }
2006 2ec1f75b 2019-03-26 stsp
2007 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2008 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2009 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2010 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2011 2ec1f75b 2019-03-26 stsp goto done;
2012 2ec1f75b 2019-03-26 stsp }
2013 2ec1f75b 2019-03-26 stsp
2014 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2015 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2016 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2017 2ec1f75b 2019-03-26 stsp goto done;
2018 2ec1f75b 2019-03-26 stsp }
2019 2ec1f75b 2019-03-26 stsp
2020 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2021 2ec1f75b 2019-03-26 stsp if (err)
2022 2ec1f75b 2019-03-26 stsp goto done;
2023 2ec1f75b 2019-03-26 stsp
2024 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2025 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
2026 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
2027 2ec1f75b 2019-03-26 stsp goto done;
2028 2ec1f75b 2019-03-26 stsp }
2029 2ec1f75b 2019-03-26 stsp
2030 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2031 2ec1f75b 2019-03-26 stsp if (err)
2032 2ec1f75b 2019-03-26 stsp goto done;
2033 2ec1f75b 2019-03-26 stsp
2034 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
2035 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
2036 2af4a041 2019-05-11 jcs err = got_error_set_errno(ENOENT, ondisk_path);
2037 71a29355 2019-03-27 stsp goto done;
2038 71a29355 2019-03-27 stsp }
2039 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
2040 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
2041 2ec1f75b 2019-03-26 stsp goto done;
2042 2ec1f75b 2019-03-26 stsp }
2043 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
2044 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
2045 2ec1f75b 2019-03-26 stsp goto done;
2046 2ec1f75b 2019-03-26 stsp }
2047 2ec1f75b 2019-03-26 stsp }
2048 2ec1f75b 2019-03-26 stsp
2049 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
2050 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2051 2ec1f75b 2019-03-26 stsp goto done;
2052 2ec1f75b 2019-03-26 stsp }
2053 2ec1f75b 2019-03-26 stsp
2054 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2055 2ec1f75b 2019-03-26 stsp
2056 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2057 2ec1f75b 2019-03-26 stsp fileindex_path);
2058 2ec1f75b 2019-03-26 stsp if (err)
2059 2ec1f75b 2019-03-26 stsp goto done;
2060 2ec1f75b 2019-03-26 stsp
2061 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
2062 2ec1f75b 2019-03-26 stsp if (err)
2063 2ec1f75b 2019-03-26 stsp goto done;
2064 2ec1f75b 2019-03-26 stsp
2065 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2066 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2067 230a42bd 2019-05-11 jcs fileindex_path);
2068 2ec1f75b 2019-03-26 stsp goto done;
2069 2ec1f75b 2019-03-26 stsp }
2070 2ec1f75b 2019-03-26 stsp
2071 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2072 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
2073 2ec1f75b 2019-03-26 stsp
2074 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2075 2ec1f75b 2019-03-26 stsp done:
2076 2ec1f75b 2019-03-26 stsp free(relpath);
2077 2ec1f75b 2019-03-26 stsp if (index) {
2078 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2079 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2080 2ec1f75b 2019-03-26 stsp }
2081 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
2082 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2083 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
2084 230a42bd 2019-05-11 jcs new_fileindex_path);
2085 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2086 2ec1f75b 2019-03-26 stsp }
2087 2ec1f75b 2019-03-26 stsp if (fileindex)
2088 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2089 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2090 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2091 2ec1f75b 2019-03-26 stsp err = unlockerr;
2092 2ec1f75b 2019-03-26 stsp return err;
2093 2ec1f75b 2019-03-26 stsp }
2094 a129376b 2019-03-28 stsp
2095 a129376b 2019-03-28 stsp const struct got_error *
2096 a129376b 2019-03-28 stsp got_worktree_revert(struct got_worktree *worktree,
2097 a129376b 2019-03-28 stsp const char *ondisk_path,
2098 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2099 a129376b 2019-03-28 stsp struct got_repository *repo)
2100 a129376b 2019-03-28 stsp {
2101 a129376b 2019-03-28 stsp struct got_fileindex *fileindex = NULL;
2102 a129376b 2019-03-28 stsp struct got_fileindex_entry *ie = NULL;
2103 a129376b 2019-03-28 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2104 a129376b 2019-03-28 stsp char *tree_path = NULL, *parent_path, *te_name;
2105 a129376b 2019-03-28 stsp FILE *index = NULL, *new_index = NULL;
2106 a129376b 2019-03-28 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2107 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2108 a129376b 2019-03-28 stsp struct got_object_id id, *tree_id = NULL;
2109 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2110 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2111 a129376b 2019-03-28 stsp unsigned char status;
2112 a129376b 2019-03-28 stsp struct stat sb;
2113 a129376b 2019-03-28 stsp
2114 a129376b 2019-03-28 stsp err = lock_worktree(worktree, LOCK_EX);
2115 a129376b 2019-03-28 stsp if (err)
2116 a129376b 2019-03-28 stsp return err;
2117 a129376b 2019-03-28 stsp
2118 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2119 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2120 a129376b 2019-03-28 stsp if (err)
2121 a129376b 2019-03-28 stsp goto done;
2122 a129376b 2019-03-28 stsp
2123 a129376b 2019-03-28 stsp fileindex = got_fileindex_alloc();
2124 a129376b 2019-03-28 stsp if (fileindex == NULL) {
2125 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2126 a129376b 2019-03-28 stsp goto done;
2127 a129376b 2019-03-28 stsp }
2128 a129376b 2019-03-28 stsp
2129 a129376b 2019-03-28 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2130 a129376b 2019-03-28 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2132 a129376b 2019-03-28 stsp fileindex_path = NULL;
2133 a129376b 2019-03-28 stsp goto done;
2134 a129376b 2019-03-28 stsp }
2135 a129376b 2019-03-28 stsp
2136 a129376b 2019-03-28 stsp index = fopen(fileindex_path, "rb");
2137 a129376b 2019-03-28 stsp if (index == NULL) {
2138 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2139 a129376b 2019-03-28 stsp goto done;
2140 a129376b 2019-03-28 stsp }
2141 a129376b 2019-03-28 stsp
2142 a129376b 2019-03-28 stsp err = got_fileindex_read(fileindex, index);
2143 a129376b 2019-03-28 stsp if (err)
2144 a129376b 2019-03-28 stsp goto done;
2145 a129376b 2019-03-28 stsp
2146 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2147 a129376b 2019-03-28 stsp if (ie == NULL) {
2148 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2149 a129376b 2019-03-28 stsp goto done;
2150 a129376b 2019-03-28 stsp }
2151 a129376b 2019-03-28 stsp
2152 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2153 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2154 a129376b 2019-03-28 stsp if (err) {
2155 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2156 a129376b 2019-03-28 stsp goto done;
2157 a129376b 2019-03-28 stsp parent_path = "/";
2158 a129376b 2019-03-28 stsp }
2159 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2160 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2161 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2162 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2163 a129376b 2019-03-28 stsp goto done;
2164 a129376b 2019-03-28 stsp }
2165 a129376b 2019-03-28 stsp } else {
2166 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2167 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2168 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2169 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2170 a129376b 2019-03-28 stsp goto done;
2171 a129376b 2019-03-28 stsp }
2172 a129376b 2019-03-28 stsp } else {
2173 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2174 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2175 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2176 a129376b 2019-03-28 stsp goto done;
2177 a129376b 2019-03-28 stsp }
2178 a129376b 2019-03-28 stsp }
2179 a129376b 2019-03-28 stsp }
2180 a129376b 2019-03-28 stsp
2181 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2182 a129376b 2019-03-28 stsp tree_path);
2183 a129376b 2019-03-28 stsp if (err)
2184 a129376b 2019-03-28 stsp goto done;
2185 a129376b 2019-03-28 stsp
2186 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2187 a129376b 2019-03-28 stsp if (err)
2188 a129376b 2019-03-28 stsp goto done;
2189 a129376b 2019-03-28 stsp
2190 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2191 a129376b 2019-03-28 stsp if (te_name == NULL) {
2192 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2193 a129376b 2019-03-28 stsp goto done;
2194 a129376b 2019-03-28 stsp }
2195 a129376b 2019-03-28 stsp
2196 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2197 a129376b 2019-03-28 stsp if (err)
2198 a129376b 2019-03-28 stsp goto done;
2199 a129376b 2019-03-28 stsp
2200 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2201 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2202 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2203 a129376b 2019-03-28 stsp goto done;
2204 a129376b 2019-03-28 stsp }
2205 a129376b 2019-03-28 stsp
2206 a129376b 2019-03-28 stsp switch (status) {
2207 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2208 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2209 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2210 a129376b 2019-03-28 stsp break;
2211 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2212 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2213 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2214 a129376b 2019-03-28 stsp case GOT_STATUS_MISSING:
2215 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2216 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2217 a129376b 2019-03-28 stsp if (err)
2218 a129376b 2019-03-28 stsp goto done;
2219 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2220 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2221 a129376b 2019-03-28 stsp progress_arg);
2222 a129376b 2019-03-28 stsp if (err)
2223 a129376b 2019-03-28 stsp goto done;
2224 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2225 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2226 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2227 a129376b 2019-03-28 stsp if (err)
2228 a129376b 2019-03-28 stsp goto done;
2229 a129376b 2019-03-28 stsp }
2230 a129376b 2019-03-28 stsp break;
2231 a129376b 2019-03-28 stsp default:
2232 a129376b 2019-03-28 stsp goto done;
2233 a129376b 2019-03-28 stsp }
2234 a129376b 2019-03-28 stsp
2235 a129376b 2019-03-28 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2236 a129376b 2019-03-28 stsp fileindex_path);
2237 a129376b 2019-03-28 stsp if (err)
2238 a129376b 2019-03-28 stsp goto done;
2239 a129376b 2019-03-28 stsp
2240 a129376b 2019-03-28 stsp err = got_fileindex_write(fileindex, new_index);
2241 a129376b 2019-03-28 stsp if (err)
2242 a129376b 2019-03-28 stsp goto done;
2243 a129376b 2019-03-28 stsp
2244 a129376b 2019-03-28 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2245 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2246 230a42bd 2019-05-11 jcs fileindex_path);
2247 a129376b 2019-03-28 stsp goto done;
2248 a129376b 2019-03-28 stsp }
2249 a129376b 2019-03-28 stsp
2250 a129376b 2019-03-28 stsp free(new_fileindex_path);
2251 a129376b 2019-03-28 stsp new_fileindex_path = NULL;
2252 a129376b 2019-03-28 stsp done:
2253 a129376b 2019-03-28 stsp free(relpath);
2254 a129376b 2019-03-28 stsp free(tree_path);
2255 a129376b 2019-03-28 stsp if (blob)
2256 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2257 a129376b 2019-03-28 stsp if (tree)
2258 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2259 a129376b 2019-03-28 stsp free(tree_id);
2260 a129376b 2019-03-28 stsp if (index) {
2261 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2262 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2263 a129376b 2019-03-28 stsp }
2264 a129376b 2019-03-28 stsp if (new_fileindex_path) {
2265 a129376b 2019-03-28 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2266 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink",
2267 230a42bd 2019-05-11 jcs new_fileindex_path);
2268 a129376b 2019-03-28 stsp free(new_fileindex_path);
2269 a129376b 2019-03-28 stsp }
2270 a129376b 2019-03-28 stsp if (fileindex)
2271 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2272 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2273 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2274 a129376b 2019-03-28 stsp err = unlockerr;
2275 c4296144 2019-05-09 stsp return err;
2276 c4296144 2019-05-09 stsp }
2277 c4296144 2019-05-09 stsp
2278 cf066bf8 2019-05-09 stsp static void
2279 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2280 cf066bf8 2019-05-09 stsp {
2281 24519714 2019-05-09 stsp free(ct->path);
2282 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2283 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2284 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2285 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2286 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2287 cf066bf8 2019-05-09 stsp free(ct);
2288 cf066bf8 2019-05-09 stsp }
2289 24519714 2019-05-09 stsp
2290 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2291 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2292 24519714 2019-05-09 stsp struct got_repository *repo;
2293 24519714 2019-05-09 stsp struct got_worktree *worktree;
2294 24519714 2019-05-09 stsp };
2295 cf066bf8 2019-05-09 stsp
2296 c4296144 2019-05-09 stsp static const struct got_error *
2297 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2298 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2299 c4296144 2019-05-09 stsp {
2300 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2301 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2302 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2303 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2304 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2305 768aea60 2019-05-09 stsp struct stat sb;
2306 c4296144 2019-05-09 stsp
2307 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2308 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2309 c4296144 2019-05-09 stsp
2310 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2311 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2312 c4296144 2019-05-09 stsp return NULL;
2313 0b5cc0d6 2019-05-09 stsp
2314 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2315 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2316 036813ee 2019-05-09 stsp goto done;
2317 036813ee 2019-05-09 stsp }
2318 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2319 036813ee 2019-05-09 stsp parent_path = strdup("");
2320 69960a46 2019-05-09 stsp if (parent_path == NULL)
2321 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2322 69960a46 2019-05-09 stsp } else {
2323 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2324 69960a46 2019-05-09 stsp if (err)
2325 69960a46 2019-05-09 stsp return err;
2326 69960a46 2019-05-09 stsp }
2327 c4296144 2019-05-09 stsp
2328 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2329 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2330 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2331 c4296144 2019-05-09 stsp goto done;
2332 768aea60 2019-05-09 stsp }
2333 768aea60 2019-05-09 stsp
2334 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2335 768aea60 2019-05-09 stsp relpath) == -1) {
2336 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2337 768aea60 2019-05-09 stsp goto done;
2338 768aea60 2019-05-09 stsp }
2339 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2340 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2341 768aea60 2019-05-09 stsp } else {
2342 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2343 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2344 768aea60 2019-05-09 stsp goto done;
2345 768aea60 2019-05-09 stsp }
2346 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2347 c4296144 2019-05-09 stsp }
2348 c4296144 2019-05-09 stsp
2349 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2350 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2351 44d03001 2019-05-09 stsp relpath) == -1) {
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2353 44d03001 2019-05-09 stsp goto done;
2354 44d03001 2019-05-09 stsp }
2355 44d03001 2019-05-09 stsp
2356 cf066bf8 2019-05-09 stsp ct->status = status;
2357 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2358 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2359 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2360 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2361 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2362 b416585c 2019-05-13 stsp goto done;
2363 b416585c 2019-05-13 stsp }
2364 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2365 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2366 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2367 036813ee 2019-05-09 stsp goto done;
2368 036813ee 2019-05-09 stsp }
2369 ca2503ea 2019-05-09 stsp }
2370 24519714 2019-05-09 stsp ct->path = strdup(path);
2371 24519714 2019-05-09 stsp if (ct->path == NULL) {
2372 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2373 24519714 2019-05-09 stsp goto done;
2374 24519714 2019-05-09 stsp }
2375 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2376 c4296144 2019-05-09 stsp done:
2377 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2378 ed175427 2019-05-09 stsp free_commitable(ct);
2379 24519714 2019-05-09 stsp free(parent_path);
2380 036813ee 2019-05-09 stsp free(path);
2381 a129376b 2019-03-28 stsp return err;
2382 a129376b 2019-03-28 stsp }
2383 c4296144 2019-05-09 stsp
2384 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2385 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2386 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2387 036813ee 2019-05-09 stsp struct got_repository *);
2388 ed175427 2019-05-09 stsp
2389 ed175427 2019-05-09 stsp static const struct got_error *
2390 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2391 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2392 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2393 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2394 afa376bf 2019-05-09 stsp struct got_repository *repo)
2395 ed175427 2019-05-09 stsp {
2396 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2397 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2398 036813ee 2019-05-09 stsp char *subpath;
2399 ed175427 2019-05-09 stsp
2400 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2401 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2402 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2403 ed175427 2019-05-09 stsp
2404 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2405 ed175427 2019-05-09 stsp if (err)
2406 ed175427 2019-05-09 stsp return err;
2407 ed175427 2019-05-09 stsp
2408 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2409 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2410 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2411 036813ee 2019-05-09 stsp free(subpath);
2412 036813ee 2019-05-09 stsp return err;
2413 036813ee 2019-05-09 stsp }
2414 ed175427 2019-05-09 stsp
2415 036813ee 2019-05-09 stsp static const struct got_error *
2416 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2417 036813ee 2019-05-09 stsp {
2418 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2419 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2420 ed175427 2019-05-09 stsp
2421 036813ee 2019-05-09 stsp *match = 0;
2422 ed175427 2019-05-09 stsp
2423 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2424 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2425 0f63689d 2019-05-10 stsp return NULL;
2426 036813ee 2019-05-09 stsp }
2427 0b5cc0d6 2019-05-09 stsp
2428 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2429 0f63689d 2019-05-10 stsp if (err)
2430 0f63689d 2019-05-10 stsp return err;
2431 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2432 036813ee 2019-05-09 stsp free(ct_parent_path);
2433 036813ee 2019-05-09 stsp return err;
2434 036813ee 2019-05-09 stsp }
2435 0b5cc0d6 2019-05-09 stsp
2436 768aea60 2019-05-09 stsp static mode_t
2437 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2438 768aea60 2019-05-09 stsp {
2439 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2440 768aea60 2019-05-09 stsp }
2441 768aea60 2019-05-09 stsp
2442 036813ee 2019-05-09 stsp static const struct got_error *
2443 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2444 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2445 036813ee 2019-05-09 stsp {
2446 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2447 ca2503ea 2019-05-09 stsp
2448 036813ee 2019-05-09 stsp *new_te = NULL;
2449 0b5cc0d6 2019-05-09 stsp
2450 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2451 036813ee 2019-05-09 stsp if (err)
2452 036813ee 2019-05-09 stsp goto done;
2453 0b5cc0d6 2019-05-09 stsp
2454 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2455 036813ee 2019-05-09 stsp
2456 036813ee 2019-05-09 stsp free((*new_te)->id);
2457 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2458 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2459 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2460 036813ee 2019-05-09 stsp goto done;
2461 036813ee 2019-05-09 stsp }
2462 036813ee 2019-05-09 stsp done:
2463 036813ee 2019-05-09 stsp if (err && *new_te) {
2464 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2465 036813ee 2019-05-09 stsp *new_te = NULL;
2466 036813ee 2019-05-09 stsp }
2467 036813ee 2019-05-09 stsp return err;
2468 036813ee 2019-05-09 stsp }
2469 036813ee 2019-05-09 stsp
2470 036813ee 2019-05-09 stsp static const struct got_error *
2471 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2472 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2473 036813ee 2019-05-09 stsp {
2474 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2475 036813ee 2019-05-09 stsp char *ct_name;
2476 036813ee 2019-05-09 stsp
2477 036813ee 2019-05-09 stsp *new_te = NULL;
2478 036813ee 2019-05-09 stsp
2479 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2480 036813ee 2019-05-09 stsp if (*new_te == NULL)
2481 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2482 036813ee 2019-05-09 stsp
2483 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2484 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2485 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2486 036813ee 2019-05-09 stsp goto done;
2487 036813ee 2019-05-09 stsp }
2488 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2489 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2490 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2491 036813ee 2019-05-09 stsp goto done;
2492 036813ee 2019-05-09 stsp }
2493 036813ee 2019-05-09 stsp
2494 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2495 036813ee 2019-05-09 stsp
2496 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2497 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2498 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2499 036813ee 2019-05-09 stsp goto done;
2500 036813ee 2019-05-09 stsp }
2501 036813ee 2019-05-09 stsp done:
2502 036813ee 2019-05-09 stsp if (err && *new_te) {
2503 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2504 036813ee 2019-05-09 stsp *new_te = NULL;
2505 036813ee 2019-05-09 stsp }
2506 036813ee 2019-05-09 stsp return err;
2507 036813ee 2019-05-09 stsp }
2508 036813ee 2019-05-09 stsp
2509 036813ee 2019-05-09 stsp static const struct got_error *
2510 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2511 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2512 036813ee 2019-05-09 stsp {
2513 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2514 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2515 036813ee 2019-05-09 stsp
2516 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2517 036813ee 2019-05-09 stsp if (err)
2518 036813ee 2019-05-09 stsp return err;
2519 036813ee 2019-05-09 stsp if (new_pe == NULL)
2520 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2521 036813ee 2019-05-09 stsp return NULL;
2522 afa376bf 2019-05-09 stsp }
2523 afa376bf 2019-05-09 stsp
2524 afa376bf 2019-05-09 stsp static const struct got_error *
2525 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2526 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2527 afa376bf 2019-05-09 stsp {
2528 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2529 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2530 afa376bf 2019-05-09 stsp ct_path++;
2531 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2532 036813ee 2019-05-09 stsp }
2533 036813ee 2019-05-09 stsp
2534 036813ee 2019-05-09 stsp static const struct got_error *
2535 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2536 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2537 44d03001 2019-05-09 stsp {
2538 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2539 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2540 44d03001 2019-05-09 stsp char *te_path;
2541 44d03001 2019-05-09 stsp
2542 44d03001 2019-05-09 stsp *modified = 0;
2543 44d03001 2019-05-09 stsp
2544 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2545 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2546 44d03001 2019-05-09 stsp te->name) == -1)
2547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2548 44d03001 2019-05-09 stsp
2549 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2550 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2551 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2552 44d03001 2019-05-09 stsp strlen(te_path));
2553 44d03001 2019-05-09 stsp if (*modified)
2554 44d03001 2019-05-09 stsp break;
2555 44d03001 2019-05-09 stsp }
2556 44d03001 2019-05-09 stsp
2557 44d03001 2019-05-09 stsp free(te_path);
2558 44d03001 2019-05-09 stsp return err;
2559 44d03001 2019-05-09 stsp }
2560 44d03001 2019-05-09 stsp
2561 44d03001 2019-05-09 stsp static const struct got_error *
2562 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2563 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2564 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2565 036813ee 2019-05-09 stsp {
2566 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2567 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2568 036813ee 2019-05-09 stsp
2569 036813ee 2019-05-09 stsp *ctp = NULL;
2570 036813ee 2019-05-09 stsp
2571 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2572 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2573 036813ee 2019-05-09 stsp char *ct_name = NULL;
2574 036813ee 2019-05-09 stsp int path_matches;
2575 036813ee 2019-05-09 stsp
2576 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2577 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2578 036813ee 2019-05-09 stsp continue;
2579 036813ee 2019-05-09 stsp
2580 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2581 036813ee 2019-05-09 stsp continue;
2582 036813ee 2019-05-09 stsp
2583 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2584 036813ee 2019-05-09 stsp if (err)
2585 036813ee 2019-05-09 stsp return err;
2586 036813ee 2019-05-09 stsp if (!path_matches)
2587 036813ee 2019-05-09 stsp continue;
2588 036813ee 2019-05-09 stsp
2589 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2590 036813ee 2019-05-09 stsp if (ct_name == NULL)
2591 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2592 036813ee 2019-05-09 stsp
2593 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2594 036813ee 2019-05-09 stsp continue;
2595 036813ee 2019-05-09 stsp
2596 036813ee 2019-05-09 stsp *ctp = ct;
2597 036813ee 2019-05-09 stsp break;
2598 036813ee 2019-05-09 stsp }
2599 036813ee 2019-05-09 stsp
2600 036813ee 2019-05-09 stsp return err;
2601 036813ee 2019-05-09 stsp }
2602 036813ee 2019-05-09 stsp
2603 036813ee 2019-05-09 stsp static const struct got_error *
2604 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2605 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2606 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2607 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2608 036813ee 2019-05-09 stsp struct got_repository *repo)
2609 036813ee 2019-05-09 stsp {
2610 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2611 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2612 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2613 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2614 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2615 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2616 036813ee 2019-05-09 stsp
2617 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2618 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2619 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2620 036813ee 2019-05-09 stsp
2621 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2622 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2623 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2624 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2625 036813ee 2019-05-09 stsp
2626 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD)
2627 036813ee 2019-05-09 stsp continue;
2628 036813ee 2019-05-09 stsp
2629 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2630 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2631 036813ee 2019-05-09 stsp continue;
2632 036813ee 2019-05-09 stsp
2633 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2634 036813ee 2019-05-09 stsp pe->path);
2635 036813ee 2019-05-09 stsp if (err)
2636 036813ee 2019-05-09 stsp goto done;
2637 036813ee 2019-05-09 stsp
2638 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2639 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2640 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2641 036813ee 2019-05-09 stsp if (err)
2642 036813ee 2019-05-09 stsp goto done;
2643 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2644 afa376bf 2019-05-09 stsp if (err)
2645 afa376bf 2019-05-09 stsp goto done;
2646 036813ee 2019-05-09 stsp } else {
2647 2f51b5b3 2019-05-09 stsp char *subtree_path;
2648 baa7dcfa 2019-05-09 stsp struct got_pathlist_entry *pe2;
2649 baa7dcfa 2019-05-09 stsp int visited = 0;
2650 036813ee 2019-05-09 stsp
2651 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2652 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2653 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2654 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2655 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2656 2f51b5b3 2019-05-09 stsp goto done;
2657 2f51b5b3 2019-05-09 stsp }
2658 baa7dcfa 2019-05-09 stsp TAILQ_FOREACH(pe2, &paths, entry) {
2659 baa7dcfa 2019-05-09 stsp if (got_path_cmp(subtree_path, pe2->path) != 0)
2660 baa7dcfa 2019-05-09 stsp continue;
2661 baa7dcfa 2019-05-09 stsp visited = 1;
2662 baa7dcfa 2019-05-09 stsp break;
2663 baa7dcfa 2019-05-09 stsp }
2664 ce0de6b6 2019-05-10 stsp if (visited) {
2665 ce0de6b6 2019-05-10 stsp free(subtree_path);
2666 baa7dcfa 2019-05-09 stsp continue;
2667 ce0de6b6 2019-05-10 stsp }
2668 baa7dcfa 2019-05-09 stsp
2669 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2670 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2671 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2672 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2673 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2674 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2675 9ba0479c 2019-05-10 stsp new_te = NULL;
2676 9ba0479c 2019-05-10 stsp goto done;
2677 9ba0479c 2019-05-10 stsp }
2678 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2679 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2680 2f51b5b3 2019-05-09 stsp free(subtree_path);
2681 9ba0479c 2019-05-10 stsp if (err) {
2682 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2683 9ba0479c 2019-05-10 stsp new_te = NULL;
2684 036813ee 2019-05-09 stsp goto done;
2685 9ba0479c 2019-05-10 stsp }
2686 ed175427 2019-05-09 stsp }
2687 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2688 2f51b5b3 2019-05-09 stsp if (err)
2689 2f51b5b3 2019-05-09 stsp goto done;
2690 2f51b5b3 2019-05-09 stsp }
2691 2f51b5b3 2019-05-09 stsp
2692 2f51b5b3 2019-05-09 stsp if (base_tree) {
2693 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2694 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2695 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2696 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2697 2f51b5b3 2019-05-09 stsp
2698 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2699 44d03001 2019-05-09 stsp int modified;
2700 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2701 036813ee 2019-05-09 stsp if (err)
2702 036813ee 2019-05-09 stsp goto done;
2703 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2704 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2705 2f51b5b3 2019-05-09 stsp if (err)
2706 2f51b5b3 2019-05-09 stsp goto done;
2707 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2708 44d03001 2019-05-09 stsp if (modified) {
2709 44d03001 2019-05-09 stsp free(new_te->id);
2710 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2711 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2712 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2713 44d03001 2019-05-09 stsp if (err)
2714 44d03001 2019-05-09 stsp goto done;
2715 44d03001 2019-05-09 stsp }
2716 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2717 036813ee 2019-05-09 stsp if (err)
2718 036813ee 2019-05-09 stsp goto done;
2719 2f51b5b3 2019-05-09 stsp continue;
2720 036813ee 2019-05-09 stsp }
2721 2f51b5b3 2019-05-09 stsp
2722 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2723 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2724 2f51b5b3 2019-05-09 stsp if (ct) {
2725 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2726 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2727 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2728 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2729 2f51b5b3 2019-05-09 stsp if (err)
2730 2f51b5b3 2019-05-09 stsp goto done;
2731 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2732 2f51b5b3 2019-05-09 stsp if (err)
2733 2f51b5b3 2019-05-09 stsp goto done;
2734 2f51b5b3 2019-05-09 stsp }
2735 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
2736 b416585c 2019-05-13 stsp status_arg);
2737 afa376bf 2019-05-09 stsp if (err)
2738 afa376bf 2019-05-09 stsp goto done;
2739 2f51b5b3 2019-05-09 stsp } else {
2740 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
2741 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2742 2f51b5b3 2019-05-09 stsp if (err)
2743 2f51b5b3 2019-05-09 stsp goto done;
2744 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2745 2f51b5b3 2019-05-09 stsp if (err)
2746 2f51b5b3 2019-05-09 stsp goto done;
2747 2f51b5b3 2019-05-09 stsp }
2748 ca2503ea 2019-05-09 stsp }
2749 ed175427 2019-05-09 stsp }
2750 0b5cc0d6 2019-05-09 stsp
2751 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
2752 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
2753 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
2754 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
2755 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2756 0b5cc0d6 2019-05-09 stsp }
2757 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2758 ed175427 2019-05-09 stsp done:
2759 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
2760 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
2761 ed175427 2019-05-09 stsp return err;
2762 ed175427 2019-05-09 stsp }
2763 ed175427 2019-05-09 stsp
2764 ebf99748 2019-05-09 stsp static const struct got_error *
2765 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2766 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2767 ebf99748 2019-05-09 stsp {
2768 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2769 ebf99748 2019-05-09 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
2770 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
2771 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
2772 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
2773 ebf99748 2019-05-09 stsp
2774 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 ebf99748 2019-05-09 stsp if (err)
2776 ebf99748 2019-05-09 stsp return err;
2777 ebf99748 2019-05-09 stsp
2778 ebf99748 2019-05-09 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2779 ebf99748 2019-05-09 stsp fileindex_path);
2780 ebf99748 2019-05-09 stsp if (err)
2781 ebf99748 2019-05-09 stsp goto done;
2782 ebf99748 2019-05-09 stsp
2783 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2784 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
2785 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2786 ebf99748 2019-05-09 stsp
2787 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
2788 ebf99748 2019-05-09 stsp if (ie) {
2789 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
2790 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
2791 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
2792 ebf99748 2019-05-09 stsp } else
2793 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
2794 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
2795 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
2796 ebf99748 2019-05-09 stsp } else {
2797 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
2798 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
2799 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
2800 ebf99748 2019-05-09 stsp if (err)
2801 ebf99748 2019-05-09 stsp goto done;
2802 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
2803 ebf99748 2019-05-09 stsp if (err)
2804 ebf99748 2019-05-09 stsp goto done;
2805 ebf99748 2019-05-09 stsp }
2806 ebf99748 2019-05-09 stsp }
2807 ebf99748 2019-05-09 stsp
2808 ebf99748 2019-05-09 stsp err = got_fileindex_write(fileindex, new_index);
2809 ebf99748 2019-05-09 stsp if (err)
2810 ebf99748 2019-05-09 stsp goto done;
2811 ebf99748 2019-05-09 stsp
2812 ebf99748 2019-05-09 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2813 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2814 230a42bd 2019-05-11 jcs fileindex_path);
2815 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2816 ebf99748 2019-05-09 stsp goto done;
2817 ebf99748 2019-05-09 stsp }
2818 ebf99748 2019-05-09 stsp
2819 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2820 ebf99748 2019-05-09 stsp new_fileindex_path = NULL;
2821 ebf99748 2019-05-09 stsp
2822 ebf99748 2019-05-09 stsp done:
2823 ebf99748 2019-05-09 stsp if (new_fileindex_path)
2824 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2825 ebf99748 2019-05-09 stsp if (new_index)
2826 ebf99748 2019-05-09 stsp fclose(new_index);
2827 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2828 ebf99748 2019-05-09 stsp free(fileindex_path);
2829 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
2830 d56d26ce 2019-05-10 stsp return err;
2831 d56d26ce 2019-05-10 stsp }
2832 d56d26ce 2019-05-10 stsp
2833 d56d26ce 2019-05-10 stsp static const struct got_error *
2834 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2835 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
2836 d56d26ce 2019-05-10 stsp {
2837 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
2838 d56d26ce 2019-05-10 stsp struct got_object_id *id_in_head;
2839 d56d26ce 2019-05-10 stsp
2840 1251a9e5 2019-05-10 stsp /*
2841 b416585c 2019-05-13 stsp * Require that modified/deleted files are based on the branch head.
2842 b416585c 2019-05-13 stsp * This requirement could be relaxed to force less update operations
2843 b416585c 2019-05-13 stsp * on users but, for now, we want to play it safe and see how it goes.
2844 b416585c 2019-05-13 stsp *
2845 b416585c 2019-05-13 stsp * If this check is relaxed, it must still ensure that no modifications
2846 b416585c 2019-05-13 stsp * were made to files *and their parents* in commits between the file's
2847 b416585c 2019-05-13 stsp * base commit and the branch head. Otherwise, tree conflicts will not
2848 b416585c 2019-05-13 stsp * be detected reliably.
2849 b416585c 2019-05-13 stsp */
2850 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
2851 b416585c 2019-05-13 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2852 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2853 b416585c 2019-05-13 stsp return NULL;
2854 d56d26ce 2019-05-10 stsp }
2855 d56d26ce 2019-05-10 stsp
2856 b416585c 2019-05-13 stsp /* Require that added files don't exist in the branch head. */
2857 b416585c 2019-05-13 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2858 b416585c 2019-05-13 stsp ct->in_repo_path);
2859 b416585c 2019-05-13 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2860 b416585c 2019-05-13 stsp return err;
2861 b416585c 2019-05-13 stsp if (id_in_head) {
2862 b416585c 2019-05-13 stsp free(id_in_head);
2863 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2864 b416585c 2019-05-13 stsp }
2865 d56d26ce 2019-05-10 stsp
2866 d56d26ce 2019-05-10 stsp free(id_in_head);
2867 b416585c 2019-05-13 stsp return NULL;
2868 ebf99748 2019-05-09 stsp }
2869 ebf99748 2019-05-09 stsp
2870 c4296144 2019-05-09 stsp const struct got_error *
2871 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
2872 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
2873 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
2874 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2875 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2876 afa376bf 2019-05-09 stsp struct got_repository *repo)
2877 c4296144 2019-05-09 stsp {
2878 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2879 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
2880 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
2881 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
2882 bc70eb79 2019-05-09 stsp char *relpath = NULL;
2883 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
2884 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
2885 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
2886 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
2887 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
2888 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
2889 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
2890 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
2891 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
2892 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
2893 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
2894 c4296144 2019-05-09 stsp
2895 c4296144 2019-05-09 stsp *new_commit_id = NULL;
2896 c4296144 2019-05-09 stsp
2897 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
2898 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
2899 c4296144 2019-05-09 stsp
2900 c4296144 2019-05-09 stsp if (ondisk_path) {
2901 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
2902 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
2903 c4296144 2019-05-09 stsp if (err)
2904 c4296144 2019-05-09 stsp return err;
2905 c4296144 2019-05-09 stsp }
2906 c4296144 2019-05-09 stsp
2907 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
2908 c4296144 2019-05-09 stsp if (err)
2909 c4296144 2019-05-09 stsp goto done;
2910 675c7539 2019-05-09 stsp
2911 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2912 36a38700 2019-05-10 stsp if (err)
2913 36a38700 2019-05-10 stsp goto done;
2914 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2915 09f5bd90 2019-05-09 stsp if (err)
2916 09f5bd90 2019-05-09 stsp goto done;
2917 09f5bd90 2019-05-09 stsp
2918 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
2919 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
2920 24519714 2019-05-09 stsp cc_arg.repo = repo;
2921 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
2922 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
2923 675c7539 2019-05-09 stsp if (err)
2924 675c7539 2019-05-09 stsp goto done;
2925 675c7539 2019-05-09 stsp
2926 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
2927 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2928 33ad4cbe 2019-05-12 jcs goto done;
2929 33ad4cbe 2019-05-12 jcs }
2930 33ad4cbe 2019-05-12 jcs
2931 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2932 588edf97 2019-05-10 stsp if (err)
2933 588edf97 2019-05-10 stsp goto done;
2934 588edf97 2019-05-10 stsp
2935 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2936 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2937 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
2938 d56d26ce 2019-05-10 stsp if (err)
2939 819f385b 2019-05-10 stsp goto done;
2940 819f385b 2019-05-10 stsp }
2941 de18fc63 2019-05-09 stsp
2942 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2943 588edf97 2019-05-10 stsp if (err)
2944 588edf97 2019-05-10 stsp goto done;
2945 588edf97 2019-05-10 stsp
2946 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
2947 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2948 33ad4cbe 2019-05-12 jcs if (err)
2949 33ad4cbe 2019-05-12 jcs goto done;
2950 33ad4cbe 2019-05-12 jcs }
2951 c4296144 2019-05-09 stsp
2952 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
2953 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2954 33ad4cbe 2019-05-12 jcs goto done;
2955 33ad4cbe 2019-05-12 jcs }
2956 33ad4cbe 2019-05-12 jcs
2957 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
2958 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2959 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2960 cf066bf8 2019-05-09 stsp char *ondisk_path;
2961 cf066bf8 2019-05-09 stsp
2962 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
2963 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
2964 cf066bf8 2019-05-09 stsp continue;
2965 cf066bf8 2019-05-09 stsp
2966 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
2967 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
2968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2969 cf066bf8 2019-05-09 stsp goto done;
2970 cf066bf8 2019-05-09 stsp }
2971 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2972 a7055788 2019-05-09 stsp free(ondisk_path);
2973 cf066bf8 2019-05-09 stsp if (err)
2974 cf066bf8 2019-05-09 stsp goto done;
2975 cf066bf8 2019-05-09 stsp }
2976 036813ee 2019-05-09 stsp
2977 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
2978 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2979 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2980 036813ee 2019-05-09 stsp if (err)
2981 036813ee 2019-05-09 stsp goto done;
2982 036813ee 2019-05-09 stsp
2983 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2984 de18fc63 2019-05-09 stsp if (err)
2985 de18fc63 2019-05-09 stsp goto done;
2986 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2987 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2988 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2989 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
2990 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
2991 33ad4cbe 2019-05-12 jcs free(logmsg);
2992 9d40349a 2019-05-09 stsp if (err)
2993 9d40349a 2019-05-09 stsp goto done;
2994 9d40349a 2019-05-09 stsp
2995 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
2996 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
2997 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
2998 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
2999 09f5bd90 2019-05-09 stsp goto done;
3000 09f5bd90 2019-05-09 stsp }
3001 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3002 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3003 09f5bd90 2019-05-09 stsp if (err)
3004 09f5bd90 2019-05-09 stsp goto done;
3005 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3006 ebf99748 2019-05-09 stsp if (err)
3007 ebf99748 2019-05-09 stsp goto done;
3008 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3009 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3010 09f5bd90 2019-05-09 stsp goto done;
3011 09f5bd90 2019-05-09 stsp }
3012 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3013 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3014 f2c16586 2019-05-09 stsp if (err)
3015 f2c16586 2019-05-09 stsp goto done;
3016 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3017 f2c16586 2019-05-09 stsp if (err)
3018 f2c16586 2019-05-09 stsp goto done;
3019 f2c16586 2019-05-09 stsp
3020 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3021 09f5bd90 2019-05-09 stsp if (err)
3022 09f5bd90 2019-05-09 stsp goto done;
3023 09f5bd90 2019-05-09 stsp
3024 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3025 ebf99748 2019-05-09 stsp if (err)
3026 ebf99748 2019-05-09 stsp goto done;
3027 ebf99748 2019-05-09 stsp
3028 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3029 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3030 ebf99748 2019-05-09 stsp if (err)
3031 ebf99748 2019-05-09 stsp goto done;
3032 c4296144 2019-05-09 stsp done:
3033 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3034 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3035 c4296144 2019-05-09 stsp err = unlockerr;
3036 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3037 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3038 ed175427 2019-05-09 stsp free_commitable(ct);
3039 cf066bf8 2019-05-09 stsp }
3040 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3041 588edf97 2019-05-10 stsp if (head_tree)
3042 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3043 588edf97 2019-05-10 stsp if (head_commit)
3044 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3045 c4296144 2019-05-09 stsp free(relpath);
3046 09f5bd90 2019-05-09 stsp free(head_commit_id);
3047 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3048 36a38700 2019-05-10 stsp if (head_ref)
3049 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3050 2f17228e 2019-05-12 stsp if (head_ref2) {
3051 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3052 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3053 2f17228e 2019-05-12 stsp err = unlockerr;
3054 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3055 2f17228e 2019-05-12 stsp }
3056 c4296144 2019-05-09 stsp return err;
3057 c4296144 2019-05-09 stsp }