2 8505ab66 2023-07-10 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 b7eff127 2023-07-10 thomas #include "got_compat.h"
19 8505ab66 2023-07-10 thomas #include <sys/stat.h>
20 8505ab66 2023-07-10 thomas #include <sys/queue.h>
22 8505ab66 2023-07-10 thomas #include <dirent.h>
23 8505ab66 2023-07-10 thomas #include <limits.h>
24 8505ab66 2023-07-10 thomas #include <stddef.h>
25 8505ab66 2023-07-10 thomas #include <string.h>
26 8505ab66 2023-07-10 thomas #include <stdio.h>
27 8505ab66 2023-07-10 thomas #include <stdlib.h>
28 8505ab66 2023-07-10 thomas #include <time.h>
29 8505ab66 2023-07-10 thomas #include <fcntl.h>
30 8505ab66 2023-07-10 thomas #include <errno.h>
31 8505ab66 2023-07-10 thomas #include <unistd.h>
32 8505ab66 2023-07-10 thomas #include <zlib.h>
33 8505ab66 2023-07-10 thomas #include <fnmatch.h>
34 8505ab66 2023-07-10 thomas #include <libgen.h>
36 8505ab66 2023-07-10 thomas #include "got_error.h"
37 8505ab66 2023-07-10 thomas #include "got_repository.h"
38 8505ab66 2023-07-10 thomas #include "got_reference.h"
39 8505ab66 2023-07-10 thomas #include "got_object.h"
40 8505ab66 2023-07-10 thomas #include "got_path.h"
41 8505ab66 2023-07-10 thomas #include "got_cancel.h"
42 8505ab66 2023-07-10 thomas #include "got_worktree.h"
43 8505ab66 2023-07-10 thomas #include "got_worktree_cvg.h"
44 8505ab66 2023-07-10 thomas #include "got_opentemp.h"
45 8505ab66 2023-07-10 thomas #include "got_diff.h"
46 8505ab66 2023-07-10 thomas #include "got_send.h"
47 8505ab66 2023-07-10 thomas #include "got_fetch.h"
49 8505ab66 2023-07-10 thomas #include "got_lib_worktree.h"
50 8505ab66 2023-07-10 thomas #include "got_lib_hash.h"
51 8505ab66 2023-07-10 thomas #include "got_lib_fileindex.h"
52 8505ab66 2023-07-10 thomas #include "got_lib_inflate.h"
53 8505ab66 2023-07-10 thomas #include "got_lib_delta.h"
54 8505ab66 2023-07-10 thomas #include "got_lib_object.h"
55 8505ab66 2023-07-10 thomas #include "got_lib_object_parse.h"
56 8505ab66 2023-07-10 thomas #include "got_lib_object_create.h"
57 8505ab66 2023-07-10 thomas #include "got_lib_object_idset.h"
58 8505ab66 2023-07-10 thomas #include "got_lib_diff.h"
60 8505ab66 2023-07-10 thomas #ifndef MIN
61 8505ab66 2023-07-10 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 8505ab66 2023-07-10 thomas #define GOT_MERGE_LABEL_MERGED "merged change"
65 8505ab66 2023-07-10 thomas #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 8505ab66 2023-07-10 thomas static const struct got_error *
68 8505ab66 2023-07-10 thomas lock_worktree(struct got_worktree *worktree, int operation)
70 8505ab66 2023-07-10 thomas if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
71 8505ab66 2023-07-10 thomas return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
72 8505ab66 2023-07-10 thomas : got_error_from_errno2("flock",
73 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree)));
74 8505ab66 2023-07-10 thomas return NULL;
77 8505ab66 2023-07-10 thomas static const struct got_error *
78 8505ab66 2023-07-10 thomas is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
79 8505ab66 2023-07-10 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path)
81 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
82 8505ab66 2023-07-10 thomas char canonpath[PATH_MAX];
83 8505ab66 2023-07-10 thomas char *path_got = NULL;
85 8505ab66 2023-07-10 thomas *is_bad_symlink = 0;
87 8505ab66 2023-07-10 thomas if (target_len >= sizeof(canonpath)) {
88 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
89 8505ab66 2023-07-10 thomas return NULL;
93 8505ab66 2023-07-10 thomas * We do not use realpath(3) to resolve the symlink's target
94 8505ab66 2023-07-10 thomas * path because we don't want to resolve symlinks recursively.
95 8505ab66 2023-07-10 thomas * Instead we make the path absolute and then canonicalize it.
96 8505ab66 2023-07-10 thomas * Relative symlink target lookup should begin at the directory
97 8505ab66 2023-07-10 thomas * in which the blob object is being installed.
99 8505ab66 2023-07-10 thomas if (!got_path_is_absolute(target_path)) {
100 8505ab66 2023-07-10 thomas char *abspath, *parent;
101 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent, ondisk_path);
103 8505ab66 2023-07-10 thomas return err;
104 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
105 8505ab66 2023-07-10 thomas free(parent);
106 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
108 8505ab66 2023-07-10 thomas free(parent);
109 8505ab66 2023-07-10 thomas if (strlen(abspath) >= sizeof(canonpath)) {
110 8505ab66 2023-07-10 thomas err = got_error_path(abspath, GOT_ERR_BAD_PATH);
111 8505ab66 2023-07-10 thomas free(abspath);
112 8505ab66 2023-07-10 thomas return err;
114 8505ab66 2023-07-10 thomas err = got_canonpath(abspath, canonpath, sizeof(canonpath));
115 8505ab66 2023-07-10 thomas free(abspath);
117 8505ab66 2023-07-10 thomas return err;
119 8505ab66 2023-07-10 thomas err = got_canonpath(target_path, canonpath, sizeof(canonpath));
121 8505ab66 2023-07-10 thomas return err;
124 8505ab66 2023-07-10 thomas /* Only allow symlinks pointing at paths within the work tree. */
125 8505ab66 2023-07-10 thomas if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
126 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
127 8505ab66 2023-07-10 thomas return NULL;
130 8505ab66 2023-07-10 thomas /* Do not allow symlinks pointing into the .got directory. */
131 8505ab66 2023-07-10 thomas if (asprintf(&path_got, "%s/%s", wtroot_path,
132 8505ab66 2023-07-10 thomas GOT_WORKTREE_GOT_DIR) == -1)
133 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
134 8505ab66 2023-07-10 thomas if (got_path_is_child(canonpath, path_got, strlen(path_got)))
135 8505ab66 2023-07-10 thomas *is_bad_symlink = 1;
137 8505ab66 2023-07-10 thomas free(path_got);
138 8505ab66 2023-07-10 thomas return NULL;
142 8505ab66 2023-07-10 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
143 8505ab66 2023-07-10 thomas * conflict marker is found in newly added lines only.
145 8505ab66 2023-07-10 thomas static const struct got_error *
146 8505ab66 2023-07-10 thomas get_modified_file_content_status(unsigned char *status,
147 8505ab66 2023-07-10 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
148 8505ab66 2023-07-10 thomas FILE *ondisk_file)
150 8505ab66 2023-07-10 thomas const struct got_error *err, *free_err;
151 8505ab66 2023-07-10 thomas const char *markers[3] = {
152 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
153 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
154 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_END
156 8505ab66 2023-07-10 thomas FILE *f1 = NULL;
157 8505ab66 2023-07-10 thomas struct got_diffreg_result *diffreg_result = NULL;
158 8505ab66 2023-07-10 thomas struct diff_result *r;
159 8505ab66 2023-07-10 thomas int nchunks_parsed, n, i = 0, ln = 0;
160 8505ab66 2023-07-10 thomas char *line = NULL;
161 8505ab66 2023-07-10 thomas size_t linesize = 0;
162 8505ab66 2023-07-10 thomas ssize_t linelen;
164 8505ab66 2023-07-10 thomas if (*status != GOT_STATUS_MODIFY)
165 8505ab66 2023-07-10 thomas return NULL;
167 8505ab66 2023-07-10 thomas f1 = got_opentemp();
168 8505ab66 2023-07-10 thomas if (f1 == NULL)
169 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp");
171 8505ab66 2023-07-10 thomas if (blob) {
172 8505ab66 2023-07-10 thomas got_object_blob_rewind(blob);
173 8505ab66 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
175 8505ab66 2023-07-10 thomas goto done;
178 8505ab66 2023-07-10 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
179 8505ab66 2023-07-10 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
181 8505ab66 2023-07-10 thomas goto done;
183 8505ab66 2023-07-10 thomas r = diffreg_result->result;
185 8505ab66 2023-07-10 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
186 8505ab66 2023-07-10 thomas struct diff_chunk *c;
187 8505ab66 2023-07-10 thomas struct diff_chunk_context cc = {};
188 8505ab66 2023-07-10 thomas off_t pos;
191 8505ab66 2023-07-10 thomas * We can optimise a little by advancing straight
192 8505ab66 2023-07-10 thomas * to the next chunk if this one has no added lines.
194 8505ab66 2023-07-10 thomas c = diff_chunk_get(r, n);
196 8505ab66 2023-07-10 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
197 8505ab66 2023-07-10 thomas nchunks_parsed = 1;
198 8505ab66 2023-07-10 thomas continue; /* removed or unchanged lines */
201 8505ab66 2023-07-10 thomas pos = diff_chunk_get_right_start_pos(c);
202 8505ab66 2023-07-10 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
203 8505ab66 2023-07-10 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
204 8505ab66 2023-07-10 thomas goto done;
207 8505ab66 2023-07-10 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
208 8505ab66 2023-07-10 thomas ln = cc.right.start;
210 8505ab66 2023-07-10 thomas while (ln < cc.right.end) {
211 8505ab66 2023-07-10 thomas linelen = getline(&line, &linesize, ondisk_file);
212 8505ab66 2023-07-10 thomas if (linelen == -1) {
213 8505ab66 2023-07-10 thomas if (feof(ondisk_file))
215 8505ab66 2023-07-10 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
219 8505ab66 2023-07-10 thomas if (line && strncmp(line, markers[i],
220 8505ab66 2023-07-10 thomas strlen(markers[i])) == 0) {
221 8505ab66 2023-07-10 thomas if (strcmp(markers[i],
222 8505ab66 2023-07-10 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
223 8505ab66 2023-07-10 thomas *status = GOT_STATUS_CONFLICT;
224 8505ab66 2023-07-10 thomas goto done;
233 8505ab66 2023-07-10 thomas free(line);
234 8505ab66 2023-07-10 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
235 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
236 8505ab66 2023-07-10 thomas free_err = got_diffreg_result_free(diffreg_result);
237 8505ab66 2023-07-10 thomas if (err == NULL)
238 8505ab66 2023-07-10 thomas err = free_err;
240 8505ab66 2023-07-10 thomas return err;
243 8505ab66 2023-07-10 thomas static int
244 8505ab66 2023-07-10 thomas xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
246 8505ab66 2023-07-10 thomas mode_t ie_mode = got_fileindex_perms_to_st(ie);
247 8505ab66 2023-07-10 thomas return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
250 8505ab66 2023-07-10 thomas static int
251 8505ab66 2023-07-10 thomas stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
253 8505ab66 2023-07-10 thomas return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
254 8505ab66 2023-07-10 thomas ie->ctime_nsec == sb->st_ctim.tv_nsec &&
255 8505ab66 2023-07-10 thomas ie->mtime_sec == sb->st_mtim.tv_sec &&
256 8505ab66 2023-07-10 thomas ie->mtime_nsec == sb->st_mtim.tv_nsec &&
257 8505ab66 2023-07-10 thomas ie->size == (sb->st_size & 0xffffffff) &&
258 8505ab66 2023-07-10 thomas !xbit_differs(ie, sb->st_mode));
261 8505ab66 2023-07-10 thomas static unsigned char
262 8505ab66 2023-07-10 thomas get_staged_status(struct got_fileindex_entry *ie)
264 8505ab66 2023-07-10 thomas switch (got_fileindex_entry_stage_get(ie)) {
265 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_ADD:
266 8505ab66 2023-07-10 thomas return GOT_STATUS_ADD;
267 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_DELETE:
268 8505ab66 2023-07-10 thomas return GOT_STATUS_DELETE;
269 8505ab66 2023-07-10 thomas case GOT_FILEIDX_STAGE_MODIFY:
270 8505ab66 2023-07-10 thomas return GOT_STATUS_MODIFY;
272 8505ab66 2023-07-10 thomas return GOT_STATUS_NO_CHANGE;
276 8505ab66 2023-07-10 thomas static const struct got_error *
277 8505ab66 2023-07-10 thomas get_symlink_modification_status(unsigned char *status,
278 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie, const char *abspath,
279 8505ab66 2023-07-10 thomas int dirfd, const char *de_name, struct got_blob_object *blob)
281 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
282 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
283 8505ab66 2023-07-10 thomas char etarget[PATH_MAX];
284 8505ab66 2023-07-10 thomas ssize_t elen;
285 8505ab66 2023-07-10 thomas size_t len, target_len = 0;
286 8505ab66 2023-07-10 thomas const uint8_t *buf = got_object_blob_get_read_buf(blob);
287 8505ab66 2023-07-10 thomas size_t hdrlen = got_object_blob_get_hdrlen(blob);
289 8505ab66 2023-07-10 thomas *status = GOT_STATUS_NO_CHANGE;
291 8505ab66 2023-07-10 thomas /* Blob object content specifies the target path of the link. */
293 8505ab66 2023-07-10 thomas err = got_object_blob_read_block(&len, blob);
295 8505ab66 2023-07-10 thomas return err;
296 8505ab66 2023-07-10 thomas if (len + target_len >= sizeof(target_path)) {
298 8505ab66 2023-07-10 thomas * Should not happen. The blob contents were OK
299 8505ab66 2023-07-10 thomas * when this symlink was installed.
301 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
303 8505ab66 2023-07-10 thomas if (len > 0) {
304 8505ab66 2023-07-10 thomas /* Skip blob object header first time around. */
305 8505ab66 2023-07-10 thomas memcpy(target_path + target_len, buf + hdrlen,
306 8505ab66 2023-07-10 thomas len - hdrlen);
307 8505ab66 2023-07-10 thomas target_len += len - hdrlen;
308 8505ab66 2023-07-10 thomas hdrlen = 0;
310 8505ab66 2023-07-10 thomas } while (len != 0);
311 8505ab66 2023-07-10 thomas target_path[target_len] = '\0';
313 8505ab66 2023-07-10 thomas if (dirfd != -1) {
314 8505ab66 2023-07-10 thomas elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
315 8505ab66 2023-07-10 thomas if (elen == -1)
316 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
318 8505ab66 2023-07-10 thomas elen = readlink(abspath, etarget, sizeof(etarget));
319 8505ab66 2023-07-10 thomas if (elen == -1)
320 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
323 8505ab66 2023-07-10 thomas if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
324 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
326 8505ab66 2023-07-10 thomas return NULL;
329 8505ab66 2023-07-10 thomas static const struct got_error *
330 8505ab66 2023-07-10 thomas get_file_status(unsigned char *status, struct stat *sb,
331 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie, const char *abspath,
332 8505ab66 2023-07-10 thomas int dirfd, const char *de_name, struct got_repository *repo)
334 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
335 8505ab66 2023-07-10 thomas struct got_object_id id;
336 8505ab66 2023-07-10 thomas size_t hdrlen;
337 8505ab66 2023-07-10 thomas int fd = -1, fd1 = -1;
338 8505ab66 2023-07-10 thomas FILE *f = NULL;
339 8505ab66 2023-07-10 thomas uint8_t fbuf[8192];
340 8505ab66 2023-07-10 thomas struct got_blob_object *blob = NULL;
341 8505ab66 2023-07-10 thomas size_t flen, blen;
342 8505ab66 2023-07-10 thomas unsigned char staged_status;
344 8505ab66 2023-07-10 thomas staged_status = get_staged_status(ie);
345 8505ab66 2023-07-10 thomas *status = GOT_STATUS_NO_CHANGE;
346 8505ab66 2023-07-10 thomas memset(sb, 0, sizeof(*sb));
349 8505ab66 2023-07-10 thomas * Whenever the caller provides a directory descriptor and a
350 8505ab66 2023-07-10 thomas * directory entry name for the file, use them! This prevents
351 8505ab66 2023-07-10 thomas * race conditions if filesystem paths change beneath our feet.
353 8505ab66 2023-07-10 thomas if (dirfd != -1) {
354 8505ab66 2023-07-10 thomas if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
355 8505ab66 2023-07-10 thomas if (errno == ENOENT) {
356 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
357 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MISSING;
359 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
360 8505ab66 2023-07-10 thomas goto done;
362 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
363 8505ab66 2023-07-10 thomas goto done;
366 8505ab66 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
367 8505ab66 2023-07-10 thomas if (fd == -1 && errno != ENOENT &&
368 8505ab66 2023-07-10 thomas !got_err_open_nofollow_on_symlink())
369 8505ab66 2023-07-10 thomas return got_error_from_errno2("open", abspath);
370 8505ab66 2023-07-10 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
371 8505ab66 2023-07-10 thomas if (lstat(abspath, sb) == -1)
372 8505ab66 2023-07-10 thomas return got_error_from_errno2("lstat", abspath);
373 8505ab66 2023-07-10 thomas } else if (fd == -1 || fstat(fd, sb) == -1) {
374 8505ab66 2023-07-10 thomas if (errno == ENOENT) {
375 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
376 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MISSING;
378 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
379 8505ab66 2023-07-10 thomas goto done;
381 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstat", abspath);
382 8505ab66 2023-07-10 thomas goto done;
386 8505ab66 2023-07-10 thomas if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
387 8505ab66 2023-07-10 thomas *status = GOT_STATUS_OBSTRUCTED;
388 8505ab66 2023-07-10 thomas goto done;
391 8505ab66 2023-07-10 thomas if (!got_fileindex_entry_has_file_on_disk(ie)) {
392 8505ab66 2023-07-10 thomas *status = GOT_STATUS_DELETE;
393 8505ab66 2023-07-10 thomas goto done;
394 8505ab66 2023-07-10 thomas } else if (!got_fileindex_entry_has_blob(ie) &&
395 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_ADD) {
396 8505ab66 2023-07-10 thomas *status = GOT_STATUS_ADD;
397 8505ab66 2023-07-10 thomas goto done;
400 8505ab66 2023-07-10 thomas if (!stat_info_differs(ie, sb))
401 8505ab66 2023-07-10 thomas goto done;
403 8505ab66 2023-07-10 thomas if (S_ISLNK(sb->st_mode) &&
404 8505ab66 2023-07-10 thomas got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
405 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
406 8505ab66 2023-07-10 thomas goto done;
409 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_MODIFY ||
410 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_ADD)
411 8505ab66 2023-07-10 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
413 8505ab66 2023-07-10 thomas got_fileindex_entry_get_blob_id(&id, ie);
415 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
416 8505ab66 2023-07-10 thomas if (fd1 == -1) {
417 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
418 8505ab66 2023-07-10 thomas goto done;
420 8505ab66 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
422 8505ab66 2023-07-10 thomas goto done;
424 8505ab66 2023-07-10 thomas if (S_ISLNK(sb->st_mode)) {
425 8505ab66 2023-07-10 thomas err = get_symlink_modification_status(status, ie,
426 8505ab66 2023-07-10 thomas abspath, dirfd, de_name, blob);
427 8505ab66 2023-07-10 thomas goto done;
430 8505ab66 2023-07-10 thomas if (dirfd != -1) {
431 8505ab66 2023-07-10 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
432 8505ab66 2023-07-10 thomas if (fd == -1) {
433 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat", abspath);
434 8505ab66 2023-07-10 thomas goto done;
438 8505ab66 2023-07-10 thomas f = fdopen(fd, "r");
439 8505ab66 2023-07-10 thomas if (f == NULL) {
440 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
441 8505ab66 2023-07-10 thomas goto done;
444 8505ab66 2023-07-10 thomas hdrlen = got_object_blob_get_hdrlen(blob);
445 8505ab66 2023-07-10 thomas for (;;) {
446 8505ab66 2023-07-10 thomas const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
447 8505ab66 2023-07-10 thomas err = got_object_blob_read_block(&blen, blob);
449 8505ab66 2023-07-10 thomas goto done;
450 8505ab66 2023-07-10 thomas /* Skip length of blob object header first time around. */
451 8505ab66 2023-07-10 thomas flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
452 8505ab66 2023-07-10 thomas if (flen == 0 && ferror(f)) {
453 8505ab66 2023-07-10 thomas err = got_error_from_errno("fread");
454 8505ab66 2023-07-10 thomas goto done;
456 8505ab66 2023-07-10 thomas if (blen - hdrlen == 0) {
457 8505ab66 2023-07-10 thomas if (flen != 0)
458 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
460 8505ab66 2023-07-10 thomas } else if (flen == 0) {
461 8505ab66 2023-07-10 thomas if (blen - hdrlen != 0)
462 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
464 8505ab66 2023-07-10 thomas } else if (blen - hdrlen == flen) {
465 8505ab66 2023-07-10 thomas /* Skip blob object header first time around. */
466 8505ab66 2023-07-10 thomas if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
467 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
471 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODIFY;
474 8505ab66 2023-07-10 thomas hdrlen = 0;
477 8505ab66 2023-07-10 thomas if (*status == GOT_STATUS_MODIFY) {
478 8505ab66 2023-07-10 thomas rewind(f);
479 8505ab66 2023-07-10 thomas err = get_modified_file_content_status(status, blob, ie->path,
481 8505ab66 2023-07-10 thomas } else if (xbit_differs(ie, sb->st_mode))
482 8505ab66 2023-07-10 thomas *status = GOT_STATUS_MODE_CHANGE;
484 8505ab66 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
485 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
486 8505ab66 2023-07-10 thomas if (blob)
487 8505ab66 2023-07-10 thomas got_object_blob_close(blob);
488 8505ab66 2023-07-10 thomas if (f != NULL && fclose(f) == EOF && err == NULL)
489 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fclose", abspath);
490 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
491 8505ab66 2023-07-10 thomas err = got_error_from_errno2("close", abspath);
492 8505ab66 2023-07-10 thomas return err;
495 8505ab66 2023-07-10 thomas static const struct got_error *
496 8505ab66 2023-07-10 thomas get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
498 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
499 8505ab66 2023-07-10 thomas char *uuidstr = NULL;
501 8505ab66 2023-07-10 thomas *refname = NULL;
503 8505ab66 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
505 8505ab66 2023-07-10 thomas return err;
507 8505ab66 2023-07-10 thomas if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
508 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
509 8505ab66 2023-07-10 thomas *refname = NULL;
511 8505ab66 2023-07-10 thomas free(uuidstr);
512 8505ab66 2023-07-10 thomas return err;
515 8505ab66 2023-07-10 thomas static const struct got_error *
516 8505ab66 2023-07-10 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
518 8505ab66 2023-07-10 thomas return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
522 8505ab66 2023-07-10 thomas * Prevent Git's garbage collector from deleting our base commit by
523 8505ab66 2023-07-10 thomas * setting a reference to our base commit's ID.
525 8505ab66 2023-07-10 thomas static const struct got_error *
526 8505ab66 2023-07-10 thomas ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
528 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
529 8505ab66 2023-07-10 thomas struct got_reference *ref = NULL;
530 8505ab66 2023-07-10 thomas char *refname;
532 8505ab66 2023-07-10 thomas err = get_base_ref_name(&refname, worktree);
534 8505ab66 2023-07-10 thomas return err;
536 8505ab66 2023-07-10 thomas err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
538 8505ab66 2023-07-10 thomas goto done;
540 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
542 8505ab66 2023-07-10 thomas free(refname);
544 8505ab66 2023-07-10 thomas got_ref_close(ref);
545 8505ab66 2023-07-10 thomas return err;
548 8505ab66 2023-07-10 thomas static const struct got_error *
549 8505ab66 2023-07-10 thomas get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
551 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
553 8505ab66 2023-07-10 thomas if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
554 8505ab66 2023-07-10 thomas GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
555 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
556 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
558 8505ab66 2023-07-10 thomas return err;
561 8505ab66 2023-07-10 thomas static const struct got_error *
562 8505ab66 2023-07-10 thomas open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
563 8505ab66 2023-07-10 thomas struct got_worktree *worktree)
565 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
566 8505ab66 2023-07-10 thomas FILE *index = NULL;
568 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
569 8505ab66 2023-07-10 thomas *fileindex = got_fileindex_alloc();
570 8505ab66 2023-07-10 thomas if (*fileindex == NULL)
571 8505ab66 2023-07-10 thomas return got_error_from_errno("got_fileindex_alloc");
573 8505ab66 2023-07-10 thomas err = get_fileindex_path(fileindex_path, worktree);
575 8505ab66 2023-07-10 thomas goto done;
577 8505ab66 2023-07-10 thomas index = fopen(*fileindex_path, "rbe");
578 8505ab66 2023-07-10 thomas if (index == NULL) {
579 8505ab66 2023-07-10 thomas if (errno != ENOENT)
580 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fopen", *fileindex_path);
582 8505ab66 2023-07-10 thomas err = got_fileindex_read(*fileindex, index);
583 8505ab66 2023-07-10 thomas if (fclose(index) == EOF && err == NULL)
584 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
587 8505ab66 2023-07-10 thomas if (err) {
588 8505ab66 2023-07-10 thomas free(*fileindex_path);
589 8505ab66 2023-07-10 thomas *fileindex_path = NULL;
590 8505ab66 2023-07-10 thomas got_fileindex_free(*fileindex);
591 8505ab66 2023-07-10 thomas *fileindex = NULL;
593 8505ab66 2023-07-10 thomas return err;
596 8505ab66 2023-07-10 thomas static const struct got_error *
597 8505ab66 2023-07-10 thomas sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
599 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
600 8505ab66 2023-07-10 thomas char *new_fileindex_path = NULL;
601 8505ab66 2023-07-10 thomas FILE *new_index = NULL;
602 8505ab66 2023-07-10 thomas struct timespec timeout;
604 8505ab66 2023-07-10 thomas err = got_opentemp_named(&new_fileindex_path, &new_index,
605 8505ab66 2023-07-10 thomas fileindex_path, "");
607 8505ab66 2023-07-10 thomas goto done;
609 8505ab66 2023-07-10 thomas err = got_fileindex_write(fileindex, new_index);
611 8505ab66 2023-07-10 thomas goto done;
613 8505ab66 2023-07-10 thomas if (rename(new_fileindex_path, fileindex_path) != 0) {
614 8505ab66 2023-07-10 thomas err = got_error_from_errno3("rename", new_fileindex_path,
615 8505ab66 2023-07-10 thomas fileindex_path);
616 8505ab66 2023-07-10 thomas unlink(new_fileindex_path);
620 8505ab66 2023-07-10 thomas * Sleep for a short amount of time to ensure that files modified after
621 8505ab66 2023-07-10 thomas * this program exits have a different time stamp from the one which
622 8505ab66 2023-07-10 thomas * was recorded in the file index.
624 8505ab66 2023-07-10 thomas timeout.tv_sec = 0;
625 8505ab66 2023-07-10 thomas timeout.tv_nsec = 1;
626 8505ab66 2023-07-10 thomas nanosleep(&timeout, NULL);
628 8505ab66 2023-07-10 thomas if (new_index)
629 8505ab66 2023-07-10 thomas fclose(new_index);
630 8505ab66 2023-07-10 thomas free(new_fileindex_path);
631 8505ab66 2023-07-10 thomas return err;
634 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg {
635 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex;
636 8505ab66 2023-07-10 thomas struct got_worktree *worktree;
637 8505ab66 2023-07-10 thomas const char *status_path;
638 8505ab66 2023-07-10 thomas size_t status_path_len;
639 8505ab66 2023-07-10 thomas struct got_repository *repo;
640 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb;
641 8505ab66 2023-07-10 thomas void *status_arg;
642 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb;
643 8505ab66 2023-07-10 thomas void *cancel_arg;
644 8505ab66 2023-07-10 thomas /* A pathlist containing per-directory pathlists of ignore patterns. */
645 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores;
646 8505ab66 2023-07-10 thomas int report_unchanged;
647 8505ab66 2023-07-10 thomas int no_ignores;
650 8505ab66 2023-07-10 thomas static const struct got_error *
651 8505ab66 2023-07-10 thomas report_file_status(struct got_fileindex_entry *ie, const char *abspath,
652 8505ab66 2023-07-10 thomas int dirfd, const char *de_name,
653 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
654 8505ab66 2023-07-10 thomas struct got_repository *repo, int report_unchanged)
656 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
657 8505ab66 2023-07-10 thomas unsigned char status = GOT_STATUS_NO_CHANGE;
658 8505ab66 2023-07-10 thomas unsigned char staged_status;
659 8505ab66 2023-07-10 thomas struct stat sb;
660 8505ab66 2023-07-10 thomas struct got_object_id blob_id, commit_id, staged_blob_id;
661 8505ab66 2023-07-10 thomas struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
662 8505ab66 2023-07-10 thomas struct got_object_id *staged_blob_idp = NULL;
664 8505ab66 2023-07-10 thomas staged_status = get_staged_status(ie);
665 8505ab66 2023-07-10 thomas err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
667 8505ab66 2023-07-10 thomas return err;
669 8505ab66 2023-07-10 thomas if (status == GOT_STATUS_NO_CHANGE &&
670 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
671 8505ab66 2023-07-10 thomas return NULL;
673 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_blob(ie))
674 8505ab66 2023-07-10 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
675 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_commit(ie))
676 8505ab66 2023-07-10 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
677 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
678 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
679 8505ab66 2023-07-10 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
680 8505ab66 2023-07-10 thomas &staged_blob_id, ie);
683 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, status, staged_status,
684 8505ab66 2023-07-10 thomas ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
687 8505ab66 2023-07-10 thomas static const struct got_error *
688 8505ab66 2023-07-10 thomas status_old_new(void *arg, struct got_fileindex_entry *ie,
689 8505ab66 2023-07-10 thomas struct dirent *de, const char *parent_path, int dirfd)
691 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
692 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
693 8505ab66 2023-07-10 thomas char *abspath;
695 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
696 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
698 8505ab66 2023-07-10 thomas if (got_path_cmp(parent_path, a->status_path,
699 8505ab66 2023-07-10 thomas strlen(parent_path), a->status_path_len) != 0 &&
700 8505ab66 2023-07-10 thomas !got_path_is_child(parent_path, a->status_path, a->status_path_len))
701 8505ab66 2023-07-10 thomas return NULL;
703 8505ab66 2023-07-10 thomas if (parent_path[0]) {
704 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
705 8505ab66 2023-07-10 thomas parent_path, de->d_name) == -1)
706 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
708 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
709 8505ab66 2023-07-10 thomas de->d_name) == -1)
710 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
713 8505ab66 2023-07-10 thomas err = report_file_status(ie, abspath, dirfd, de->d_name,
714 8505ab66 2023-07-10 thomas a->status_cb, a->status_arg, a->repo, a->report_unchanged);
715 8505ab66 2023-07-10 thomas free(abspath);
716 8505ab66 2023-07-10 thomas return err;
719 8505ab66 2023-07-10 thomas static const struct got_error *
720 8505ab66 2023-07-10 thomas status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
722 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
723 8505ab66 2023-07-10 thomas struct got_object_id blob_id, commit_id;
724 8505ab66 2023-07-10 thomas unsigned char status;
726 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
727 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
729 8505ab66 2023-07-10 thomas if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
730 8505ab66 2023-07-10 thomas return NULL;
732 8505ab66 2023-07-10 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
733 8505ab66 2023-07-10 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
734 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
735 8505ab66 2023-07-10 thomas status = GOT_STATUS_MISSING;
737 8505ab66 2023-07-10 thomas status = GOT_STATUS_DELETE;
738 8505ab66 2023-07-10 thomas return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
739 8505ab66 2023-07-10 thomas ie->path, &blob_id, NULL, &commit_id, -1, NULL);
742 8505ab66 2023-07-10 thomas static void
743 8505ab66 2023-07-10 thomas free_ignores(struct got_pathlist_head *ignores)
745 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
747 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
748 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
750 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
752 8505ab66 2023-07-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
755 8505ab66 2023-07-10 thomas static const struct got_error *
756 8505ab66 2023-07-10 thomas read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
758 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
759 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe = NULL;
760 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist;
761 8505ab66 2023-07-10 thomas char *line = NULL, *pattern, *dirpath = NULL;
762 8505ab66 2023-07-10 thomas size_t linesize = 0;
763 8505ab66 2023-07-10 thomas ssize_t linelen;
765 8505ab66 2023-07-10 thomas ignorelist = calloc(1, sizeof(*ignorelist));
766 8505ab66 2023-07-10 thomas if (ignorelist == NULL)
767 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
768 8505ab66 2023-07-10 thomas TAILQ_INIT(ignorelist);
770 8505ab66 2023-07-10 thomas while ((linelen = getline(&line, &linesize, f)) != -1) {
771 8505ab66 2023-07-10 thomas if (linelen > 0 && line[linelen - 1] == '\n')
772 8505ab66 2023-07-10 thomas line[linelen - 1] = '\0';
774 8505ab66 2023-07-10 thomas /* Git's ignores may contain comments. */
775 8505ab66 2023-07-10 thomas if (line[0] == '#')
776 8505ab66 2023-07-10 thomas continue;
778 8505ab66 2023-07-10 thomas /* Git's negated patterns are not (yet?) supported. */
779 8505ab66 2023-07-10 thomas if (line[0] == '!')
780 8505ab66 2023-07-10 thomas continue;
782 8505ab66 2023-07-10 thomas if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
783 8505ab66 2023-07-10 thomas line) == -1) {
784 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
785 8505ab66 2023-07-10 thomas goto done;
787 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
789 8505ab66 2023-07-10 thomas goto done;
791 8505ab66 2023-07-10 thomas if (ferror(f)) {
792 8505ab66 2023-07-10 thomas err = got_error_from_errno("getline");
793 8505ab66 2023-07-10 thomas goto done;
796 8505ab66 2023-07-10 thomas dirpath = strdup(path);
797 8505ab66 2023-07-10 thomas if (dirpath == NULL) {
798 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
799 8505ab66 2023-07-10 thomas goto done;
801 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
803 8505ab66 2023-07-10 thomas free(line);
804 8505ab66 2023-07-10 thomas if (err || pe == NULL) {
805 8505ab66 2023-07-10 thomas free(dirpath);
806 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
808 8505ab66 2023-07-10 thomas return err;
811 8505ab66 2023-07-10 thomas static int
812 8505ab66 2023-07-10 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
813 8505ab66 2023-07-10 thomas int flags)
815 8505ab66 2023-07-10 thomas char buf[PATH_MAX];
818 8505ab66 2023-07-10 thomas * Trailing slashes signify directories.
819 8505ab66 2023-07-10 thomas * Append a * to make such patterns conform to fnmatch rules.
821 8505ab66 2023-07-10 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
822 8505ab66 2023-07-10 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
823 8505ab66 2023-07-10 thomas return FNM_NOMATCH; /* XXX */
825 8505ab66 2023-07-10 thomas return fnmatch(buf, path, flags);
828 8505ab66 2023-07-10 thomas return fnmatch(pattern, path, flags);
831 8505ab66 2023-07-10 thomas static int
832 8505ab66 2023-07-10 thomas match_ignores(struct got_pathlist_head *ignores, const char *path)
834 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
836 8505ab66 2023-07-10 thomas /* Handle patterns which match in all directories. */
837 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
838 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
839 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
841 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
842 8505ab66 2023-07-10 thomas const char *p;
844 8505ab66 2023-07-10 thomas if (pi->path_len < 3 ||
845 8505ab66 2023-07-10 thomas strncmp(pi->path, "**/", 3) != 0)
846 8505ab66 2023-07-10 thomas continue;
847 8505ab66 2023-07-10 thomas p = path;
848 8505ab66 2023-07-10 thomas while (*p) {
849 8505ab66 2023-07-10 thomas if (match_path(pi->path + 3,
850 8505ab66 2023-07-10 thomas pi->path_len - 3, p,
851 8505ab66 2023-07-10 thomas FNM_PATHNAME | FNM_LEADING_DIR)) {
852 8505ab66 2023-07-10 thomas /* Retry in next directory. */
853 8505ab66 2023-07-10 thomas while (*p && *p != '/')
855 8505ab66 2023-07-10 thomas while (*p == '/')
857 8505ab66 2023-07-10 thomas continue;
859 8505ab66 2023-07-10 thomas return 1;
865 8505ab66 2023-07-10 thomas * The ignores pathlist contains ignore lists from children before
866 8505ab66 2023-07-10 thomas * parents, so we can find the most specific ignorelist by walking
867 8505ab66 2023-07-10 thomas * ignores backwards.
869 8505ab66 2023-07-10 thomas pe = TAILQ_LAST(ignores, got_pathlist_head);
870 8505ab66 2023-07-10 thomas while (pe) {
871 8505ab66 2023-07-10 thomas if (got_path_is_child(path, pe->path, pe->path_len)) {
872 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
873 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
874 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
875 8505ab66 2023-07-10 thomas int flags = FNM_LEADING_DIR;
876 8505ab66 2023-07-10 thomas if (strstr(pi->path, "/**/") == NULL)
877 8505ab66 2023-07-10 thomas flags |= FNM_PATHNAME;
878 8505ab66 2023-07-10 thomas if (match_path(pi->path, pi->path_len,
879 8505ab66 2023-07-10 thomas path, flags))
880 8505ab66 2023-07-10 thomas continue;
881 8505ab66 2023-07-10 thomas return 1;
884 8505ab66 2023-07-10 thomas pe = TAILQ_PREV(pe, got_pathlist_head, entry);
887 8505ab66 2023-07-10 thomas return 0;
890 8505ab66 2023-07-10 thomas static const struct got_error *
891 8505ab66 2023-07-10 thomas add_ignores(struct got_pathlist_head *ignores, const char *root_path,
892 8505ab66 2023-07-10 thomas const char *path, int dirfd, const char *ignores_filename)
894 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
895 8505ab66 2023-07-10 thomas char *ignorespath;
896 8505ab66 2023-07-10 thomas int fd = -1;
897 8505ab66 2023-07-10 thomas FILE *ignoresfile = NULL;
899 8505ab66 2023-07-10 thomas if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
900 8505ab66 2023-07-10 thomas path[0] ? "/" : "", ignores_filename) == -1)
901 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
903 8505ab66 2023-07-10 thomas if (dirfd != -1) {
904 8505ab66 2023-07-10 thomas fd = openat(dirfd, ignores_filename,
905 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
906 8505ab66 2023-07-10 thomas if (fd == -1) {
907 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
908 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
909 8505ab66 2023-07-10 thomas ignorespath);
911 8505ab66 2023-07-10 thomas ignoresfile = fdopen(fd, "r");
912 8505ab66 2023-07-10 thomas if (ignoresfile == NULL)
913 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen",
914 8505ab66 2023-07-10 thomas ignorespath);
917 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
921 8505ab66 2023-07-10 thomas ignoresfile = fopen(ignorespath, "re");
922 8505ab66 2023-07-10 thomas if (ignoresfile == NULL) {
923 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
924 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fopen",
925 8505ab66 2023-07-10 thomas ignorespath);
927 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
930 8505ab66 2023-07-10 thomas if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
931 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
932 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
933 8505ab66 2023-07-10 thomas err = got_error_from_errno2("close", path);
934 8505ab66 2023-07-10 thomas free(ignorespath);
935 8505ab66 2023-07-10 thomas return err;
938 8505ab66 2023-07-10 thomas static const struct got_error *
939 8505ab66 2023-07-10 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
940 8505ab66 2023-07-10 thomas int dirfd)
942 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
943 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
944 8505ab66 2023-07-10 thomas char *path = NULL;
946 8505ab66 2023-07-10 thomas if (ignore != NULL)
947 8505ab66 2023-07-10 thomas *ignore = 0;
949 8505ab66 2023-07-10 thomas if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
950 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
952 8505ab66 2023-07-10 thomas if (parent_path[0]) {
953 8505ab66 2023-07-10 thomas if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
954 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
956 8505ab66 2023-07-10 thomas path = de->d_name;
959 8505ab66 2023-07-10 thomas if (de->d_type == DT_DIR) {
960 8505ab66 2023-07-10 thomas if (!a->no_ignores && ignore != NULL &&
961 8505ab66 2023-07-10 thomas match_ignores(a->ignores, path))
962 8505ab66 2023-07-10 thomas *ignore = 1;
963 8505ab66 2023-07-10 thomas } else if (!match_ignores(a->ignores, path) &&
964 8505ab66 2023-07-10 thomas got_path_is_child(path, a->status_path, a->status_path_len))
965 8505ab66 2023-07-10 thomas err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
966 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
967 8505ab66 2023-07-10 thomas if (parent_path[0])
968 8505ab66 2023-07-10 thomas free(path);
969 8505ab66 2023-07-10 thomas return err;
972 8505ab66 2023-07-10 thomas static const struct got_error *
973 8505ab66 2023-07-10 thomas status_traverse(void *arg, const char *path, int dirfd)
975 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
976 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
978 8505ab66 2023-07-10 thomas if (a->no_ignores)
979 8505ab66 2023-07-10 thomas return NULL;
981 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path,
982 8505ab66 2023-07-10 thomas path, dirfd, ".cvsignore");
984 8505ab66 2023-07-10 thomas return err;
986 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path, path,
987 8505ab66 2023-07-10 thomas dirfd, ".gitignore");
989 8505ab66 2023-07-10 thomas return err;
992 8505ab66 2023-07-10 thomas static const struct got_error *
993 8505ab66 2023-07-10 thomas report_single_file_status(const char *path, const char *ondisk_path,
994 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
995 8505ab66 2023-07-10 thomas void *status_arg, struct got_repository *repo, int report_unchanged,
996 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores)
998 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
999 8505ab66 2023-07-10 thomas struct stat sb;
1001 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1003 8505ab66 2023-07-10 thomas return report_file_status(ie, ondisk_path, -1, NULL,
1004 8505ab66 2023-07-10 thomas status_cb, status_arg, repo, report_unchanged);
1006 8505ab66 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
1007 8505ab66 2023-07-10 thomas if (errno != ENOENT)
1008 8505ab66 2023-07-10 thomas return got_error_from_errno2("lstat", ondisk_path);
1009 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1010 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1013 8505ab66 2023-07-10 thomas if (!no_ignores && match_ignores(ignores, path))
1014 8505ab66 2023-07-10 thomas return NULL;
1016 8505ab66 2023-07-10 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1017 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1018 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1020 8505ab66 2023-07-10 thomas return NULL;
1023 8505ab66 2023-07-10 thomas static const struct got_error *
1024 8505ab66 2023-07-10 thomas add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1025 8505ab66 2023-07-10 thomas const char *root_path, const char *path)
1027 8505ab66 2023-07-10 thomas const struct got_error *err;
1028 8505ab66 2023-07-10 thomas char *parent_path, *next_parent_path = NULL;
1030 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1031 8505ab66 2023-07-10 thomas ".cvsignore");
1032 8505ab66 2023-07-10 thomas if (err)
1033 8505ab66 2023-07-10 thomas return err;
1035 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1036 8505ab66 2023-07-10 thomas ".gitignore");
1037 8505ab66 2023-07-10 thomas if (err)
1038 8505ab66 2023-07-10 thomas return err;
1040 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1041 8505ab66 2023-07-10 thomas if (err) {
1042 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1043 8505ab66 2023-07-10 thomas return NULL; /* cannot traverse parent */
1044 8505ab66 2023-07-10 thomas return err;
1046 8505ab66 2023-07-10 thomas for (;;) {
1047 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1048 8505ab66 2023-07-10 thomas ".cvsignore");
1049 8505ab66 2023-07-10 thomas if (err)
1051 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1052 8505ab66 2023-07-10 thomas ".gitignore");
1053 8505ab66 2023-07-10 thomas if (err)
1055 8505ab66 2023-07-10 thomas err = got_path_dirname(&next_parent_path, parent_path);
1056 8505ab66 2023-07-10 thomas if (err) {
1057 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1058 8505ab66 2023-07-10 thomas err = NULL; /* traversed everything */
1061 8505ab66 2023-07-10 thomas if (got_path_is_root_dir(parent_path))
1063 8505ab66 2023-07-10 thomas free(parent_path);
1064 8505ab66 2023-07-10 thomas parent_path = next_parent_path;
1065 8505ab66 2023-07-10 thomas next_parent_path = NULL;
1068 8505ab66 2023-07-10 thomas free(parent_path);
1069 8505ab66 2023-07-10 thomas free(next_parent_path);
1070 8505ab66 2023-07-10 thomas return err;
1073 8505ab66 2023-07-10 thomas struct find_missing_children_args {
1074 8505ab66 2023-07-10 thomas const char *parent_path;
1075 8505ab66 2023-07-10 thomas size_t parent_len;
1076 8505ab66 2023-07-10 thomas struct got_pathlist_head *children;
1077 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb;
1078 8505ab66 2023-07-10 thomas void *cancel_arg;
1081 8505ab66 2023-07-10 thomas static const struct got_error *
1082 8505ab66 2023-07-10 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
1084 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1085 8505ab66 2023-07-10 thomas struct find_missing_children_args *a = arg;
1087 8505ab66 2023-07-10 thomas if (a->cancel_cb) {
1088 8505ab66 2023-07-10 thomas err = a->cancel_cb(a->cancel_arg);
1089 8505ab66 2023-07-10 thomas if (err)
1090 8505ab66 2023-07-10 thomas return err;
1093 8505ab66 2023-07-10 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1094 8505ab66 2023-07-10 thomas err = got_pathlist_append(a->children, ie->path, NULL);
1096 8505ab66 2023-07-10 thomas return err;
1099 8505ab66 2023-07-10 thomas static const struct got_error *
1100 8505ab66 2023-07-10 thomas report_children(struct got_pathlist_head *children,
1101 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
1102 8505ab66 2023-07-10 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
1103 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores,
1104 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1105 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg)
1107 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1108 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1109 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1111 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, children, entry) {
1112 8505ab66 2023-07-10 thomas if (cancel_cb) {
1113 8505ab66 2023-07-10 thomas err = cancel_cb(cancel_arg);
1114 8505ab66 2023-07-10 thomas if (err)
1118 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1119 8505ab66 2023-07-10 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
1120 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1121 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1125 8505ab66 2023-07-10 thomas err = report_single_file_status(pe->path, ondisk_path,
1126 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
1127 8505ab66 2023-07-10 thomas ignores, no_ignores);
1128 8505ab66 2023-07-10 thomas if (err)
1131 8505ab66 2023-07-10 thomas free(ondisk_path);
1132 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1135 8505ab66 2023-07-10 thomas free(ondisk_path);
1136 8505ab66 2023-07-10 thomas return err;
1139 8505ab66 2023-07-10 thomas static const struct got_error *
1140 8505ab66 2023-07-10 thomas worktree_status(struct got_worktree *worktree, const char *path,
1141 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, struct got_repository *repo,
1142 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1143 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1144 8505ab66 2023-07-10 thomas int report_unchanged)
1146 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1147 8505ab66 2023-07-10 thomas int fd = -1;
1148 8505ab66 2023-07-10 thomas struct got_fileindex_diff_dir_cb fdiff_cb;
1149 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg arg;
1150 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1151 8505ab66 2023-07-10 thomas struct got_pathlist_head ignores, missing_children;
1152 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1154 8505ab66 2023-07-10 thomas TAILQ_INIT(&ignores);
1155 8505ab66 2023-07-10 thomas TAILQ_INIT(&missing_children);
1157 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s",
1158 8505ab66 2023-07-10 thomas worktree->root_path, path[0] ? "/" : "", path) == -1)
1159 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1161 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1162 8505ab66 2023-07-10 thomas if (ie) {
1163 8505ab66 2023-07-10 thomas err = report_single_file_status(path, ondisk_path,
1164 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo,
1165 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1166 8505ab66 2023-07-10 thomas goto done;
1167 8505ab66 2023-07-10 thomas } else {
1168 8505ab66 2023-07-10 thomas struct find_missing_children_args fmca;
1169 8505ab66 2023-07-10 thomas fmca.parent_path = path;
1170 8505ab66 2023-07-10 thomas fmca.parent_len = strlen(path);
1171 8505ab66 2023-07-10 thomas fmca.children = &missing_children;
1172 8505ab66 2023-07-10 thomas fmca.cancel_cb = cancel_cb;
1173 8505ab66 2023-07-10 thomas fmca.cancel_arg = cancel_arg;
1174 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex,
1175 8505ab66 2023-07-10 thomas find_missing_children, &fmca);
1176 8505ab66 2023-07-10 thomas if (err)
1177 8505ab66 2023-07-10 thomas goto done;
1180 8505ab66 2023-07-10 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1181 8505ab66 2023-07-10 thomas if (fd == -1) {
1182 8505ab66 2023-07-10 thomas if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1183 8505ab66 2023-07-10 thomas !got_err_open_nofollow_on_symlink())
1184 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open", ondisk_path);
1186 8505ab66 2023-07-10 thomas if (!no_ignores) {
1187 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1188 8505ab66 2023-07-10 thomas worktree->root_path, ondisk_path);
1189 8505ab66 2023-07-10 thomas if (err)
1190 8505ab66 2023-07-10 thomas goto done;
1192 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&missing_children)) {
1193 8505ab66 2023-07-10 thomas err = report_single_file_status(path,
1194 8505ab66 2023-07-10 thomas ondisk_path, fileindex,
1195 8505ab66 2023-07-10 thomas status_cb, status_arg, repo,
1196 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1197 8505ab66 2023-07-10 thomas if (err)
1198 8505ab66 2023-07-10 thomas goto done;
1199 8505ab66 2023-07-10 thomas } else {
1200 8505ab66 2023-07-10 thomas err = report_children(&missing_children,
1201 8505ab66 2023-07-10 thomas worktree, fileindex, repo,
1202 8505ab66 2023-07-10 thomas (path[0] == '\0'), report_unchanged,
1203 8505ab66 2023-07-10 thomas &ignores, no_ignores,
1204 8505ab66 2023-07-10 thomas status_cb, status_arg,
1205 8505ab66 2023-07-10 thomas cancel_cb, cancel_arg);
1206 8505ab66 2023-07-10 thomas if (err)
1207 8505ab66 2023-07-10 thomas goto done;
1210 8505ab66 2023-07-10 thomas } else {
1211 8505ab66 2023-07-10 thomas fdiff_cb.diff_old_new = status_old_new;
1212 8505ab66 2023-07-10 thomas fdiff_cb.diff_old = status_old;
1213 8505ab66 2023-07-10 thomas fdiff_cb.diff_new = status_new;
1214 8505ab66 2023-07-10 thomas fdiff_cb.diff_traverse = status_traverse;
1215 8505ab66 2023-07-10 thomas arg.fileindex = fileindex;
1216 8505ab66 2023-07-10 thomas arg.worktree = worktree;
1217 8505ab66 2023-07-10 thomas arg.status_path = path;
1218 8505ab66 2023-07-10 thomas arg.status_path_len = strlen(path);
1219 8505ab66 2023-07-10 thomas arg.repo = repo;
1220 8505ab66 2023-07-10 thomas arg.status_cb = status_cb;
1221 8505ab66 2023-07-10 thomas arg.status_arg = status_arg;
1222 8505ab66 2023-07-10 thomas arg.cancel_cb = cancel_cb;
1223 8505ab66 2023-07-10 thomas arg.cancel_arg = cancel_arg;
1224 8505ab66 2023-07-10 thomas arg.report_unchanged = report_unchanged;
1225 8505ab66 2023-07-10 thomas arg.no_ignores = no_ignores;
1226 8505ab66 2023-07-10 thomas if (!no_ignores) {
1227 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1228 8505ab66 2023-07-10 thomas worktree->root_path, path);
1229 8505ab66 2023-07-10 thomas if (err)
1230 8505ab66 2023-07-10 thomas goto done;
1232 8505ab66 2023-07-10 thomas arg.ignores = &ignores;
1233 8505ab66 2023-07-10 thomas err = got_fileindex_diff_dir(fileindex, fd,
1234 8505ab66 2023-07-10 thomas worktree->root_path, path, repo, &fdiff_cb, &arg);
1237 8505ab66 2023-07-10 thomas free_ignores(&ignores);
1238 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1239 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1240 8505ab66 2023-07-10 thomas free(ondisk_path);
1241 8505ab66 2023-07-10 thomas return err;
1244 8505ab66 2023-07-10 thomas static void
1245 8505ab66 2023-07-10 thomas free_commitable(struct got_commitable *ct)
1247 8505ab66 2023-07-10 thomas free(ct->path);
1248 8505ab66 2023-07-10 thomas free(ct->in_repo_path);
1249 8505ab66 2023-07-10 thomas free(ct->ondisk_path);
1250 8505ab66 2023-07-10 thomas free(ct->blob_id);
1251 8505ab66 2023-07-10 thomas free(ct->base_blob_id);
1252 8505ab66 2023-07-10 thomas free(ct->staged_blob_id);
1253 8505ab66 2023-07-10 thomas free(ct->base_commit_id);
1254 8505ab66 2023-07-10 thomas free(ct);
1257 8505ab66 2023-07-10 thomas struct collect_commitables_arg {
1258 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths;
1259 8505ab66 2023-07-10 thomas struct got_repository *repo;
1260 8505ab66 2023-07-10 thomas struct got_worktree *worktree;
1261 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex;
1262 8505ab66 2023-07-10 thomas int have_staged_files;
1263 8505ab66 2023-07-10 thomas int allow_bad_symlinks;
1264 8505ab66 2023-07-10 thomas int diff_header_shown;
1265 8505ab66 2023-07-10 thomas int commit_conflicts;
1266 8505ab66 2023-07-10 thomas FILE *diff_outfile;
1267 8505ab66 2023-07-10 thomas FILE *f1;
1268 8505ab66 2023-07-10 thomas FILE *f2;
1272 8505ab66 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
1273 8505ab66 2023-07-10 thomas * it as content to the diff engine.
1275 8505ab66 2023-07-10 thomas static const struct got_error *
1276 8505ab66 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1277 8505ab66 2023-07-10 thomas const char *abspath)
1279 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1280 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1281 8505ab66 2023-07-10 thomas ssize_t target_len, outlen;
1283 8505ab66 2023-07-10 thomas *fd = -1;
1285 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1286 8505ab66 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1287 8505ab66 2023-07-10 thomas if (target_len == -1)
1288 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
1289 8505ab66 2023-07-10 thomas } else {
1290 8505ab66 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
1291 8505ab66 2023-07-10 thomas if (target_len == -1)
1292 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
1295 8505ab66 2023-07-10 thomas *fd = got_opentempfd();
1296 8505ab66 2023-07-10 thomas if (*fd == -1)
1297 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
1299 8505ab66 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
1300 8505ab66 2023-07-10 thomas if (outlen == -1) {
1301 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1302 8505ab66 2023-07-10 thomas goto done;
1305 8505ab66 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
1306 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
1307 8505ab66 2023-07-10 thomas goto done;
1310 8505ab66 2023-07-10 thomas if (err) {
1311 8505ab66 2023-07-10 thomas close(*fd);
1312 8505ab66 2023-07-10 thomas *fd = -1;
1314 8505ab66 2023-07-10 thomas return err;
1317 8505ab66 2023-07-10 thomas static const struct got_error *
1318 8505ab66 2023-07-10 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1319 8505ab66 2023-07-10 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1320 8505ab66 2023-07-10 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1322 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1323 8505ab66 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
1324 8505ab66 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
1325 8505ab66 2023-07-10 thomas FILE *ondisk_file = NULL;
1326 8505ab66 2023-07-10 thomas char *label1 = NULL;
1327 8505ab66 2023-07-10 thomas struct stat sb;
1328 8505ab66 2023-07-10 thomas off_t size1 = 0;
1329 8505ab66 2023-07-10 thomas int f2_exists = 0;
1330 8505ab66 2023-07-10 thomas char *id_str = NULL;
1332 8505ab66 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
1334 8505ab66 2023-07-10 thomas if (diff_staged) {
1335 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1336 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD &&
1337 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1338 8505ab66 2023-07-10 thomas return NULL;
1339 8505ab66 2023-07-10 thomas } else {
1340 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1341 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_ADD &&
1342 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1343 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1344 8505ab66 2023-07-10 thomas return NULL;
1347 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f1);
1348 8505ab66 2023-07-10 thomas if (err)
1349 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1350 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f2);
1351 8505ab66 2023-07-10 thomas if (err)
1352 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1354 8505ab66 2023-07-10 thomas if (!*diff_header_shown) {
1355 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
1356 8505ab66 2023-07-10 thomas if (err)
1357 8505ab66 2023-07-10 thomas return err;
1358 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1359 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree));
1360 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
1361 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "path + %s%s\n",
1362 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree),
1363 8505ab66 2023-07-10 thomas diff_staged ? " (staged changes)" : "");
1364 8505ab66 2023-07-10 thomas *diff_header_shown = 1;
1367 8505ab66 2023-07-10 thomas if (diff_staged) {
1368 8505ab66 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
1369 8505ab66 2023-07-10 thomas switch (ct->staged_status) {
1370 8505ab66 2023-07-10 thomas case GOT_STATUS_MODIFY:
1371 8505ab66 2023-07-10 thomas label1 = ct->path;
1372 8505ab66 2023-07-10 thomas label2 = ct->path;
1374 8505ab66 2023-07-10 thomas case GOT_STATUS_ADD:
1375 8505ab66 2023-07-10 thomas label2 = ct->path;
1377 8505ab66 2023-07-10 thomas case GOT_STATUS_DELETE:
1378 8505ab66 2023-07-10 thomas label1 = ct->path;
1380 8505ab66 2023-07-10 thomas default:
1381 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
1383 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1384 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1385 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1386 8505ab66 2023-07-10 thomas goto done;
1388 8505ab66 2023-07-10 thomas fd2 = got_opentempfd();
1389 8505ab66 2023-07-10 thomas if (fd2 == -1) {
1390 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1391 8505ab66 2023-07-10 thomas goto done;
1393 8505ab66 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1394 8505ab66 2023-07-10 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1395 8505ab66 2023-07-10 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1396 8505ab66 2023-07-10 thomas NULL, repo, diff_outfile);
1397 8505ab66 2023-07-10 thomas goto done;
1400 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1401 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1402 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1403 8505ab66 2023-07-10 thomas goto done;
1406 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD) {
1407 8505ab66 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1408 8505ab66 2023-07-10 thomas 8192, fd1);
1409 8505ab66 2023-07-10 thomas if (err)
1410 8505ab66 2023-07-10 thomas goto done;
1413 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_DELETE) {
1414 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1415 8505ab66 2023-07-10 thomas fd = openat(dirfd, de_name,
1416 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1417 8505ab66 2023-07-10 thomas if (fd == -1) {
1418 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1419 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
1420 8505ab66 2023-07-10 thomas ct->ondisk_path);
1421 8505ab66 2023-07-10 thomas goto done;
1423 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1424 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1425 8505ab66 2023-07-10 thomas if (err)
1426 8505ab66 2023-07-10 thomas goto done;
1428 8505ab66 2023-07-10 thomas } else {
1429 8505ab66 2023-07-10 thomas fd = open(ct->ondisk_path,
1430 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1431 8505ab66 2023-07-10 thomas if (fd == -1) {
1432 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1433 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open",
1434 8505ab66 2023-07-10 thomas ct->ondisk_path);
1435 8505ab66 2023-07-10 thomas goto done;
1437 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1438 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1439 8505ab66 2023-07-10 thomas if (err)
1440 8505ab66 2023-07-10 thomas goto done;
1443 8505ab66 2023-07-10 thomas if (fstatat(fd, ct->ondisk_path, &sb,
1444 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1445 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
1446 8505ab66 2023-07-10 thomas goto done;
1448 8505ab66 2023-07-10 thomas ondisk_file = fdopen(fd, "r");
1449 8505ab66 2023-07-10 thomas if (ondisk_file == NULL) {
1450 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
1451 8505ab66 2023-07-10 thomas goto done;
1453 8505ab66 2023-07-10 thomas fd = -1;
1454 8505ab66 2023-07-10 thomas f2_exists = 1;
1457 8505ab66 2023-07-10 thomas if (blob1) {
1458 8505ab66 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1459 8505ab66 2023-07-10 thomas f1, blob1);
1460 8505ab66 2023-07-10 thomas if (err)
1461 8505ab66 2023-07-10 thomas goto done;
1464 8505ab66 2023-07-10 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
1465 8505ab66 2023-07-10 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1466 8505ab66 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1468 8505ab66 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1469 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1470 8505ab66 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1471 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1472 8505ab66 2023-07-10 thomas if (blob1)
1473 8505ab66 2023-07-10 thomas got_object_blob_close(blob1);
1474 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1475 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1476 8505ab66 2023-07-10 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1477 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
1478 8505ab66 2023-07-10 thomas return err;
1481 8505ab66 2023-07-10 thomas static const struct got_error *
1482 8505ab66 2023-07-10 thomas collect_commitables(void *arg, unsigned char status,
1483 8505ab66 2023-07-10 thomas unsigned char staged_status, const char *relpath,
1484 8505ab66 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1485 8505ab66 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
1487 8505ab66 2023-07-10 thomas struct collect_commitables_arg *a = arg;
1488 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1489 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
1490 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new = NULL;
1491 8505ab66 2023-07-10 thomas char *parent_path = NULL, *path = NULL;
1492 8505ab66 2023-07-10 thomas struct stat sb;
1494 8505ab66 2023-07-10 thomas if (a->have_staged_files) {
1495 8505ab66 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
1496 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
1497 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
1498 8505ab66 2023-07-10 thomas return NULL;
1499 8505ab66 2023-07-10 thomas } else {
1500 8505ab66 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1501 8505ab66 2023-07-10 thomas printf("C %s\n", relpath);
1502 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_CONFLICT);
1505 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
1506 8505ab66 2023-07-10 thomas status != GOT_STATUS_MODE_CHANGE &&
1507 8505ab66 2023-07-10 thomas status != GOT_STATUS_ADD &&
1508 8505ab66 2023-07-10 thomas status != GOT_STATUS_DELETE &&
1509 8505ab66 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
1510 8505ab66 2023-07-10 thomas return NULL;
1513 8505ab66 2023-07-10 thomas if (asprintf(&path, "/%s", relpath) == -1) {
1514 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1515 8505ab66 2023-07-10 thomas goto done;
1517 8505ab66 2023-07-10 thomas if (strcmp(path, "/") == 0) {
1518 8505ab66 2023-07-10 thomas parent_path = strdup("");
1519 8505ab66 2023-07-10 thomas if (parent_path == NULL)
1520 8505ab66 2023-07-10 thomas return got_error_from_errno("strdup");
1521 8505ab66 2023-07-10 thomas } else {
1522 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1523 8505ab66 2023-07-10 thomas if (err)
1524 8505ab66 2023-07-10 thomas return err;
1527 8505ab66 2023-07-10 thomas ct = calloc(1, sizeof(*ct));
1528 8505ab66 2023-07-10 thomas if (ct == NULL) {
1529 8505ab66 2023-07-10 thomas err = got_error_from_errno("calloc");
1530 8505ab66 2023-07-10 thomas goto done;
1533 8505ab66 2023-07-10 thomas if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1534 8505ab66 2023-07-10 thomas relpath) == -1) {
1535 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1536 8505ab66 2023-07-10 thomas goto done;
1539 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
1540 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
1541 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1542 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1543 8505ab66 2023-07-10 thomas switch (got_fileindex_entry_staged_filetype_get(ie)) {
1544 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_REGULAR_FILE:
1545 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_BAD_SYMLINK:
1546 8505ab66 2023-07-10 thomas ct->mode = S_IFREG;
1548 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_SYMLINK:
1549 8505ab66 2023-07-10 thomas ct->mode = S_IFLNK;
1551 8505ab66 2023-07-10 thomas default:
1552 8505ab66 2023-07-10 thomas err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1553 8505ab66 2023-07-10 thomas goto done;
1555 8505ab66 2023-07-10 thomas ct->mode |= got_fileindex_entry_perms_get(ie);
1556 8505ab66 2023-07-10 thomas } else if (status != GOT_STATUS_DELETE &&
1557 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE) {
1558 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1559 8505ab66 2023-07-10 thomas if (fstatat(dirfd, de_name, &sb,
1560 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1561 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat",
1562 8505ab66 2023-07-10 thomas ct->ondisk_path);
1563 8505ab66 2023-07-10 thomas goto done;
1565 8505ab66 2023-07-10 thomas } else if (lstat(ct->ondisk_path, &sb) == -1) {
1566 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lstat", ct->ondisk_path);
1567 8505ab66 2023-07-10 thomas goto done;
1569 8505ab66 2023-07-10 thomas ct->mode = sb.st_mode;
1572 8505ab66 2023-07-10 thomas if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1573 8505ab66 2023-07-10 thomas got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1574 8505ab66 2023-07-10 thomas relpath) == -1) {
1575 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1576 8505ab66 2023-07-10 thomas goto done;
1579 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1580 8505ab66 2023-07-10 thomas status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1581 8505ab66 2023-07-10 thomas int is_bad_symlink;
1582 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1583 8505ab66 2023-07-10 thomas ssize_t target_len;
1584 8505ab66 2023-07-10 thomas target_len = readlink(ct->ondisk_path, target_path,
1585 8505ab66 2023-07-10 thomas sizeof(target_path));
1586 8505ab66 2023-07-10 thomas if (target_len == -1) {
1587 8505ab66 2023-07-10 thomas err = got_error_from_errno2("readlink",
1588 8505ab66 2023-07-10 thomas ct->ondisk_path);
1589 8505ab66 2023-07-10 thomas goto done;
1591 8505ab66 2023-07-10 thomas err = is_bad_symlink_target(&is_bad_symlink, target_path,
1592 8505ab66 2023-07-10 thomas target_len, ct->ondisk_path, a->worktree->root_path);
1593 8505ab66 2023-07-10 thomas if (err)
1594 8505ab66 2023-07-10 thomas goto done;
1595 8505ab66 2023-07-10 thomas if (is_bad_symlink) {
1596 8505ab66 2023-07-10 thomas err = got_error_path(ct->ondisk_path,
1597 8505ab66 2023-07-10 thomas GOT_ERR_BAD_SYMLINK);
1598 8505ab66 2023-07-10 thomas goto done;
1602 8505ab66 2023-07-10 thomas ct->status = status;
1603 8505ab66 2023-07-10 thomas ct->staged_status = staged_status;
1604 8505ab66 2023-07-10 thomas ct->blob_id = NULL; /* will be filled in when blob gets created */
1605 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
1606 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) {
1607 8505ab66 2023-07-10 thomas ct->base_blob_id = got_object_id_dup(blob_id);
1608 8505ab66 2023-07-10 thomas if (ct->base_blob_id == NULL) {
1609 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1610 8505ab66 2023-07-10 thomas goto done;
1612 8505ab66 2023-07-10 thomas ct->base_commit_id = got_object_id_dup(commit_id);
1613 8505ab66 2023-07-10 thomas if (ct->base_commit_id == NULL) {
1614 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1615 8505ab66 2023-07-10 thomas goto done;
1618 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
1619 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
1620 8505ab66 2023-07-10 thomas ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1621 8505ab66 2023-07-10 thomas if (ct->staged_blob_id == NULL) {
1622 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1623 8505ab66 2023-07-10 thomas goto done;
1626 8505ab66 2023-07-10 thomas ct->path = strdup(path);
1627 8505ab66 2023-07-10 thomas if (ct->path == NULL) {
1628 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
1629 8505ab66 2023-07-10 thomas goto done;
1631 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1632 8505ab66 2023-07-10 thomas if (err)
1633 8505ab66 2023-07-10 thomas goto done;
1635 8505ab66 2023-07-10 thomas if (a->diff_outfile && ct && new != NULL) {
1636 8505ab66 2023-07-10 thomas err = append_ct_diff(ct, &a->diff_header_shown,
1637 8505ab66 2023-07-10 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1638 8505ab66 2023-07-10 thomas a->have_staged_files, a->repo, a->worktree);
1639 8505ab66 2023-07-10 thomas if (err)
1640 8505ab66 2023-07-10 thomas goto done;
1643 8505ab66 2023-07-10 thomas if (ct && (err || new == NULL))
1644 8505ab66 2023-07-10 thomas free_commitable(ct);
1645 8505ab66 2023-07-10 thomas free(parent_path);
1646 8505ab66 2023-07-10 thomas free(path);
1647 8505ab66 2023-07-10 thomas return err;
1650 8505ab66 2023-07-10 thomas static const struct got_error *write_tree(struct got_object_id **, int *,
1651 8505ab66 2023-07-10 thomas struct got_tree_object *, const char *, struct got_pathlist_head *,
1652 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1653 8505ab66 2023-07-10 thomas struct got_repository *);
1655 8505ab66 2023-07-10 thomas static const struct got_error *
1656 8505ab66 2023-07-10 thomas write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1657 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *parent_path,
1658 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1659 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1660 8505ab66 2023-07-10 thomas struct got_repository *repo)
1662 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1663 8505ab66 2023-07-10 thomas struct got_tree_object *subtree;
1664 8505ab66 2023-07-10 thomas char *subpath;
1666 8505ab66 2023-07-10 thomas if (asprintf(&subpath, "%s%s%s", parent_path,
1667 8505ab66 2023-07-10 thomas got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1668 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1670 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&subtree, repo, &te->id);
1671 8505ab66 2023-07-10 thomas if (err)
1672 8505ab66 2023-07-10 thomas return err;
1674 8505ab66 2023-07-10 thomas err = write_tree(new_subtree_id, nentries, subtree, subpath,
1675 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1676 8505ab66 2023-07-10 thomas got_object_tree_close(subtree);
1677 8505ab66 2023-07-10 thomas free(subpath);
1678 8505ab66 2023-07-10 thomas return err;
1681 8505ab66 2023-07-10 thomas static const struct got_error *
1682 8505ab66 2023-07-10 thomas match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1684 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1685 8505ab66 2023-07-10 thomas char *ct_parent_path = NULL;
1687 8505ab66 2023-07-10 thomas *match = 0;
1689 8505ab66 2023-07-10 thomas if (strchr(ct->in_repo_path, '/') == NULL) {
1690 8505ab66 2023-07-10 thomas *match = got_path_is_root_dir(path);
1691 8505ab66 2023-07-10 thomas return NULL;
1694 8505ab66 2023-07-10 thomas err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1695 8505ab66 2023-07-10 thomas if (err)
1696 8505ab66 2023-07-10 thomas return err;
1697 8505ab66 2023-07-10 thomas *match = (strcmp(path, ct_parent_path) == 0);
1698 8505ab66 2023-07-10 thomas free(ct_parent_path);
1699 8505ab66 2023-07-10 thomas return err;
1702 8505ab66 2023-07-10 thomas static mode_t
1703 8505ab66 2023-07-10 thomas get_ct_file_mode(struct got_commitable *ct)
1705 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode))
1706 8505ab66 2023-07-10 thomas return S_IFLNK;
1708 8505ab66 2023-07-10 thomas return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1711 8505ab66 2023-07-10 thomas static const struct got_error *
1712 8505ab66 2023-07-10 thomas alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1713 8505ab66 2023-07-10 thomas struct got_tree_entry *te, struct got_commitable *ct)
1715 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1717 8505ab66 2023-07-10 thomas *new_te = NULL;
1719 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(new_te, te);
1720 8505ab66 2023-07-10 thomas if (err)
1721 8505ab66 2023-07-10 thomas goto done;
1723 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1725 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_MODIFY)
1726 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1727 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1729 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1731 8505ab66 2023-07-10 thomas if (err && *new_te) {
1732 8505ab66 2023-07-10 thomas free(*new_te);
1733 8505ab66 2023-07-10 thomas *new_te = NULL;
1735 8505ab66 2023-07-10 thomas return err;
1738 8505ab66 2023-07-10 thomas static const struct got_error *
1739 8505ab66 2023-07-10 thomas alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1740 8505ab66 2023-07-10 thomas struct got_commitable *ct)
1742 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1743 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1745 8505ab66 2023-07-10 thomas *new_te = NULL;
1747 8505ab66 2023-07-10 thomas *new_te = calloc(1, sizeof(**new_te));
1748 8505ab66 2023-07-10 thomas if (*new_te == NULL)
1749 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1751 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, ct->path);
1752 8505ab66 2023-07-10 thomas if (err)
1753 8505ab66 2023-07-10 thomas goto done;
1754 8505ab66 2023-07-10 thomas if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1755 8505ab66 2023-07-10 thomas sizeof((*new_te)->name)) {
1756 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1757 8505ab66 2023-07-10 thomas goto done;
1760 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1762 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD)
1763 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1764 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1766 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1768 8505ab66 2023-07-10 thomas free(ct_name);
1769 8505ab66 2023-07-10 thomas if (err && *new_te) {
1770 8505ab66 2023-07-10 thomas free(*new_te);
1771 8505ab66 2023-07-10 thomas *new_te = NULL;
1773 8505ab66 2023-07-10 thomas return err;
1776 8505ab66 2023-07-10 thomas static const struct got_error *
1777 8505ab66 2023-07-10 thomas insert_tree_entry(struct got_tree_entry *new_te,
1778 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
1780 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1781 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new_pe;
1783 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1784 8505ab66 2023-07-10 thomas if (err)
1785 8505ab66 2023-07-10 thomas return err;
1786 8505ab66 2023-07-10 thomas if (new_pe == NULL)
1787 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_TREE_DUP_ENTRY);
1788 8505ab66 2023-07-10 thomas return NULL;
1791 8505ab66 2023-07-10 thomas static const struct got_error *
1792 8505ab66 2023-07-10 thomas report_ct_status(struct got_commitable *ct,
1793 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg)
1795 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
1796 8505ab66 2023-07-10 thomas unsigned char status;
1798 8505ab66 2023-07-10 thomas if (status_cb == NULL) /* no commit progress output desired */
1799 8505ab66 2023-07-10 thomas return NULL;
1801 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
1802 8505ab66 2023-07-10 thomas ct_path++;
1804 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1805 8505ab66 2023-07-10 thomas status = ct->staged_status;
1807 8505ab66 2023-07-10 thomas status = ct->status;
1809 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1810 8505ab66 2023-07-10 thomas ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1813 8505ab66 2023-07-10 thomas static const struct got_error *
1814 8505ab66 2023-07-10 thomas match_modified_subtree(int *modified, struct got_tree_entry *te,
1815 8505ab66 2023-07-10 thomas const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1817 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1818 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1819 8505ab66 2023-07-10 thomas char *te_path;
1821 8505ab66 2023-07-10 thomas *modified = 0;
1823 8505ab66 2023-07-10 thomas if (asprintf(&te_path, "%s%s%s", base_tree_path,
1824 8505ab66 2023-07-10 thomas got_path_is_root_dir(base_tree_path) ? "" : "/",
1825 8505ab66 2023-07-10 thomas te->name) == -1)
1826 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1828 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1829 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1830 8505ab66 2023-07-10 thomas *modified = got_path_is_child(ct->in_repo_path, te_path,
1831 8505ab66 2023-07-10 thomas strlen(te_path));
1832 8505ab66 2023-07-10 thomas if (*modified)
1836 8505ab66 2023-07-10 thomas free(te_path);
1837 8505ab66 2023-07-10 thomas return err;
1840 8505ab66 2023-07-10 thomas static const struct got_error *
1841 8505ab66 2023-07-10 thomas match_deleted_or_modified_ct(struct got_commitable **ctp,
1842 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *base_tree_path,
1843 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
1845 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1846 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1848 8505ab66 2023-07-10 thomas *ctp = NULL;
1850 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1851 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1852 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1853 8505ab66 2023-07-10 thomas int path_matches;
1855 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1856 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1857 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
1858 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1859 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1860 8505ab66 2023-07-10 thomas continue;
1861 8505ab66 2023-07-10 thomas } else {
1862 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1863 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1864 8505ab66 2023-07-10 thomas continue;
1867 8505ab66 2023-07-10 thomas if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1868 8505ab66 2023-07-10 thomas continue;
1870 8505ab66 2023-07-10 thomas err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1871 8505ab66 2023-07-10 thomas if (err)
1872 8505ab66 2023-07-10 thomas return err;
1873 8505ab66 2023-07-10 thomas if (!path_matches)
1874 8505ab66 2023-07-10 thomas continue;
1876 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, pe->path);
1877 8505ab66 2023-07-10 thomas if (err)
1878 8505ab66 2023-07-10 thomas return err;
1880 8505ab66 2023-07-10 thomas if (strcmp(te->name, ct_name) != 0) {
1881 8505ab66 2023-07-10 thomas free(ct_name);
1882 8505ab66 2023-07-10 thomas continue;
1884 8505ab66 2023-07-10 thomas free(ct_name);
1886 8505ab66 2023-07-10 thomas *ctp = ct;
1890 8505ab66 2023-07-10 thomas return err;
1893 8505ab66 2023-07-10 thomas static const struct got_error *
1894 8505ab66 2023-07-10 thomas make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1895 8505ab66 2023-07-10 thomas const char *child_path, const char *path_base_tree,
1896 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1897 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1898 8505ab66 2023-07-10 thomas struct got_repository *repo)
1900 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1901 8505ab66 2023-07-10 thomas struct got_tree_entry *new_te;
1902 8505ab66 2023-07-10 thomas char *subtree_path;
1903 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
1904 8505ab66 2023-07-10 thomas int nentries;
1906 8505ab66 2023-07-10 thomas *new_tep = NULL;
1908 8505ab66 2023-07-10 thomas if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1909 8505ab66 2023-07-10 thomas got_path_is_root_dir(path_base_tree) ? "" : "/",
1910 8505ab66 2023-07-10 thomas child_path) == -1)
1911 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1913 8505ab66 2023-07-10 thomas new_te = calloc(1, sizeof(*new_te));
1914 8505ab66 2023-07-10 thomas if (new_te == NULL)
1915 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1916 8505ab66 2023-07-10 thomas new_te->mode = S_IFDIR;
1918 8505ab66 2023-07-10 thomas if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1919 8505ab66 2023-07-10 thomas sizeof(new_te->name)) {
1920 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1921 8505ab66 2023-07-10 thomas goto done;
1923 8505ab66 2023-07-10 thomas err = write_tree(&id, &nentries, NULL, subtree_path,
1924 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1925 8505ab66 2023-07-10 thomas if (err) {
1926 8505ab66 2023-07-10 thomas free(new_te);
1927 8505ab66 2023-07-10 thomas goto done;
1929 8505ab66 2023-07-10 thomas memcpy(&new_te->id, id, sizeof(new_te->id));
1931 8505ab66 2023-07-10 thomas free(id);
1932 8505ab66 2023-07-10 thomas free(subtree_path);
1933 8505ab66 2023-07-10 thomas if (err == NULL)
1934 8505ab66 2023-07-10 thomas *new_tep = new_te;
1935 8505ab66 2023-07-10 thomas return err;
1938 8505ab66 2023-07-10 thomas static const struct got_error *
1939 8505ab66 2023-07-10 thomas write_tree(struct got_object_id **new_tree_id, int *nentries,
1940 8505ab66 2023-07-10 thomas struct got_tree_object *base_tree, const char *path_base_tree,
1941 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1942 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1943 8505ab66 2023-07-10 thomas struct got_repository *repo)
1945 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1946 8505ab66 2023-07-10 thomas struct got_pathlist_head paths;
1947 8505ab66 2023-07-10 thomas struct got_tree_entry *te, *new_te = NULL;
1948 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1950 8505ab66 2023-07-10 thomas TAILQ_INIT(&paths);
1951 8505ab66 2023-07-10 thomas *nentries = 0;
1953 8505ab66 2023-07-10 thomas /* Insert, and recurse into, newly added entries first. */
1954 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1955 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1956 8505ab66 2023-07-10 thomas char *child_path = NULL, *slash;
1958 8505ab66 2023-07-10 thomas if ((ct->status != GOT_STATUS_ADD &&
1959 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) ||
1960 8505ab66 2023-07-10 thomas (ct->flags & GOT_COMMITABLE_ADDED))
1961 8505ab66 2023-07-10 thomas continue;
1963 8505ab66 2023-07-10 thomas if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1964 8505ab66 2023-07-10 thomas strlen(path_base_tree)))
1965 8505ab66 2023-07-10 thomas continue;
1967 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1968 8505ab66 2023-07-10 thomas ct->in_repo_path);
1969 8505ab66 2023-07-10 thomas if (err)
1970 8505ab66 2023-07-10 thomas goto done;
1972 8505ab66 2023-07-10 thomas slash = strchr(child_path, '/');
1973 8505ab66 2023-07-10 thomas if (slash == NULL) {
1974 8505ab66 2023-07-10 thomas err = alloc_added_blob_tree_entry(&new_te, ct);
1975 8505ab66 2023-07-10 thomas if (err)
1976 8505ab66 2023-07-10 thomas goto done;
1977 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb, status_arg);
1978 8505ab66 2023-07-10 thomas if (err)
1979 8505ab66 2023-07-10 thomas goto done;
1980 8505ab66 2023-07-10 thomas ct->flags |= GOT_COMMITABLE_ADDED;
1981 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
1982 8505ab66 2023-07-10 thomas if (err)
1983 8505ab66 2023-07-10 thomas goto done;
1984 8505ab66 2023-07-10 thomas (*nentries)++;
1985 8505ab66 2023-07-10 thomas } else {
1986 8505ab66 2023-07-10 thomas *slash = '\0'; /* trim trailing path components */
1987 8505ab66 2023-07-10 thomas if (base_tree == NULL ||
1988 8505ab66 2023-07-10 thomas got_object_tree_find_entry(base_tree, child_path)
1989 8505ab66 2023-07-10 thomas == NULL) {
1990 8505ab66 2023-07-10 thomas err = make_subtree_for_added_blob(&new_te,
1991 8505ab66 2023-07-10 thomas child_path, path_base_tree,
1992 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg,
1994 8505ab66 2023-07-10 thomas if (err)
1995 8505ab66 2023-07-10 thomas goto done;
1996 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
1997 8505ab66 2023-07-10 thomas if (err)
1998 8505ab66 2023-07-10 thomas goto done;
1999 8505ab66 2023-07-10 thomas (*nentries)++;
2004 8505ab66 2023-07-10 thomas if (base_tree) {
2005 8505ab66 2023-07-10 thomas int i, nbase_entries;
2006 8505ab66 2023-07-10 thomas /* Handle modified and deleted entries. */
2007 8505ab66 2023-07-10 thomas nbase_entries = got_object_tree_get_nentries(base_tree);
2008 8505ab66 2023-07-10 thomas for (i = 0; i < nbase_entries; i++) {
2009 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
2011 8505ab66 2023-07-10 thomas te = got_object_tree_get_entry(base_tree, i);
2012 8505ab66 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te)) {
2013 8505ab66 2023-07-10 thomas /* Entry is a submodule; just copy it. */
2014 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2015 8505ab66 2023-07-10 thomas if (err)
2016 8505ab66 2023-07-10 thomas goto done;
2017 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2018 8505ab66 2023-07-10 thomas if (err)
2019 8505ab66 2023-07-10 thomas goto done;
2020 8505ab66 2023-07-10 thomas (*nentries)++;
2021 8505ab66 2023-07-10 thomas continue;
2024 8505ab66 2023-07-10 thomas if (S_ISDIR(te->mode)) {
2025 8505ab66 2023-07-10 thomas int modified;
2026 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2027 8505ab66 2023-07-10 thomas if (err)
2028 8505ab66 2023-07-10 thomas goto done;
2029 8505ab66 2023-07-10 thomas err = match_modified_subtree(&modified, te,
2030 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2031 8505ab66 2023-07-10 thomas if (err)
2032 8505ab66 2023-07-10 thomas goto done;
2033 8505ab66 2023-07-10 thomas /* Avoid recursion into unmodified subtrees. */
2034 8505ab66 2023-07-10 thomas if (modified) {
2035 8505ab66 2023-07-10 thomas struct got_object_id *new_id;
2036 8505ab66 2023-07-10 thomas int nsubentries;
2037 8505ab66 2023-07-10 thomas err = write_subtree(&new_id,
2038 8505ab66 2023-07-10 thomas &nsubentries, te,
2039 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths,
2040 8505ab66 2023-07-10 thomas status_cb, status_arg, repo);
2041 8505ab66 2023-07-10 thomas if (err)
2042 8505ab66 2023-07-10 thomas goto done;
2043 8505ab66 2023-07-10 thomas if (nsubentries == 0) {
2044 8505ab66 2023-07-10 thomas /* All entries were deleted. */
2045 8505ab66 2023-07-10 thomas free(new_id);
2046 8505ab66 2023-07-10 thomas continue;
2048 8505ab66 2023-07-10 thomas memcpy(&new_te->id, new_id,
2049 8505ab66 2023-07-10 thomas sizeof(new_te->id));
2050 8505ab66 2023-07-10 thomas free(new_id);
2052 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2053 8505ab66 2023-07-10 thomas if (err)
2054 8505ab66 2023-07-10 thomas goto done;
2055 8505ab66 2023-07-10 thomas (*nentries)++;
2056 8505ab66 2023-07-10 thomas continue;
2059 8505ab66 2023-07-10 thomas err = match_deleted_or_modified_ct(&ct, te,
2060 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2061 8505ab66 2023-07-10 thomas if (err)
2062 8505ab66 2023-07-10 thomas goto done;
2063 8505ab66 2023-07-10 thomas if (ct) {
2064 8505ab66 2023-07-10 thomas /* NB: Deleted entries get dropped here. */
2065 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_MODIFY ||
2066 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_MODE_CHANGE ||
2067 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_CONFLICT ||
2068 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2069 8505ab66 2023-07-10 thomas err = alloc_modified_blob_tree_entry(
2070 8505ab66 2023-07-10 thomas &new_te, te, ct);
2071 8505ab66 2023-07-10 thomas if (err)
2072 8505ab66 2023-07-10 thomas goto done;
2073 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2074 8505ab66 2023-07-10 thomas if (err)
2075 8505ab66 2023-07-10 thomas goto done;
2076 8505ab66 2023-07-10 thomas (*nentries)++;
2078 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb,
2079 8505ab66 2023-07-10 thomas status_arg);
2080 8505ab66 2023-07-10 thomas if (err)
2081 8505ab66 2023-07-10 thomas goto done;
2082 8505ab66 2023-07-10 thomas } else {
2083 8505ab66 2023-07-10 thomas /* Entry is unchanged; just copy it. */
2084 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2085 8505ab66 2023-07-10 thomas if (err)
2086 8505ab66 2023-07-10 thomas goto done;
2087 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2088 8505ab66 2023-07-10 thomas if (err)
2089 8505ab66 2023-07-10 thomas goto done;
2090 8505ab66 2023-07-10 thomas (*nentries)++;
2095 8505ab66 2023-07-10 thomas /* Write new list of entries; deleted entries have been dropped. */
2096 8505ab66 2023-07-10 thomas err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2098 8505ab66 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2099 8505ab66 2023-07-10 thomas return err;
2102 8505ab66 2023-07-10 thomas static const struct got_error *
2103 8505ab66 2023-07-10 thomas update_fileindex_after_commit(struct got_worktree *worktree,
2104 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2105 8505ab66 2023-07-10 thomas struct got_object_id *new_base_commit_id,
2106 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, int have_staged_files)
2108 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2109 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2110 8505ab66 2023-07-10 thomas char *relpath = NULL;
2112 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2113 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2114 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2116 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2118 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&relpath,
2119 8505ab66 2023-07-10 thomas worktree->root_path, ct->ondisk_path);
2120 8505ab66 2023-07-10 thomas if (err)
2121 8505ab66 2023-07-10 thomas goto done;
2123 8505ab66 2023-07-10 thomas if (ie) {
2124 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_DELETE ||
2125 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_DELETE) {
2126 8505ab66 2023-07-10 thomas got_fileindex_entry_remove(fileindex, ie);
2127 8505ab66 2023-07-10 thomas } else if (ct->staged_status == GOT_STATUS_ADD ||
2128 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2129 8505ab66 2023-07-10 thomas got_fileindex_entry_stage_set(ie,
2130 8505ab66 2023-07-10 thomas GOT_FILEIDX_STAGE_NONE);
2131 8505ab66 2023-07-10 thomas got_fileindex_entry_staged_filetype_set(ie, 0);
2133 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2134 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2135 8505ab66 2023-07-10 thomas ct->staged_blob_id->sha1,
2136 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2137 8505ab66 2023-07-10 thomas !have_staged_files);
2139 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2140 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2141 8505ab66 2023-07-10 thomas ct->blob_id->sha1,
2142 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2143 8505ab66 2023-07-10 thomas !have_staged_files);
2144 8505ab66 2023-07-10 thomas } else {
2145 8505ab66 2023-07-10 thomas err = got_fileindex_entry_alloc(&ie, pe->path);
2146 8505ab66 2023-07-10 thomas if (err)
2147 8505ab66 2023-07-10 thomas goto done;
2148 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2149 8505ab66 2023-07-10 thomas worktree->root_fd, relpath, ct->blob_id->sha1,
2150 8505ab66 2023-07-10 thomas new_base_commit_id->sha1, 1);
2151 8505ab66 2023-07-10 thomas if (err) {
2152 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2153 8505ab66 2023-07-10 thomas goto done;
2155 8505ab66 2023-07-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2156 8505ab66 2023-07-10 thomas if (err) {
2157 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2158 8505ab66 2023-07-10 thomas goto done;
2161 8505ab66 2023-07-10 thomas free(relpath);
2162 8505ab66 2023-07-10 thomas relpath = NULL;
2165 8505ab66 2023-07-10 thomas free(relpath);
2166 8505ab66 2023-07-10 thomas return err;
2169 8505ab66 2023-07-10 thomas static const struct got_error *
2170 8505ab66 2023-07-10 thomas check_out_of_date(const char *in_repo_path, unsigned char status,
2171 8505ab66 2023-07-10 thomas unsigned char staged_status, struct got_object_id *base_blob_id,
2172 8505ab66 2023-07-10 thomas struct got_object_id *base_commit_id,
2173 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id, struct got_repository *repo,
2174 8505ab66 2023-07-10 thomas int ood_errcode)
2176 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2177 8505ab66 2023-07-10 thomas struct got_commit_object *commit = NULL;
2178 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
2180 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2181 8505ab66 2023-07-10 thomas /* Trivial case: base commit == head commit */
2182 8505ab66 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2183 8505ab66 2023-07-10 thomas return NULL;
2185 8505ab66 2023-07-10 thomas * Ensure file content which local changes were based
2186 8505ab66 2023-07-10 thomas * on matches file content in the branch head.
2188 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2189 8505ab66 2023-07-10 thomas if (err)
2190 8505ab66 2023-07-10 thomas goto done;
2191 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2192 8505ab66 2023-07-10 thomas if (err) {
2193 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NO_TREE_ENTRY)
2194 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2195 8505ab66 2023-07-10 thomas goto done;
2196 8505ab66 2023-07-10 thomas } else if (got_object_id_cmp(id, base_blob_id) != 0)
2197 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2198 8505ab66 2023-07-10 thomas } else {
2199 8505ab66 2023-07-10 thomas /* Require that added files don't exist in the branch head. */
2200 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2201 8505ab66 2023-07-10 thomas if (err)
2202 8505ab66 2023-07-10 thomas goto done;
2203 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2204 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2205 8505ab66 2023-07-10 thomas goto done;
2206 8505ab66 2023-07-10 thomas err = id ? got_error(ood_errcode) : NULL;
2209 8505ab66 2023-07-10 thomas free(id);
2210 8505ab66 2023-07-10 thomas if (commit)
2211 8505ab66 2023-07-10 thomas got_object_commit_close(commit);
2212 8505ab66 2023-07-10 thomas return err;
2215 8505ab66 2023-07-10 thomas static const struct got_error *
2216 8505ab66 2023-07-10 thomas commit_worktree(struct got_object_id **new_commit_id,
2217 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2218 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id,
2219 8505ab66 2023-07-10 thomas struct got_object_id *parent_id2,
2220 8505ab66 2023-07-10 thomas struct got_worktree *worktree,
2221 8505ab66 2023-07-10 thomas const char *author, const char *committer, char *diff_path,
2222 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2223 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2224 8505ab66 2023-07-10 thomas struct got_repository *repo)
2226 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2227 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2228 8505ab66 2023-07-10 thomas struct got_commit_object *head_commit = NULL;
2229 8505ab66 2023-07-10 thomas struct got_tree_object *head_tree = NULL;
2230 8505ab66 2023-07-10 thomas struct got_object_id *new_tree_id = NULL;
2231 8505ab66 2023-07-10 thomas int nentries, nparents = 0;
2232 8505ab66 2023-07-10 thomas struct got_object_id_queue parent_ids;
2233 8505ab66 2023-07-10 thomas struct got_object_qid *pid = NULL;
2234 8505ab66 2023-07-10 thomas char *logmsg = NULL;
2235 8505ab66 2023-07-10 thomas time_t timestamp;
2237 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2239 8505ab66 2023-07-10 thomas STAILQ_INIT(&parent_ids);
2241 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2242 8505ab66 2023-07-10 thomas if (err)
2243 8505ab66 2023-07-10 thomas goto done;
2245 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2246 8505ab66 2023-07-10 thomas if (err)
2247 8505ab66 2023-07-10 thomas goto done;
2249 8505ab66 2023-07-10 thomas if (commit_msg_cb != NULL) {
2250 8505ab66 2023-07-10 thomas err = commit_msg_cb(commitable_paths, diff_path,
2251 8505ab66 2023-07-10 thomas &logmsg, commit_arg);
2252 8505ab66 2023-07-10 thomas if (err)
2253 8505ab66 2023-07-10 thomas goto done;
2256 8505ab66 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0) {
2257 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2258 8505ab66 2023-07-10 thomas goto done;
2261 8505ab66 2023-07-10 thomas /* Create blobs from added and modified files and record their IDs. */
2262 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2263 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2264 8505ab66 2023-07-10 thomas char *ondisk_path;
2266 8505ab66 2023-07-10 thomas /* Blobs for staged files already exist. */
2267 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
2268 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY)
2269 8505ab66 2023-07-10 thomas continue;
2271 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
2272 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODIFY &&
2273 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
2274 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
2275 8505ab66 2023-07-10 thomas continue;
2277 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
2278 8505ab66 2023-07-10 thomas worktree->root_path, pe->path) == -1) {
2279 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2280 8505ab66 2023-07-10 thomas goto done;
2282 8505ab66 2023-07-10 thomas err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2283 8505ab66 2023-07-10 thomas free(ondisk_path);
2284 8505ab66 2023-07-10 thomas if (err)
2285 8505ab66 2023-07-10 thomas goto done;
2288 8505ab66 2023-07-10 thomas /* Recursively write new tree objects. */
2289 8505ab66 2023-07-10 thomas err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2290 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
2291 8505ab66 2023-07-10 thomas if (err)
2292 8505ab66 2023-07-10 thomas goto done;
2294 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, head_commit_id);
2295 8505ab66 2023-07-10 thomas if (err)
2296 8505ab66 2023-07-10 thomas goto done;
2297 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2298 8505ab66 2023-07-10 thomas nparents++;
2299 8505ab66 2023-07-10 thomas if (parent_id2) {
2300 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, parent_id2);
2301 8505ab66 2023-07-10 thomas if (err)
2302 8505ab66 2023-07-10 thomas goto done;
2303 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2304 8505ab66 2023-07-10 thomas nparents++;
2306 8505ab66 2023-07-10 thomas timestamp = time(NULL);
2307 8505ab66 2023-07-10 thomas err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2308 8505ab66 2023-07-10 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
2309 8505ab66 2023-07-10 thomas if (logmsg != NULL)
2310 8505ab66 2023-07-10 thomas free(logmsg);
2311 8505ab66 2023-07-10 thomas if (err)
2312 8505ab66 2023-07-10 thomas goto done;
2314 8505ab66 2023-07-10 thomas got_object_id_queue_free(&parent_ids);
2315 8505ab66 2023-07-10 thomas if (head_tree)
2316 8505ab66 2023-07-10 thomas got_object_tree_close(head_tree);
2317 8505ab66 2023-07-10 thomas if (head_commit)
2318 8505ab66 2023-07-10 thomas got_object_commit_close(head_commit);
2319 8505ab66 2023-07-10 thomas return err;
2322 8505ab66 2023-07-10 thomas static const struct got_error *
2323 8505ab66 2023-07-10 thomas check_path_is_commitable(const char *path,
2324 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
2326 8505ab66 2023-07-10 thomas struct got_pathlist_entry *cpe = NULL;
2327 8505ab66 2023-07-10 thomas size_t path_len = strlen(path);
2329 8505ab66 2023-07-10 thomas TAILQ_FOREACH(cpe, commitable_paths, entry) {
2330 8505ab66 2023-07-10 thomas struct got_commitable *ct = cpe->data;
2331 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
2333 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
2334 8505ab66 2023-07-10 thomas ct_path++;
2336 8505ab66 2023-07-10 thomas if (strcmp(path, ct_path) == 0 ||
2337 8505ab66 2023-07-10 thomas got_path_is_child(ct_path, path, path_len))
2341 8505ab66 2023-07-10 thomas if (cpe == NULL)
2342 8505ab66 2023-07-10 thomas return got_error_path(path, GOT_ERR_BAD_PATH);
2344 8505ab66 2023-07-10 thomas return NULL;
2347 8505ab66 2023-07-10 thomas static const struct got_error *
2348 8505ab66 2023-07-10 thomas check_staged_file(void *arg, struct got_fileindex_entry *ie)
2350 8505ab66 2023-07-10 thomas int *have_staged_files = arg;
2352 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2353 8505ab66 2023-07-10 thomas *have_staged_files = 1;
2354 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2357 8505ab66 2023-07-10 thomas return NULL;
2360 8505ab66 2023-07-10 thomas static const struct got_error *
2361 8505ab66 2023-07-10 thomas check_non_staged_files(struct got_fileindex *fileindex,
2362 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
2364 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2365 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2367 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
2368 8505ab66 2023-07-10 thomas if (pe->path[0] == '\0')
2369 8505ab66 2023-07-10 thomas continue;
2370 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2371 8505ab66 2023-07-10 thomas if (ie == NULL)
2372 8505ab66 2023-07-10 thomas return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2373 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2374 8505ab66 2023-07-10 thomas return got_error_path(pe->path,
2375 8505ab66 2023-07-10 thomas GOT_ERR_FILE_NOT_STAGED);
2378 8505ab66 2023-07-10 thomas return NULL;
2381 8505ab66 2023-07-10 thomas static void
2382 8505ab66 2023-07-10 thomas print_load_info(int print_colored, int print_found, int print_trees,
2383 8505ab66 2023-07-10 thomas int ncolored, int nfound, int ntrees)
2385 8505ab66 2023-07-10 thomas if (print_colored) {
2386 8505ab66 2023-07-10 thomas printf("%d commit%s colored", ncolored,
2387 8505ab66 2023-07-10 thomas ncolored == 1 ? "" : "s");
2389 8505ab66 2023-07-10 thomas if (print_found) {
2390 8505ab66 2023-07-10 thomas printf("%s%d object%s found",
2391 8505ab66 2023-07-10 thomas ncolored > 0 ? "; " : "",
2392 8505ab66 2023-07-10 thomas nfound, nfound == 1 ? "" : "s");
2394 8505ab66 2023-07-10 thomas if (print_trees) {
2395 8505ab66 2023-07-10 thomas printf("; %d tree%s scanned", ntrees,
2396 8505ab66 2023-07-10 thomas ntrees == 1 ? "" : "s");
2400 8505ab66 2023-07-10 thomas struct got_send_progress_arg {
2401 8505ab66 2023-07-10 thomas char last_scaled_packsize[FMT_SCALED_STRSIZE];
2402 8505ab66 2023-07-10 thomas int verbosity;
2403 8505ab66 2023-07-10 thomas int last_ncolored;
2404 8505ab66 2023-07-10 thomas int last_nfound;
2405 8505ab66 2023-07-10 thomas int last_ntrees;
2406 8505ab66 2023-07-10 thomas int loading_done;
2407 8505ab66 2023-07-10 thomas int last_ncommits;
2408 8505ab66 2023-07-10 thomas int last_nobj_total;
2409 8505ab66 2023-07-10 thomas int last_p_deltify;
2410 8505ab66 2023-07-10 thomas int last_p_written;
2411 8505ab66 2023-07-10 thomas int last_p_sent;
2412 8505ab66 2023-07-10 thomas int printed_something;
2413 8505ab66 2023-07-10 thomas int sent_something;
2414 8505ab66 2023-07-10 thomas struct got_pathlist_head *delete_branches;
2417 8505ab66 2023-07-10 thomas static const struct got_error *
2418 8505ab66 2023-07-10 thomas send_progress(void *arg, int ncolored, int nfound, int ntrees,
2419 8505ab66 2023-07-10 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2420 8505ab66 2023-07-10 thomas int nobj_written, off_t bytes_sent, const char *refname,
2421 8505ab66 2023-07-10 thomas const char *errmsg, int success)
2423 8505ab66 2023-07-10 thomas struct got_send_progress_arg *a = arg;
2424 8505ab66 2023-07-10 thomas char scaled_packsize[FMT_SCALED_STRSIZE];
2425 8505ab66 2023-07-10 thomas char scaled_sent[FMT_SCALED_STRSIZE];
2426 8505ab66 2023-07-10 thomas int p_deltify = 0, p_written = 0, p_sent = 0;
2427 8505ab66 2023-07-10 thomas int print_colored = 0, print_found = 0, print_trees = 0;
2428 8505ab66 2023-07-10 thomas int print_searching = 0, print_total = 0;
2429 8505ab66 2023-07-10 thomas int print_deltify = 0, print_written = 0, print_sent = 0;
2431 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2432 8505ab66 2023-07-10 thomas return NULL;
2434 8505ab66 2023-07-10 thomas if (refname) {
2435 8505ab66 2023-07-10 thomas const char *status = success ? "accepted" : "rejected";
2437 8505ab66 2023-07-10 thomas if (success) {
2438 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2439 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, a->delete_branches, entry) {
2440 8505ab66 2023-07-10 thomas const char *branchname = pe->path;
2441 8505ab66 2023-07-10 thomas if (got_path_cmp(branchname, refname,
2442 8505ab66 2023-07-10 thomas strlen(branchname), strlen(refname)) == 0) {
2443 8505ab66 2023-07-10 thomas status = "deleted";
2444 8505ab66 2023-07-10 thomas a->sent_something = 1;
2450 8505ab66 2023-07-10 thomas if (a->printed_something)
2451 8505ab66 2023-07-10 thomas putchar('\n');
2452 8505ab66 2023-07-10 thomas printf("Server has %s %s", status, refname);
2453 8505ab66 2023-07-10 thomas if (errmsg)
2454 8505ab66 2023-07-10 thomas printf(": %s", errmsg);
2455 8505ab66 2023-07-10 thomas a->printed_something = 1;
2456 8505ab66 2023-07-10 thomas return NULL;
2459 8505ab66 2023-07-10 thomas if (a->last_ncolored != ncolored) {
2460 8505ab66 2023-07-10 thomas print_colored = 1;
2461 8505ab66 2023-07-10 thomas a->last_ncolored = ncolored;
2464 8505ab66 2023-07-10 thomas if (a->last_nfound != nfound) {
2465 8505ab66 2023-07-10 thomas print_colored = 1;
2466 8505ab66 2023-07-10 thomas print_found = 1;
2467 8505ab66 2023-07-10 thomas a->last_nfound = nfound;
2470 8505ab66 2023-07-10 thomas if (a->last_ntrees != ntrees) {
2471 8505ab66 2023-07-10 thomas print_colored = 1;
2472 8505ab66 2023-07-10 thomas print_found = 1;
2473 8505ab66 2023-07-10 thomas print_trees = 1;
2474 8505ab66 2023-07-10 thomas a->last_ntrees = ntrees;
2477 8505ab66 2023-07-10 thomas if ((print_colored || print_found || print_trees) &&
2478 8505ab66 2023-07-10 thomas !a->loading_done) {
2479 8505ab66 2023-07-10 thomas printf("\r");
2480 8505ab66 2023-07-10 thomas print_load_info(print_colored, print_found, print_trees,
2481 8505ab66 2023-07-10 thomas ncolored, nfound, ntrees);
2482 8505ab66 2023-07-10 thomas a->printed_something = 1;
2483 8505ab66 2023-07-10 thomas fflush(stdout);
2484 8505ab66 2023-07-10 thomas return NULL;
2485 8505ab66 2023-07-10 thomas } else if (!a->loading_done) {
2486 8505ab66 2023-07-10 thomas printf("\r");
2487 8505ab66 2023-07-10 thomas print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2488 8505ab66 2023-07-10 thomas printf("\n");
2489 8505ab66 2023-07-10 thomas a->loading_done = 1;
2492 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2493 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2494 8505ab66 2023-07-10 thomas if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2495 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2497 8505ab66 2023-07-10 thomas if (a->last_ncommits != ncommits) {
2498 8505ab66 2023-07-10 thomas print_searching = 1;
2499 8505ab66 2023-07-10 thomas a->last_ncommits = ncommits;
2502 8505ab66 2023-07-10 thomas if (a->last_nobj_total != nobj_total) {
2503 8505ab66 2023-07-10 thomas print_searching = 1;
2504 8505ab66 2023-07-10 thomas print_total = 1;
2505 8505ab66 2023-07-10 thomas a->last_nobj_total = nobj_total;
2508 8505ab66 2023-07-10 thomas if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2509 8505ab66 2023-07-10 thomas strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2510 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2511 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2512 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2515 8505ab66 2023-07-10 thomas if (nobj_deltify > 0 || nobj_written > 0) {
2516 8505ab66 2023-07-10 thomas if (nobj_deltify > 0) {
2517 8505ab66 2023-07-10 thomas p_deltify = (nobj_deltify * 100) / nobj_total;
2518 8505ab66 2023-07-10 thomas if (p_deltify != a->last_p_deltify) {
2519 8505ab66 2023-07-10 thomas a->last_p_deltify = p_deltify;
2520 8505ab66 2023-07-10 thomas print_searching = 1;
2521 8505ab66 2023-07-10 thomas print_total = 1;
2522 8505ab66 2023-07-10 thomas print_deltify = 1;
2525 8505ab66 2023-07-10 thomas if (nobj_written > 0) {
2526 8505ab66 2023-07-10 thomas p_written = (nobj_written * 100) / nobj_total;
2527 8505ab66 2023-07-10 thomas if (p_written != a->last_p_written) {
2528 8505ab66 2023-07-10 thomas a->last_p_written = p_written;
2529 8505ab66 2023-07-10 thomas print_searching = 1;
2530 8505ab66 2023-07-10 thomas print_total = 1;
2531 8505ab66 2023-07-10 thomas print_deltify = 1;
2532 8505ab66 2023-07-10 thomas print_written = 1;
2537 8505ab66 2023-07-10 thomas if (bytes_sent > 0) {
2538 8505ab66 2023-07-10 thomas p_sent = (bytes_sent * 100) / packfile_size;
2539 8505ab66 2023-07-10 thomas if (p_sent != a->last_p_sent) {
2540 8505ab66 2023-07-10 thomas a->last_p_sent = p_sent;
2541 8505ab66 2023-07-10 thomas print_searching = 1;
2542 8505ab66 2023-07-10 thomas print_total = 1;
2543 8505ab66 2023-07-10 thomas print_deltify = 1;
2544 8505ab66 2023-07-10 thomas print_written = 1;
2545 8505ab66 2023-07-10 thomas print_sent = 1;
2547 8505ab66 2023-07-10 thomas a->sent_something = 1;
2550 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify || print_written ||
2551 8505ab66 2023-07-10 thomas print_sent)
2552 8505ab66 2023-07-10 thomas printf("\r");
2553 8505ab66 2023-07-10 thomas if (print_searching)
2554 8505ab66 2023-07-10 thomas printf("packing %d reference%s", ncommits,
2555 8505ab66 2023-07-10 thomas ncommits == 1 ? "" : "s");
2556 8505ab66 2023-07-10 thomas if (print_total)
2557 8505ab66 2023-07-10 thomas printf("; %d object%s", nobj_total,
2558 8505ab66 2023-07-10 thomas nobj_total == 1 ? "" : "s");
2559 8505ab66 2023-07-10 thomas if (print_deltify)
2560 8505ab66 2023-07-10 thomas printf("; deltify: %d%%", p_deltify);
2561 8505ab66 2023-07-10 thomas if (print_sent)
2562 8505ab66 2023-07-10 thomas printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2563 8505ab66 2023-07-10 thomas scaled_packsize, p_sent);
2564 8505ab66 2023-07-10 thomas else if (print_written)
2565 8505ab66 2023-07-10 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2566 8505ab66 2023-07-10 thomas scaled_packsize, p_written);
2567 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify ||
2568 8505ab66 2023-07-10 thomas print_written || print_sent) {
2569 8505ab66 2023-07-10 thomas a->printed_something = 1;
2570 8505ab66 2023-07-10 thomas fflush(stdout);
2572 8505ab66 2023-07-10 thomas return NULL;
2575 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg {
2576 8505ab66 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
2577 8505ab66 2023-07-10 thomas int last_p_indexed;
2578 8505ab66 2023-07-10 thomas int last_p_resolved;
2579 8505ab66 2023-07-10 thomas int verbosity;
2581 8505ab66 2023-07-10 thomas struct got_repository *repo;
2584 8505ab66 2023-07-10 thomas static const struct got_error *
2585 8505ab66 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
2586 8505ab66 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2588 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
2589 8505ab66 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
2590 8505ab66 2023-07-10 thomas int p_indexed, p_resolved;
2591 8505ab66 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
2593 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2594 8505ab66 2023-07-10 thomas return NULL;
2596 8505ab66 2023-07-10 thomas if (message && message[0] != '\0') {
2597 8505ab66 2023-07-10 thomas printf("\rserver: %s", message);
2598 8505ab66 2023-07-10 thomas fflush(stdout);
2599 8505ab66 2023-07-10 thomas return NULL;
2602 8505ab66 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
2603 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2604 8505ab66 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
2605 8505ab66 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
2606 8505ab66 2023-07-10 thomas print_size = 1;
2607 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
2608 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2609 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2611 8505ab66 2023-07-10 thomas if (nobj_indexed > 0) {
2612 8505ab66 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
2613 8505ab66 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
2614 8505ab66 2023-07-10 thomas a->last_p_indexed = p_indexed;
2615 8505ab66 2023-07-10 thomas print_indexed = 1;
2616 8505ab66 2023-07-10 thomas print_size = 1;
2619 8505ab66 2023-07-10 thomas if (nobj_resolved > 0) {
2620 8505ab66 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
2621 8505ab66 2023-07-10 thomas (nobj_total - nobj_loose);
2622 8505ab66 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
2623 8505ab66 2023-07-10 thomas a->last_p_resolved = p_resolved;
2624 8505ab66 2023-07-10 thomas print_resolved = 1;
2625 8505ab66 2023-07-10 thomas print_indexed = 1;
2626 8505ab66 2023-07-10 thomas print_size = 1;
2631 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
2632 8505ab66 2023-07-10 thomas printf("\r");
2633 8505ab66 2023-07-10 thomas if (print_size)
2634 8505ab66 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2635 8505ab66 2023-07-10 thomas if (print_indexed)
2636 8505ab66 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
2637 8505ab66 2023-07-10 thomas if (print_resolved)
2638 8505ab66 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
2639 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved) {
2640 8505ab66 2023-07-10 thomas putchar('\n');
2641 8505ab66 2023-07-10 thomas fflush(stdout);
2644 8505ab66 2023-07-10 thomas return NULL;
2647 8505ab66 2023-07-10 thomas static const struct got_error *
2648 8505ab66 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
2649 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2651 8505ab66 2023-07-10 thomas const struct got_error *err;
2652 8505ab66 2023-07-10 thomas struct got_reference *head_symref;
2654 8505ab66 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2655 8505ab66 2023-07-10 thomas if (err)
2656 8505ab66 2023-07-10 thomas return err;
2658 8505ab66 2023-07-10 thomas err = got_ref_write(head_symref, repo);
2659 8505ab66 2023-07-10 thomas if (err == NULL && verbosity > 0) {
2660 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
2661 8505ab66 2023-07-10 thomas got_ref_get_name(target_ref));
2663 8505ab66 2023-07-10 thomas got_ref_close(head_symref);
2664 8505ab66 2023-07-10 thomas return err;
2667 8505ab66 2023-07-10 thomas static const struct got_error *
2668 8505ab66 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
2669 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2671 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2672 8505ab66 2023-07-10 thomas struct got_reference *ref;
2673 8505ab66 2023-07-10 thomas char *id_str;
2675 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, id);
2676 8505ab66 2023-07-10 thomas if (err)
2677 8505ab66 2023-07-10 thomas return err;
2679 8505ab66 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
2680 8505ab66 2023-07-10 thomas if (err)
2681 8505ab66 2023-07-10 thomas goto done;
2683 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2684 8505ab66 2023-07-10 thomas got_ref_close(ref);
2686 8505ab66 2023-07-10 thomas if (err == NULL && verbosity >= 0)
2687 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
2689 8505ab66 2023-07-10 thomas free(id_str);
2690 8505ab66 2023-07-10 thomas return err;
2693 8505ab66 2023-07-10 thomas static const struct got_error *
2694 8505ab66 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
2695 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2697 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2698 8505ab66 2023-07-10 thomas char *new_id_str = NULL;
2699 8505ab66 2023-07-10 thomas struct got_object_id *old_id = NULL;
2701 8505ab66 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
2702 8505ab66 2023-07-10 thomas if (err)
2703 8505ab66 2023-07-10 thomas goto done;
2705 8505ab66 2023-07-10 thomas if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2706 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2707 8505ab66 2023-07-10 thomas if (err)
2708 8505ab66 2023-07-10 thomas goto done;
2709 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2710 8505ab66 2023-07-10 thomas goto done;
2711 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2712 8505ab66 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
2713 8505ab66 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
2715 8505ab66 2023-07-10 thomas goto done;
2718 8505ab66 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
2719 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2720 8505ab66 2023-07-10 thomas printf("Replacing reference %s: %s\n",
2721 8505ab66 2023-07-10 thomas got_ref_get_name(ref),
2722 8505ab66 2023-07-10 thomas got_ref_get_symref_target(ref));
2724 8505ab66 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
2725 8505ab66 2023-07-10 thomas if (err)
2726 8505ab66 2023-07-10 thomas goto done;
2727 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2728 8505ab66 2023-07-10 thomas if (err)
2729 8505ab66 2023-07-10 thomas goto done;
2730 8505ab66 2023-07-10 thomas } else {
2731 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2732 8505ab66 2023-07-10 thomas if (err)
2733 8505ab66 2023-07-10 thomas goto done;
2734 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2735 8505ab66 2023-07-10 thomas goto done;
2737 8505ab66 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
2738 8505ab66 2023-07-10 thomas if (err)
2739 8505ab66 2023-07-10 thomas goto done;
2740 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2741 8505ab66 2023-07-10 thomas if (err)
2742 8505ab66 2023-07-10 thomas goto done;
2745 8505ab66 2023-07-10 thomas if (verbosity >= 0)
2746 8505ab66 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
2747 8505ab66 2023-07-10 thomas new_id_str);
2749 8505ab66 2023-07-10 thomas free(old_id);
2750 8505ab66 2023-07-10 thomas free(new_id_str);
2751 8505ab66 2023-07-10 thomas return err;
2754 8505ab66 2023-07-10 thomas static const struct got_error *
2755 8505ab66 2023-07-10 thomas fetch_updated_remote(const char *proto, const char *host, const char *port,
2756 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2757 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote, struct got_repository *repo,
2758 8505ab66 2023-07-10 thomas struct got_reference *head_ref, const char *head_refname)
2760 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlock_err = NULL;
2761 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2762 8505ab66 2023-07-10 thomas struct got_pathlist_head learned_refs;
2763 8505ab66 2023-07-10 thomas struct got_pathlist_head symrefs;
2764 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_branches;
2765 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_refs;
2766 8505ab66 2023-07-10 thomas struct got_object_id *pack_hash;
2767 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2768 8505ab66 2023-07-10 thomas int fetchfd = -1;
2769 8505ab66 2023-07-10 thomas pid_t fetchpid = -1;
2771 8505ab66 2023-07-10 thomas TAILQ_INIT(&learned_refs);
2772 8505ab66 2023-07-10 thomas TAILQ_INIT(&symrefs);
2773 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2774 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2776 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2778 8505ab66 2023-07-10 thomas if (err)
2779 8505ab66 2023-07-10 thomas goto done;
2781 8505ab66 2023-07-10 thomas err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2782 8505ab66 2023-07-10 thomas port, server_path, verbosity);
2783 8505ab66 2023-07-10 thomas if (err)
2784 8505ab66 2023-07-10 thomas goto done;
2786 8505ab66 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2787 8505ab66 2023-07-10 thomas fpa.last_p_indexed = -1;
2788 8505ab66 2023-07-10 thomas fpa.last_p_resolved = -1;
2789 8505ab66 2023-07-10 thomas fpa.verbosity = verbosity;
2790 8505ab66 2023-07-10 thomas fpa.repo = repo;
2792 8505ab66 2023-07-10 thomas err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2793 8505ab66 2023-07-10 thomas remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2794 8505ab66 2023-07-10 thomas fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2795 8505ab66 2023-07-10 thomas if (err)
2796 8505ab66 2023-07-10 thomas goto done;
2798 8505ab66 2023-07-10 thomas /* Update references provided with the pack file. */
2799 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &learned_refs, entry) {
2800 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2801 8505ab66 2023-07-10 thomas struct got_object_id *id = pe->data;
2802 8505ab66 2023-07-10 thomas struct got_reference *ref;
2804 8505ab66 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
2805 8505ab66 2023-07-10 thomas if (err) {
2806 8505ab66 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2807 8505ab66 2023-07-10 thomas goto done;
2808 8505ab66 2023-07-10 thomas err = create_ref(refname, id, verbosity, repo);
2809 8505ab66 2023-07-10 thomas if (err)
2810 8505ab66 2023-07-10 thomas goto done;
2811 8505ab66 2023-07-10 thomas } else {
2812 8505ab66 2023-07-10 thomas err = update_ref(ref, id, verbosity, repo);
2813 8505ab66 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2814 8505ab66 2023-07-10 thomas if (unlock_err && err == NULL)
2815 8505ab66 2023-07-10 thomas err = unlock_err;
2816 8505ab66 2023-07-10 thomas got_ref_close(ref);
2817 8505ab66 2023-07-10 thomas if (err)
2818 8505ab66 2023-07-10 thomas goto done;
2822 8505ab66 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
2823 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
2824 8505ab66 2023-07-10 thomas struct got_reference *target_ref;
2825 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2826 8505ab66 2023-07-10 thomas const char *target = pe->data;
2827 8505ab66 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
2829 8505ab66 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
2830 8505ab66 2023-07-10 thomas continue;
2832 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, target, 0);
2833 8505ab66 2023-07-10 thomas if (err) {
2834 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2835 8505ab66 2023-07-10 thomas err = NULL;
2836 8505ab66 2023-07-10 thomas continue;
2838 8505ab66 2023-07-10 thomas goto done;
2841 8505ab66 2023-07-10 thomas err = create_symref(refname, target_ref, verbosity, repo);
2842 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2843 8505ab66 2023-07-10 thomas if (err)
2844 8505ab66 2023-07-10 thomas goto done;
2846 8505ab66 2023-07-10 thomas if (remote->mirror_references)
2847 8505ab66 2023-07-10 thomas continue;
2849 8505ab66 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
2850 8505ab66 2023-07-10 thomas continue;
2852 8505ab66 2023-07-10 thomas if (asprintf(&remote_refname,
2853 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2854 8505ab66 2023-07-10 thomas refname) == -1) {
2855 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2856 8505ab66 2023-07-10 thomas goto done;
2858 8505ab66 2023-07-10 thomas if (asprintf(&remote_target,
2859 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2860 8505ab66 2023-07-10 thomas target + 11) == -1) {
2861 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2862 8505ab66 2023-07-10 thomas free(remote_refname);
2863 8505ab66 2023-07-10 thomas goto done;
2865 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, remote_target, 0);
2866 8505ab66 2023-07-10 thomas if (err) {
2867 8505ab66 2023-07-10 thomas free(remote_refname);
2868 8505ab66 2023-07-10 thomas free(remote_target);
2869 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2870 8505ab66 2023-07-10 thomas err = NULL;
2871 8505ab66 2023-07-10 thomas continue;
2873 8505ab66 2023-07-10 thomas goto done;
2875 8505ab66 2023-07-10 thomas err = create_symref(remote_refname, target_ref,
2876 8505ab66 2023-07-10 thomas verbosity - 1, repo);
2877 8505ab66 2023-07-10 thomas free(remote_refname);
2878 8505ab66 2023-07-10 thomas free(remote_target);
2879 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2880 8505ab66 2023-07-10 thomas if (err)
2881 8505ab66 2023-07-10 thomas goto done;
2885 8505ab66 2023-07-10 thomas got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2886 8505ab66 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2887 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2888 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2889 8505ab66 2023-07-10 thomas return err;
2893 8505ab66 2023-07-10 thomas const struct got_error *
2894 8505ab66 2023-07-10 thomas got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2895 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_pathlist_head *paths,
2896 8505ab66 2023-07-10 thomas const char *author, const char *committer, int allow_bad_symlinks,
2897 8505ab66 2023-07-10 thomas int show_diff, int commit_conflicts,
2898 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2899 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2900 8505ab66 2023-07-10 thomas const char *proto, const char *host, const char *port,
2901 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2902 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote,
2903 8505ab66 2023-07-10 thomas got_cancel_cb check_cancelled,
2904 8505ab66 2023-07-10 thomas struct got_repository *repo)
2906 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2907 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
2908 8505ab66 2023-07-10 thomas char *fileindex_path = NULL;
2909 8505ab66 2023-07-10 thomas struct got_pathlist_head commitable_paths;
2910 8505ab66 2023-07-10 thomas struct collect_commitables_arg cc_arg;
2911 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2912 8505ab66 2023-07-10 thomas struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2913 8505ab66 2023-07-10 thomas struct got_reference *commit_ref = NULL;
2914 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2915 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id2 = NULL;
2916 8505ab66 2023-07-10 thomas char *head_refname = NULL;
2917 8505ab66 2023-07-10 thomas char *commit_refname = NULL;
2918 8505ab66 2023-07-10 thomas char *diff_path = NULL;
2919 8505ab66 2023-07-10 thomas int have_staged_files = 0;
2920 8505ab66 2023-07-10 thomas int sendfd = -1;
2921 8505ab66 2023-07-10 thomas pid_t sendpid = -1;
2922 8505ab66 2023-07-10 thomas struct got_send_progress_arg spa;
2923 8505ab66 2023-07-10 thomas struct got_pathlist_head commit_reflist;
2924 8505ab66 2023-07-10 thomas struct got_pathlist_head tag_names;
2925 8505ab66 2023-07-10 thomas struct got_pathlist_head delete_branches;
2927 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2929 8505ab66 2023-07-10 thomas memset(&cc_arg, 0, sizeof(cc_arg));
2930 8505ab66 2023-07-10 thomas TAILQ_INIT(&commitable_paths);
2931 8505ab66 2023-07-10 thomas TAILQ_INIT(&commit_reflist);
2932 8505ab66 2023-07-10 thomas TAILQ_INIT(&tag_names);
2933 8505ab66 2023-07-10 thomas TAILQ_INIT(&delete_branches);
2935 8505ab66 2023-07-10 thomas err = lock_worktree(worktree, LOCK_EX);
2936 8505ab66 2023-07-10 thomas if (err)
2937 8505ab66 2023-07-10 thomas goto done;
2939 8505ab66 2023-07-10 thomas err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2940 8505ab66 2023-07-10 thomas worktree);
2941 8505ab66 2023-07-10 thomas if (err)
2942 8505ab66 2023-07-10 thomas goto done;
2944 8505ab66 2023-07-10 thomas head_refname = worktree->head_ref_name;
2945 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref, repo, head_refname, 0);
2946 8505ab66 2023-07-10 thomas if (err)
2947 8505ab66 2023-07-10 thomas goto done;
2948 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2949 8505ab66 2023-07-10 thomas if (err)
2950 8505ab66 2023-07-10 thomas goto done;
2952 8505ab66 2023-07-10 thomas err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2953 8505ab66 2023-07-10 thomas if (err)
2954 8505ab66 2023-07-10 thomas goto done;
2955 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
2956 8505ab66 2023-07-10 thomas if (err)
2957 8505ab66 2023-07-10 thomas goto done;
2959 8505ab66 2023-07-10 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
2960 8505ab66 2023-07-10 thomas if (err)
2961 8505ab66 2023-07-10 thomas goto done;
2963 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2964 8505ab66 2023-07-10 thomas &have_staged_files);
2965 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_CANCELLED)
2966 8505ab66 2023-07-10 thomas goto done;
2967 8505ab66 2023-07-10 thomas if (have_staged_files) {
2968 8505ab66 2023-07-10 thomas err = check_non_staged_files(fileindex, paths);
2969 8505ab66 2023-07-10 thomas if (err)
2970 8505ab66 2023-07-10 thomas goto done;
2973 8505ab66 2023-07-10 thomas cc_arg.commitable_paths = &commitable_paths;
2974 8505ab66 2023-07-10 thomas cc_arg.worktree = worktree;
2975 8505ab66 2023-07-10 thomas cc_arg.fileindex = fileindex;
2976 8505ab66 2023-07-10 thomas cc_arg.repo = repo;
2977 8505ab66 2023-07-10 thomas cc_arg.have_staged_files = have_staged_files;
2978 8505ab66 2023-07-10 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2979 8505ab66 2023-07-10 thomas cc_arg.diff_header_shown = 0;
2980 8505ab66 2023-07-10 thomas cc_arg.commit_conflicts = commit_conflicts;
2981 8505ab66 2023-07-10 thomas if (show_diff) {
2982 8505ab66 2023-07-10 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2983 8505ab66 2023-07-10 thomas GOT_TMPDIR_STR "/got", ".diff");
2984 8505ab66 2023-07-10 thomas if (err)
2985 8505ab66 2023-07-10 thomas goto done;
2986 8505ab66 2023-07-10 thomas cc_arg.f1 = got_opentemp();
2987 8505ab66 2023-07-10 thomas if (cc_arg.f1 == NULL) {
2988 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
2989 8505ab66 2023-07-10 thomas goto done;
2991 8505ab66 2023-07-10 thomas cc_arg.f2 = got_opentemp();
2992 8505ab66 2023-07-10 thomas if (cc_arg.f2 == NULL) {
2993 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
2994 8505ab66 2023-07-10 thomas goto done;
2998 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
2999 8505ab66 2023-07-10 thomas err = worktree_status(worktree, pe->path, fileindex, repo,
3000 8505ab66 2023-07-10 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3001 8505ab66 2023-07-10 thomas if (err)
3002 8505ab66 2023-07-10 thomas goto done;
3005 8505ab66 2023-07-10 thomas if (show_diff) {
3006 8505ab66 2023-07-10 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
3007 8505ab66 2023-07-10 thomas err = got_error_from_errno("fflush");
3008 8505ab66 2023-07-10 thomas goto done;
3012 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&commitable_paths)) {
3013 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3014 8505ab66 2023-07-10 thomas goto done;
3017 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
3018 8505ab66 2023-07-10 thomas err = check_path_is_commitable(pe->path, &commitable_paths);
3019 8505ab66 2023-07-10 thomas if (err)
3020 8505ab66 2023-07-10 thomas goto done;
3023 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3024 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3025 8505ab66 2023-07-10 thomas const char *ct_path = ct->in_repo_path;
3027 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
3028 8505ab66 2023-07-10 thomas ct_path++;
3029 8505ab66 2023-07-10 thomas err = check_out_of_date(ct_path, ct->status,
3030 8505ab66 2023-07-10 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3031 8505ab66 2023-07-10 thomas head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3032 8505ab66 2023-07-10 thomas if (err)
3033 8505ab66 2023-07-10 thomas goto done;
3036 8505ab66 2023-07-10 thomas err = commit_worktree(new_commit_id, &commitable_paths,
3037 8505ab66 2023-07-10 thomas head_commit_id, NULL, worktree, author, committer,
3038 8505ab66 2023-07-10 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3039 8505ab66 2023-07-10 thomas commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3040 8505ab66 2023-07-10 thomas if (err)
3041 8505ab66 2023-07-10 thomas goto done;
3044 8505ab66 2023-07-10 thomas * Check if a concurrent commit to our branch has occurred.
3045 8505ab66 2023-07-10 thomas * Lock the reference here to prevent concurrent modification.
3047 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref2, repo, head_refname, 1);
3048 8505ab66 2023-07-10 thomas if (err)
3049 8505ab66 2023-07-10 thomas goto done;
3050 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3051 8505ab66 2023-07-10 thomas if (err)
3052 8505ab66 2023-07-10 thomas goto done;
3053 8505ab66 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3054 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3055 8505ab66 2023-07-10 thomas goto done;
3058 8505ab66 2023-07-10 thomas err = got_pathlist_append(&commit_reflist, commit_refname,
3059 8505ab66 2023-07-10 thomas head_refname);
3060 8505ab66 2023-07-10 thomas if (err)
3061 8505ab66 2023-07-10 thomas goto done;
3063 8505ab66 2023-07-10 thomas /* Update commit ref in repository. */
3064 8505ab66 2023-07-10 thomas err = got_ref_change_ref(commit_ref, *new_commit_id);
3065 8505ab66 2023-07-10 thomas if (err)
3066 8505ab66 2023-07-10 thomas goto done;
3067 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
3068 8505ab66 2023-07-10 thomas if (err)
3069 8505ab66 2023-07-10 thomas goto done;
3071 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
3072 8505ab66 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3073 8505ab66 2023-07-10 thomas remote->name, proto, host,
3074 8505ab66 2023-07-10 thomas port ? ":" : "", port ? port : "",
3075 8505ab66 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
3078 8505ab66 2023-07-10 thomas /* Attempt send to remote branch. */
3079 8505ab66 2023-07-10 thomas err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3080 8505ab66 2023-07-10 thomas server_path, verbosity);
3081 8505ab66 2023-07-10 thomas if (err)
3082 8505ab66 2023-07-10 thomas goto done;
3084 8505ab66 2023-07-10 thomas memset(&spa, 0, sizeof(spa));
3085 8505ab66 2023-07-10 thomas spa.last_scaled_packsize[0] = '\0';
3086 8505ab66 2023-07-10 thomas spa.last_p_deltify = -1;
3087 8505ab66 2023-07-10 thomas spa.last_p_written = -1;
3088 8505ab66 2023-07-10 thomas spa.verbosity = verbosity;
3089 8505ab66 2023-07-10 thomas spa.delete_branches = &delete_branches;
3090 8505ab66 2023-07-10 thomas err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3091 8505ab66 2023-07-10 thomas &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3092 8505ab66 2023-07-10 thomas check_cancelled, NULL);
3093 8505ab66 2023-07-10 thomas if (spa.printed_something)
3094 8505ab66 2023-07-10 thomas putchar('\n');
3095 8505ab66 2023-07-10 thomas if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3097 8505ab66 2023-07-10 thomas * Fetch new changes since remote has diverged.
3098 8505ab66 2023-07-10 thomas * No trivial-rebase yet; require update to be run manually.
3100 8505ab66 2023-07-10 thomas err = fetch_updated_remote(proto, host, port, server_path,
3101 8505ab66 2023-07-10 thomas verbosity, remote, repo, head_ref, head_refname);
3102 8505ab66 2023-07-10 thomas if (err == NULL)
3103 8505ab66 2023-07-10 thomas goto done;
3104 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3105 8505ab66 2023-07-10 thomas goto done;
3106 8505ab66 2023-07-10 thomas /* XXX: Rebase commit over fetched remote branch. */
3108 8505ab66 2023-07-10 thomas if (err) {
3109 8505ab66 2023-07-10 thomas goto done;
3112 8505ab66 2023-07-10 thomas /* Update branch head in repository. */
3113 8505ab66 2023-07-10 thomas err = got_ref_change_ref(head_ref2, *new_commit_id);
3114 8505ab66 2023-07-10 thomas if (err)
3115 8505ab66 2023-07-10 thomas goto done;
3116 8505ab66 2023-07-10 thomas err = got_ref_write(head_ref2, repo);
3117 8505ab66 2023-07-10 thomas if (err)
3118 8505ab66 2023-07-10 thomas goto done;
3120 8505ab66 2023-07-10 thomas err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3121 8505ab66 2023-07-10 thomas if (err)
3122 8505ab66 2023-07-10 thomas goto done;
3124 8505ab66 2023-07-10 thomas err = ref_base_commit(worktree, repo);
3125 8505ab66 2023-07-10 thomas if (err)
3126 8505ab66 2023-07-10 thomas goto done;
3128 8505ab66 2023-07-10 thomas /* XXX: fileindex must be updated for other fetched changes? */
3129 8505ab66 2023-07-10 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
3130 8505ab66 2023-07-10 thomas *new_commit_id, fileindex, have_staged_files);
3131 8505ab66 2023-07-10 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
3132 8505ab66 2023-07-10 thomas if (sync_err && err == NULL)
3133 8505ab66 2023-07-10 thomas err = sync_err;
3135 8505ab66 2023-07-10 thomas if (head_ref2) {
3136 8505ab66 2023-07-10 thomas unlockerr = got_ref_unlock(head_ref2);
3137 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3138 8505ab66 2023-07-10 thomas err = unlockerr;
3139 8505ab66 2023-07-10 thomas got_ref_close(head_ref2);
3141 8505ab66 2023-07-10 thomas if (commit_ref)
3142 8505ab66 2023-07-10 thomas got_ref_close(commit_ref);
3143 8505ab66 2023-07-10 thomas if (fileindex)
3144 8505ab66 2023-07-10 thomas got_fileindex_free(fileindex);
3145 8505ab66 2023-07-10 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
3146 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3147 8505ab66 2023-07-10 thomas err = unlockerr;
3148 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3149 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3151 8505ab66 2023-07-10 thomas free_commitable(ct);
3153 8505ab66 2023-07-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3154 8505ab66 2023-07-10 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
3155 8505ab66 2023-07-10 thomas err = got_error_from_errno2("unlink", diff_path);
3156 8505ab66 2023-07-10 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3157 8505ab66 2023-07-10 thomas err == NULL)
3158 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
3159 8505ab66 2023-07-10 thomas free(head_commit_id);
3160 8505ab66 2023-07-10 thomas free(head_commit_id2);
3161 8505ab66 2023-07-10 thomas free(commit_refname);
3162 8505ab66 2023-07-10 thomas free(fileindex_path);
3163 8505ab66 2023-07-10 thomas free(diff_path);
3164 8505ab66 2023-07-10 thomas return err;
3167 8505ab66 2023-07-10 thomas const struct got_error *
3168 8505ab66 2023-07-10 thomas got_worktree_cvg_get_commit_ref_name(char **refname,
3169 8505ab66 2023-07-10 thomas struct got_worktree *worktree)
3171 8505ab66 2023-07-10 thomas return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);