Blame


1 8505ab66 2023-07-10 thomas /*
2 8505ab66 2023-07-10 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 8505ab66 2023-07-10 thomas *
4 8505ab66 2023-07-10 thomas * Permission to use, copy, modify, and distribute this software for any
5 8505ab66 2023-07-10 thomas * purpose with or without fee is hereby granted, provided that the above
6 8505ab66 2023-07-10 thomas * copyright notice and this permission notice appear in all copies.
7 8505ab66 2023-07-10 thomas *
8 8505ab66 2023-07-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8505ab66 2023-07-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8505ab66 2023-07-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8505ab66 2023-07-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8505ab66 2023-07-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8505ab66 2023-07-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8505ab66 2023-07-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 8505ab66 2023-07-10 thomas */
16 8505ab66 2023-07-10 thomas
17 8505ab66 2023-07-10 thomas #include <sys/stat.h>
18 8505ab66 2023-07-10 thomas #include <sys/queue.h>
19 8505ab66 2023-07-10 thomas #include <sys/tree.h>
20 8505ab66 2023-07-10 thomas
21 8505ab66 2023-07-10 thomas #include <dirent.h>
22 8505ab66 2023-07-10 thomas #include <limits.h>
23 8505ab66 2023-07-10 thomas #include <stddef.h>
24 8505ab66 2023-07-10 thomas #include <string.h>
25 8505ab66 2023-07-10 thomas #include <stdio.h>
26 8505ab66 2023-07-10 thomas #include <stdlib.h>
27 8505ab66 2023-07-10 thomas #include <time.h>
28 8505ab66 2023-07-10 thomas #include <fcntl.h>
29 8505ab66 2023-07-10 thomas #include <errno.h>
30 8505ab66 2023-07-10 thomas #include <unistd.h>
31 8505ab66 2023-07-10 thomas #include <sha1.h>
32 8505ab66 2023-07-10 thomas #include <sha2.h>
33 8505ab66 2023-07-10 thomas #include <zlib.h>
34 8505ab66 2023-07-10 thomas #include <fnmatch.h>
35 8505ab66 2023-07-10 thomas #include <libgen.h>
36 8505ab66 2023-07-10 thomas #include <uuid.h>
37 8505ab66 2023-07-10 thomas #include <util.h>
38 8505ab66 2023-07-10 thomas
39 8505ab66 2023-07-10 thomas #include "got_error.h"
40 8505ab66 2023-07-10 thomas #include "got_repository.h"
41 8505ab66 2023-07-10 thomas #include "got_reference.h"
42 8505ab66 2023-07-10 thomas #include "got_object.h"
43 8505ab66 2023-07-10 thomas #include "got_path.h"
44 8505ab66 2023-07-10 thomas #include "got_cancel.h"
45 8505ab66 2023-07-10 thomas #include "got_worktree.h"
46 8505ab66 2023-07-10 thomas #include "got_worktree_cvg.h"
47 8505ab66 2023-07-10 thomas #include "got_opentemp.h"
48 8505ab66 2023-07-10 thomas #include "got_diff.h"
49 8505ab66 2023-07-10 thomas #include "got_send.h"
50 8505ab66 2023-07-10 thomas #include "got_fetch.h"
51 8505ab66 2023-07-10 thomas
52 8505ab66 2023-07-10 thomas #include "got_lib_worktree.h"
53 8505ab66 2023-07-10 thomas #include "got_lib_hash.h"
54 8505ab66 2023-07-10 thomas #include "got_lib_fileindex.h"
55 8505ab66 2023-07-10 thomas #include "got_lib_inflate.h"
56 8505ab66 2023-07-10 thomas #include "got_lib_delta.h"
57 8505ab66 2023-07-10 thomas #include "got_lib_object.h"
58 8505ab66 2023-07-10 thomas #include "got_lib_object_parse.h"
59 8505ab66 2023-07-10 thomas #include "got_lib_object_create.h"
60 8505ab66 2023-07-10 thomas #include "got_lib_object_idset.h"
61 8505ab66 2023-07-10 thomas #include "got_lib_diff.h"
62 8505ab66 2023-07-10 thomas
63 8505ab66 2023-07-10 thomas #ifndef MIN
64 8505ab66 2023-07-10 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 8505ab66 2023-07-10 thomas #endif
66 8505ab66 2023-07-10 thomas
67 8505ab66 2023-07-10 thomas #define GOT_MERGE_LABEL_MERGED "merged change"
68 8505ab66 2023-07-10 thomas #define GOT_MERGE_LABEL_BASE "3-way merge base"
69 8505ab66 2023-07-10 thomas
70 8505ab66 2023-07-10 thomas static const struct got_error *
71 8505ab66 2023-07-10 thomas lock_worktree(struct got_worktree *worktree, int operation)
72 8505ab66 2023-07-10 thomas {
73 8505ab66 2023-07-10 thomas if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
74 8505ab66 2023-07-10 thomas return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
75 8505ab66 2023-07-10 thomas : got_error_from_errno2("flock",
76 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree)));
77 8505ab66 2023-07-10 thomas return NULL;
78 8505ab66 2023-07-10 thomas }
79 8505ab66 2023-07-10 thomas
80 8505ab66 2023-07-10 thomas static const struct got_error *
81 8505ab66 2023-07-10 thomas is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
82 8505ab66 2023-07-10 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path)
83 8505ab66 2023-07-10 thomas {
84 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
85 8505ab66 2023-07-10 thomas char canonpath[PATH_MAX];
86 8505ab66 2023-07-10 thomas char *path_got = NULL;
87 8505ab66 2023-07-10 thomas
88 8505ab66 2023-07-10 thomas *is_bad_symlink = 0;
89 8505ab66 2023-07-10 thomas
90 8505ab66 2023-07-10 thomas if (target_len >= sizeof(canonpath)) {
91 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
92 8505ab66 2023-07-10 thomas return NULL;
93 8505ab66 2023-07-10 thomas }
94 8505ab66 2023-07-10 thomas
95 8505ab66 2023-07-10 thomas /*
96 8505ab66 2023-07-10 thomas * We do not use realpath(3) to resolve the symlink's target
97 8505ab66 2023-07-10 thomas * path because we don't want to resolve symlinks recursively.
98 8505ab66 2023-07-10 thomas * Instead we make the path absolute and then canonicalize it.
99 8505ab66 2023-07-10 thomas * Relative symlink target lookup should begin at the directory
100 8505ab66 2023-07-10 thomas * in which the blob object is being installed.
101 8505ab66 2023-07-10 thomas */
102 8505ab66 2023-07-10 thomas if (!got_path_is_absolute(target_path)) {
103 8505ab66 2023-07-10 thomas char *abspath, *parent;
104 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent, ondisk_path);
105 8505ab66 2023-07-10 thomas if (err)
106 8505ab66 2023-07-10 thomas return err;
107 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
108 8505ab66 2023-07-10 thomas free(parent);
109 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
110 8505ab66 2023-07-10 thomas }
111 8505ab66 2023-07-10 thomas free(parent);
112 8505ab66 2023-07-10 thomas if (strlen(abspath) >= sizeof(canonpath)) {
113 8505ab66 2023-07-10 thomas err = got_error_path(abspath, GOT_ERR_BAD_PATH);
114 8505ab66 2023-07-10 thomas free(abspath);
115 8505ab66 2023-07-10 thomas return err;
116 8505ab66 2023-07-10 thomas }
117 8505ab66 2023-07-10 thomas err = got_canonpath(abspath, canonpath, sizeof(canonpath));
118 8505ab66 2023-07-10 thomas free(abspath);
119 8505ab66 2023-07-10 thomas if (err)
120 8505ab66 2023-07-10 thomas return err;
121 8505ab66 2023-07-10 thomas } else {
122 8505ab66 2023-07-10 thomas err = got_canonpath(target_path, canonpath, sizeof(canonpath));
123 8505ab66 2023-07-10 thomas if (err)
124 8505ab66 2023-07-10 thomas return err;
125 8505ab66 2023-07-10 thomas }
126 8505ab66 2023-07-10 thomas
127 8505ab66 2023-07-10 thomas /* Only allow symlinks pointing at paths within the work tree. */
128 8505ab66 2023-07-10 thomas if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
129 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
130 8505ab66 2023-07-10 thomas return NULL;
131 8505ab66 2023-07-10 thomas }
132 8505ab66 2023-07-10 thomas
133 8505ab66 2023-07-10 thomas /* Do not allow symlinks pointing into the .got directory. */
134 8505ab66 2023-07-10 thomas if (asprintf(&path_got, "%s/%s", wtroot_path,
135 8505ab66 2023-07-10 thomas GOT_WORKTREE_GOT_DIR) == -1)
136 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
137 8505ab66 2023-07-10 thomas if (got_path_is_child(canonpath, path_got, strlen(path_got)))
138 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
139 8505ab66 2023-07-10 thomas
140 8505ab66 2023-07-10 thomas free(path_got);
141 8505ab66 2023-07-10 thomas return NULL;
142 8505ab66 2023-07-10 thomas }
143 8505ab66 2023-07-10 thomas
144 8505ab66 2023-07-10 thomas /*
145 8505ab66 2023-07-10 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
146 8505ab66 2023-07-10 thomas * conflict marker is found in newly added lines only.
147 8505ab66 2023-07-10 thomas */
148 8505ab66 2023-07-10 thomas static const struct got_error *
149 8505ab66 2023-07-10 thomas get_modified_file_content_status(unsigned char *status,
150 8505ab66 2023-07-10 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
151 8505ab66 2023-07-10 thomas FILE *ondisk_file)
152 8505ab66 2023-07-10 thomas {
153 8505ab66 2023-07-10 thomas const struct got_error *err, *free_err;
154 8505ab66 2023-07-10 thomas const char *markers[3] = {
155 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
156 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
157 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_END
158 8505ab66 2023-07-10 thomas };
159 8505ab66 2023-07-10 thomas FILE *f1 = NULL;
160 8505ab66 2023-07-10 thomas struct got_diffreg_result *diffreg_result = NULL;
161 8505ab66 2023-07-10 thomas struct diff_result *r;
162 8505ab66 2023-07-10 thomas int nchunks_parsed, n, i = 0, ln = 0;
163 8505ab66 2023-07-10 thomas char *line = NULL;
164 8505ab66 2023-07-10 thomas size_t linesize = 0;
165 8505ab66 2023-07-10 thomas ssize_t linelen;
166 8505ab66 2023-07-10 thomas
167 8505ab66 2023-07-10 thomas if (*status != GOT_STATUS_MODIFY)
168 8505ab66 2023-07-10 thomas return NULL;
169 8505ab66 2023-07-10 thomas
170 8505ab66 2023-07-10 thomas f1 = got_opentemp();
171 8505ab66 2023-07-10 thomas if (f1 == NULL)
172 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp");
173 8505ab66 2023-07-10 thomas
174 8505ab66 2023-07-10 thomas if (blob) {
175 8505ab66 2023-07-10 thomas got_object_blob_rewind(blob);
176 8505ab66 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
177 8505ab66 2023-07-10 thomas if (err)
178 8505ab66 2023-07-10 thomas goto done;
179 8505ab66 2023-07-10 thomas }
180 8505ab66 2023-07-10 thomas
181 8505ab66 2023-07-10 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
182 8505ab66 2023-07-10 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
183 8505ab66 2023-07-10 thomas if (err)
184 8505ab66 2023-07-10 thomas goto done;
185 8505ab66 2023-07-10 thomas
186 8505ab66 2023-07-10 thomas r = diffreg_result->result;
187 8505ab66 2023-07-10 thomas
188 8505ab66 2023-07-10 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
189 8505ab66 2023-07-10 thomas struct diff_chunk *c;
190 8505ab66 2023-07-10 thomas struct diff_chunk_context cc = {};
191 8505ab66 2023-07-10 thomas off_t pos;
192 8505ab66 2023-07-10 thomas
193 8505ab66 2023-07-10 thomas /*
194 8505ab66 2023-07-10 thomas * We can optimise a little by advancing straight
195 8505ab66 2023-07-10 thomas * to the next chunk if this one has no added lines.
196 8505ab66 2023-07-10 thomas */
197 8505ab66 2023-07-10 thomas c = diff_chunk_get(r, n);
198 8505ab66 2023-07-10 thomas
199 8505ab66 2023-07-10 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
200 8505ab66 2023-07-10 thomas nchunks_parsed = 1;
201 8505ab66 2023-07-10 thomas continue; /* removed or unchanged lines */
202 8505ab66 2023-07-10 thomas }
203 8505ab66 2023-07-10 thomas
204 8505ab66 2023-07-10 thomas pos = diff_chunk_get_right_start_pos(c);
205 8505ab66 2023-07-10 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
206 8505ab66 2023-07-10 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
207 8505ab66 2023-07-10 thomas goto done;
208 8505ab66 2023-07-10 thomas }
209 8505ab66 2023-07-10 thomas
210 8505ab66 2023-07-10 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
211 8505ab66 2023-07-10 thomas ln = cc.right.start;
212 8505ab66 2023-07-10 thomas
213 8505ab66 2023-07-10 thomas while (ln < cc.right.end) {
214 8505ab66 2023-07-10 thomas linelen = getline(&line, &linesize, ondisk_file);
215 8505ab66 2023-07-10 thomas if (linelen == -1) {
216 8505ab66 2023-07-10 thomas if (feof(ondisk_file))
217 8505ab66 2023-07-10 thomas break;
218 8505ab66 2023-07-10 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
219 8505ab66 2023-07-10 thomas break;
220 8505ab66 2023-07-10 thomas }
221 8505ab66 2023-07-10 thomas
222 8505ab66 2023-07-10 thomas if (line && strncmp(line, markers[i],
223 8505ab66 2023-07-10 thomas strlen(markers[i])) == 0) {
224 8505ab66 2023-07-10 thomas if (strcmp(markers[i],
225 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
226 8505ab66 2023-07-10 thomas *status = GOT_STATUS_CONFLICT;
227 8505ab66 2023-07-10 thomas goto done;
228 8505ab66 2023-07-10 thomas } else
229 8505ab66 2023-07-10 thomas i++;
230 8505ab66 2023-07-10 thomas }
231 8505ab66 2023-07-10 thomas ++ln;
232 8505ab66 2023-07-10 thomas }
233 8505ab66 2023-07-10 thomas }
234 8505ab66 2023-07-10 thomas
235 8505ab66 2023-07-10 thomas done:
236 8505ab66 2023-07-10 thomas free(line);
237 8505ab66 2023-07-10 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
238 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
239 8505ab66 2023-07-10 thomas free_err = got_diffreg_result_free(diffreg_result);
240 8505ab66 2023-07-10 thomas if (err == NULL)
241 8505ab66 2023-07-10 thomas err = free_err;
242 8505ab66 2023-07-10 thomas
243 8505ab66 2023-07-10 thomas return err;
244 8505ab66 2023-07-10 thomas }
245 8505ab66 2023-07-10 thomas
246 8505ab66 2023-07-10 thomas static int
247 8505ab66 2023-07-10 thomas xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
248 8505ab66 2023-07-10 thomas {
249 8505ab66 2023-07-10 thomas mode_t ie_mode = got_fileindex_perms_to_st(ie);
250 8505ab66 2023-07-10 thomas return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
251 8505ab66 2023-07-10 thomas }
252 8505ab66 2023-07-10 thomas
253 8505ab66 2023-07-10 thomas static int
254 8505ab66 2023-07-10 thomas stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
255 8505ab66 2023-07-10 thomas {
256 8505ab66 2023-07-10 thomas return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
257 8505ab66 2023-07-10 thomas ie->ctime_nsec == sb->st_ctim.tv_nsec &&
258 8505ab66 2023-07-10 thomas ie->mtime_sec == sb->st_mtim.tv_sec &&
259 8505ab66 2023-07-10 thomas ie->mtime_nsec == sb->st_mtim.tv_nsec &&
260 8505ab66 2023-07-10 thomas ie->size == (sb->st_size & 0xffffffff) &&
261 8505ab66 2023-07-10 thomas !xbit_differs(ie, sb->st_mode));
262 8505ab66 2023-07-10 thomas }
263 8505ab66 2023-07-10 thomas
264 8505ab66 2023-07-10 thomas static unsigned char
265 8505ab66 2023-07-10 thomas get_staged_status(struct got_fileindex_entry *ie)
266 8505ab66 2023-07-10 thomas {
267 8505ab66 2023-07-10 thomas switch (got_fileindex_entry_stage_get(ie)) {
268 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_ADD:
269 8505ab66 2023-07-10 thomas return GOT_STATUS_ADD;
270 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_DELETE:
271 8505ab66 2023-07-10 thomas return GOT_STATUS_DELETE;
272 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_MODIFY:
273 8505ab66 2023-07-10 thomas return GOT_STATUS_MODIFY;
274 8505ab66 2023-07-10 thomas default:
275 8505ab66 2023-07-10 thomas return GOT_STATUS_NO_CHANGE;
276 8505ab66 2023-07-10 thomas }
277 8505ab66 2023-07-10 thomas }
278 8505ab66 2023-07-10 thomas
279 8505ab66 2023-07-10 thomas static const struct got_error *
280 8505ab66 2023-07-10 thomas get_symlink_modification_status(unsigned char *status,
281 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie, const char *abspath,
282 8505ab66 2023-07-10 thomas int dirfd, const char *de_name, struct got_blob_object *blob)
283 8505ab66 2023-07-10 thomas {
284 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
285 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
286 8505ab66 2023-07-10 thomas char etarget[PATH_MAX];
287 8505ab66 2023-07-10 thomas ssize_t elen;
288 8505ab66 2023-07-10 thomas size_t len, target_len = 0;
289 8505ab66 2023-07-10 thomas const uint8_t *buf = got_object_blob_get_read_buf(blob);
290 8505ab66 2023-07-10 thomas size_t hdrlen = got_object_blob_get_hdrlen(blob);
291 8505ab66 2023-07-10 thomas
292 8505ab66 2023-07-10 thomas *status = GOT_STATUS_NO_CHANGE;
293 8505ab66 2023-07-10 thomas
294 8505ab66 2023-07-10 thomas /* Blob object content specifies the target path of the link. */
295 8505ab66 2023-07-10 thomas do {
296 8505ab66 2023-07-10 thomas err = got_object_blob_read_block(&len, blob);
297 8505ab66 2023-07-10 thomas if (err)
298 8505ab66 2023-07-10 thomas return err;
299 8505ab66 2023-07-10 thomas if (len + target_len >= sizeof(target_path)) {
300 8505ab66 2023-07-10 thomas /*
301 8505ab66 2023-07-10 thomas * Should not happen. The blob contents were OK
302 8505ab66 2023-07-10 thomas * when this symlink was installed.
303 8505ab66 2023-07-10 thomas */
304 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
305 8505ab66 2023-07-10 thomas }
306 8505ab66 2023-07-10 thomas if (len > 0) {
307 8505ab66 2023-07-10 thomas /* Skip blob object header first time around. */
308 8505ab66 2023-07-10 thomas memcpy(target_path + target_len, buf + hdrlen,
309 8505ab66 2023-07-10 thomas len - hdrlen);
310 8505ab66 2023-07-10 thomas target_len += len - hdrlen;
311 8505ab66 2023-07-10 thomas hdrlen = 0;
312 8505ab66 2023-07-10 thomas }
313 8505ab66 2023-07-10 thomas } while (len != 0);
314 8505ab66 2023-07-10 thomas target_path[target_len] = '\0';
315 8505ab66 2023-07-10 thomas
316 8505ab66 2023-07-10 thomas if (dirfd != -1) {
317 8505ab66 2023-07-10 thomas elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
318 8505ab66 2023-07-10 thomas if (elen == -1)
319 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
320 8505ab66 2023-07-10 thomas } else {
321 8505ab66 2023-07-10 thomas elen = readlink(abspath, etarget, sizeof(etarget));
322 8505ab66 2023-07-10 thomas if (elen == -1)
323 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
324 8505ab66 2023-07-10 thomas }
325 8505ab66 2023-07-10 thomas
326 8505ab66 2023-07-10 thomas if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
327 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
328 8505ab66 2023-07-10 thomas
329 8505ab66 2023-07-10 thomas return NULL;
330 8505ab66 2023-07-10 thomas }
331 8505ab66 2023-07-10 thomas
332 8505ab66 2023-07-10 thomas static const struct got_error *
333 8505ab66 2023-07-10 thomas get_file_status(unsigned char *status, struct stat *sb,
334 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie, const char *abspath,
335 8505ab66 2023-07-10 thomas int dirfd, const char *de_name, struct got_repository *repo)
336 8505ab66 2023-07-10 thomas {
337 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
338 8505ab66 2023-07-10 thomas struct got_object_id id;
339 8505ab66 2023-07-10 thomas size_t hdrlen;
340 8505ab66 2023-07-10 thomas int fd = -1, fd1 = -1;
341 8505ab66 2023-07-10 thomas FILE *f = NULL;
342 8505ab66 2023-07-10 thomas uint8_t fbuf[8192];
343 8505ab66 2023-07-10 thomas struct got_blob_object *blob = NULL;
344 8505ab66 2023-07-10 thomas size_t flen, blen;
345 8505ab66 2023-07-10 thomas unsigned char staged_status;
346 8505ab66 2023-07-10 thomas
347 8505ab66 2023-07-10 thomas staged_status = get_staged_status(ie);
348 8505ab66 2023-07-10 thomas *status = GOT_STATUS_NO_CHANGE;
349 8505ab66 2023-07-10 thomas memset(sb, 0, sizeof(*sb));
350 8505ab66 2023-07-10 thomas
351 8505ab66 2023-07-10 thomas /*
352 8505ab66 2023-07-10 thomas * Whenever the caller provides a directory descriptor and a
353 8505ab66 2023-07-10 thomas * directory entry name for the file, use them! This prevents
354 8505ab66 2023-07-10 thomas * race conditions if filesystem paths change beneath our feet.
355 8505ab66 2023-07-10 thomas */
356 8505ab66 2023-07-10 thomas if (dirfd != -1) {
357 8505ab66 2023-07-10 thomas if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
358 8505ab66 2023-07-10 thomas if (errno == ENOENT) {
359 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
360 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MISSING;
361 8505ab66 2023-07-10 thomas else
362 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
363 8505ab66 2023-07-10 thomas goto done;
364 8505ab66 2023-07-10 thomas }
365 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
366 8505ab66 2023-07-10 thomas goto done;
367 8505ab66 2023-07-10 thomas }
368 8505ab66 2023-07-10 thomas } else {
369 8505ab66 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
370 8505ab66 2023-07-10 thomas if (fd == -1 && errno != ENOENT &&
371 8505ab66 2023-07-10 thomas !got_err_open_nofollow_on_symlink())
372 8505ab66 2023-07-10 thomas return got_error_from_errno2("open", abspath);
373 8505ab66 2023-07-10 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
374 8505ab66 2023-07-10 thomas if (lstat(abspath, sb) == -1)
375 8505ab66 2023-07-10 thomas return got_error_from_errno2("lstat", abspath);
376 8505ab66 2023-07-10 thomas } else if (fd == -1 || fstat(fd, sb) == -1) {
377 8505ab66 2023-07-10 thomas if (errno == ENOENT) {
378 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
379 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MISSING;
380 8505ab66 2023-07-10 thomas else
381 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
382 8505ab66 2023-07-10 thomas goto done;
383 8505ab66 2023-07-10 thomas }
384 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstat", abspath);
385 8505ab66 2023-07-10 thomas goto done;
386 8505ab66 2023-07-10 thomas }
387 8505ab66 2023-07-10 thomas }
388 8505ab66 2023-07-10 thomas
389 8505ab66 2023-07-10 thomas if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
390 8505ab66 2023-07-10 thomas *status = GOT_STATUS_OBSTRUCTED;
391 8505ab66 2023-07-10 thomas goto done;
392 8505ab66 2023-07-10 thomas }
393 8505ab66 2023-07-10 thomas
394 8505ab66 2023-07-10 thomas if (!got_fileindex_entry_has_file_on_disk(ie)) {
395 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
396 8505ab66 2023-07-10 thomas goto done;
397 8505ab66 2023-07-10 thomas } else if (!got_fileindex_entry_has_blob(ie) &&
398 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_ADD) {
399 8505ab66 2023-07-10 thomas *status = GOT_STATUS_ADD;
400 8505ab66 2023-07-10 thomas goto done;
401 8505ab66 2023-07-10 thomas }
402 8505ab66 2023-07-10 thomas
403 8505ab66 2023-07-10 thomas if (!stat_info_differs(ie, sb))
404 8505ab66 2023-07-10 thomas goto done;
405 8505ab66 2023-07-10 thomas
406 8505ab66 2023-07-10 thomas if (S_ISLNK(sb->st_mode) &&
407 8505ab66 2023-07-10 thomas got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
408 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
409 8505ab66 2023-07-10 thomas goto done;
410 8505ab66 2023-07-10 thomas }
411 8505ab66 2023-07-10 thomas
412 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_MODIFY ||
413 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_ADD)
414 8505ab66 2023-07-10 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
415 8505ab66 2023-07-10 thomas else
416 8505ab66 2023-07-10 thomas got_fileindex_entry_get_blob_id(&id, ie);
417 8505ab66 2023-07-10 thomas
418 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
419 8505ab66 2023-07-10 thomas if (fd1 == -1) {
420 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
421 8505ab66 2023-07-10 thomas goto done;
422 8505ab66 2023-07-10 thomas }
423 8505ab66 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
424 8505ab66 2023-07-10 thomas if (err)
425 8505ab66 2023-07-10 thomas goto done;
426 8505ab66 2023-07-10 thomas
427 8505ab66 2023-07-10 thomas if (S_ISLNK(sb->st_mode)) {
428 8505ab66 2023-07-10 thomas err = get_symlink_modification_status(status, ie,
429 8505ab66 2023-07-10 thomas abspath, dirfd, de_name, blob);
430 8505ab66 2023-07-10 thomas goto done;
431 8505ab66 2023-07-10 thomas }
432 8505ab66 2023-07-10 thomas
433 8505ab66 2023-07-10 thomas if (dirfd != -1) {
434 8505ab66 2023-07-10 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
435 8505ab66 2023-07-10 thomas if (fd == -1) {
436 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat", abspath);
437 8505ab66 2023-07-10 thomas goto done;
438 8505ab66 2023-07-10 thomas }
439 8505ab66 2023-07-10 thomas }
440 8505ab66 2023-07-10 thomas
441 8505ab66 2023-07-10 thomas f = fdopen(fd, "r");
442 8505ab66 2023-07-10 thomas if (f == NULL) {
443 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
444 8505ab66 2023-07-10 thomas goto done;
445 8505ab66 2023-07-10 thomas }
446 8505ab66 2023-07-10 thomas fd = -1;
447 8505ab66 2023-07-10 thomas hdrlen = got_object_blob_get_hdrlen(blob);
448 8505ab66 2023-07-10 thomas for (;;) {
449 8505ab66 2023-07-10 thomas const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
450 8505ab66 2023-07-10 thomas err = got_object_blob_read_block(&blen, blob);
451 8505ab66 2023-07-10 thomas if (err)
452 8505ab66 2023-07-10 thomas goto done;
453 8505ab66 2023-07-10 thomas /* Skip length of blob object header first time around. */
454 8505ab66 2023-07-10 thomas flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
455 8505ab66 2023-07-10 thomas if (flen == 0 && ferror(f)) {
456 8505ab66 2023-07-10 thomas err = got_error_from_errno("fread");
457 8505ab66 2023-07-10 thomas goto done;
458 8505ab66 2023-07-10 thomas }
459 8505ab66 2023-07-10 thomas if (blen - hdrlen == 0) {
460 8505ab66 2023-07-10 thomas if (flen != 0)
461 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
462 8505ab66 2023-07-10 thomas break;
463 8505ab66 2023-07-10 thomas } else if (flen == 0) {
464 8505ab66 2023-07-10 thomas if (blen - hdrlen != 0)
465 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
466 8505ab66 2023-07-10 thomas break;
467 8505ab66 2023-07-10 thomas } else if (blen - hdrlen == flen) {
468 8505ab66 2023-07-10 thomas /* Skip blob object header first time around. */
469 8505ab66 2023-07-10 thomas if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
470 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
471 8505ab66 2023-07-10 thomas break;
472 8505ab66 2023-07-10 thomas }
473 8505ab66 2023-07-10 thomas } else {
474 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
475 8505ab66 2023-07-10 thomas break;
476 8505ab66 2023-07-10 thomas }
477 8505ab66 2023-07-10 thomas hdrlen = 0;
478 8505ab66 2023-07-10 thomas }
479 8505ab66 2023-07-10 thomas
480 8505ab66 2023-07-10 thomas if (*status == GOT_STATUS_MODIFY) {
481 8505ab66 2023-07-10 thomas rewind(f);
482 8505ab66 2023-07-10 thomas err = get_modified_file_content_status(status, blob, ie->path,
483 8505ab66 2023-07-10 thomas sb, f);
484 8505ab66 2023-07-10 thomas } else if (xbit_differs(ie, sb->st_mode))
485 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODE_CHANGE;
486 8505ab66 2023-07-10 thomas done:
487 8505ab66 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
488 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
489 8505ab66 2023-07-10 thomas if (blob)
490 8505ab66 2023-07-10 thomas got_object_blob_close(blob);
491 8505ab66 2023-07-10 thomas if (f != NULL && fclose(f) == EOF && err == NULL)
492 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fclose", abspath);
493 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
494 8505ab66 2023-07-10 thomas err = got_error_from_errno2("close", abspath);
495 8505ab66 2023-07-10 thomas return err;
496 8505ab66 2023-07-10 thomas }
497 8505ab66 2023-07-10 thomas
498 8505ab66 2023-07-10 thomas static const struct got_error *
499 8505ab66 2023-07-10 thomas get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
500 8505ab66 2023-07-10 thomas {
501 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
502 8505ab66 2023-07-10 thomas char *uuidstr = NULL;
503 8505ab66 2023-07-10 thomas
504 8505ab66 2023-07-10 thomas *refname = NULL;
505 8505ab66 2023-07-10 thomas
506 8505ab66 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
507 8505ab66 2023-07-10 thomas if (err)
508 8505ab66 2023-07-10 thomas return err;
509 8505ab66 2023-07-10 thomas
510 8505ab66 2023-07-10 thomas if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
511 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
512 8505ab66 2023-07-10 thomas *refname = NULL;
513 8505ab66 2023-07-10 thomas }
514 8505ab66 2023-07-10 thomas free(uuidstr);
515 8505ab66 2023-07-10 thomas return err;
516 8505ab66 2023-07-10 thomas }
517 8505ab66 2023-07-10 thomas
518 8505ab66 2023-07-10 thomas static const struct got_error *
519 8505ab66 2023-07-10 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
520 8505ab66 2023-07-10 thomas {
521 8505ab66 2023-07-10 thomas return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
522 8505ab66 2023-07-10 thomas }
523 8505ab66 2023-07-10 thomas
524 8505ab66 2023-07-10 thomas /*
525 8505ab66 2023-07-10 thomas * Prevent Git's garbage collector from deleting our base commit by
526 8505ab66 2023-07-10 thomas * setting a reference to our base commit's ID.
527 8505ab66 2023-07-10 thomas */
528 8505ab66 2023-07-10 thomas static const struct got_error *
529 8505ab66 2023-07-10 thomas ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
530 8505ab66 2023-07-10 thomas {
531 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
532 8505ab66 2023-07-10 thomas struct got_reference *ref = NULL;
533 8505ab66 2023-07-10 thomas char *refname;
534 8505ab66 2023-07-10 thomas
535 8505ab66 2023-07-10 thomas err = get_base_ref_name(&refname, worktree);
536 8505ab66 2023-07-10 thomas if (err)
537 8505ab66 2023-07-10 thomas return err;
538 8505ab66 2023-07-10 thomas
539 8505ab66 2023-07-10 thomas err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
540 8505ab66 2023-07-10 thomas if (err)
541 8505ab66 2023-07-10 thomas goto done;
542 8505ab66 2023-07-10 thomas
543 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
544 8505ab66 2023-07-10 thomas done:
545 8505ab66 2023-07-10 thomas free(refname);
546 8505ab66 2023-07-10 thomas if (ref)
547 8505ab66 2023-07-10 thomas got_ref_close(ref);
548 8505ab66 2023-07-10 thomas return err;
549 8505ab66 2023-07-10 thomas }
550 8505ab66 2023-07-10 thomas
551 8505ab66 2023-07-10 thomas static const struct got_error *
552 8505ab66 2023-07-10 thomas get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
553 8505ab66 2023-07-10 thomas {
554 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
555 8505ab66 2023-07-10 thomas
556 8505ab66 2023-07-10 thomas if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
557 8505ab66 2023-07-10 thomas GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
558 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
559 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
560 8505ab66 2023-07-10 thomas }
561 8505ab66 2023-07-10 thomas return err;
562 8505ab66 2023-07-10 thomas }
563 8505ab66 2023-07-10 thomas
564 8505ab66 2023-07-10 thomas static const struct got_error *
565 8505ab66 2023-07-10 thomas open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
566 8505ab66 2023-07-10 thomas struct got_worktree *worktree)
567 8505ab66 2023-07-10 thomas {
568 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
569 8505ab66 2023-07-10 thomas FILE *index = NULL;
570 8505ab66 2023-07-10 thomas
571 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
572 8505ab66 2023-07-10 thomas *fileindex = got_fileindex_alloc();
573 8505ab66 2023-07-10 thomas if (*fileindex == NULL)
574 8505ab66 2023-07-10 thomas return got_error_from_errno("got_fileindex_alloc");
575 8505ab66 2023-07-10 thomas
576 8505ab66 2023-07-10 thomas err = get_fileindex_path(fileindex_path, worktree);
577 8505ab66 2023-07-10 thomas if (err)
578 8505ab66 2023-07-10 thomas goto done;
579 8505ab66 2023-07-10 thomas
580 8505ab66 2023-07-10 thomas index = fopen(*fileindex_path, "rbe");
581 8505ab66 2023-07-10 thomas if (index == NULL) {
582 8505ab66 2023-07-10 thomas if (errno != ENOENT)
583 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fopen", *fileindex_path);
584 8505ab66 2023-07-10 thomas } else {
585 8505ab66 2023-07-10 thomas err = got_fileindex_read(*fileindex, index);
586 8505ab66 2023-07-10 thomas if (fclose(index) == EOF && err == NULL)
587 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
588 8505ab66 2023-07-10 thomas }
589 8505ab66 2023-07-10 thomas done:
590 8505ab66 2023-07-10 thomas if (err) {
591 8505ab66 2023-07-10 thomas free(*fileindex_path);
592 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
593 8505ab66 2023-07-10 thomas got_fileindex_free(*fileindex);
594 8505ab66 2023-07-10 thomas *fileindex = NULL;
595 8505ab66 2023-07-10 thomas }
596 8505ab66 2023-07-10 thomas return err;
597 8505ab66 2023-07-10 thomas }
598 8505ab66 2023-07-10 thomas
599 8505ab66 2023-07-10 thomas static const struct got_error *
600 8505ab66 2023-07-10 thomas sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
601 8505ab66 2023-07-10 thomas {
602 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
603 8505ab66 2023-07-10 thomas char *new_fileindex_path = NULL;
604 8505ab66 2023-07-10 thomas FILE *new_index = NULL;
605 8505ab66 2023-07-10 thomas struct timespec timeout;
606 8505ab66 2023-07-10 thomas
607 8505ab66 2023-07-10 thomas err = got_opentemp_named(&new_fileindex_path, &new_index,
608 8505ab66 2023-07-10 thomas fileindex_path, "");
609 8505ab66 2023-07-10 thomas if (err)
610 8505ab66 2023-07-10 thomas goto done;
611 8505ab66 2023-07-10 thomas
612 8505ab66 2023-07-10 thomas err = got_fileindex_write(fileindex, new_index);
613 8505ab66 2023-07-10 thomas if (err)
614 8505ab66 2023-07-10 thomas goto done;
615 8505ab66 2023-07-10 thomas
616 8505ab66 2023-07-10 thomas if (rename(new_fileindex_path, fileindex_path) != 0) {
617 8505ab66 2023-07-10 thomas err = got_error_from_errno3("rename", new_fileindex_path,
618 8505ab66 2023-07-10 thomas fileindex_path);
619 8505ab66 2023-07-10 thomas unlink(new_fileindex_path);
620 8505ab66 2023-07-10 thomas }
621 8505ab66 2023-07-10 thomas
622 8505ab66 2023-07-10 thomas /*
623 8505ab66 2023-07-10 thomas * Sleep for a short amount of time to ensure that files modified after
624 8505ab66 2023-07-10 thomas * this program exits have a different time stamp from the one which
625 8505ab66 2023-07-10 thomas * was recorded in the file index.
626 8505ab66 2023-07-10 thomas */
627 8505ab66 2023-07-10 thomas timeout.tv_sec = 0;
628 8505ab66 2023-07-10 thomas timeout.tv_nsec = 1;
629 8505ab66 2023-07-10 thomas nanosleep(&timeout, NULL);
630 8505ab66 2023-07-10 thomas done:
631 8505ab66 2023-07-10 thomas if (new_index)
632 8505ab66 2023-07-10 thomas fclose(new_index);
633 8505ab66 2023-07-10 thomas free(new_fileindex_path);
634 8505ab66 2023-07-10 thomas return err;
635 8505ab66 2023-07-10 thomas }
636 8505ab66 2023-07-10 thomas
637 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg {
638 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex;
639 8505ab66 2023-07-10 thomas struct got_worktree *worktree;
640 8505ab66 2023-07-10 thomas const char *status_path;
641 8505ab66 2023-07-10 thomas size_t status_path_len;
642 8505ab66 2023-07-10 thomas struct got_repository *repo;
643 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb;
644 8505ab66 2023-07-10 thomas void *status_arg;
645 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb;
646 8505ab66 2023-07-10 thomas void *cancel_arg;
647 8505ab66 2023-07-10 thomas /* A pathlist containing per-directory pathlists of ignore patterns. */
648 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores;
649 8505ab66 2023-07-10 thomas int report_unchanged;
650 8505ab66 2023-07-10 thomas int no_ignores;
651 8505ab66 2023-07-10 thomas };
652 8505ab66 2023-07-10 thomas
653 8505ab66 2023-07-10 thomas static const struct got_error *
654 8505ab66 2023-07-10 thomas report_file_status(struct got_fileindex_entry *ie, const char *abspath,
655 8505ab66 2023-07-10 thomas int dirfd, const char *de_name,
656 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
657 8505ab66 2023-07-10 thomas struct got_repository *repo, int report_unchanged)
658 8505ab66 2023-07-10 thomas {
659 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
660 8505ab66 2023-07-10 thomas unsigned char status = GOT_STATUS_NO_CHANGE;
661 8505ab66 2023-07-10 thomas unsigned char staged_status;
662 8505ab66 2023-07-10 thomas struct stat sb;
663 8505ab66 2023-07-10 thomas struct got_object_id blob_id, commit_id, staged_blob_id;
664 8505ab66 2023-07-10 thomas struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
665 8505ab66 2023-07-10 thomas struct got_object_id *staged_blob_idp = NULL;
666 8505ab66 2023-07-10 thomas
667 8505ab66 2023-07-10 thomas staged_status = get_staged_status(ie);
668 8505ab66 2023-07-10 thomas err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
669 8505ab66 2023-07-10 thomas if (err)
670 8505ab66 2023-07-10 thomas return err;
671 8505ab66 2023-07-10 thomas
672 8505ab66 2023-07-10 thomas if (status == GOT_STATUS_NO_CHANGE &&
673 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
674 8505ab66 2023-07-10 thomas return NULL;
675 8505ab66 2023-07-10 thomas
676 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_blob(ie))
677 8505ab66 2023-07-10 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
678 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_commit(ie))
679 8505ab66 2023-07-10 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
680 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
681 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
682 8505ab66 2023-07-10 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
683 8505ab66 2023-07-10 thomas &staged_blob_id, ie);
684 8505ab66 2023-07-10 thomas }
685 8505ab66 2023-07-10 thomas
686 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, status, staged_status,
687 8505ab66 2023-07-10 thomas ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
688 8505ab66 2023-07-10 thomas }
689 8505ab66 2023-07-10 thomas
690 8505ab66 2023-07-10 thomas static const struct got_error *
691 8505ab66 2023-07-10 thomas status_old_new(void *arg, struct got_fileindex_entry *ie,
692 8505ab66 2023-07-10 thomas struct dirent *de, const char *parent_path, int dirfd)
693 8505ab66 2023-07-10 thomas {
694 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
695 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
696 8505ab66 2023-07-10 thomas char *abspath;
697 8505ab66 2023-07-10 thomas
698 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
699 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
700 8505ab66 2023-07-10 thomas
701 8505ab66 2023-07-10 thomas if (got_path_cmp(parent_path, a->status_path,
702 8505ab66 2023-07-10 thomas strlen(parent_path), a->status_path_len) != 0 &&
703 8505ab66 2023-07-10 thomas !got_path_is_child(parent_path, a->status_path, a->status_path_len))
704 8505ab66 2023-07-10 thomas return NULL;
705 8505ab66 2023-07-10 thomas
706 8505ab66 2023-07-10 thomas if (parent_path[0]) {
707 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
708 8505ab66 2023-07-10 thomas parent_path, de->d_name) == -1)
709 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
710 8505ab66 2023-07-10 thomas } else {
711 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
712 8505ab66 2023-07-10 thomas de->d_name) == -1)
713 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
714 8505ab66 2023-07-10 thomas }
715 8505ab66 2023-07-10 thomas
716 8505ab66 2023-07-10 thomas err = report_file_status(ie, abspath, dirfd, de->d_name,
717 8505ab66 2023-07-10 thomas a->status_cb, a->status_arg, a->repo, a->report_unchanged);
718 8505ab66 2023-07-10 thomas free(abspath);
719 8505ab66 2023-07-10 thomas return err;
720 8505ab66 2023-07-10 thomas }
721 8505ab66 2023-07-10 thomas
722 8505ab66 2023-07-10 thomas static const struct got_error *
723 8505ab66 2023-07-10 thomas status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
724 8505ab66 2023-07-10 thomas {
725 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
726 8505ab66 2023-07-10 thomas struct got_object_id blob_id, commit_id;
727 8505ab66 2023-07-10 thomas unsigned char status;
728 8505ab66 2023-07-10 thomas
729 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
730 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
731 8505ab66 2023-07-10 thomas
732 8505ab66 2023-07-10 thomas if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
733 8505ab66 2023-07-10 thomas return NULL;
734 8505ab66 2023-07-10 thomas
735 8505ab66 2023-07-10 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
736 8505ab66 2023-07-10 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
737 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
738 8505ab66 2023-07-10 thomas status = GOT_STATUS_MISSING;
739 8505ab66 2023-07-10 thomas else
740 8505ab66 2023-07-10 thomas status = GOT_STATUS_DELETE;
741 8505ab66 2023-07-10 thomas return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
742 8505ab66 2023-07-10 thomas ie->path, &blob_id, NULL, &commit_id, -1, NULL);
743 8505ab66 2023-07-10 thomas }
744 8505ab66 2023-07-10 thomas
745 8505ab66 2023-07-10 thomas static void
746 8505ab66 2023-07-10 thomas free_ignores(struct got_pathlist_head *ignores)
747 8505ab66 2023-07-10 thomas {
748 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
749 8505ab66 2023-07-10 thomas
750 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
751 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
752 8505ab66 2023-07-10 thomas
753 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
754 8505ab66 2023-07-10 thomas }
755 8505ab66 2023-07-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
756 8505ab66 2023-07-10 thomas }
757 8505ab66 2023-07-10 thomas
758 8505ab66 2023-07-10 thomas static const struct got_error *
759 8505ab66 2023-07-10 thomas read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
760 8505ab66 2023-07-10 thomas {
761 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
762 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe = NULL;
763 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist;
764 8505ab66 2023-07-10 thomas char *line = NULL, *pattern, *dirpath = NULL;
765 8505ab66 2023-07-10 thomas size_t linesize = 0;
766 8505ab66 2023-07-10 thomas ssize_t linelen;
767 8505ab66 2023-07-10 thomas
768 8505ab66 2023-07-10 thomas ignorelist = calloc(1, sizeof(*ignorelist));
769 8505ab66 2023-07-10 thomas if (ignorelist == NULL)
770 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
771 8505ab66 2023-07-10 thomas TAILQ_INIT(ignorelist);
772 8505ab66 2023-07-10 thomas
773 8505ab66 2023-07-10 thomas while ((linelen = getline(&line, &linesize, f)) != -1) {
774 8505ab66 2023-07-10 thomas if (linelen > 0 && line[linelen - 1] == '\n')
775 8505ab66 2023-07-10 thomas line[linelen - 1] = '\0';
776 8505ab66 2023-07-10 thomas
777 8505ab66 2023-07-10 thomas /* Git's ignores may contain comments. */
778 8505ab66 2023-07-10 thomas if (line[0] == '#')
779 8505ab66 2023-07-10 thomas continue;
780 8505ab66 2023-07-10 thomas
781 8505ab66 2023-07-10 thomas /* Git's negated patterns are not (yet?) supported. */
782 8505ab66 2023-07-10 thomas if (line[0] == '!')
783 8505ab66 2023-07-10 thomas continue;
784 8505ab66 2023-07-10 thomas
785 8505ab66 2023-07-10 thomas if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
786 8505ab66 2023-07-10 thomas line) == -1) {
787 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
788 8505ab66 2023-07-10 thomas goto done;
789 8505ab66 2023-07-10 thomas }
790 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
791 8505ab66 2023-07-10 thomas if (err)
792 8505ab66 2023-07-10 thomas goto done;
793 8505ab66 2023-07-10 thomas }
794 8505ab66 2023-07-10 thomas if (ferror(f)) {
795 8505ab66 2023-07-10 thomas err = got_error_from_errno("getline");
796 8505ab66 2023-07-10 thomas goto done;
797 8505ab66 2023-07-10 thomas }
798 8505ab66 2023-07-10 thomas
799 8505ab66 2023-07-10 thomas dirpath = strdup(path);
800 8505ab66 2023-07-10 thomas if (dirpath == NULL) {
801 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
802 8505ab66 2023-07-10 thomas goto done;
803 8505ab66 2023-07-10 thomas }
804 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
805 8505ab66 2023-07-10 thomas done:
806 8505ab66 2023-07-10 thomas free(line);
807 8505ab66 2023-07-10 thomas if (err || pe == NULL) {
808 8505ab66 2023-07-10 thomas free(dirpath);
809 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
810 8505ab66 2023-07-10 thomas }
811 8505ab66 2023-07-10 thomas return err;
812 8505ab66 2023-07-10 thomas }
813 8505ab66 2023-07-10 thomas
814 8505ab66 2023-07-10 thomas static int
815 8505ab66 2023-07-10 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
816 8505ab66 2023-07-10 thomas int flags)
817 8505ab66 2023-07-10 thomas {
818 8505ab66 2023-07-10 thomas char buf[PATH_MAX];
819 8505ab66 2023-07-10 thomas
820 8505ab66 2023-07-10 thomas /*
821 8505ab66 2023-07-10 thomas * Trailing slashes signify directories.
822 8505ab66 2023-07-10 thomas * Append a * to make such patterns conform to fnmatch rules.
823 8505ab66 2023-07-10 thomas */
824 8505ab66 2023-07-10 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
825 8505ab66 2023-07-10 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
826 8505ab66 2023-07-10 thomas return FNM_NOMATCH; /* XXX */
827 8505ab66 2023-07-10 thomas
828 8505ab66 2023-07-10 thomas return fnmatch(buf, path, flags);
829 8505ab66 2023-07-10 thomas }
830 8505ab66 2023-07-10 thomas
831 8505ab66 2023-07-10 thomas return fnmatch(pattern, path, flags);
832 8505ab66 2023-07-10 thomas }
833 8505ab66 2023-07-10 thomas
834 8505ab66 2023-07-10 thomas static int
835 8505ab66 2023-07-10 thomas match_ignores(struct got_pathlist_head *ignores, const char *path)
836 8505ab66 2023-07-10 thomas {
837 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
838 8505ab66 2023-07-10 thomas
839 8505ab66 2023-07-10 thomas /* Handle patterns which match in all directories. */
840 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
841 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
842 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
843 8505ab66 2023-07-10 thomas
844 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
845 8505ab66 2023-07-10 thomas const char *p;
846 8505ab66 2023-07-10 thomas
847 8505ab66 2023-07-10 thomas if (pi->path_len < 3 ||
848 8505ab66 2023-07-10 thomas strncmp(pi->path, "**/", 3) != 0)
849 8505ab66 2023-07-10 thomas continue;
850 8505ab66 2023-07-10 thomas p = path;
851 8505ab66 2023-07-10 thomas while (*p) {
852 8505ab66 2023-07-10 thomas if (match_path(pi->path + 3,
853 8505ab66 2023-07-10 thomas pi->path_len - 3, p,
854 8505ab66 2023-07-10 thomas FNM_PATHNAME | FNM_LEADING_DIR)) {
855 8505ab66 2023-07-10 thomas /* Retry in next directory. */
856 8505ab66 2023-07-10 thomas while (*p && *p != '/')
857 8505ab66 2023-07-10 thomas p++;
858 8505ab66 2023-07-10 thomas while (*p == '/')
859 8505ab66 2023-07-10 thomas p++;
860 8505ab66 2023-07-10 thomas continue;
861 8505ab66 2023-07-10 thomas }
862 8505ab66 2023-07-10 thomas return 1;
863 8505ab66 2023-07-10 thomas }
864 8505ab66 2023-07-10 thomas }
865 8505ab66 2023-07-10 thomas }
866 8505ab66 2023-07-10 thomas
867 8505ab66 2023-07-10 thomas /*
868 8505ab66 2023-07-10 thomas * The ignores pathlist contains ignore lists from children before
869 8505ab66 2023-07-10 thomas * parents, so we can find the most specific ignorelist by walking
870 8505ab66 2023-07-10 thomas * ignores backwards.
871 8505ab66 2023-07-10 thomas */
872 8505ab66 2023-07-10 thomas pe = TAILQ_LAST(ignores, got_pathlist_head);
873 8505ab66 2023-07-10 thomas while (pe) {
874 8505ab66 2023-07-10 thomas if (got_path_is_child(path, pe->path, pe->path_len)) {
875 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
876 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
877 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
878 8505ab66 2023-07-10 thomas int flags = FNM_LEADING_DIR;
879 8505ab66 2023-07-10 thomas if (strstr(pi->path, "/**/") == NULL)
880 8505ab66 2023-07-10 thomas flags |= FNM_PATHNAME;
881 8505ab66 2023-07-10 thomas if (match_path(pi->path, pi->path_len,
882 8505ab66 2023-07-10 thomas path, flags))
883 8505ab66 2023-07-10 thomas continue;
884 8505ab66 2023-07-10 thomas return 1;
885 8505ab66 2023-07-10 thomas }
886 8505ab66 2023-07-10 thomas }
887 8505ab66 2023-07-10 thomas pe = TAILQ_PREV(pe, got_pathlist_head, entry);
888 8505ab66 2023-07-10 thomas }
889 8505ab66 2023-07-10 thomas
890 8505ab66 2023-07-10 thomas return 0;
891 8505ab66 2023-07-10 thomas }
892 8505ab66 2023-07-10 thomas
893 8505ab66 2023-07-10 thomas static const struct got_error *
894 8505ab66 2023-07-10 thomas add_ignores(struct got_pathlist_head *ignores, const char *root_path,
895 8505ab66 2023-07-10 thomas const char *path, int dirfd, const char *ignores_filename)
896 8505ab66 2023-07-10 thomas {
897 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
898 8505ab66 2023-07-10 thomas char *ignorespath;
899 8505ab66 2023-07-10 thomas int fd = -1;
900 8505ab66 2023-07-10 thomas FILE *ignoresfile = NULL;
901 8505ab66 2023-07-10 thomas
902 8505ab66 2023-07-10 thomas if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
903 8505ab66 2023-07-10 thomas path[0] ? "/" : "", ignores_filename) == -1)
904 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
905 8505ab66 2023-07-10 thomas
906 8505ab66 2023-07-10 thomas if (dirfd != -1) {
907 8505ab66 2023-07-10 thomas fd = openat(dirfd, ignores_filename,
908 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
909 8505ab66 2023-07-10 thomas if (fd == -1) {
910 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
911 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
912 8505ab66 2023-07-10 thomas ignorespath);
913 8505ab66 2023-07-10 thomas } else {
914 8505ab66 2023-07-10 thomas ignoresfile = fdopen(fd, "r");
915 8505ab66 2023-07-10 thomas if (ignoresfile == NULL)
916 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen",
917 8505ab66 2023-07-10 thomas ignorespath);
918 8505ab66 2023-07-10 thomas else {
919 8505ab66 2023-07-10 thomas fd = -1;
920 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
921 8505ab66 2023-07-10 thomas }
922 8505ab66 2023-07-10 thomas }
923 8505ab66 2023-07-10 thomas } else {
924 8505ab66 2023-07-10 thomas ignoresfile = fopen(ignorespath, "re");
925 8505ab66 2023-07-10 thomas if (ignoresfile == NULL) {
926 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
927 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fopen",
928 8505ab66 2023-07-10 thomas ignorespath);
929 8505ab66 2023-07-10 thomas } else
930 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
931 8505ab66 2023-07-10 thomas }
932 8505ab66 2023-07-10 thomas
933 8505ab66 2023-07-10 thomas if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
934 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
935 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
936 8505ab66 2023-07-10 thomas err = got_error_from_errno2("close", path);
937 8505ab66 2023-07-10 thomas free(ignorespath);
938 8505ab66 2023-07-10 thomas return err;
939 8505ab66 2023-07-10 thomas }
940 8505ab66 2023-07-10 thomas
941 8505ab66 2023-07-10 thomas static const struct got_error *
942 8505ab66 2023-07-10 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
943 8505ab66 2023-07-10 thomas int dirfd)
944 8505ab66 2023-07-10 thomas {
945 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
946 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
947 8505ab66 2023-07-10 thomas char *path = NULL;
948 8505ab66 2023-07-10 thomas
949 8505ab66 2023-07-10 thomas if (ignore != NULL)
950 8505ab66 2023-07-10 thomas *ignore = 0;
951 8505ab66 2023-07-10 thomas
952 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
953 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
954 8505ab66 2023-07-10 thomas
955 8505ab66 2023-07-10 thomas if (parent_path[0]) {
956 8505ab66 2023-07-10 thomas if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
957 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
958 8505ab66 2023-07-10 thomas } else {
959 8505ab66 2023-07-10 thomas path = de->d_name;
960 8505ab66 2023-07-10 thomas }
961 8505ab66 2023-07-10 thomas
962 8505ab66 2023-07-10 thomas if (de->d_type == DT_DIR) {
963 8505ab66 2023-07-10 thomas if (!a->no_ignores && ignore != NULL &&
964 8505ab66 2023-07-10 thomas match_ignores(a->ignores, path))
965 8505ab66 2023-07-10 thomas *ignore = 1;
966 8505ab66 2023-07-10 thomas } else if (!match_ignores(a->ignores, path) &&
967 8505ab66 2023-07-10 thomas got_path_is_child(path, a->status_path, a->status_path_len))
968 8505ab66 2023-07-10 thomas err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
969 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
970 8505ab66 2023-07-10 thomas if (parent_path[0])
971 8505ab66 2023-07-10 thomas free(path);
972 8505ab66 2023-07-10 thomas return err;
973 8505ab66 2023-07-10 thomas }
974 8505ab66 2023-07-10 thomas
975 8505ab66 2023-07-10 thomas static const struct got_error *
976 8505ab66 2023-07-10 thomas status_traverse(void *arg, const char *path, int dirfd)
977 8505ab66 2023-07-10 thomas {
978 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
979 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
980 8505ab66 2023-07-10 thomas
981 8505ab66 2023-07-10 thomas if (a->no_ignores)
982 8505ab66 2023-07-10 thomas return NULL;
983 8505ab66 2023-07-10 thomas
984 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path,
985 8505ab66 2023-07-10 thomas path, dirfd, ".cvsignore");
986 8505ab66 2023-07-10 thomas if (err)
987 8505ab66 2023-07-10 thomas return err;
988 8505ab66 2023-07-10 thomas
989 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path, path,
990 8505ab66 2023-07-10 thomas dirfd, ".gitignore");
991 8505ab66 2023-07-10 thomas
992 8505ab66 2023-07-10 thomas return err;
993 8505ab66 2023-07-10 thomas }
994 8505ab66 2023-07-10 thomas
995 8505ab66 2023-07-10 thomas static const struct got_error *
996 8505ab66 2023-07-10 thomas report_single_file_status(const char *path, const char *ondisk_path,
997 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
998 8505ab66 2023-07-10 thomas void *status_arg, struct got_repository *repo, int report_unchanged,
999 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores)
1000 8505ab66 2023-07-10 thomas {
1001 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1002 8505ab66 2023-07-10 thomas struct stat sb;
1003 8505ab66 2023-07-10 thomas
1004 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1005 8505ab66 2023-07-10 thomas if (ie)
1006 8505ab66 2023-07-10 thomas return report_file_status(ie, ondisk_path, -1, NULL,
1007 8505ab66 2023-07-10 thomas status_cb, status_arg, repo, report_unchanged);
1008 8505ab66 2023-07-10 thomas
1009 8505ab66 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
1010 8505ab66 2023-07-10 thomas if (errno != ENOENT)
1011 8505ab66 2023-07-10 thomas return got_error_from_errno2("lstat", ondisk_path);
1012 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1013 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1014 8505ab66 2023-07-10 thomas }
1015 8505ab66 2023-07-10 thomas
1016 8505ab66 2023-07-10 thomas if (!no_ignores && match_ignores(ignores, path))
1017 8505ab66 2023-07-10 thomas return NULL;
1018 8505ab66 2023-07-10 thomas
1019 8505ab66 2023-07-10 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1020 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1021 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1022 8505ab66 2023-07-10 thomas
1023 8505ab66 2023-07-10 thomas return NULL;
1024 8505ab66 2023-07-10 thomas }
1025 8505ab66 2023-07-10 thomas
1026 8505ab66 2023-07-10 thomas static const struct got_error *
1027 8505ab66 2023-07-10 thomas add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1028 8505ab66 2023-07-10 thomas const char *root_path, const char *path)
1029 8505ab66 2023-07-10 thomas {
1030 8505ab66 2023-07-10 thomas const struct got_error *err;
1031 8505ab66 2023-07-10 thomas char *parent_path, *next_parent_path = NULL;
1032 8505ab66 2023-07-10 thomas
1033 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1034 8505ab66 2023-07-10 thomas ".cvsignore");
1035 8505ab66 2023-07-10 thomas if (err)
1036 8505ab66 2023-07-10 thomas return err;
1037 8505ab66 2023-07-10 thomas
1038 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1039 8505ab66 2023-07-10 thomas ".gitignore");
1040 8505ab66 2023-07-10 thomas if (err)
1041 8505ab66 2023-07-10 thomas return err;
1042 8505ab66 2023-07-10 thomas
1043 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1044 8505ab66 2023-07-10 thomas if (err) {
1045 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1046 8505ab66 2023-07-10 thomas return NULL; /* cannot traverse parent */
1047 8505ab66 2023-07-10 thomas return err;
1048 8505ab66 2023-07-10 thomas }
1049 8505ab66 2023-07-10 thomas for (;;) {
1050 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1051 8505ab66 2023-07-10 thomas ".cvsignore");
1052 8505ab66 2023-07-10 thomas if (err)
1053 8505ab66 2023-07-10 thomas break;
1054 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1055 8505ab66 2023-07-10 thomas ".gitignore");
1056 8505ab66 2023-07-10 thomas if (err)
1057 8505ab66 2023-07-10 thomas break;
1058 8505ab66 2023-07-10 thomas err = got_path_dirname(&next_parent_path, parent_path);
1059 8505ab66 2023-07-10 thomas if (err) {
1060 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1061 8505ab66 2023-07-10 thomas err = NULL; /* traversed everything */
1062 8505ab66 2023-07-10 thomas break;
1063 8505ab66 2023-07-10 thomas }
1064 8505ab66 2023-07-10 thomas if (got_path_is_root_dir(parent_path))
1065 8505ab66 2023-07-10 thomas break;
1066 8505ab66 2023-07-10 thomas free(parent_path);
1067 8505ab66 2023-07-10 thomas parent_path = next_parent_path;
1068 8505ab66 2023-07-10 thomas next_parent_path = NULL;
1069 8505ab66 2023-07-10 thomas }
1070 8505ab66 2023-07-10 thomas
1071 8505ab66 2023-07-10 thomas free(parent_path);
1072 8505ab66 2023-07-10 thomas free(next_parent_path);
1073 8505ab66 2023-07-10 thomas return err;
1074 8505ab66 2023-07-10 thomas }
1075 8505ab66 2023-07-10 thomas
1076 8505ab66 2023-07-10 thomas struct find_missing_children_args {
1077 8505ab66 2023-07-10 thomas const char *parent_path;
1078 8505ab66 2023-07-10 thomas size_t parent_len;
1079 8505ab66 2023-07-10 thomas struct got_pathlist_head *children;
1080 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb;
1081 8505ab66 2023-07-10 thomas void *cancel_arg;
1082 8505ab66 2023-07-10 thomas };
1083 8505ab66 2023-07-10 thomas
1084 8505ab66 2023-07-10 thomas static const struct got_error *
1085 8505ab66 2023-07-10 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
1086 8505ab66 2023-07-10 thomas {
1087 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1088 8505ab66 2023-07-10 thomas struct find_missing_children_args *a = arg;
1089 8505ab66 2023-07-10 thomas
1090 8505ab66 2023-07-10 thomas if (a->cancel_cb) {
1091 8505ab66 2023-07-10 thomas err = a->cancel_cb(a->cancel_arg);
1092 8505ab66 2023-07-10 thomas if (err)
1093 8505ab66 2023-07-10 thomas return err;
1094 8505ab66 2023-07-10 thomas }
1095 8505ab66 2023-07-10 thomas
1096 8505ab66 2023-07-10 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1097 8505ab66 2023-07-10 thomas err = got_pathlist_append(a->children, ie->path, NULL);
1098 8505ab66 2023-07-10 thomas
1099 8505ab66 2023-07-10 thomas return err;
1100 8505ab66 2023-07-10 thomas }
1101 8505ab66 2023-07-10 thomas
1102 8505ab66 2023-07-10 thomas static const struct got_error *
1103 8505ab66 2023-07-10 thomas report_children(struct got_pathlist_head *children,
1104 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
1105 8505ab66 2023-07-10 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
1106 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores,
1107 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1108 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg)
1109 8505ab66 2023-07-10 thomas {
1110 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1111 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1112 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1113 8505ab66 2023-07-10 thomas
1114 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, children, entry) {
1115 8505ab66 2023-07-10 thomas if (cancel_cb) {
1116 8505ab66 2023-07-10 thomas err = cancel_cb(cancel_arg);
1117 8505ab66 2023-07-10 thomas if (err)
1118 8505ab66 2023-07-10 thomas break;
1119 8505ab66 2023-07-10 thomas }
1120 8505ab66 2023-07-10 thomas
1121 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1122 8505ab66 2023-07-10 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
1123 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1124 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1125 8505ab66 2023-07-10 thomas break;
1126 8505ab66 2023-07-10 thomas }
1127 8505ab66 2023-07-10 thomas
1128 8505ab66 2023-07-10 thomas err = report_single_file_status(pe->path, ondisk_path,
1129 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
1130 8505ab66 2023-07-10 thomas ignores, no_ignores);
1131 8505ab66 2023-07-10 thomas if (err)
1132 8505ab66 2023-07-10 thomas break;
1133 8505ab66 2023-07-10 thomas
1134 8505ab66 2023-07-10 thomas free(ondisk_path);
1135 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1136 8505ab66 2023-07-10 thomas }
1137 8505ab66 2023-07-10 thomas
1138 8505ab66 2023-07-10 thomas free(ondisk_path);
1139 8505ab66 2023-07-10 thomas return err;
1140 8505ab66 2023-07-10 thomas }
1141 8505ab66 2023-07-10 thomas
1142 8505ab66 2023-07-10 thomas static const struct got_error *
1143 8505ab66 2023-07-10 thomas worktree_status(struct got_worktree *worktree, const char *path,
1144 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, struct got_repository *repo,
1145 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1146 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1147 8505ab66 2023-07-10 thomas int report_unchanged)
1148 8505ab66 2023-07-10 thomas {
1149 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1150 8505ab66 2023-07-10 thomas int fd = -1;
1151 8505ab66 2023-07-10 thomas struct got_fileindex_diff_dir_cb fdiff_cb;
1152 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg arg;
1153 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1154 8505ab66 2023-07-10 thomas struct got_pathlist_head ignores, missing_children;
1155 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1156 8505ab66 2023-07-10 thomas
1157 8505ab66 2023-07-10 thomas TAILQ_INIT(&ignores);
1158 8505ab66 2023-07-10 thomas TAILQ_INIT(&missing_children);
1159 8505ab66 2023-07-10 thomas
1160 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s",
1161 8505ab66 2023-07-10 thomas worktree->root_path, path[0] ? "/" : "", path) == -1)
1162 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1163 8505ab66 2023-07-10 thomas
1164 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1165 8505ab66 2023-07-10 thomas if (ie) {
1166 8505ab66 2023-07-10 thomas err = report_single_file_status(path, ondisk_path,
1167 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo,
1168 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1169 8505ab66 2023-07-10 thomas goto done;
1170 8505ab66 2023-07-10 thomas } else {
1171 8505ab66 2023-07-10 thomas struct find_missing_children_args fmca;
1172 8505ab66 2023-07-10 thomas fmca.parent_path = path;
1173 8505ab66 2023-07-10 thomas fmca.parent_len = strlen(path);
1174 8505ab66 2023-07-10 thomas fmca.children = &missing_children;
1175 8505ab66 2023-07-10 thomas fmca.cancel_cb = cancel_cb;
1176 8505ab66 2023-07-10 thomas fmca.cancel_arg = cancel_arg;
1177 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex,
1178 8505ab66 2023-07-10 thomas find_missing_children, &fmca);
1179 8505ab66 2023-07-10 thomas if (err)
1180 8505ab66 2023-07-10 thomas goto done;
1181 8505ab66 2023-07-10 thomas }
1182 8505ab66 2023-07-10 thomas
1183 8505ab66 2023-07-10 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1184 8505ab66 2023-07-10 thomas if (fd == -1) {
1185 8505ab66 2023-07-10 thomas if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1186 8505ab66 2023-07-10 thomas !got_err_open_nofollow_on_symlink())
1187 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open", ondisk_path);
1188 8505ab66 2023-07-10 thomas else {
1189 8505ab66 2023-07-10 thomas if (!no_ignores) {
1190 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1191 8505ab66 2023-07-10 thomas worktree->root_path, ondisk_path);
1192 8505ab66 2023-07-10 thomas if (err)
1193 8505ab66 2023-07-10 thomas goto done;
1194 8505ab66 2023-07-10 thomas }
1195 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&missing_children)) {
1196 8505ab66 2023-07-10 thomas err = report_single_file_status(path,
1197 8505ab66 2023-07-10 thomas ondisk_path, fileindex,
1198 8505ab66 2023-07-10 thomas status_cb, status_arg, repo,
1199 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1200 8505ab66 2023-07-10 thomas if (err)
1201 8505ab66 2023-07-10 thomas goto done;
1202 8505ab66 2023-07-10 thomas } else {
1203 8505ab66 2023-07-10 thomas err = report_children(&missing_children,
1204 8505ab66 2023-07-10 thomas worktree, fileindex, repo,
1205 8505ab66 2023-07-10 thomas (path[0] == '\0'), report_unchanged,
1206 8505ab66 2023-07-10 thomas &ignores, no_ignores,
1207 8505ab66 2023-07-10 thomas status_cb, status_arg,
1208 8505ab66 2023-07-10 thomas cancel_cb, cancel_arg);
1209 8505ab66 2023-07-10 thomas if (err)
1210 8505ab66 2023-07-10 thomas goto done;
1211 8505ab66 2023-07-10 thomas }
1212 8505ab66 2023-07-10 thomas }
1213 8505ab66 2023-07-10 thomas } else {
1214 8505ab66 2023-07-10 thomas fdiff_cb.diff_old_new = status_old_new;
1215 8505ab66 2023-07-10 thomas fdiff_cb.diff_old = status_old;
1216 8505ab66 2023-07-10 thomas fdiff_cb.diff_new = status_new;
1217 8505ab66 2023-07-10 thomas fdiff_cb.diff_traverse = status_traverse;
1218 8505ab66 2023-07-10 thomas arg.fileindex = fileindex;
1219 8505ab66 2023-07-10 thomas arg.worktree = worktree;
1220 8505ab66 2023-07-10 thomas arg.status_path = path;
1221 8505ab66 2023-07-10 thomas arg.status_path_len = strlen(path);
1222 8505ab66 2023-07-10 thomas arg.repo = repo;
1223 8505ab66 2023-07-10 thomas arg.status_cb = status_cb;
1224 8505ab66 2023-07-10 thomas arg.status_arg = status_arg;
1225 8505ab66 2023-07-10 thomas arg.cancel_cb = cancel_cb;
1226 8505ab66 2023-07-10 thomas arg.cancel_arg = cancel_arg;
1227 8505ab66 2023-07-10 thomas arg.report_unchanged = report_unchanged;
1228 8505ab66 2023-07-10 thomas arg.no_ignores = no_ignores;
1229 8505ab66 2023-07-10 thomas if (!no_ignores) {
1230 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1231 8505ab66 2023-07-10 thomas worktree->root_path, path);
1232 8505ab66 2023-07-10 thomas if (err)
1233 8505ab66 2023-07-10 thomas goto done;
1234 8505ab66 2023-07-10 thomas }
1235 8505ab66 2023-07-10 thomas arg.ignores = &ignores;
1236 8505ab66 2023-07-10 thomas err = got_fileindex_diff_dir(fileindex, fd,
1237 8505ab66 2023-07-10 thomas worktree->root_path, path, repo, &fdiff_cb, &arg);
1238 8505ab66 2023-07-10 thomas }
1239 8505ab66 2023-07-10 thomas done:
1240 8505ab66 2023-07-10 thomas free_ignores(&ignores);
1241 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1242 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1243 8505ab66 2023-07-10 thomas free(ondisk_path);
1244 8505ab66 2023-07-10 thomas return err;
1245 8505ab66 2023-07-10 thomas }
1246 8505ab66 2023-07-10 thomas
1247 8505ab66 2023-07-10 thomas static void
1248 8505ab66 2023-07-10 thomas free_commitable(struct got_commitable *ct)
1249 8505ab66 2023-07-10 thomas {
1250 8505ab66 2023-07-10 thomas free(ct->path);
1251 8505ab66 2023-07-10 thomas free(ct->in_repo_path);
1252 8505ab66 2023-07-10 thomas free(ct->ondisk_path);
1253 8505ab66 2023-07-10 thomas free(ct->blob_id);
1254 8505ab66 2023-07-10 thomas free(ct->base_blob_id);
1255 8505ab66 2023-07-10 thomas free(ct->staged_blob_id);
1256 8505ab66 2023-07-10 thomas free(ct->base_commit_id);
1257 8505ab66 2023-07-10 thomas free(ct);
1258 8505ab66 2023-07-10 thomas }
1259 8505ab66 2023-07-10 thomas
1260 8505ab66 2023-07-10 thomas struct collect_commitables_arg {
1261 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths;
1262 8505ab66 2023-07-10 thomas struct got_repository *repo;
1263 8505ab66 2023-07-10 thomas struct got_worktree *worktree;
1264 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex;
1265 8505ab66 2023-07-10 thomas int have_staged_files;
1266 8505ab66 2023-07-10 thomas int allow_bad_symlinks;
1267 8505ab66 2023-07-10 thomas int diff_header_shown;
1268 8505ab66 2023-07-10 thomas int commit_conflicts;
1269 8505ab66 2023-07-10 thomas FILE *diff_outfile;
1270 8505ab66 2023-07-10 thomas FILE *f1;
1271 8505ab66 2023-07-10 thomas FILE *f2;
1272 8505ab66 2023-07-10 thomas };
1273 8505ab66 2023-07-10 thomas
1274 8505ab66 2023-07-10 thomas /*
1275 8505ab66 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
1276 8505ab66 2023-07-10 thomas * it as content to the diff engine.
1277 8505ab66 2023-07-10 thomas */
1278 8505ab66 2023-07-10 thomas static const struct got_error *
1279 8505ab66 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1280 8505ab66 2023-07-10 thomas const char *abspath)
1281 8505ab66 2023-07-10 thomas {
1282 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1283 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1284 8505ab66 2023-07-10 thomas ssize_t target_len, outlen;
1285 8505ab66 2023-07-10 thomas
1286 8505ab66 2023-07-10 thomas *fd = -1;
1287 8505ab66 2023-07-10 thomas
1288 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1289 8505ab66 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1290 8505ab66 2023-07-10 thomas if (target_len == -1)
1291 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
1292 8505ab66 2023-07-10 thomas } else {
1293 8505ab66 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
1294 8505ab66 2023-07-10 thomas if (target_len == -1)
1295 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
1296 8505ab66 2023-07-10 thomas }
1297 8505ab66 2023-07-10 thomas
1298 8505ab66 2023-07-10 thomas *fd = got_opentempfd();
1299 8505ab66 2023-07-10 thomas if (*fd == -1)
1300 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
1301 8505ab66 2023-07-10 thomas
1302 8505ab66 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
1303 8505ab66 2023-07-10 thomas if (outlen == -1) {
1304 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1305 8505ab66 2023-07-10 thomas goto done;
1306 8505ab66 2023-07-10 thomas }
1307 8505ab66 2023-07-10 thomas
1308 8505ab66 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
1309 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
1310 8505ab66 2023-07-10 thomas goto done;
1311 8505ab66 2023-07-10 thomas }
1312 8505ab66 2023-07-10 thomas done:
1313 8505ab66 2023-07-10 thomas if (err) {
1314 8505ab66 2023-07-10 thomas close(*fd);
1315 8505ab66 2023-07-10 thomas *fd = -1;
1316 8505ab66 2023-07-10 thomas }
1317 8505ab66 2023-07-10 thomas return err;
1318 8505ab66 2023-07-10 thomas }
1319 8505ab66 2023-07-10 thomas
1320 8505ab66 2023-07-10 thomas static const struct got_error *
1321 8505ab66 2023-07-10 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1322 8505ab66 2023-07-10 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1323 8505ab66 2023-07-10 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1324 8505ab66 2023-07-10 thomas {
1325 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1326 8505ab66 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
1327 8505ab66 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
1328 8505ab66 2023-07-10 thomas FILE *ondisk_file = NULL;
1329 8505ab66 2023-07-10 thomas char *label1 = NULL;
1330 8505ab66 2023-07-10 thomas struct stat sb;
1331 8505ab66 2023-07-10 thomas off_t size1 = 0;
1332 8505ab66 2023-07-10 thomas int f2_exists = 0;
1333 8505ab66 2023-07-10 thomas char *id_str = NULL;
1334 8505ab66 2023-07-10 thomas
1335 8505ab66 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
1336 8505ab66 2023-07-10 thomas
1337 8505ab66 2023-07-10 thomas if (diff_staged) {
1338 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1339 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD &&
1340 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1341 8505ab66 2023-07-10 thomas return NULL;
1342 8505ab66 2023-07-10 thomas } else {
1343 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1344 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_ADD &&
1345 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1346 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1347 8505ab66 2023-07-10 thomas return NULL;
1348 8505ab66 2023-07-10 thomas }
1349 8505ab66 2023-07-10 thomas
1350 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f1);
1351 8505ab66 2023-07-10 thomas if (err)
1352 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1353 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f2);
1354 8505ab66 2023-07-10 thomas if (err)
1355 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1356 8505ab66 2023-07-10 thomas
1357 8505ab66 2023-07-10 thomas if (!*diff_header_shown) {
1358 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
1359 8505ab66 2023-07-10 thomas if (err)
1360 8505ab66 2023-07-10 thomas return err;
1361 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1362 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree));
1363 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
1364 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "path + %s%s\n",
1365 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree),
1366 8505ab66 2023-07-10 thomas diff_staged ? " (staged changes)" : "");
1367 8505ab66 2023-07-10 thomas *diff_header_shown = 1;
1368 8505ab66 2023-07-10 thomas }
1369 8505ab66 2023-07-10 thomas
1370 8505ab66 2023-07-10 thomas if (diff_staged) {
1371 8505ab66 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
1372 8505ab66 2023-07-10 thomas switch (ct->staged_status) {
1373 8505ab66 2023-07-10 thomas case GOT_STATUS_MODIFY:
1374 8505ab66 2023-07-10 thomas label1 = ct->path;
1375 8505ab66 2023-07-10 thomas label2 = ct->path;
1376 8505ab66 2023-07-10 thomas break;
1377 8505ab66 2023-07-10 thomas case GOT_STATUS_ADD:
1378 8505ab66 2023-07-10 thomas label2 = ct->path;
1379 8505ab66 2023-07-10 thomas break;
1380 8505ab66 2023-07-10 thomas case GOT_STATUS_DELETE:
1381 8505ab66 2023-07-10 thomas label1 = ct->path;
1382 8505ab66 2023-07-10 thomas break;
1383 8505ab66 2023-07-10 thomas default:
1384 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
1385 8505ab66 2023-07-10 thomas }
1386 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1387 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1388 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1389 8505ab66 2023-07-10 thomas goto done;
1390 8505ab66 2023-07-10 thomas }
1391 8505ab66 2023-07-10 thomas fd2 = got_opentempfd();
1392 8505ab66 2023-07-10 thomas if (fd2 == -1) {
1393 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1394 8505ab66 2023-07-10 thomas goto done;
1395 8505ab66 2023-07-10 thomas }
1396 8505ab66 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1397 8505ab66 2023-07-10 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1398 8505ab66 2023-07-10 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1399 8505ab66 2023-07-10 thomas NULL, repo, diff_outfile);
1400 8505ab66 2023-07-10 thomas goto done;
1401 8505ab66 2023-07-10 thomas }
1402 8505ab66 2023-07-10 thomas
1403 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1404 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1405 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1406 8505ab66 2023-07-10 thomas goto done;
1407 8505ab66 2023-07-10 thomas }
1408 8505ab66 2023-07-10 thomas
1409 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD) {
1410 8505ab66 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1411 8505ab66 2023-07-10 thomas 8192, fd1);
1412 8505ab66 2023-07-10 thomas if (err)
1413 8505ab66 2023-07-10 thomas goto done;
1414 8505ab66 2023-07-10 thomas }
1415 8505ab66 2023-07-10 thomas
1416 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_DELETE) {
1417 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1418 8505ab66 2023-07-10 thomas fd = openat(dirfd, de_name,
1419 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1420 8505ab66 2023-07-10 thomas if (fd == -1) {
1421 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1422 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
1423 8505ab66 2023-07-10 thomas ct->ondisk_path);
1424 8505ab66 2023-07-10 thomas goto done;
1425 8505ab66 2023-07-10 thomas }
1426 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1427 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1428 8505ab66 2023-07-10 thomas if (err)
1429 8505ab66 2023-07-10 thomas goto done;
1430 8505ab66 2023-07-10 thomas }
1431 8505ab66 2023-07-10 thomas } else {
1432 8505ab66 2023-07-10 thomas fd = open(ct->ondisk_path,
1433 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1434 8505ab66 2023-07-10 thomas if (fd == -1) {
1435 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1436 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open",
1437 8505ab66 2023-07-10 thomas ct->ondisk_path);
1438 8505ab66 2023-07-10 thomas goto done;
1439 8505ab66 2023-07-10 thomas }
1440 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1441 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1442 8505ab66 2023-07-10 thomas if (err)
1443 8505ab66 2023-07-10 thomas goto done;
1444 8505ab66 2023-07-10 thomas }
1445 8505ab66 2023-07-10 thomas }
1446 8505ab66 2023-07-10 thomas if (fstatat(fd, ct->ondisk_path, &sb,
1447 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1448 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
1449 8505ab66 2023-07-10 thomas goto done;
1450 8505ab66 2023-07-10 thomas }
1451 8505ab66 2023-07-10 thomas ondisk_file = fdopen(fd, "r");
1452 8505ab66 2023-07-10 thomas if (ondisk_file == NULL) {
1453 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
1454 8505ab66 2023-07-10 thomas goto done;
1455 8505ab66 2023-07-10 thomas }
1456 8505ab66 2023-07-10 thomas fd = -1;
1457 8505ab66 2023-07-10 thomas f2_exists = 1;
1458 8505ab66 2023-07-10 thomas }
1459 8505ab66 2023-07-10 thomas
1460 8505ab66 2023-07-10 thomas if (blob1) {
1461 8505ab66 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1462 8505ab66 2023-07-10 thomas f1, blob1);
1463 8505ab66 2023-07-10 thomas if (err)
1464 8505ab66 2023-07-10 thomas goto done;
1465 8505ab66 2023-07-10 thomas }
1466 8505ab66 2023-07-10 thomas
1467 8505ab66 2023-07-10 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
1468 8505ab66 2023-07-10 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1469 8505ab66 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1470 8505ab66 2023-07-10 thomas done:
1471 8505ab66 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1472 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1473 8505ab66 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1474 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1475 8505ab66 2023-07-10 thomas if (blob1)
1476 8505ab66 2023-07-10 thomas got_object_blob_close(blob1);
1477 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1478 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1479 8505ab66 2023-07-10 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1480 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
1481 8505ab66 2023-07-10 thomas return err;
1482 8505ab66 2023-07-10 thomas }
1483 8505ab66 2023-07-10 thomas
1484 8505ab66 2023-07-10 thomas static const struct got_error *
1485 8505ab66 2023-07-10 thomas collect_commitables(void *arg, unsigned char status,
1486 8505ab66 2023-07-10 thomas unsigned char staged_status, const char *relpath,
1487 8505ab66 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1488 8505ab66 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
1489 8505ab66 2023-07-10 thomas {
1490 8505ab66 2023-07-10 thomas struct collect_commitables_arg *a = arg;
1491 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1492 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
1493 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new = NULL;
1494 8505ab66 2023-07-10 thomas char *parent_path = NULL, *path = NULL;
1495 8505ab66 2023-07-10 thomas struct stat sb;
1496 8505ab66 2023-07-10 thomas
1497 8505ab66 2023-07-10 thomas if (a->have_staged_files) {
1498 8505ab66 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
1499 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
1500 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
1501 8505ab66 2023-07-10 thomas return NULL;
1502 8505ab66 2023-07-10 thomas } else {
1503 8505ab66 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1504 8505ab66 2023-07-10 thomas printf("C %s\n", relpath);
1505 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_CONFLICT);
1506 8505ab66 2023-07-10 thomas }
1507 8505ab66 2023-07-10 thomas
1508 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
1509 8505ab66 2023-07-10 thomas status != GOT_STATUS_MODE_CHANGE &&
1510 8505ab66 2023-07-10 thomas status != GOT_STATUS_ADD &&
1511 8505ab66 2023-07-10 thomas status != GOT_STATUS_DELETE &&
1512 8505ab66 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
1513 8505ab66 2023-07-10 thomas return NULL;
1514 8505ab66 2023-07-10 thomas }
1515 8505ab66 2023-07-10 thomas
1516 8505ab66 2023-07-10 thomas if (asprintf(&path, "/%s", relpath) == -1) {
1517 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1518 8505ab66 2023-07-10 thomas goto done;
1519 8505ab66 2023-07-10 thomas }
1520 8505ab66 2023-07-10 thomas if (strcmp(path, "/") == 0) {
1521 8505ab66 2023-07-10 thomas parent_path = strdup("");
1522 8505ab66 2023-07-10 thomas if (parent_path == NULL)
1523 8505ab66 2023-07-10 thomas return got_error_from_errno("strdup");
1524 8505ab66 2023-07-10 thomas } else {
1525 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1526 8505ab66 2023-07-10 thomas if (err)
1527 8505ab66 2023-07-10 thomas return err;
1528 8505ab66 2023-07-10 thomas }
1529 8505ab66 2023-07-10 thomas
1530 8505ab66 2023-07-10 thomas ct = calloc(1, sizeof(*ct));
1531 8505ab66 2023-07-10 thomas if (ct == NULL) {
1532 8505ab66 2023-07-10 thomas err = got_error_from_errno("calloc");
1533 8505ab66 2023-07-10 thomas goto done;
1534 8505ab66 2023-07-10 thomas }
1535 8505ab66 2023-07-10 thomas
1536 8505ab66 2023-07-10 thomas if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1537 8505ab66 2023-07-10 thomas relpath) == -1) {
1538 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1539 8505ab66 2023-07-10 thomas goto done;
1540 8505ab66 2023-07-10 thomas }
1541 8505ab66 2023-07-10 thomas
1542 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
1543 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
1544 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1545 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1546 8505ab66 2023-07-10 thomas switch (got_fileindex_entry_staged_filetype_get(ie)) {
1547 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_REGULAR_FILE:
1548 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_BAD_SYMLINK:
1549 8505ab66 2023-07-10 thomas ct->mode = S_IFREG;
1550 8505ab66 2023-07-10 thomas break;
1551 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_SYMLINK:
1552 8505ab66 2023-07-10 thomas ct->mode = S_IFLNK;
1553 8505ab66 2023-07-10 thomas break;
1554 8505ab66 2023-07-10 thomas default:
1555 8505ab66 2023-07-10 thomas err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1556 8505ab66 2023-07-10 thomas goto done;
1557 8505ab66 2023-07-10 thomas }
1558 8505ab66 2023-07-10 thomas ct->mode |= got_fileindex_entry_perms_get(ie);
1559 8505ab66 2023-07-10 thomas } else if (status != GOT_STATUS_DELETE &&
1560 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE) {
1561 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1562 8505ab66 2023-07-10 thomas if (fstatat(dirfd, de_name, &sb,
1563 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1564 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat",
1565 8505ab66 2023-07-10 thomas ct->ondisk_path);
1566 8505ab66 2023-07-10 thomas goto done;
1567 8505ab66 2023-07-10 thomas }
1568 8505ab66 2023-07-10 thomas } else if (lstat(ct->ondisk_path, &sb) == -1) {
1569 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lstat", ct->ondisk_path);
1570 8505ab66 2023-07-10 thomas goto done;
1571 8505ab66 2023-07-10 thomas }
1572 8505ab66 2023-07-10 thomas ct->mode = sb.st_mode;
1573 8505ab66 2023-07-10 thomas }
1574 8505ab66 2023-07-10 thomas
1575 8505ab66 2023-07-10 thomas if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1576 8505ab66 2023-07-10 thomas got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1577 8505ab66 2023-07-10 thomas relpath) == -1) {
1578 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1579 8505ab66 2023-07-10 thomas goto done;
1580 8505ab66 2023-07-10 thomas }
1581 8505ab66 2023-07-10 thomas
1582 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1583 8505ab66 2023-07-10 thomas status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1584 8505ab66 2023-07-10 thomas int is_bad_symlink;
1585 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1586 8505ab66 2023-07-10 thomas ssize_t target_len;
1587 8505ab66 2023-07-10 thomas target_len = readlink(ct->ondisk_path, target_path,
1588 8505ab66 2023-07-10 thomas sizeof(target_path));
1589 8505ab66 2023-07-10 thomas if (target_len == -1) {
1590 8505ab66 2023-07-10 thomas err = got_error_from_errno2("readlink",
1591 8505ab66 2023-07-10 thomas ct->ondisk_path);
1592 8505ab66 2023-07-10 thomas goto done;
1593 8505ab66 2023-07-10 thomas }
1594 8505ab66 2023-07-10 thomas err = is_bad_symlink_target(&is_bad_symlink, target_path,
1595 8505ab66 2023-07-10 thomas target_len, ct->ondisk_path, a->worktree->root_path);
1596 8505ab66 2023-07-10 thomas if (err)
1597 8505ab66 2023-07-10 thomas goto done;
1598 8505ab66 2023-07-10 thomas if (is_bad_symlink) {
1599 8505ab66 2023-07-10 thomas err = got_error_path(ct->ondisk_path,
1600 8505ab66 2023-07-10 thomas GOT_ERR_BAD_SYMLINK);
1601 8505ab66 2023-07-10 thomas goto done;
1602 8505ab66 2023-07-10 thomas }
1603 8505ab66 2023-07-10 thomas }
1604 8505ab66 2023-07-10 thomas
1605 8505ab66 2023-07-10 thomas ct->status = status;
1606 8505ab66 2023-07-10 thomas ct->staged_status = staged_status;
1607 8505ab66 2023-07-10 thomas ct->blob_id = NULL; /* will be filled in when blob gets created */
1608 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
1609 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) {
1610 8505ab66 2023-07-10 thomas ct->base_blob_id = got_object_id_dup(blob_id);
1611 8505ab66 2023-07-10 thomas if (ct->base_blob_id == NULL) {
1612 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1613 8505ab66 2023-07-10 thomas goto done;
1614 8505ab66 2023-07-10 thomas }
1615 8505ab66 2023-07-10 thomas ct->base_commit_id = got_object_id_dup(commit_id);
1616 8505ab66 2023-07-10 thomas if (ct->base_commit_id == NULL) {
1617 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1618 8505ab66 2023-07-10 thomas goto done;
1619 8505ab66 2023-07-10 thomas }
1620 8505ab66 2023-07-10 thomas }
1621 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
1622 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
1623 8505ab66 2023-07-10 thomas ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1624 8505ab66 2023-07-10 thomas if (ct->staged_blob_id == NULL) {
1625 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1626 8505ab66 2023-07-10 thomas goto done;
1627 8505ab66 2023-07-10 thomas }
1628 8505ab66 2023-07-10 thomas }
1629 8505ab66 2023-07-10 thomas ct->path = strdup(path);
1630 8505ab66 2023-07-10 thomas if (ct->path == NULL) {
1631 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
1632 8505ab66 2023-07-10 thomas goto done;
1633 8505ab66 2023-07-10 thomas }
1634 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1635 8505ab66 2023-07-10 thomas if (err)
1636 8505ab66 2023-07-10 thomas goto done;
1637 8505ab66 2023-07-10 thomas
1638 8505ab66 2023-07-10 thomas if (a->diff_outfile && ct && new != NULL) {
1639 8505ab66 2023-07-10 thomas err = append_ct_diff(ct, &a->diff_header_shown,
1640 8505ab66 2023-07-10 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1641 8505ab66 2023-07-10 thomas a->have_staged_files, a->repo, a->worktree);
1642 8505ab66 2023-07-10 thomas if (err)
1643 8505ab66 2023-07-10 thomas goto done;
1644 8505ab66 2023-07-10 thomas }
1645 8505ab66 2023-07-10 thomas done:
1646 8505ab66 2023-07-10 thomas if (ct && (err || new == NULL))
1647 8505ab66 2023-07-10 thomas free_commitable(ct);
1648 8505ab66 2023-07-10 thomas free(parent_path);
1649 8505ab66 2023-07-10 thomas free(path);
1650 8505ab66 2023-07-10 thomas return err;
1651 8505ab66 2023-07-10 thomas }
1652 8505ab66 2023-07-10 thomas
1653 8505ab66 2023-07-10 thomas static const struct got_error *write_tree(struct got_object_id **, int *,
1654 8505ab66 2023-07-10 thomas struct got_tree_object *, const char *, struct got_pathlist_head *,
1655 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1656 8505ab66 2023-07-10 thomas struct got_repository *);
1657 8505ab66 2023-07-10 thomas
1658 8505ab66 2023-07-10 thomas static const struct got_error *
1659 8505ab66 2023-07-10 thomas write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1660 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *parent_path,
1661 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1662 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1663 8505ab66 2023-07-10 thomas struct got_repository *repo)
1664 8505ab66 2023-07-10 thomas {
1665 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1666 8505ab66 2023-07-10 thomas struct got_tree_object *subtree;
1667 8505ab66 2023-07-10 thomas char *subpath;
1668 8505ab66 2023-07-10 thomas
1669 8505ab66 2023-07-10 thomas if (asprintf(&subpath, "%s%s%s", parent_path,
1670 8505ab66 2023-07-10 thomas got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1671 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1672 8505ab66 2023-07-10 thomas
1673 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&subtree, repo, &te->id);
1674 8505ab66 2023-07-10 thomas if (err)
1675 8505ab66 2023-07-10 thomas return err;
1676 8505ab66 2023-07-10 thomas
1677 8505ab66 2023-07-10 thomas err = write_tree(new_subtree_id, nentries, subtree, subpath,
1678 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1679 8505ab66 2023-07-10 thomas got_object_tree_close(subtree);
1680 8505ab66 2023-07-10 thomas free(subpath);
1681 8505ab66 2023-07-10 thomas return err;
1682 8505ab66 2023-07-10 thomas }
1683 8505ab66 2023-07-10 thomas
1684 8505ab66 2023-07-10 thomas static const struct got_error *
1685 8505ab66 2023-07-10 thomas match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1686 8505ab66 2023-07-10 thomas {
1687 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1688 8505ab66 2023-07-10 thomas char *ct_parent_path = NULL;
1689 8505ab66 2023-07-10 thomas
1690 8505ab66 2023-07-10 thomas *match = 0;
1691 8505ab66 2023-07-10 thomas
1692 8505ab66 2023-07-10 thomas if (strchr(ct->in_repo_path, '/') == NULL) {
1693 8505ab66 2023-07-10 thomas *match = got_path_is_root_dir(path);
1694 8505ab66 2023-07-10 thomas return NULL;
1695 8505ab66 2023-07-10 thomas }
1696 8505ab66 2023-07-10 thomas
1697 8505ab66 2023-07-10 thomas err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1698 8505ab66 2023-07-10 thomas if (err)
1699 8505ab66 2023-07-10 thomas return err;
1700 8505ab66 2023-07-10 thomas *match = (strcmp(path, ct_parent_path) == 0);
1701 8505ab66 2023-07-10 thomas free(ct_parent_path);
1702 8505ab66 2023-07-10 thomas return err;
1703 8505ab66 2023-07-10 thomas }
1704 8505ab66 2023-07-10 thomas
1705 8505ab66 2023-07-10 thomas static mode_t
1706 8505ab66 2023-07-10 thomas get_ct_file_mode(struct got_commitable *ct)
1707 8505ab66 2023-07-10 thomas {
1708 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode))
1709 8505ab66 2023-07-10 thomas return S_IFLNK;
1710 8505ab66 2023-07-10 thomas
1711 8505ab66 2023-07-10 thomas return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1712 8505ab66 2023-07-10 thomas }
1713 8505ab66 2023-07-10 thomas
1714 8505ab66 2023-07-10 thomas static const struct got_error *
1715 8505ab66 2023-07-10 thomas alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1716 8505ab66 2023-07-10 thomas struct got_tree_entry *te, struct got_commitable *ct)
1717 8505ab66 2023-07-10 thomas {
1718 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1719 8505ab66 2023-07-10 thomas
1720 8505ab66 2023-07-10 thomas *new_te = NULL;
1721 8505ab66 2023-07-10 thomas
1722 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(new_te, te);
1723 8505ab66 2023-07-10 thomas if (err)
1724 8505ab66 2023-07-10 thomas goto done;
1725 8505ab66 2023-07-10 thomas
1726 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1727 8505ab66 2023-07-10 thomas
1728 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_MODIFY)
1729 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1730 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1731 8505ab66 2023-07-10 thomas else
1732 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1733 8505ab66 2023-07-10 thomas done:
1734 8505ab66 2023-07-10 thomas if (err && *new_te) {
1735 8505ab66 2023-07-10 thomas free(*new_te);
1736 8505ab66 2023-07-10 thomas *new_te = NULL;
1737 8505ab66 2023-07-10 thomas }
1738 8505ab66 2023-07-10 thomas return err;
1739 8505ab66 2023-07-10 thomas }
1740 8505ab66 2023-07-10 thomas
1741 8505ab66 2023-07-10 thomas static const struct got_error *
1742 8505ab66 2023-07-10 thomas alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1743 8505ab66 2023-07-10 thomas struct got_commitable *ct)
1744 8505ab66 2023-07-10 thomas {
1745 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1746 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1747 8505ab66 2023-07-10 thomas
1748 8505ab66 2023-07-10 thomas *new_te = NULL;
1749 8505ab66 2023-07-10 thomas
1750 8505ab66 2023-07-10 thomas *new_te = calloc(1, sizeof(**new_te));
1751 8505ab66 2023-07-10 thomas if (*new_te == NULL)
1752 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1753 8505ab66 2023-07-10 thomas
1754 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, ct->path);
1755 8505ab66 2023-07-10 thomas if (err)
1756 8505ab66 2023-07-10 thomas goto done;
1757 8505ab66 2023-07-10 thomas if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1758 8505ab66 2023-07-10 thomas sizeof((*new_te)->name)) {
1759 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1760 8505ab66 2023-07-10 thomas goto done;
1761 8505ab66 2023-07-10 thomas }
1762 8505ab66 2023-07-10 thomas
1763 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1764 8505ab66 2023-07-10 thomas
1765 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD)
1766 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1767 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1768 8505ab66 2023-07-10 thomas else
1769 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1770 8505ab66 2023-07-10 thomas done:
1771 8505ab66 2023-07-10 thomas free(ct_name);
1772 8505ab66 2023-07-10 thomas if (err && *new_te) {
1773 8505ab66 2023-07-10 thomas free(*new_te);
1774 8505ab66 2023-07-10 thomas *new_te = NULL;
1775 8505ab66 2023-07-10 thomas }
1776 8505ab66 2023-07-10 thomas return err;
1777 8505ab66 2023-07-10 thomas }
1778 8505ab66 2023-07-10 thomas
1779 8505ab66 2023-07-10 thomas static const struct got_error *
1780 8505ab66 2023-07-10 thomas insert_tree_entry(struct got_tree_entry *new_te,
1781 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
1782 8505ab66 2023-07-10 thomas {
1783 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1784 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new_pe;
1785 8505ab66 2023-07-10 thomas
1786 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1787 8505ab66 2023-07-10 thomas if (err)
1788 8505ab66 2023-07-10 thomas return err;
1789 8505ab66 2023-07-10 thomas if (new_pe == NULL)
1790 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_TREE_DUP_ENTRY);
1791 8505ab66 2023-07-10 thomas return NULL;
1792 8505ab66 2023-07-10 thomas }
1793 8505ab66 2023-07-10 thomas
1794 8505ab66 2023-07-10 thomas static const struct got_error *
1795 8505ab66 2023-07-10 thomas report_ct_status(struct got_commitable *ct,
1796 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg)
1797 8505ab66 2023-07-10 thomas {
1798 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
1799 8505ab66 2023-07-10 thomas unsigned char status;
1800 8505ab66 2023-07-10 thomas
1801 8505ab66 2023-07-10 thomas if (status_cb == NULL) /* no commit progress output desired */
1802 8505ab66 2023-07-10 thomas return NULL;
1803 8505ab66 2023-07-10 thomas
1804 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
1805 8505ab66 2023-07-10 thomas ct_path++;
1806 8505ab66 2023-07-10 thomas
1807 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1808 8505ab66 2023-07-10 thomas status = ct->staged_status;
1809 8505ab66 2023-07-10 thomas else
1810 8505ab66 2023-07-10 thomas status = ct->status;
1811 8505ab66 2023-07-10 thomas
1812 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1813 8505ab66 2023-07-10 thomas ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1814 8505ab66 2023-07-10 thomas }
1815 8505ab66 2023-07-10 thomas
1816 8505ab66 2023-07-10 thomas static const struct got_error *
1817 8505ab66 2023-07-10 thomas match_modified_subtree(int *modified, struct got_tree_entry *te,
1818 8505ab66 2023-07-10 thomas const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1819 8505ab66 2023-07-10 thomas {
1820 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1821 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1822 8505ab66 2023-07-10 thomas char *te_path;
1823 8505ab66 2023-07-10 thomas
1824 8505ab66 2023-07-10 thomas *modified = 0;
1825 8505ab66 2023-07-10 thomas
1826 8505ab66 2023-07-10 thomas if (asprintf(&te_path, "%s%s%s", base_tree_path,
1827 8505ab66 2023-07-10 thomas got_path_is_root_dir(base_tree_path) ? "" : "/",
1828 8505ab66 2023-07-10 thomas te->name) == -1)
1829 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1830 8505ab66 2023-07-10 thomas
1831 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1832 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1833 8505ab66 2023-07-10 thomas *modified = got_path_is_child(ct->in_repo_path, te_path,
1834 8505ab66 2023-07-10 thomas strlen(te_path));
1835 8505ab66 2023-07-10 thomas if (*modified)
1836 8505ab66 2023-07-10 thomas break;
1837 8505ab66 2023-07-10 thomas }
1838 8505ab66 2023-07-10 thomas
1839 8505ab66 2023-07-10 thomas free(te_path);
1840 8505ab66 2023-07-10 thomas return err;
1841 8505ab66 2023-07-10 thomas }
1842 8505ab66 2023-07-10 thomas
1843 8505ab66 2023-07-10 thomas static const struct got_error *
1844 8505ab66 2023-07-10 thomas match_deleted_or_modified_ct(struct got_commitable **ctp,
1845 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *base_tree_path,
1846 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
1847 8505ab66 2023-07-10 thomas {
1848 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1849 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1850 8505ab66 2023-07-10 thomas
1851 8505ab66 2023-07-10 thomas *ctp = NULL;
1852 8505ab66 2023-07-10 thomas
1853 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1854 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1855 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1856 8505ab66 2023-07-10 thomas int path_matches;
1857 8505ab66 2023-07-10 thomas
1858 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1859 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1860 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
1861 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1862 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1863 8505ab66 2023-07-10 thomas continue;
1864 8505ab66 2023-07-10 thomas } else {
1865 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1866 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1867 8505ab66 2023-07-10 thomas continue;
1868 8505ab66 2023-07-10 thomas }
1869 8505ab66 2023-07-10 thomas
1870 8505ab66 2023-07-10 thomas if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1871 8505ab66 2023-07-10 thomas continue;
1872 8505ab66 2023-07-10 thomas
1873 8505ab66 2023-07-10 thomas err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1874 8505ab66 2023-07-10 thomas if (err)
1875 8505ab66 2023-07-10 thomas return err;
1876 8505ab66 2023-07-10 thomas if (!path_matches)
1877 8505ab66 2023-07-10 thomas continue;
1878 8505ab66 2023-07-10 thomas
1879 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, pe->path);
1880 8505ab66 2023-07-10 thomas if (err)
1881 8505ab66 2023-07-10 thomas return err;
1882 8505ab66 2023-07-10 thomas
1883 8505ab66 2023-07-10 thomas if (strcmp(te->name, ct_name) != 0) {
1884 8505ab66 2023-07-10 thomas free(ct_name);
1885 8505ab66 2023-07-10 thomas continue;
1886 8505ab66 2023-07-10 thomas }
1887 8505ab66 2023-07-10 thomas free(ct_name);
1888 8505ab66 2023-07-10 thomas
1889 8505ab66 2023-07-10 thomas *ctp = ct;
1890 8505ab66 2023-07-10 thomas break;
1891 8505ab66 2023-07-10 thomas }
1892 8505ab66 2023-07-10 thomas
1893 8505ab66 2023-07-10 thomas return err;
1894 8505ab66 2023-07-10 thomas }
1895 8505ab66 2023-07-10 thomas
1896 8505ab66 2023-07-10 thomas static const struct got_error *
1897 8505ab66 2023-07-10 thomas make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1898 8505ab66 2023-07-10 thomas const char *child_path, const char *path_base_tree,
1899 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1900 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1901 8505ab66 2023-07-10 thomas struct got_repository *repo)
1902 8505ab66 2023-07-10 thomas {
1903 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1904 8505ab66 2023-07-10 thomas struct got_tree_entry *new_te;
1905 8505ab66 2023-07-10 thomas char *subtree_path;
1906 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
1907 8505ab66 2023-07-10 thomas int nentries;
1908 8505ab66 2023-07-10 thomas
1909 8505ab66 2023-07-10 thomas *new_tep = NULL;
1910 8505ab66 2023-07-10 thomas
1911 8505ab66 2023-07-10 thomas if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1912 8505ab66 2023-07-10 thomas got_path_is_root_dir(path_base_tree) ? "" : "/",
1913 8505ab66 2023-07-10 thomas child_path) == -1)
1914 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1915 8505ab66 2023-07-10 thomas
1916 8505ab66 2023-07-10 thomas new_te = calloc(1, sizeof(*new_te));
1917 8505ab66 2023-07-10 thomas if (new_te == NULL)
1918 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1919 8505ab66 2023-07-10 thomas new_te->mode = S_IFDIR;
1920 8505ab66 2023-07-10 thomas
1921 8505ab66 2023-07-10 thomas if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1922 8505ab66 2023-07-10 thomas sizeof(new_te->name)) {
1923 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1924 8505ab66 2023-07-10 thomas goto done;
1925 8505ab66 2023-07-10 thomas }
1926 8505ab66 2023-07-10 thomas err = write_tree(&id, &nentries, NULL, subtree_path,
1927 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1928 8505ab66 2023-07-10 thomas if (err) {
1929 8505ab66 2023-07-10 thomas free(new_te);
1930 8505ab66 2023-07-10 thomas goto done;
1931 8505ab66 2023-07-10 thomas }
1932 8505ab66 2023-07-10 thomas memcpy(&new_te->id, id, sizeof(new_te->id));
1933 8505ab66 2023-07-10 thomas done:
1934 8505ab66 2023-07-10 thomas free(id);
1935 8505ab66 2023-07-10 thomas free(subtree_path);
1936 8505ab66 2023-07-10 thomas if (err == NULL)
1937 8505ab66 2023-07-10 thomas *new_tep = new_te;
1938 8505ab66 2023-07-10 thomas return err;
1939 8505ab66 2023-07-10 thomas }
1940 8505ab66 2023-07-10 thomas
1941 8505ab66 2023-07-10 thomas static const struct got_error *
1942 8505ab66 2023-07-10 thomas write_tree(struct got_object_id **new_tree_id, int *nentries,
1943 8505ab66 2023-07-10 thomas struct got_tree_object *base_tree, const char *path_base_tree,
1944 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1945 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1946 8505ab66 2023-07-10 thomas struct got_repository *repo)
1947 8505ab66 2023-07-10 thomas {
1948 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1949 8505ab66 2023-07-10 thomas struct got_pathlist_head paths;
1950 8505ab66 2023-07-10 thomas struct got_tree_entry *te, *new_te = NULL;
1951 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1952 8505ab66 2023-07-10 thomas
1953 8505ab66 2023-07-10 thomas TAILQ_INIT(&paths);
1954 8505ab66 2023-07-10 thomas *nentries = 0;
1955 8505ab66 2023-07-10 thomas
1956 8505ab66 2023-07-10 thomas /* Insert, and recurse into, newly added entries first. */
1957 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1958 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1959 8505ab66 2023-07-10 thomas char *child_path = NULL, *slash;
1960 8505ab66 2023-07-10 thomas
1961 8505ab66 2023-07-10 thomas if ((ct->status != GOT_STATUS_ADD &&
1962 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) ||
1963 8505ab66 2023-07-10 thomas (ct->flags & GOT_COMMITABLE_ADDED))
1964 8505ab66 2023-07-10 thomas continue;
1965 8505ab66 2023-07-10 thomas
1966 8505ab66 2023-07-10 thomas if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1967 8505ab66 2023-07-10 thomas strlen(path_base_tree)))
1968 8505ab66 2023-07-10 thomas continue;
1969 8505ab66 2023-07-10 thomas
1970 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1971 8505ab66 2023-07-10 thomas ct->in_repo_path);
1972 8505ab66 2023-07-10 thomas if (err)
1973 8505ab66 2023-07-10 thomas goto done;
1974 8505ab66 2023-07-10 thomas
1975 8505ab66 2023-07-10 thomas slash = strchr(child_path, '/');
1976 8505ab66 2023-07-10 thomas if (slash == NULL) {
1977 8505ab66 2023-07-10 thomas err = alloc_added_blob_tree_entry(&new_te, ct);
1978 8505ab66 2023-07-10 thomas if (err)
1979 8505ab66 2023-07-10 thomas goto done;
1980 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb, status_arg);
1981 8505ab66 2023-07-10 thomas if (err)
1982 8505ab66 2023-07-10 thomas goto done;
1983 8505ab66 2023-07-10 thomas ct->flags |= GOT_COMMITABLE_ADDED;
1984 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
1985 8505ab66 2023-07-10 thomas if (err)
1986 8505ab66 2023-07-10 thomas goto done;
1987 8505ab66 2023-07-10 thomas (*nentries)++;
1988 8505ab66 2023-07-10 thomas } else {
1989 8505ab66 2023-07-10 thomas *slash = '\0'; /* trim trailing path components */
1990 8505ab66 2023-07-10 thomas if (base_tree == NULL ||
1991 8505ab66 2023-07-10 thomas got_object_tree_find_entry(base_tree, child_path)
1992 8505ab66 2023-07-10 thomas == NULL) {
1993 8505ab66 2023-07-10 thomas err = make_subtree_for_added_blob(&new_te,
1994 8505ab66 2023-07-10 thomas child_path, path_base_tree,
1995 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg,
1996 8505ab66 2023-07-10 thomas repo);
1997 8505ab66 2023-07-10 thomas if (err)
1998 8505ab66 2023-07-10 thomas goto done;
1999 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2000 8505ab66 2023-07-10 thomas if (err)
2001 8505ab66 2023-07-10 thomas goto done;
2002 8505ab66 2023-07-10 thomas (*nentries)++;
2003 8505ab66 2023-07-10 thomas }
2004 8505ab66 2023-07-10 thomas }
2005 8505ab66 2023-07-10 thomas }
2006 8505ab66 2023-07-10 thomas
2007 8505ab66 2023-07-10 thomas if (base_tree) {
2008 8505ab66 2023-07-10 thomas int i, nbase_entries;
2009 8505ab66 2023-07-10 thomas /* Handle modified and deleted entries. */
2010 8505ab66 2023-07-10 thomas nbase_entries = got_object_tree_get_nentries(base_tree);
2011 8505ab66 2023-07-10 thomas for (i = 0; i < nbase_entries; i++) {
2012 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
2013 8505ab66 2023-07-10 thomas
2014 8505ab66 2023-07-10 thomas te = got_object_tree_get_entry(base_tree, i);
2015 8505ab66 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te)) {
2016 8505ab66 2023-07-10 thomas /* Entry is a submodule; just copy it. */
2017 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2018 8505ab66 2023-07-10 thomas if (err)
2019 8505ab66 2023-07-10 thomas goto done;
2020 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2021 8505ab66 2023-07-10 thomas if (err)
2022 8505ab66 2023-07-10 thomas goto done;
2023 8505ab66 2023-07-10 thomas (*nentries)++;
2024 8505ab66 2023-07-10 thomas continue;
2025 8505ab66 2023-07-10 thomas }
2026 8505ab66 2023-07-10 thomas
2027 8505ab66 2023-07-10 thomas if (S_ISDIR(te->mode)) {
2028 8505ab66 2023-07-10 thomas int modified;
2029 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2030 8505ab66 2023-07-10 thomas if (err)
2031 8505ab66 2023-07-10 thomas goto done;
2032 8505ab66 2023-07-10 thomas err = match_modified_subtree(&modified, te,
2033 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2034 8505ab66 2023-07-10 thomas if (err)
2035 8505ab66 2023-07-10 thomas goto done;
2036 8505ab66 2023-07-10 thomas /* Avoid recursion into unmodified subtrees. */
2037 8505ab66 2023-07-10 thomas if (modified) {
2038 8505ab66 2023-07-10 thomas struct got_object_id *new_id;
2039 8505ab66 2023-07-10 thomas int nsubentries;
2040 8505ab66 2023-07-10 thomas err = write_subtree(&new_id,
2041 8505ab66 2023-07-10 thomas &nsubentries, te,
2042 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths,
2043 8505ab66 2023-07-10 thomas status_cb, status_arg, repo);
2044 8505ab66 2023-07-10 thomas if (err)
2045 8505ab66 2023-07-10 thomas goto done;
2046 8505ab66 2023-07-10 thomas if (nsubentries == 0) {
2047 8505ab66 2023-07-10 thomas /* All entries were deleted. */
2048 8505ab66 2023-07-10 thomas free(new_id);
2049 8505ab66 2023-07-10 thomas continue;
2050 8505ab66 2023-07-10 thomas }
2051 8505ab66 2023-07-10 thomas memcpy(&new_te->id, new_id,
2052 8505ab66 2023-07-10 thomas sizeof(new_te->id));
2053 8505ab66 2023-07-10 thomas free(new_id);
2054 8505ab66 2023-07-10 thomas }
2055 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2056 8505ab66 2023-07-10 thomas if (err)
2057 8505ab66 2023-07-10 thomas goto done;
2058 8505ab66 2023-07-10 thomas (*nentries)++;
2059 8505ab66 2023-07-10 thomas continue;
2060 8505ab66 2023-07-10 thomas }
2061 8505ab66 2023-07-10 thomas
2062 8505ab66 2023-07-10 thomas err = match_deleted_or_modified_ct(&ct, te,
2063 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2064 8505ab66 2023-07-10 thomas if (err)
2065 8505ab66 2023-07-10 thomas goto done;
2066 8505ab66 2023-07-10 thomas if (ct) {
2067 8505ab66 2023-07-10 thomas /* NB: Deleted entries get dropped here. */
2068 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_MODIFY ||
2069 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_MODE_CHANGE ||
2070 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_CONFLICT ||
2071 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2072 8505ab66 2023-07-10 thomas err = alloc_modified_blob_tree_entry(
2073 8505ab66 2023-07-10 thomas &new_te, te, ct);
2074 8505ab66 2023-07-10 thomas if (err)
2075 8505ab66 2023-07-10 thomas goto done;
2076 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2077 8505ab66 2023-07-10 thomas if (err)
2078 8505ab66 2023-07-10 thomas goto done;
2079 8505ab66 2023-07-10 thomas (*nentries)++;
2080 8505ab66 2023-07-10 thomas }
2081 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb,
2082 8505ab66 2023-07-10 thomas status_arg);
2083 8505ab66 2023-07-10 thomas if (err)
2084 8505ab66 2023-07-10 thomas goto done;
2085 8505ab66 2023-07-10 thomas } else {
2086 8505ab66 2023-07-10 thomas /* Entry is unchanged; just copy it. */
2087 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2088 8505ab66 2023-07-10 thomas if (err)
2089 8505ab66 2023-07-10 thomas goto done;
2090 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2091 8505ab66 2023-07-10 thomas if (err)
2092 8505ab66 2023-07-10 thomas goto done;
2093 8505ab66 2023-07-10 thomas (*nentries)++;
2094 8505ab66 2023-07-10 thomas }
2095 8505ab66 2023-07-10 thomas }
2096 8505ab66 2023-07-10 thomas }
2097 8505ab66 2023-07-10 thomas
2098 8505ab66 2023-07-10 thomas /* Write new list of entries; deleted entries have been dropped. */
2099 8505ab66 2023-07-10 thomas err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2100 8505ab66 2023-07-10 thomas done:
2101 8505ab66 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2102 8505ab66 2023-07-10 thomas return err;
2103 8505ab66 2023-07-10 thomas }
2104 8505ab66 2023-07-10 thomas
2105 8505ab66 2023-07-10 thomas static const struct got_error *
2106 8505ab66 2023-07-10 thomas update_fileindex_after_commit(struct got_worktree *worktree,
2107 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2108 8505ab66 2023-07-10 thomas struct got_object_id *new_base_commit_id,
2109 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, int have_staged_files)
2110 8505ab66 2023-07-10 thomas {
2111 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2112 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2113 8505ab66 2023-07-10 thomas char *relpath = NULL;
2114 8505ab66 2023-07-10 thomas
2115 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2116 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2117 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2118 8505ab66 2023-07-10 thomas
2119 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2120 8505ab66 2023-07-10 thomas
2121 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&relpath,
2122 8505ab66 2023-07-10 thomas worktree->root_path, ct->ondisk_path);
2123 8505ab66 2023-07-10 thomas if (err)
2124 8505ab66 2023-07-10 thomas goto done;
2125 8505ab66 2023-07-10 thomas
2126 8505ab66 2023-07-10 thomas if (ie) {
2127 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_DELETE ||
2128 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_DELETE) {
2129 8505ab66 2023-07-10 thomas got_fileindex_entry_remove(fileindex, ie);
2130 8505ab66 2023-07-10 thomas } else if (ct->staged_status == GOT_STATUS_ADD ||
2131 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2132 8505ab66 2023-07-10 thomas got_fileindex_entry_stage_set(ie,
2133 8505ab66 2023-07-10 thomas GOT_FILEIDX_STAGE_NONE);
2134 8505ab66 2023-07-10 thomas got_fileindex_entry_staged_filetype_set(ie, 0);
2135 8505ab66 2023-07-10 thomas
2136 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2137 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2138 8505ab66 2023-07-10 thomas ct->staged_blob_id->sha1,
2139 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2140 8505ab66 2023-07-10 thomas !have_staged_files);
2141 8505ab66 2023-07-10 thomas } else
2142 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2143 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2144 8505ab66 2023-07-10 thomas ct->blob_id->sha1,
2145 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2146 8505ab66 2023-07-10 thomas !have_staged_files);
2147 8505ab66 2023-07-10 thomas } else {
2148 8505ab66 2023-07-10 thomas err = got_fileindex_entry_alloc(&ie, pe->path);
2149 8505ab66 2023-07-10 thomas if (err)
2150 8505ab66 2023-07-10 thomas goto done;
2151 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2152 8505ab66 2023-07-10 thomas worktree->root_fd, relpath, ct->blob_id->sha1,
2153 8505ab66 2023-07-10 thomas new_base_commit_id->sha1, 1);
2154 8505ab66 2023-07-10 thomas if (err) {
2155 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2156 8505ab66 2023-07-10 thomas goto done;
2157 8505ab66 2023-07-10 thomas }
2158 8505ab66 2023-07-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2159 8505ab66 2023-07-10 thomas if (err) {
2160 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2161 8505ab66 2023-07-10 thomas goto done;
2162 8505ab66 2023-07-10 thomas }
2163 8505ab66 2023-07-10 thomas }
2164 8505ab66 2023-07-10 thomas free(relpath);
2165 8505ab66 2023-07-10 thomas relpath = NULL;
2166 8505ab66 2023-07-10 thomas }
2167 8505ab66 2023-07-10 thomas done:
2168 8505ab66 2023-07-10 thomas free(relpath);
2169 8505ab66 2023-07-10 thomas return err;
2170 8505ab66 2023-07-10 thomas }
2171 8505ab66 2023-07-10 thomas
2172 8505ab66 2023-07-10 thomas static const struct got_error *
2173 8505ab66 2023-07-10 thomas check_out_of_date(const char *in_repo_path, unsigned char status,
2174 8505ab66 2023-07-10 thomas unsigned char staged_status, struct got_object_id *base_blob_id,
2175 8505ab66 2023-07-10 thomas struct got_object_id *base_commit_id,
2176 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id, struct got_repository *repo,
2177 8505ab66 2023-07-10 thomas int ood_errcode)
2178 8505ab66 2023-07-10 thomas {
2179 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2180 8505ab66 2023-07-10 thomas struct got_commit_object *commit = NULL;
2181 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
2182 8505ab66 2023-07-10 thomas
2183 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2184 8505ab66 2023-07-10 thomas /* Trivial case: base commit == head commit */
2185 8505ab66 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2186 8505ab66 2023-07-10 thomas return NULL;
2187 8505ab66 2023-07-10 thomas /*
2188 8505ab66 2023-07-10 thomas * Ensure file content which local changes were based
2189 8505ab66 2023-07-10 thomas * on matches file content in the branch head.
2190 8505ab66 2023-07-10 thomas */
2191 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2192 8505ab66 2023-07-10 thomas if (err)
2193 8505ab66 2023-07-10 thomas goto done;
2194 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2195 8505ab66 2023-07-10 thomas if (err) {
2196 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NO_TREE_ENTRY)
2197 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2198 8505ab66 2023-07-10 thomas goto done;
2199 8505ab66 2023-07-10 thomas } else if (got_object_id_cmp(id, base_blob_id) != 0)
2200 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2201 8505ab66 2023-07-10 thomas } else {
2202 8505ab66 2023-07-10 thomas /* Require that added files don't exist in the branch head. */
2203 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2204 8505ab66 2023-07-10 thomas if (err)
2205 8505ab66 2023-07-10 thomas goto done;
2206 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2207 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2208 8505ab66 2023-07-10 thomas goto done;
2209 8505ab66 2023-07-10 thomas err = id ? got_error(ood_errcode) : NULL;
2210 8505ab66 2023-07-10 thomas }
2211 8505ab66 2023-07-10 thomas done:
2212 8505ab66 2023-07-10 thomas free(id);
2213 8505ab66 2023-07-10 thomas if (commit)
2214 8505ab66 2023-07-10 thomas got_object_commit_close(commit);
2215 8505ab66 2023-07-10 thomas return err;
2216 8505ab66 2023-07-10 thomas }
2217 8505ab66 2023-07-10 thomas
2218 8505ab66 2023-07-10 thomas static const struct got_error *
2219 8505ab66 2023-07-10 thomas commit_worktree(struct got_object_id **new_commit_id,
2220 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2221 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id,
2222 8505ab66 2023-07-10 thomas struct got_object_id *parent_id2,
2223 8505ab66 2023-07-10 thomas struct got_worktree *worktree,
2224 8505ab66 2023-07-10 thomas const char *author, const char *committer, char *diff_path,
2225 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2226 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2227 8505ab66 2023-07-10 thomas struct got_repository *repo)
2228 8505ab66 2023-07-10 thomas {
2229 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2230 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2231 8505ab66 2023-07-10 thomas struct got_commit_object *head_commit = NULL;
2232 8505ab66 2023-07-10 thomas struct got_tree_object *head_tree = NULL;
2233 8505ab66 2023-07-10 thomas struct got_object_id *new_tree_id = NULL;
2234 8505ab66 2023-07-10 thomas int nentries, nparents = 0;
2235 8505ab66 2023-07-10 thomas struct got_object_id_queue parent_ids;
2236 8505ab66 2023-07-10 thomas struct got_object_qid *pid = NULL;
2237 8505ab66 2023-07-10 thomas char *logmsg = NULL;
2238 8505ab66 2023-07-10 thomas time_t timestamp;
2239 8505ab66 2023-07-10 thomas
2240 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2241 8505ab66 2023-07-10 thomas
2242 8505ab66 2023-07-10 thomas STAILQ_INIT(&parent_ids);
2243 8505ab66 2023-07-10 thomas
2244 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2245 8505ab66 2023-07-10 thomas if (err)
2246 8505ab66 2023-07-10 thomas goto done;
2247 8505ab66 2023-07-10 thomas
2248 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2249 8505ab66 2023-07-10 thomas if (err)
2250 8505ab66 2023-07-10 thomas goto done;
2251 8505ab66 2023-07-10 thomas
2252 8505ab66 2023-07-10 thomas if (commit_msg_cb != NULL) {
2253 8505ab66 2023-07-10 thomas err = commit_msg_cb(commitable_paths, diff_path,
2254 8505ab66 2023-07-10 thomas &logmsg, commit_arg);
2255 8505ab66 2023-07-10 thomas if (err)
2256 8505ab66 2023-07-10 thomas goto done;
2257 8505ab66 2023-07-10 thomas }
2258 8505ab66 2023-07-10 thomas
2259 8505ab66 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0) {
2260 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2261 8505ab66 2023-07-10 thomas goto done;
2262 8505ab66 2023-07-10 thomas }
2263 8505ab66 2023-07-10 thomas
2264 8505ab66 2023-07-10 thomas /* Create blobs from added and modified files and record their IDs. */
2265 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2266 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2267 8505ab66 2023-07-10 thomas char *ondisk_path;
2268 8505ab66 2023-07-10 thomas
2269 8505ab66 2023-07-10 thomas /* Blobs for staged files already exist. */
2270 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
2271 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY)
2272 8505ab66 2023-07-10 thomas continue;
2273 8505ab66 2023-07-10 thomas
2274 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
2275 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODIFY &&
2276 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
2277 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
2278 8505ab66 2023-07-10 thomas continue;
2279 8505ab66 2023-07-10 thomas
2280 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
2281 8505ab66 2023-07-10 thomas worktree->root_path, pe->path) == -1) {
2282 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2283 8505ab66 2023-07-10 thomas goto done;
2284 8505ab66 2023-07-10 thomas }
2285 8505ab66 2023-07-10 thomas err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2286 8505ab66 2023-07-10 thomas free(ondisk_path);
2287 8505ab66 2023-07-10 thomas if (err)
2288 8505ab66 2023-07-10 thomas goto done;
2289 8505ab66 2023-07-10 thomas }
2290 8505ab66 2023-07-10 thomas
2291 8505ab66 2023-07-10 thomas /* Recursively write new tree objects. */
2292 8505ab66 2023-07-10 thomas err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2293 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
2294 8505ab66 2023-07-10 thomas if (err)
2295 8505ab66 2023-07-10 thomas goto done;
2296 8505ab66 2023-07-10 thomas
2297 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, head_commit_id);
2298 8505ab66 2023-07-10 thomas if (err)
2299 8505ab66 2023-07-10 thomas goto done;
2300 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2301 8505ab66 2023-07-10 thomas nparents++;
2302 8505ab66 2023-07-10 thomas if (parent_id2) {
2303 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, parent_id2);
2304 8505ab66 2023-07-10 thomas if (err)
2305 8505ab66 2023-07-10 thomas goto done;
2306 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2307 8505ab66 2023-07-10 thomas nparents++;
2308 8505ab66 2023-07-10 thomas }
2309 8505ab66 2023-07-10 thomas timestamp = time(NULL);
2310 8505ab66 2023-07-10 thomas err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2311 8505ab66 2023-07-10 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
2312 8505ab66 2023-07-10 thomas if (logmsg != NULL)
2313 8505ab66 2023-07-10 thomas free(logmsg);
2314 8505ab66 2023-07-10 thomas if (err)
2315 8505ab66 2023-07-10 thomas goto done;
2316 8505ab66 2023-07-10 thomas done:
2317 8505ab66 2023-07-10 thomas got_object_id_queue_free(&parent_ids);
2318 8505ab66 2023-07-10 thomas if (head_tree)
2319 8505ab66 2023-07-10 thomas got_object_tree_close(head_tree);
2320 8505ab66 2023-07-10 thomas if (head_commit)
2321 8505ab66 2023-07-10 thomas got_object_commit_close(head_commit);
2322 8505ab66 2023-07-10 thomas return err;
2323 8505ab66 2023-07-10 thomas }
2324 8505ab66 2023-07-10 thomas
2325 8505ab66 2023-07-10 thomas static const struct got_error *
2326 8505ab66 2023-07-10 thomas check_path_is_commitable(const char *path,
2327 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
2328 8505ab66 2023-07-10 thomas {
2329 8505ab66 2023-07-10 thomas struct got_pathlist_entry *cpe = NULL;
2330 8505ab66 2023-07-10 thomas size_t path_len = strlen(path);
2331 8505ab66 2023-07-10 thomas
2332 8505ab66 2023-07-10 thomas TAILQ_FOREACH(cpe, commitable_paths, entry) {
2333 8505ab66 2023-07-10 thomas struct got_commitable *ct = cpe->data;
2334 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
2335 8505ab66 2023-07-10 thomas
2336 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
2337 8505ab66 2023-07-10 thomas ct_path++;
2338 8505ab66 2023-07-10 thomas
2339 8505ab66 2023-07-10 thomas if (strcmp(path, ct_path) == 0 ||
2340 8505ab66 2023-07-10 thomas got_path_is_child(ct_path, path, path_len))
2341 8505ab66 2023-07-10 thomas break;
2342 8505ab66 2023-07-10 thomas }
2343 8505ab66 2023-07-10 thomas
2344 8505ab66 2023-07-10 thomas if (cpe == NULL)
2345 8505ab66 2023-07-10 thomas return got_error_path(path, GOT_ERR_BAD_PATH);
2346 8505ab66 2023-07-10 thomas
2347 8505ab66 2023-07-10 thomas return NULL;
2348 8505ab66 2023-07-10 thomas }
2349 8505ab66 2023-07-10 thomas
2350 8505ab66 2023-07-10 thomas static const struct got_error *
2351 8505ab66 2023-07-10 thomas check_staged_file(void *arg, struct got_fileindex_entry *ie)
2352 8505ab66 2023-07-10 thomas {
2353 8505ab66 2023-07-10 thomas int *have_staged_files = arg;
2354 8505ab66 2023-07-10 thomas
2355 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2356 8505ab66 2023-07-10 thomas *have_staged_files = 1;
2357 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2358 8505ab66 2023-07-10 thomas }
2359 8505ab66 2023-07-10 thomas
2360 8505ab66 2023-07-10 thomas return NULL;
2361 8505ab66 2023-07-10 thomas }
2362 8505ab66 2023-07-10 thomas
2363 8505ab66 2023-07-10 thomas static const struct got_error *
2364 8505ab66 2023-07-10 thomas check_non_staged_files(struct got_fileindex *fileindex,
2365 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
2366 8505ab66 2023-07-10 thomas {
2367 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2368 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2369 8505ab66 2023-07-10 thomas
2370 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
2371 8505ab66 2023-07-10 thomas if (pe->path[0] == '\0')
2372 8505ab66 2023-07-10 thomas continue;
2373 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2374 8505ab66 2023-07-10 thomas if (ie == NULL)
2375 8505ab66 2023-07-10 thomas return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2376 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2377 8505ab66 2023-07-10 thomas return got_error_path(pe->path,
2378 8505ab66 2023-07-10 thomas GOT_ERR_FILE_NOT_STAGED);
2379 8505ab66 2023-07-10 thomas }
2380 8505ab66 2023-07-10 thomas
2381 8505ab66 2023-07-10 thomas return NULL;
2382 8505ab66 2023-07-10 thomas }
2383 8505ab66 2023-07-10 thomas
2384 8505ab66 2023-07-10 thomas static void
2385 8505ab66 2023-07-10 thomas print_load_info(int print_colored, int print_found, int print_trees,
2386 8505ab66 2023-07-10 thomas int ncolored, int nfound, int ntrees)
2387 8505ab66 2023-07-10 thomas {
2388 8505ab66 2023-07-10 thomas if (print_colored) {
2389 8505ab66 2023-07-10 thomas printf("%d commit%s colored", ncolored,
2390 8505ab66 2023-07-10 thomas ncolored == 1 ? "" : "s");
2391 8505ab66 2023-07-10 thomas }
2392 8505ab66 2023-07-10 thomas if (print_found) {
2393 8505ab66 2023-07-10 thomas printf("%s%d object%s found",
2394 8505ab66 2023-07-10 thomas ncolored > 0 ? "; " : "",
2395 8505ab66 2023-07-10 thomas nfound, nfound == 1 ? "" : "s");
2396 8505ab66 2023-07-10 thomas }
2397 8505ab66 2023-07-10 thomas if (print_trees) {
2398 8505ab66 2023-07-10 thomas printf("; %d tree%s scanned", ntrees,
2399 8505ab66 2023-07-10 thomas ntrees == 1 ? "" : "s");
2400 8505ab66 2023-07-10 thomas }
2401 8505ab66 2023-07-10 thomas }
2402 8505ab66 2023-07-10 thomas
2403 8505ab66 2023-07-10 thomas struct got_send_progress_arg {
2404 8505ab66 2023-07-10 thomas char last_scaled_packsize[FMT_SCALED_STRSIZE];
2405 8505ab66 2023-07-10 thomas int verbosity;
2406 8505ab66 2023-07-10 thomas int last_ncolored;
2407 8505ab66 2023-07-10 thomas int last_nfound;
2408 8505ab66 2023-07-10 thomas int last_ntrees;
2409 8505ab66 2023-07-10 thomas int loading_done;
2410 8505ab66 2023-07-10 thomas int last_ncommits;
2411 8505ab66 2023-07-10 thomas int last_nobj_total;
2412 8505ab66 2023-07-10 thomas int last_p_deltify;
2413 8505ab66 2023-07-10 thomas int last_p_written;
2414 8505ab66 2023-07-10 thomas int last_p_sent;
2415 8505ab66 2023-07-10 thomas int printed_something;
2416 8505ab66 2023-07-10 thomas int sent_something;
2417 8505ab66 2023-07-10 thomas struct got_pathlist_head *delete_branches;
2418 8505ab66 2023-07-10 thomas };
2419 8505ab66 2023-07-10 thomas
2420 8505ab66 2023-07-10 thomas static const struct got_error *
2421 8505ab66 2023-07-10 thomas send_progress(void *arg, int ncolored, int nfound, int ntrees,
2422 8505ab66 2023-07-10 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2423 8505ab66 2023-07-10 thomas int nobj_written, off_t bytes_sent, const char *refname,
2424 8505ab66 2023-07-10 thomas const char *errmsg, int success)
2425 8505ab66 2023-07-10 thomas {
2426 8505ab66 2023-07-10 thomas struct got_send_progress_arg *a = arg;
2427 8505ab66 2023-07-10 thomas char scaled_packsize[FMT_SCALED_STRSIZE];
2428 8505ab66 2023-07-10 thomas char scaled_sent[FMT_SCALED_STRSIZE];
2429 8505ab66 2023-07-10 thomas int p_deltify = 0, p_written = 0, p_sent = 0;
2430 8505ab66 2023-07-10 thomas int print_colored = 0, print_found = 0, print_trees = 0;
2431 8505ab66 2023-07-10 thomas int print_searching = 0, print_total = 0;
2432 8505ab66 2023-07-10 thomas int print_deltify = 0, print_written = 0, print_sent = 0;
2433 8505ab66 2023-07-10 thomas
2434 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2435 8505ab66 2023-07-10 thomas return NULL;
2436 8505ab66 2023-07-10 thomas
2437 8505ab66 2023-07-10 thomas if (refname) {
2438 8505ab66 2023-07-10 thomas const char *status = success ? "accepted" : "rejected";
2439 8505ab66 2023-07-10 thomas
2440 8505ab66 2023-07-10 thomas if (success) {
2441 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2442 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, a->delete_branches, entry) {
2443 8505ab66 2023-07-10 thomas const char *branchname = pe->path;
2444 8505ab66 2023-07-10 thomas if (got_path_cmp(branchname, refname,
2445 8505ab66 2023-07-10 thomas strlen(branchname), strlen(refname)) == 0) {
2446 8505ab66 2023-07-10 thomas status = "deleted";
2447 8505ab66 2023-07-10 thomas a->sent_something = 1;
2448 8505ab66 2023-07-10 thomas break;
2449 8505ab66 2023-07-10 thomas }
2450 8505ab66 2023-07-10 thomas }
2451 8505ab66 2023-07-10 thomas }
2452 8505ab66 2023-07-10 thomas
2453 8505ab66 2023-07-10 thomas if (a->printed_something)
2454 8505ab66 2023-07-10 thomas putchar('\n');
2455 8505ab66 2023-07-10 thomas printf("Server has %s %s", status, refname);
2456 8505ab66 2023-07-10 thomas if (errmsg)
2457 8505ab66 2023-07-10 thomas printf(": %s", errmsg);
2458 8505ab66 2023-07-10 thomas a->printed_something = 1;
2459 8505ab66 2023-07-10 thomas return NULL;
2460 8505ab66 2023-07-10 thomas }
2461 8505ab66 2023-07-10 thomas
2462 8505ab66 2023-07-10 thomas if (a->last_ncolored != ncolored) {
2463 8505ab66 2023-07-10 thomas print_colored = 1;
2464 8505ab66 2023-07-10 thomas a->last_ncolored = ncolored;
2465 8505ab66 2023-07-10 thomas }
2466 8505ab66 2023-07-10 thomas
2467 8505ab66 2023-07-10 thomas if (a->last_nfound != nfound) {
2468 8505ab66 2023-07-10 thomas print_colored = 1;
2469 8505ab66 2023-07-10 thomas print_found = 1;
2470 8505ab66 2023-07-10 thomas a->last_nfound = nfound;
2471 8505ab66 2023-07-10 thomas }
2472 8505ab66 2023-07-10 thomas
2473 8505ab66 2023-07-10 thomas if (a->last_ntrees != ntrees) {
2474 8505ab66 2023-07-10 thomas print_colored = 1;
2475 8505ab66 2023-07-10 thomas print_found = 1;
2476 8505ab66 2023-07-10 thomas print_trees = 1;
2477 8505ab66 2023-07-10 thomas a->last_ntrees = ntrees;
2478 8505ab66 2023-07-10 thomas }
2479 8505ab66 2023-07-10 thomas
2480 8505ab66 2023-07-10 thomas if ((print_colored || print_found || print_trees) &&
2481 8505ab66 2023-07-10 thomas !a->loading_done) {
2482 8505ab66 2023-07-10 thomas printf("\r");
2483 8505ab66 2023-07-10 thomas print_load_info(print_colored, print_found, print_trees,
2484 8505ab66 2023-07-10 thomas ncolored, nfound, ntrees);
2485 8505ab66 2023-07-10 thomas a->printed_something = 1;
2486 8505ab66 2023-07-10 thomas fflush(stdout);
2487 8505ab66 2023-07-10 thomas return NULL;
2488 8505ab66 2023-07-10 thomas } else if (!a->loading_done) {
2489 8505ab66 2023-07-10 thomas printf("\r");
2490 8505ab66 2023-07-10 thomas print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2491 8505ab66 2023-07-10 thomas printf("\n");
2492 8505ab66 2023-07-10 thomas a->loading_done = 1;
2493 8505ab66 2023-07-10 thomas }
2494 8505ab66 2023-07-10 thomas
2495 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2496 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2497 8505ab66 2023-07-10 thomas if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2498 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2499 8505ab66 2023-07-10 thomas
2500 8505ab66 2023-07-10 thomas if (a->last_ncommits != ncommits) {
2501 8505ab66 2023-07-10 thomas print_searching = 1;
2502 8505ab66 2023-07-10 thomas a->last_ncommits = ncommits;
2503 8505ab66 2023-07-10 thomas }
2504 8505ab66 2023-07-10 thomas
2505 8505ab66 2023-07-10 thomas if (a->last_nobj_total != nobj_total) {
2506 8505ab66 2023-07-10 thomas print_searching = 1;
2507 8505ab66 2023-07-10 thomas print_total = 1;
2508 8505ab66 2023-07-10 thomas a->last_nobj_total = nobj_total;
2509 8505ab66 2023-07-10 thomas }
2510 8505ab66 2023-07-10 thomas
2511 8505ab66 2023-07-10 thomas if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2512 8505ab66 2023-07-10 thomas strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2513 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2514 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2515 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2516 8505ab66 2023-07-10 thomas }
2517 8505ab66 2023-07-10 thomas
2518 8505ab66 2023-07-10 thomas if (nobj_deltify > 0 || nobj_written > 0) {
2519 8505ab66 2023-07-10 thomas if (nobj_deltify > 0) {
2520 8505ab66 2023-07-10 thomas p_deltify = (nobj_deltify * 100) / nobj_total;
2521 8505ab66 2023-07-10 thomas if (p_deltify != a->last_p_deltify) {
2522 8505ab66 2023-07-10 thomas a->last_p_deltify = p_deltify;
2523 8505ab66 2023-07-10 thomas print_searching = 1;
2524 8505ab66 2023-07-10 thomas print_total = 1;
2525 8505ab66 2023-07-10 thomas print_deltify = 1;
2526 8505ab66 2023-07-10 thomas }
2527 8505ab66 2023-07-10 thomas }
2528 8505ab66 2023-07-10 thomas if (nobj_written > 0) {
2529 8505ab66 2023-07-10 thomas p_written = (nobj_written * 100) / nobj_total;
2530 8505ab66 2023-07-10 thomas if (p_written != a->last_p_written) {
2531 8505ab66 2023-07-10 thomas a->last_p_written = p_written;
2532 8505ab66 2023-07-10 thomas print_searching = 1;
2533 8505ab66 2023-07-10 thomas print_total = 1;
2534 8505ab66 2023-07-10 thomas print_deltify = 1;
2535 8505ab66 2023-07-10 thomas print_written = 1;
2536 8505ab66 2023-07-10 thomas }
2537 8505ab66 2023-07-10 thomas }
2538 8505ab66 2023-07-10 thomas }
2539 8505ab66 2023-07-10 thomas
2540 8505ab66 2023-07-10 thomas if (bytes_sent > 0) {
2541 8505ab66 2023-07-10 thomas p_sent = (bytes_sent * 100) / packfile_size;
2542 8505ab66 2023-07-10 thomas if (p_sent != a->last_p_sent) {
2543 8505ab66 2023-07-10 thomas a->last_p_sent = p_sent;
2544 8505ab66 2023-07-10 thomas print_searching = 1;
2545 8505ab66 2023-07-10 thomas print_total = 1;
2546 8505ab66 2023-07-10 thomas print_deltify = 1;
2547 8505ab66 2023-07-10 thomas print_written = 1;
2548 8505ab66 2023-07-10 thomas print_sent = 1;
2549 8505ab66 2023-07-10 thomas }
2550 8505ab66 2023-07-10 thomas a->sent_something = 1;
2551 8505ab66 2023-07-10 thomas }
2552 8505ab66 2023-07-10 thomas
2553 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify || print_written ||
2554 8505ab66 2023-07-10 thomas print_sent)
2555 8505ab66 2023-07-10 thomas printf("\r");
2556 8505ab66 2023-07-10 thomas if (print_searching)
2557 8505ab66 2023-07-10 thomas printf("packing %d reference%s", ncommits,
2558 8505ab66 2023-07-10 thomas ncommits == 1 ? "" : "s");
2559 8505ab66 2023-07-10 thomas if (print_total)
2560 8505ab66 2023-07-10 thomas printf("; %d object%s", nobj_total,
2561 8505ab66 2023-07-10 thomas nobj_total == 1 ? "" : "s");
2562 8505ab66 2023-07-10 thomas if (print_deltify)
2563 8505ab66 2023-07-10 thomas printf("; deltify: %d%%", p_deltify);
2564 8505ab66 2023-07-10 thomas if (print_sent)
2565 8505ab66 2023-07-10 thomas printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2566 8505ab66 2023-07-10 thomas scaled_packsize, p_sent);
2567 8505ab66 2023-07-10 thomas else if (print_written)
2568 8505ab66 2023-07-10 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2569 8505ab66 2023-07-10 thomas scaled_packsize, p_written);
2570 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify ||
2571 8505ab66 2023-07-10 thomas print_written || print_sent) {
2572 8505ab66 2023-07-10 thomas a->printed_something = 1;
2573 8505ab66 2023-07-10 thomas fflush(stdout);
2574 8505ab66 2023-07-10 thomas }
2575 8505ab66 2023-07-10 thomas return NULL;
2576 8505ab66 2023-07-10 thomas }
2577 8505ab66 2023-07-10 thomas
2578 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg {
2579 8505ab66 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
2580 8505ab66 2023-07-10 thomas int last_p_indexed;
2581 8505ab66 2023-07-10 thomas int last_p_resolved;
2582 8505ab66 2023-07-10 thomas int verbosity;
2583 8505ab66 2023-07-10 thomas
2584 8505ab66 2023-07-10 thomas struct got_repository *repo;
2585 8505ab66 2023-07-10 thomas };
2586 8505ab66 2023-07-10 thomas
2587 8505ab66 2023-07-10 thomas static const struct got_error *
2588 8505ab66 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
2589 8505ab66 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2590 8505ab66 2023-07-10 thomas {
2591 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
2592 8505ab66 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
2593 8505ab66 2023-07-10 thomas int p_indexed, p_resolved;
2594 8505ab66 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
2595 8505ab66 2023-07-10 thomas
2596 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2597 8505ab66 2023-07-10 thomas return NULL;
2598 8505ab66 2023-07-10 thomas
2599 8505ab66 2023-07-10 thomas if (message && message[0] != '\0') {
2600 8505ab66 2023-07-10 thomas printf("\rserver: %s", message);
2601 8505ab66 2023-07-10 thomas fflush(stdout);
2602 8505ab66 2023-07-10 thomas return NULL;
2603 8505ab66 2023-07-10 thomas }
2604 8505ab66 2023-07-10 thomas
2605 8505ab66 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
2606 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2607 8505ab66 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
2608 8505ab66 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
2609 8505ab66 2023-07-10 thomas print_size = 1;
2610 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
2611 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2612 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2613 8505ab66 2023-07-10 thomas }
2614 8505ab66 2023-07-10 thomas if (nobj_indexed > 0) {
2615 8505ab66 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
2616 8505ab66 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
2617 8505ab66 2023-07-10 thomas a->last_p_indexed = p_indexed;
2618 8505ab66 2023-07-10 thomas print_indexed = 1;
2619 8505ab66 2023-07-10 thomas print_size = 1;
2620 8505ab66 2023-07-10 thomas }
2621 8505ab66 2023-07-10 thomas }
2622 8505ab66 2023-07-10 thomas if (nobj_resolved > 0) {
2623 8505ab66 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
2624 8505ab66 2023-07-10 thomas (nobj_total - nobj_loose);
2625 8505ab66 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
2626 8505ab66 2023-07-10 thomas a->last_p_resolved = p_resolved;
2627 8505ab66 2023-07-10 thomas print_resolved = 1;
2628 8505ab66 2023-07-10 thomas print_indexed = 1;
2629 8505ab66 2023-07-10 thomas print_size = 1;
2630 8505ab66 2023-07-10 thomas }
2631 8505ab66 2023-07-10 thomas }
2632 8505ab66 2023-07-10 thomas
2633 8505ab66 2023-07-10 thomas }
2634 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
2635 8505ab66 2023-07-10 thomas printf("\r");
2636 8505ab66 2023-07-10 thomas if (print_size)
2637 8505ab66 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2638 8505ab66 2023-07-10 thomas if (print_indexed)
2639 8505ab66 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
2640 8505ab66 2023-07-10 thomas if (print_resolved)
2641 8505ab66 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
2642 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved) {
2643 8505ab66 2023-07-10 thomas putchar('\n');
2644 8505ab66 2023-07-10 thomas fflush(stdout);
2645 8505ab66 2023-07-10 thomas }
2646 8505ab66 2023-07-10 thomas
2647 8505ab66 2023-07-10 thomas return NULL;
2648 8505ab66 2023-07-10 thomas }
2649 8505ab66 2023-07-10 thomas
2650 8505ab66 2023-07-10 thomas static const struct got_error *
2651 8505ab66 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
2652 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2653 8505ab66 2023-07-10 thomas {
2654 8505ab66 2023-07-10 thomas const struct got_error *err;
2655 8505ab66 2023-07-10 thomas struct got_reference *head_symref;
2656 8505ab66 2023-07-10 thomas
2657 8505ab66 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2658 8505ab66 2023-07-10 thomas if (err)
2659 8505ab66 2023-07-10 thomas return err;
2660 8505ab66 2023-07-10 thomas
2661 8505ab66 2023-07-10 thomas err = got_ref_write(head_symref, repo);
2662 8505ab66 2023-07-10 thomas if (err == NULL && verbosity > 0) {
2663 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
2664 8505ab66 2023-07-10 thomas got_ref_get_name(target_ref));
2665 8505ab66 2023-07-10 thomas }
2666 8505ab66 2023-07-10 thomas got_ref_close(head_symref);
2667 8505ab66 2023-07-10 thomas return err;
2668 8505ab66 2023-07-10 thomas }
2669 8505ab66 2023-07-10 thomas
2670 8505ab66 2023-07-10 thomas static const struct got_error *
2671 8505ab66 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
2672 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2673 8505ab66 2023-07-10 thomas {
2674 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2675 8505ab66 2023-07-10 thomas struct got_reference *ref;
2676 8505ab66 2023-07-10 thomas char *id_str;
2677 8505ab66 2023-07-10 thomas
2678 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, id);
2679 8505ab66 2023-07-10 thomas if (err)
2680 8505ab66 2023-07-10 thomas return err;
2681 8505ab66 2023-07-10 thomas
2682 8505ab66 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
2683 8505ab66 2023-07-10 thomas if (err)
2684 8505ab66 2023-07-10 thomas goto done;
2685 8505ab66 2023-07-10 thomas
2686 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2687 8505ab66 2023-07-10 thomas got_ref_close(ref);
2688 8505ab66 2023-07-10 thomas
2689 8505ab66 2023-07-10 thomas if (err == NULL && verbosity >= 0)
2690 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
2691 8505ab66 2023-07-10 thomas done:
2692 8505ab66 2023-07-10 thomas free(id_str);
2693 8505ab66 2023-07-10 thomas return err;
2694 8505ab66 2023-07-10 thomas }
2695 8505ab66 2023-07-10 thomas
2696 8505ab66 2023-07-10 thomas static const struct got_error *
2697 8505ab66 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
2698 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2699 8505ab66 2023-07-10 thomas {
2700 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2701 8505ab66 2023-07-10 thomas char *new_id_str = NULL;
2702 8505ab66 2023-07-10 thomas struct got_object_id *old_id = NULL;
2703 8505ab66 2023-07-10 thomas
2704 8505ab66 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
2705 8505ab66 2023-07-10 thomas if (err)
2706 8505ab66 2023-07-10 thomas goto done;
2707 8505ab66 2023-07-10 thomas
2708 8505ab66 2023-07-10 thomas if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2709 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2710 8505ab66 2023-07-10 thomas if (err)
2711 8505ab66 2023-07-10 thomas goto done;
2712 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2713 8505ab66 2023-07-10 thomas goto done;
2714 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2715 8505ab66 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
2716 8505ab66 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
2717 8505ab66 2023-07-10 thomas }
2718 8505ab66 2023-07-10 thomas goto done;
2719 8505ab66 2023-07-10 thomas }
2720 8505ab66 2023-07-10 thomas
2721 8505ab66 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
2722 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2723 8505ab66 2023-07-10 thomas printf("Replacing reference %s: %s\n",
2724 8505ab66 2023-07-10 thomas got_ref_get_name(ref),
2725 8505ab66 2023-07-10 thomas got_ref_get_symref_target(ref));
2726 8505ab66 2023-07-10 thomas }
2727 8505ab66 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
2728 8505ab66 2023-07-10 thomas if (err)
2729 8505ab66 2023-07-10 thomas goto done;
2730 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2731 8505ab66 2023-07-10 thomas if (err)
2732 8505ab66 2023-07-10 thomas goto done;
2733 8505ab66 2023-07-10 thomas } else {
2734 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2735 8505ab66 2023-07-10 thomas if (err)
2736 8505ab66 2023-07-10 thomas goto done;
2737 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2738 8505ab66 2023-07-10 thomas goto done;
2739 8505ab66 2023-07-10 thomas
2740 8505ab66 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
2741 8505ab66 2023-07-10 thomas if (err)
2742 8505ab66 2023-07-10 thomas goto done;
2743 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2744 8505ab66 2023-07-10 thomas if (err)
2745 8505ab66 2023-07-10 thomas goto done;
2746 8505ab66 2023-07-10 thomas }
2747 8505ab66 2023-07-10 thomas
2748 8505ab66 2023-07-10 thomas if (verbosity >= 0)
2749 8505ab66 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
2750 8505ab66 2023-07-10 thomas new_id_str);
2751 8505ab66 2023-07-10 thomas done:
2752 8505ab66 2023-07-10 thomas free(old_id);
2753 8505ab66 2023-07-10 thomas free(new_id_str);
2754 8505ab66 2023-07-10 thomas return err;
2755 8505ab66 2023-07-10 thomas }
2756 8505ab66 2023-07-10 thomas
2757 8505ab66 2023-07-10 thomas static const struct got_error *
2758 8505ab66 2023-07-10 thomas fetch_updated_remote(const char *proto, const char *host, const char *port,
2759 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2760 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote, struct got_repository *repo,
2761 8505ab66 2023-07-10 thomas struct got_reference *head_ref, const char *head_refname)
2762 8505ab66 2023-07-10 thomas {
2763 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlock_err = NULL;
2764 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2765 8505ab66 2023-07-10 thomas struct got_pathlist_head learned_refs;
2766 8505ab66 2023-07-10 thomas struct got_pathlist_head symrefs;
2767 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_branches;
2768 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_refs;
2769 8505ab66 2023-07-10 thomas struct got_object_id *pack_hash;
2770 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2771 8505ab66 2023-07-10 thomas int fetchfd = -1;
2772 8505ab66 2023-07-10 thomas pid_t fetchpid = -1;
2773 8505ab66 2023-07-10 thomas
2774 8505ab66 2023-07-10 thomas TAILQ_INIT(&learned_refs);
2775 8505ab66 2023-07-10 thomas TAILQ_INIT(&symrefs);
2776 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2777 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2778 8505ab66 2023-07-10 thomas
2779 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2780 8505ab66 2023-07-10 thomas NULL);
2781 8505ab66 2023-07-10 thomas if (err)
2782 8505ab66 2023-07-10 thomas goto done;
2783 8505ab66 2023-07-10 thomas
2784 8505ab66 2023-07-10 thomas err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2785 8505ab66 2023-07-10 thomas port, server_path, verbosity);
2786 8505ab66 2023-07-10 thomas if (err)
2787 8505ab66 2023-07-10 thomas goto done;
2788 8505ab66 2023-07-10 thomas
2789 8505ab66 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2790 8505ab66 2023-07-10 thomas fpa.last_p_indexed = -1;
2791 8505ab66 2023-07-10 thomas fpa.last_p_resolved = -1;
2792 8505ab66 2023-07-10 thomas fpa.verbosity = verbosity;
2793 8505ab66 2023-07-10 thomas fpa.repo = repo;
2794 8505ab66 2023-07-10 thomas
2795 8505ab66 2023-07-10 thomas err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2796 8505ab66 2023-07-10 thomas remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2797 8505ab66 2023-07-10 thomas fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2798 8505ab66 2023-07-10 thomas if (err)
2799 8505ab66 2023-07-10 thomas goto done;
2800 8505ab66 2023-07-10 thomas
2801 8505ab66 2023-07-10 thomas /* Update references provided with the pack file. */
2802 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &learned_refs, entry) {
2803 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2804 8505ab66 2023-07-10 thomas struct got_object_id *id = pe->data;
2805 8505ab66 2023-07-10 thomas struct got_reference *ref;
2806 8505ab66 2023-07-10 thomas
2807 8505ab66 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
2808 8505ab66 2023-07-10 thomas if (err) {
2809 8505ab66 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2810 8505ab66 2023-07-10 thomas goto done;
2811 8505ab66 2023-07-10 thomas err = create_ref(refname, id, verbosity, repo);
2812 8505ab66 2023-07-10 thomas if (err)
2813 8505ab66 2023-07-10 thomas goto done;
2814 8505ab66 2023-07-10 thomas } else {
2815 8505ab66 2023-07-10 thomas err = update_ref(ref, id, verbosity, repo);
2816 8505ab66 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2817 8505ab66 2023-07-10 thomas if (unlock_err && err == NULL)
2818 8505ab66 2023-07-10 thomas err = unlock_err;
2819 8505ab66 2023-07-10 thomas got_ref_close(ref);
2820 8505ab66 2023-07-10 thomas if (err)
2821 8505ab66 2023-07-10 thomas goto done;
2822 8505ab66 2023-07-10 thomas }
2823 8505ab66 2023-07-10 thomas }
2824 8505ab66 2023-07-10 thomas
2825 8505ab66 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
2826 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
2827 8505ab66 2023-07-10 thomas struct got_reference *target_ref;
2828 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2829 8505ab66 2023-07-10 thomas const char *target = pe->data;
2830 8505ab66 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
2831 8505ab66 2023-07-10 thomas
2832 8505ab66 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
2833 8505ab66 2023-07-10 thomas continue;
2834 8505ab66 2023-07-10 thomas
2835 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, target, 0);
2836 8505ab66 2023-07-10 thomas if (err) {
2837 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2838 8505ab66 2023-07-10 thomas err = NULL;
2839 8505ab66 2023-07-10 thomas continue;
2840 8505ab66 2023-07-10 thomas }
2841 8505ab66 2023-07-10 thomas goto done;
2842 8505ab66 2023-07-10 thomas }
2843 8505ab66 2023-07-10 thomas
2844 8505ab66 2023-07-10 thomas err = create_symref(refname, target_ref, verbosity, repo);
2845 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2846 8505ab66 2023-07-10 thomas if (err)
2847 8505ab66 2023-07-10 thomas goto done;
2848 8505ab66 2023-07-10 thomas
2849 8505ab66 2023-07-10 thomas if (remote->mirror_references)
2850 8505ab66 2023-07-10 thomas continue;
2851 8505ab66 2023-07-10 thomas
2852 8505ab66 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
2853 8505ab66 2023-07-10 thomas continue;
2854 8505ab66 2023-07-10 thomas
2855 8505ab66 2023-07-10 thomas if (asprintf(&remote_refname,
2856 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2857 8505ab66 2023-07-10 thomas refname) == -1) {
2858 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2859 8505ab66 2023-07-10 thomas goto done;
2860 8505ab66 2023-07-10 thomas }
2861 8505ab66 2023-07-10 thomas if (asprintf(&remote_target,
2862 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2863 8505ab66 2023-07-10 thomas target + 11) == -1) {
2864 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2865 8505ab66 2023-07-10 thomas free(remote_refname);
2866 8505ab66 2023-07-10 thomas goto done;
2867 8505ab66 2023-07-10 thomas }
2868 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, remote_target, 0);
2869 8505ab66 2023-07-10 thomas if (err) {
2870 8505ab66 2023-07-10 thomas free(remote_refname);
2871 8505ab66 2023-07-10 thomas free(remote_target);
2872 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2873 8505ab66 2023-07-10 thomas err = NULL;
2874 8505ab66 2023-07-10 thomas continue;
2875 8505ab66 2023-07-10 thomas }
2876 8505ab66 2023-07-10 thomas goto done;
2877 8505ab66 2023-07-10 thomas }
2878 8505ab66 2023-07-10 thomas err = create_symref(remote_refname, target_ref,
2879 8505ab66 2023-07-10 thomas verbosity - 1, repo);
2880 8505ab66 2023-07-10 thomas free(remote_refname);
2881 8505ab66 2023-07-10 thomas free(remote_target);
2882 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2883 8505ab66 2023-07-10 thomas if (err)
2884 8505ab66 2023-07-10 thomas goto done;
2885 8505ab66 2023-07-10 thomas }
2886 8505ab66 2023-07-10 thomas
2887 8505ab66 2023-07-10 thomas done:
2888 8505ab66 2023-07-10 thomas got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2889 8505ab66 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2890 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2891 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2892 8505ab66 2023-07-10 thomas return err;
2893 8505ab66 2023-07-10 thomas }
2894 8505ab66 2023-07-10 thomas
2895 8505ab66 2023-07-10 thomas
2896 8505ab66 2023-07-10 thomas const struct got_error *
2897 8505ab66 2023-07-10 thomas got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2898 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_pathlist_head *paths,
2899 8505ab66 2023-07-10 thomas const char *author, const char *committer, int allow_bad_symlinks,
2900 8505ab66 2023-07-10 thomas int show_diff, int commit_conflicts,
2901 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2902 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2903 8505ab66 2023-07-10 thomas const char *proto, const char *host, const char *port,
2904 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2905 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote,
2906 8505ab66 2023-07-10 thomas got_cancel_cb check_cancelled,
2907 8505ab66 2023-07-10 thomas struct got_repository *repo)
2908 8505ab66 2023-07-10 thomas {
2909 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2910 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
2911 8505ab66 2023-07-10 thomas char *fileindex_path = NULL;
2912 8505ab66 2023-07-10 thomas struct got_pathlist_head commitable_paths;
2913 8505ab66 2023-07-10 thomas struct collect_commitables_arg cc_arg;
2914 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2915 8505ab66 2023-07-10 thomas struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2916 8505ab66 2023-07-10 thomas struct got_reference *commit_ref = NULL;
2917 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2918 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id2 = NULL;
2919 8505ab66 2023-07-10 thomas char *head_refname = NULL;
2920 8505ab66 2023-07-10 thomas char *commit_refname = NULL;
2921 8505ab66 2023-07-10 thomas char *diff_path = NULL;
2922 8505ab66 2023-07-10 thomas int have_staged_files = 0;
2923 8505ab66 2023-07-10 thomas int sendfd = -1;
2924 8505ab66 2023-07-10 thomas pid_t sendpid = -1;
2925 8505ab66 2023-07-10 thomas struct got_send_progress_arg spa;
2926 8505ab66 2023-07-10 thomas struct got_pathlist_head commit_reflist;
2927 8505ab66 2023-07-10 thomas struct got_pathlist_head tag_names;
2928 8505ab66 2023-07-10 thomas struct got_pathlist_head delete_branches;
2929 8505ab66 2023-07-10 thomas
2930 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2931 8505ab66 2023-07-10 thomas
2932 8505ab66 2023-07-10 thomas memset(&cc_arg, 0, sizeof(cc_arg));
2933 8505ab66 2023-07-10 thomas TAILQ_INIT(&commitable_paths);
2934 8505ab66 2023-07-10 thomas TAILQ_INIT(&commit_reflist);
2935 8505ab66 2023-07-10 thomas TAILQ_INIT(&tag_names);
2936 8505ab66 2023-07-10 thomas TAILQ_INIT(&delete_branches);
2937 8505ab66 2023-07-10 thomas
2938 8505ab66 2023-07-10 thomas err = lock_worktree(worktree, LOCK_EX);
2939 8505ab66 2023-07-10 thomas if (err)
2940 8505ab66 2023-07-10 thomas goto done;
2941 8505ab66 2023-07-10 thomas
2942 8505ab66 2023-07-10 thomas err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2943 8505ab66 2023-07-10 thomas worktree);
2944 8505ab66 2023-07-10 thomas if (err)
2945 8505ab66 2023-07-10 thomas goto done;
2946 8505ab66 2023-07-10 thomas
2947 8505ab66 2023-07-10 thomas head_refname = worktree->head_ref_name;
2948 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref, repo, head_refname, 0);
2949 8505ab66 2023-07-10 thomas if (err)
2950 8505ab66 2023-07-10 thomas goto done;
2951 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2952 8505ab66 2023-07-10 thomas if (err)
2953 8505ab66 2023-07-10 thomas goto done;
2954 8505ab66 2023-07-10 thomas
2955 8505ab66 2023-07-10 thomas err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2956 8505ab66 2023-07-10 thomas if (err)
2957 8505ab66 2023-07-10 thomas goto done;
2958 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
2959 8505ab66 2023-07-10 thomas if (err)
2960 8505ab66 2023-07-10 thomas goto done;
2961 8505ab66 2023-07-10 thomas
2962 8505ab66 2023-07-10 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
2963 8505ab66 2023-07-10 thomas if (err)
2964 8505ab66 2023-07-10 thomas goto done;
2965 8505ab66 2023-07-10 thomas
2966 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2967 8505ab66 2023-07-10 thomas &have_staged_files);
2968 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_CANCELLED)
2969 8505ab66 2023-07-10 thomas goto done;
2970 8505ab66 2023-07-10 thomas if (have_staged_files) {
2971 8505ab66 2023-07-10 thomas err = check_non_staged_files(fileindex, paths);
2972 8505ab66 2023-07-10 thomas if (err)
2973 8505ab66 2023-07-10 thomas goto done;
2974 8505ab66 2023-07-10 thomas }
2975 8505ab66 2023-07-10 thomas
2976 8505ab66 2023-07-10 thomas cc_arg.commitable_paths = &commitable_paths;
2977 8505ab66 2023-07-10 thomas cc_arg.worktree = worktree;
2978 8505ab66 2023-07-10 thomas cc_arg.fileindex = fileindex;
2979 8505ab66 2023-07-10 thomas cc_arg.repo = repo;
2980 8505ab66 2023-07-10 thomas cc_arg.have_staged_files = have_staged_files;
2981 8505ab66 2023-07-10 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2982 8505ab66 2023-07-10 thomas cc_arg.diff_header_shown = 0;
2983 8505ab66 2023-07-10 thomas cc_arg.commit_conflicts = commit_conflicts;
2984 8505ab66 2023-07-10 thomas if (show_diff) {
2985 8505ab66 2023-07-10 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2986 8505ab66 2023-07-10 thomas GOT_TMPDIR_STR "/got", ".diff");
2987 8505ab66 2023-07-10 thomas if (err)
2988 8505ab66 2023-07-10 thomas goto done;
2989 8505ab66 2023-07-10 thomas cc_arg.f1 = got_opentemp();
2990 8505ab66 2023-07-10 thomas if (cc_arg.f1 == NULL) {
2991 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
2992 8505ab66 2023-07-10 thomas goto done;
2993 8505ab66 2023-07-10 thomas }
2994 8505ab66 2023-07-10 thomas cc_arg.f2 = got_opentemp();
2995 8505ab66 2023-07-10 thomas if (cc_arg.f2 == NULL) {
2996 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
2997 8505ab66 2023-07-10 thomas goto done;
2998 8505ab66 2023-07-10 thomas }
2999 8505ab66 2023-07-10 thomas }
3000 8505ab66 2023-07-10 thomas
3001 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
3002 8505ab66 2023-07-10 thomas err = worktree_status(worktree, pe->path, fileindex, repo,
3003 8505ab66 2023-07-10 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3004 8505ab66 2023-07-10 thomas if (err)
3005 8505ab66 2023-07-10 thomas goto done;
3006 8505ab66 2023-07-10 thomas }
3007 8505ab66 2023-07-10 thomas
3008 8505ab66 2023-07-10 thomas if (show_diff) {
3009 8505ab66 2023-07-10 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
3010 8505ab66 2023-07-10 thomas err = got_error_from_errno("fflush");
3011 8505ab66 2023-07-10 thomas goto done;
3012 8505ab66 2023-07-10 thomas }
3013 8505ab66 2023-07-10 thomas }
3014 8505ab66 2023-07-10 thomas
3015 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&commitable_paths)) {
3016 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3017 8505ab66 2023-07-10 thomas goto done;
3018 8505ab66 2023-07-10 thomas }
3019 8505ab66 2023-07-10 thomas
3020 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
3021 8505ab66 2023-07-10 thomas err = check_path_is_commitable(pe->path, &commitable_paths);
3022 8505ab66 2023-07-10 thomas if (err)
3023 8505ab66 2023-07-10 thomas goto done;
3024 8505ab66 2023-07-10 thomas }
3025 8505ab66 2023-07-10 thomas
3026 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3027 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3028 8505ab66 2023-07-10 thomas const char *ct_path = ct->in_repo_path;
3029 8505ab66 2023-07-10 thomas
3030 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
3031 8505ab66 2023-07-10 thomas ct_path++;
3032 8505ab66 2023-07-10 thomas err = check_out_of_date(ct_path, ct->status,
3033 8505ab66 2023-07-10 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3034 8505ab66 2023-07-10 thomas head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3035 8505ab66 2023-07-10 thomas if (err)
3036 8505ab66 2023-07-10 thomas goto done;
3037 8505ab66 2023-07-10 thomas }
3038 8505ab66 2023-07-10 thomas
3039 8505ab66 2023-07-10 thomas err = commit_worktree(new_commit_id, &commitable_paths,
3040 8505ab66 2023-07-10 thomas head_commit_id, NULL, worktree, author, committer,
3041 8505ab66 2023-07-10 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3042 8505ab66 2023-07-10 thomas commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3043 8505ab66 2023-07-10 thomas if (err)
3044 8505ab66 2023-07-10 thomas goto done;
3045 8505ab66 2023-07-10 thomas
3046 8505ab66 2023-07-10 thomas /*
3047 8505ab66 2023-07-10 thomas * Check if a concurrent commit to our branch has occurred.
3048 8505ab66 2023-07-10 thomas * Lock the reference here to prevent concurrent modification.
3049 8505ab66 2023-07-10 thomas */
3050 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref2, repo, head_refname, 1);
3051 8505ab66 2023-07-10 thomas if (err)
3052 8505ab66 2023-07-10 thomas goto done;
3053 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3054 8505ab66 2023-07-10 thomas if (err)
3055 8505ab66 2023-07-10 thomas goto done;
3056 8505ab66 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3057 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3058 8505ab66 2023-07-10 thomas goto done;
3059 8505ab66 2023-07-10 thomas }
3060 8505ab66 2023-07-10 thomas
3061 8505ab66 2023-07-10 thomas err = got_pathlist_append(&commit_reflist, commit_refname,
3062 8505ab66 2023-07-10 thomas head_refname);
3063 8505ab66 2023-07-10 thomas if (err)
3064 8505ab66 2023-07-10 thomas goto done;
3065 8505ab66 2023-07-10 thomas
3066 8505ab66 2023-07-10 thomas /* Update commit ref in repository. */
3067 8505ab66 2023-07-10 thomas err = got_ref_change_ref(commit_ref, *new_commit_id);
3068 8505ab66 2023-07-10 thomas if (err)
3069 8505ab66 2023-07-10 thomas goto done;
3070 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
3071 8505ab66 2023-07-10 thomas if (err)
3072 8505ab66 2023-07-10 thomas goto done;
3073 8505ab66 2023-07-10 thomas
3074 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
3075 8505ab66 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3076 8505ab66 2023-07-10 thomas remote->name, proto, host,
3077 8505ab66 2023-07-10 thomas port ? ":" : "", port ? port : "",
3078 8505ab66 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
3079 8505ab66 2023-07-10 thomas }
3080 8505ab66 2023-07-10 thomas
3081 8505ab66 2023-07-10 thomas /* Attempt send to remote branch. */
3082 8505ab66 2023-07-10 thomas err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3083 8505ab66 2023-07-10 thomas server_path, verbosity);
3084 8505ab66 2023-07-10 thomas if (err)
3085 8505ab66 2023-07-10 thomas goto done;
3086 8505ab66 2023-07-10 thomas
3087 8505ab66 2023-07-10 thomas memset(&spa, 0, sizeof(spa));
3088 8505ab66 2023-07-10 thomas spa.last_scaled_packsize[0] = '\0';
3089 8505ab66 2023-07-10 thomas spa.last_p_deltify = -1;
3090 8505ab66 2023-07-10 thomas spa.last_p_written = -1;
3091 8505ab66 2023-07-10 thomas spa.verbosity = verbosity;
3092 8505ab66 2023-07-10 thomas spa.delete_branches = &delete_branches;
3093 8505ab66 2023-07-10 thomas err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3094 8505ab66 2023-07-10 thomas &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3095 8505ab66 2023-07-10 thomas check_cancelled, NULL);
3096 8505ab66 2023-07-10 thomas if (spa.printed_something)
3097 8505ab66 2023-07-10 thomas putchar('\n');
3098 8505ab66 2023-07-10 thomas if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3099 8505ab66 2023-07-10 thomas /*
3100 8505ab66 2023-07-10 thomas * Fetch new changes since remote has diverged.
3101 8505ab66 2023-07-10 thomas * No trivial-rebase yet; require update to be run manually.
3102 8505ab66 2023-07-10 thomas */
3103 8505ab66 2023-07-10 thomas err = fetch_updated_remote(proto, host, port, server_path,
3104 8505ab66 2023-07-10 thomas verbosity, remote, repo, head_ref, head_refname);
3105 8505ab66 2023-07-10 thomas if (err == NULL)
3106 8505ab66 2023-07-10 thomas goto done;
3107 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3108 8505ab66 2023-07-10 thomas goto done;
3109 8505ab66 2023-07-10 thomas /* XXX: Rebase commit over fetched remote branch. */
3110 8505ab66 2023-07-10 thomas }
3111 8505ab66 2023-07-10 thomas if (err) {
3112 8505ab66 2023-07-10 thomas goto done;
3113 8505ab66 2023-07-10 thomas }
3114 8505ab66 2023-07-10 thomas
3115 8505ab66 2023-07-10 thomas /* Update branch head in repository. */
3116 8505ab66 2023-07-10 thomas err = got_ref_change_ref(head_ref2, *new_commit_id);
3117 8505ab66 2023-07-10 thomas if (err)
3118 8505ab66 2023-07-10 thomas goto done;
3119 8505ab66 2023-07-10 thomas err = got_ref_write(head_ref2, repo);
3120 8505ab66 2023-07-10 thomas if (err)
3121 8505ab66 2023-07-10 thomas goto done;
3122 8505ab66 2023-07-10 thomas
3123 8505ab66 2023-07-10 thomas err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3124 8505ab66 2023-07-10 thomas if (err)
3125 8505ab66 2023-07-10 thomas goto done;
3126 8505ab66 2023-07-10 thomas
3127 8505ab66 2023-07-10 thomas err = ref_base_commit(worktree, repo);
3128 8505ab66 2023-07-10 thomas if (err)
3129 8505ab66 2023-07-10 thomas goto done;
3130 8505ab66 2023-07-10 thomas
3131 8505ab66 2023-07-10 thomas /* XXX: fileindex must be updated for other fetched changes? */
3132 8505ab66 2023-07-10 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
3133 8505ab66 2023-07-10 thomas *new_commit_id, fileindex, have_staged_files);
3134 8505ab66 2023-07-10 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
3135 8505ab66 2023-07-10 thomas if (sync_err && err == NULL)
3136 8505ab66 2023-07-10 thomas err = sync_err;
3137 8505ab66 2023-07-10 thomas done:
3138 8505ab66 2023-07-10 thomas if (head_ref2) {
3139 8505ab66 2023-07-10 thomas unlockerr = got_ref_unlock(head_ref2);
3140 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3141 8505ab66 2023-07-10 thomas err = unlockerr;
3142 8505ab66 2023-07-10 thomas got_ref_close(head_ref2);
3143 8505ab66 2023-07-10 thomas }
3144 8505ab66 2023-07-10 thomas if (commit_ref)
3145 8505ab66 2023-07-10 thomas got_ref_close(commit_ref);
3146 8505ab66 2023-07-10 thomas if (fileindex)
3147 8505ab66 2023-07-10 thomas got_fileindex_free(fileindex);
3148 8505ab66 2023-07-10 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
3149 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3150 8505ab66 2023-07-10 thomas err = unlockerr;
3151 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3152 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3153 8505ab66 2023-07-10 thomas
3154 8505ab66 2023-07-10 thomas free_commitable(ct);
3155 8505ab66 2023-07-10 thomas }
3156 8505ab66 2023-07-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3157 8505ab66 2023-07-10 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
3158 8505ab66 2023-07-10 thomas err = got_error_from_errno2("unlink", diff_path);
3159 8505ab66 2023-07-10 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3160 8505ab66 2023-07-10 thomas err == NULL)
3161 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
3162 8505ab66 2023-07-10 thomas free(head_commit_id);
3163 8505ab66 2023-07-10 thomas free(head_commit_id2);
3164 8505ab66 2023-07-10 thomas free(commit_refname);
3165 8505ab66 2023-07-10 thomas free(fileindex_path);
3166 8505ab66 2023-07-10 thomas free(diff_path);
3167 8505ab66 2023-07-10 thomas return err;
3168 8505ab66 2023-07-10 thomas }
3169 8505ab66 2023-07-10 thomas
3170 8505ab66 2023-07-10 thomas const struct got_error *
3171 8505ab66 2023-07-10 thomas got_worktree_cvg_get_commit_ref_name(char **refname,
3172 8505ab66 2023-07-10 thomas struct got_worktree *worktree)
3173 8505ab66 2023-07-10 thomas {
3174 8505ab66 2023-07-10 thomas return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);
3175 8505ab66 2023-07-10 thomas }