Blame


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