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 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
696 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
698 eb6fb9cd 2023-07-26 thomas return err;
701 8505ab66 2023-07-10 thomas if (got_path_cmp(parent_path, a->status_path,
702 8505ab66 2023-07-10 thomas strlen(parent_path), a->status_path_len) != 0 &&
703 8505ab66 2023-07-10 thomas !got_path_is_child(parent_path, a->status_path, a->status_path_len))
704 8505ab66 2023-07-10 thomas return NULL;
706 8505ab66 2023-07-10 thomas if (parent_path[0]) {
707 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
708 8505ab66 2023-07-10 thomas parent_path, de->d_name) == -1)
709 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
711 8505ab66 2023-07-10 thomas if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
712 8505ab66 2023-07-10 thomas de->d_name) == -1)
713 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
716 8505ab66 2023-07-10 thomas err = report_file_status(ie, abspath, dirfd, de->d_name,
717 8505ab66 2023-07-10 thomas a->status_cb, a->status_arg, a->repo, a->report_unchanged);
718 8505ab66 2023-07-10 thomas free(abspath);
719 8505ab66 2023-07-10 thomas return err;
722 8505ab66 2023-07-10 thomas static const struct got_error *
723 8505ab66 2023-07-10 thomas status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
725 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
726 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
727 8505ab66 2023-07-10 thomas struct got_object_id blob_id, commit_id;
728 8505ab66 2023-07-10 thomas unsigned char status;
730 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
731 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
733 eb6fb9cd 2023-07-26 thomas return err;
736 8505ab66 2023-07-10 thomas if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
737 8505ab66 2023-07-10 thomas return NULL;
739 8505ab66 2023-07-10 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
740 8505ab66 2023-07-10 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
741 8505ab66 2023-07-10 thomas if (got_fileindex_entry_has_file_on_disk(ie))
742 8505ab66 2023-07-10 thomas status = GOT_STATUS_MISSING;
744 8505ab66 2023-07-10 thomas status = GOT_STATUS_DELETE;
745 8505ab66 2023-07-10 thomas return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
746 8505ab66 2023-07-10 thomas ie->path, &blob_id, NULL, &commit_id, -1, NULL);
749 8505ab66 2023-07-10 thomas static void
750 8505ab66 2023-07-10 thomas free_ignores(struct got_pathlist_head *ignores)
752 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
754 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
755 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
757 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
759 8505ab66 2023-07-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
762 8505ab66 2023-07-10 thomas static const struct got_error *
763 8505ab66 2023-07-10 thomas read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
765 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
766 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe = NULL;
767 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist;
768 8505ab66 2023-07-10 thomas char *line = NULL, *pattern, *dirpath = NULL;
769 8505ab66 2023-07-10 thomas size_t linesize = 0;
770 8505ab66 2023-07-10 thomas ssize_t linelen;
772 8505ab66 2023-07-10 thomas ignorelist = calloc(1, sizeof(*ignorelist));
773 8505ab66 2023-07-10 thomas if (ignorelist == NULL)
774 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
775 8505ab66 2023-07-10 thomas TAILQ_INIT(ignorelist);
777 8505ab66 2023-07-10 thomas while ((linelen = getline(&line, &linesize, f)) != -1) {
778 8505ab66 2023-07-10 thomas if (linelen > 0 && line[linelen - 1] == '\n')
779 8505ab66 2023-07-10 thomas line[linelen - 1] = '\0';
781 8505ab66 2023-07-10 thomas /* Git's ignores may contain comments. */
782 8505ab66 2023-07-10 thomas if (line[0] == '#')
783 8505ab66 2023-07-10 thomas continue;
785 8505ab66 2023-07-10 thomas /* Git's negated patterns are not (yet?) supported. */
786 8505ab66 2023-07-10 thomas if (line[0] == '!')
787 8505ab66 2023-07-10 thomas continue;
789 8505ab66 2023-07-10 thomas if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
790 8505ab66 2023-07-10 thomas line) == -1) {
791 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
792 8505ab66 2023-07-10 thomas goto done;
794 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
796 8505ab66 2023-07-10 thomas goto done;
798 8505ab66 2023-07-10 thomas if (ferror(f)) {
799 8505ab66 2023-07-10 thomas err = got_error_from_errno("getline");
800 8505ab66 2023-07-10 thomas goto done;
803 8505ab66 2023-07-10 thomas dirpath = strdup(path);
804 8505ab66 2023-07-10 thomas if (dirpath == NULL) {
805 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
806 8505ab66 2023-07-10 thomas goto done;
808 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
810 8505ab66 2023-07-10 thomas free(line);
811 8505ab66 2023-07-10 thomas if (err || pe == NULL) {
812 8505ab66 2023-07-10 thomas free(dirpath);
813 8505ab66 2023-07-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
815 8505ab66 2023-07-10 thomas return err;
818 8505ab66 2023-07-10 thomas static int
819 8505ab66 2023-07-10 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
820 8505ab66 2023-07-10 thomas int flags)
822 8505ab66 2023-07-10 thomas char buf[PATH_MAX];
825 8505ab66 2023-07-10 thomas * Trailing slashes signify directories.
826 8505ab66 2023-07-10 thomas * Append a * to make such patterns conform to fnmatch rules.
828 8505ab66 2023-07-10 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
829 8505ab66 2023-07-10 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
830 8505ab66 2023-07-10 thomas return FNM_NOMATCH; /* XXX */
832 8505ab66 2023-07-10 thomas return fnmatch(buf, path, flags);
835 8505ab66 2023-07-10 thomas return fnmatch(pattern, path, flags);
838 8505ab66 2023-07-10 thomas static int
839 8505ab66 2023-07-10 thomas match_ignores(struct got_pathlist_head *ignores, const char *path)
841 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
843 8505ab66 2023-07-10 thomas /* Handle patterns which match in all directories. */
844 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, ignores, entry) {
845 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
846 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
848 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
849 8505ab66 2023-07-10 thomas const char *p;
851 8505ab66 2023-07-10 thomas if (pi->path_len < 3 ||
852 8505ab66 2023-07-10 thomas strncmp(pi->path, "**/", 3) != 0)
853 8505ab66 2023-07-10 thomas continue;
854 8505ab66 2023-07-10 thomas p = path;
855 8505ab66 2023-07-10 thomas while (*p) {
856 8505ab66 2023-07-10 thomas if (match_path(pi->path + 3,
857 8505ab66 2023-07-10 thomas pi->path_len - 3, p,
858 8505ab66 2023-07-10 thomas FNM_PATHNAME | FNM_LEADING_DIR)) {
859 8505ab66 2023-07-10 thomas /* Retry in next directory. */
860 8505ab66 2023-07-10 thomas while (*p && *p != '/')
862 8505ab66 2023-07-10 thomas while (*p == '/')
864 8505ab66 2023-07-10 thomas continue;
866 8505ab66 2023-07-10 thomas return 1;
872 8505ab66 2023-07-10 thomas * The ignores pathlist contains ignore lists from children before
873 8505ab66 2023-07-10 thomas * parents, so we can find the most specific ignorelist by walking
874 8505ab66 2023-07-10 thomas * ignores backwards.
876 8505ab66 2023-07-10 thomas pe = TAILQ_LAST(ignores, got_pathlist_head);
877 8505ab66 2023-07-10 thomas while (pe) {
878 8505ab66 2023-07-10 thomas if (got_path_is_child(path, pe->path, pe->path_len)) {
879 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignorelist = pe->data;
880 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pi;
881 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pi, ignorelist, entry) {
882 8505ab66 2023-07-10 thomas int flags = FNM_LEADING_DIR;
883 8505ab66 2023-07-10 thomas if (strstr(pi->path, "/**/") == NULL)
884 8505ab66 2023-07-10 thomas flags |= FNM_PATHNAME;
885 8505ab66 2023-07-10 thomas if (match_path(pi->path, pi->path_len,
886 8505ab66 2023-07-10 thomas path, flags))
887 8505ab66 2023-07-10 thomas continue;
888 8505ab66 2023-07-10 thomas return 1;
891 8505ab66 2023-07-10 thomas pe = TAILQ_PREV(pe, got_pathlist_head, entry);
894 8505ab66 2023-07-10 thomas return 0;
897 8505ab66 2023-07-10 thomas static const struct got_error *
898 8505ab66 2023-07-10 thomas add_ignores(struct got_pathlist_head *ignores, const char *root_path,
899 8505ab66 2023-07-10 thomas const char *path, int dirfd, const char *ignores_filename)
901 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
902 8505ab66 2023-07-10 thomas char *ignorespath;
903 8505ab66 2023-07-10 thomas int fd = -1;
904 8505ab66 2023-07-10 thomas FILE *ignoresfile = NULL;
906 8505ab66 2023-07-10 thomas if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
907 8505ab66 2023-07-10 thomas path[0] ? "/" : "", ignores_filename) == -1)
908 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
910 8505ab66 2023-07-10 thomas if (dirfd != -1) {
911 8505ab66 2023-07-10 thomas fd = openat(dirfd, ignores_filename,
912 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
913 8505ab66 2023-07-10 thomas if (fd == -1) {
914 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
915 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
916 8505ab66 2023-07-10 thomas ignorespath);
918 8505ab66 2023-07-10 thomas ignoresfile = fdopen(fd, "r");
919 8505ab66 2023-07-10 thomas if (ignoresfile == NULL)
920 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen",
921 8505ab66 2023-07-10 thomas ignorespath);
924 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
928 8505ab66 2023-07-10 thomas ignoresfile = fopen(ignorespath, "re");
929 8505ab66 2023-07-10 thomas if (ignoresfile == NULL) {
930 8505ab66 2023-07-10 thomas if (errno != ENOENT && errno != EACCES)
931 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fopen",
932 8505ab66 2023-07-10 thomas ignorespath);
934 8505ab66 2023-07-10 thomas err = read_ignores(ignores, path, ignoresfile);
937 8505ab66 2023-07-10 thomas if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
938 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
939 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
940 8505ab66 2023-07-10 thomas err = got_error_from_errno2("close", path);
941 8505ab66 2023-07-10 thomas free(ignorespath);
942 8505ab66 2023-07-10 thomas return err;
945 8505ab66 2023-07-10 thomas static const struct got_error *
946 8505ab66 2023-07-10 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
947 8505ab66 2023-07-10 thomas int dirfd)
949 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
950 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
951 8505ab66 2023-07-10 thomas char *path = NULL;
953 8505ab66 2023-07-10 thomas if (ignore != NULL)
954 8505ab66 2023-07-10 thomas *ignore = 0;
956 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
957 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
959 eb6fb9cd 2023-07-26 thomas return err;
962 8505ab66 2023-07-10 thomas if (parent_path[0]) {
963 8505ab66 2023-07-10 thomas if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
964 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
966 8505ab66 2023-07-10 thomas path = de->d_name;
969 8505ab66 2023-07-10 thomas if (de->d_type == DT_DIR) {
970 8505ab66 2023-07-10 thomas if (!a->no_ignores && ignore != NULL &&
971 8505ab66 2023-07-10 thomas match_ignores(a->ignores, path))
972 8505ab66 2023-07-10 thomas *ignore = 1;
973 8505ab66 2023-07-10 thomas } else if (!match_ignores(a->ignores, path) &&
974 8505ab66 2023-07-10 thomas got_path_is_child(path, a->status_path, a->status_path_len))
975 8505ab66 2023-07-10 thomas err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
976 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
977 8505ab66 2023-07-10 thomas if (parent_path[0])
978 8505ab66 2023-07-10 thomas free(path);
979 8505ab66 2023-07-10 thomas return err;
982 8505ab66 2023-07-10 thomas static const struct got_error *
983 8505ab66 2023-07-10 thomas status_traverse(void *arg, const char *path, int dirfd)
985 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
986 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg *a = arg;
988 8505ab66 2023-07-10 thomas if (a->no_ignores)
989 8505ab66 2023-07-10 thomas return NULL;
991 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path,
992 8505ab66 2023-07-10 thomas path, dirfd, ".cvsignore");
994 8505ab66 2023-07-10 thomas return err;
996 8505ab66 2023-07-10 thomas err = add_ignores(a->ignores, a->worktree->root_path, path,
997 8505ab66 2023-07-10 thomas dirfd, ".gitignore");
999 8505ab66 2023-07-10 thomas return err;
1002 8505ab66 2023-07-10 thomas static const struct got_error *
1003 8505ab66 2023-07-10 thomas report_single_file_status(const char *path, const char *ondisk_path,
1004 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
1005 8505ab66 2023-07-10 thomas void *status_arg, struct got_repository *repo, int report_unchanged,
1006 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores)
1008 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1009 8505ab66 2023-07-10 thomas struct stat sb;
1011 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1013 8505ab66 2023-07-10 thomas return report_file_status(ie, ondisk_path, -1, NULL,
1014 8505ab66 2023-07-10 thomas status_cb, status_arg, repo, report_unchanged);
1016 8505ab66 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
1017 8505ab66 2023-07-10 thomas if (errno != ENOENT)
1018 8505ab66 2023-07-10 thomas return got_error_from_errno2("lstat", ondisk_path);
1019 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1020 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1023 8505ab66 2023-07-10 thomas if (!no_ignores && match_ignores(ignores, path))
1024 8505ab66 2023-07-10 thomas return NULL;
1026 8505ab66 2023-07-10 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1027 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1028 8505ab66 2023-07-10 thomas GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1030 8505ab66 2023-07-10 thomas return NULL;
1033 8505ab66 2023-07-10 thomas static const struct got_error *
1034 8505ab66 2023-07-10 thomas add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1035 8505ab66 2023-07-10 thomas const char *root_path, const char *path)
1037 8505ab66 2023-07-10 thomas const struct got_error *err;
1038 8505ab66 2023-07-10 thomas char *parent_path, *next_parent_path = NULL;
1040 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1041 8505ab66 2023-07-10 thomas ".cvsignore");
1042 8505ab66 2023-07-10 thomas if (err)
1043 8505ab66 2023-07-10 thomas return err;
1045 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, "", -1,
1046 8505ab66 2023-07-10 thomas ".gitignore");
1047 8505ab66 2023-07-10 thomas if (err)
1048 8505ab66 2023-07-10 thomas return err;
1050 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1051 8505ab66 2023-07-10 thomas if (err) {
1052 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1053 8505ab66 2023-07-10 thomas return NULL; /* cannot traverse parent */
1054 8505ab66 2023-07-10 thomas return err;
1056 8505ab66 2023-07-10 thomas for (;;) {
1057 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1058 8505ab66 2023-07-10 thomas ".cvsignore");
1059 8505ab66 2023-07-10 thomas if (err)
1061 8505ab66 2023-07-10 thomas err = add_ignores(ignores, root_path, parent_path, -1,
1062 8505ab66 2023-07-10 thomas ".gitignore");
1063 8505ab66 2023-07-10 thomas if (err)
1065 8505ab66 2023-07-10 thomas err = got_path_dirname(&next_parent_path, parent_path);
1066 8505ab66 2023-07-10 thomas if (err) {
1067 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_BAD_PATH)
1068 8505ab66 2023-07-10 thomas err = NULL; /* traversed everything */
1071 8505ab66 2023-07-10 thomas if (got_path_is_root_dir(parent_path))
1073 8505ab66 2023-07-10 thomas free(parent_path);
1074 8505ab66 2023-07-10 thomas parent_path = next_parent_path;
1075 8505ab66 2023-07-10 thomas next_parent_path = NULL;
1078 8505ab66 2023-07-10 thomas free(parent_path);
1079 8505ab66 2023-07-10 thomas free(next_parent_path);
1080 8505ab66 2023-07-10 thomas return err;
1083 8505ab66 2023-07-10 thomas struct find_missing_children_args {
1084 8505ab66 2023-07-10 thomas const char *parent_path;
1085 8505ab66 2023-07-10 thomas size_t parent_len;
1086 8505ab66 2023-07-10 thomas struct got_pathlist_head *children;
1087 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb;
1088 8505ab66 2023-07-10 thomas void *cancel_arg;
1091 8505ab66 2023-07-10 thomas static const struct got_error *
1092 8505ab66 2023-07-10 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
1094 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1095 8505ab66 2023-07-10 thomas struct find_missing_children_args *a = arg;
1097 8505ab66 2023-07-10 thomas if (a->cancel_cb) {
1098 8505ab66 2023-07-10 thomas err = a->cancel_cb(a->cancel_arg);
1099 8505ab66 2023-07-10 thomas if (err)
1100 8505ab66 2023-07-10 thomas return err;
1103 8505ab66 2023-07-10 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1104 8505ab66 2023-07-10 thomas err = got_pathlist_append(a->children, ie->path, NULL);
1106 8505ab66 2023-07-10 thomas return err;
1109 8505ab66 2023-07-10 thomas static const struct got_error *
1110 8505ab66 2023-07-10 thomas report_children(struct got_pathlist_head *children,
1111 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
1112 8505ab66 2023-07-10 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
1113 8505ab66 2023-07-10 thomas struct got_pathlist_head *ignores, int no_ignores,
1114 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1115 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg)
1117 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1118 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1119 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1121 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, children, entry) {
1122 8505ab66 2023-07-10 thomas if (cancel_cb) {
1123 8505ab66 2023-07-10 thomas err = cancel_cb(cancel_arg);
1124 8505ab66 2023-07-10 thomas if (err)
1128 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1129 8505ab66 2023-07-10 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
1130 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1131 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1135 8505ab66 2023-07-10 thomas err = report_single_file_status(pe->path, ondisk_path,
1136 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
1137 8505ab66 2023-07-10 thomas ignores, no_ignores);
1138 8505ab66 2023-07-10 thomas if (err)
1141 8505ab66 2023-07-10 thomas free(ondisk_path);
1142 8505ab66 2023-07-10 thomas ondisk_path = NULL;
1145 8505ab66 2023-07-10 thomas free(ondisk_path);
1146 8505ab66 2023-07-10 thomas return err;
1149 8505ab66 2023-07-10 thomas static const struct got_error *
1150 8505ab66 2023-07-10 thomas worktree_status(struct got_worktree *worktree, const char *path,
1151 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, struct got_repository *repo,
1152 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1153 8505ab66 2023-07-10 thomas got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1154 8505ab66 2023-07-10 thomas int report_unchanged)
1156 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1157 8505ab66 2023-07-10 thomas int fd = -1;
1158 8505ab66 2023-07-10 thomas struct got_fileindex_diff_dir_cb fdiff_cb;
1159 8505ab66 2023-07-10 thomas struct diff_dir_cb_arg arg;
1160 8505ab66 2023-07-10 thomas char *ondisk_path = NULL;
1161 8505ab66 2023-07-10 thomas struct got_pathlist_head ignores, missing_children;
1162 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1164 8505ab66 2023-07-10 thomas TAILQ_INIT(&ignores);
1165 8505ab66 2023-07-10 thomas TAILQ_INIT(&missing_children);
1167 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s%s%s",
1168 8505ab66 2023-07-10 thomas worktree->root_path, path[0] ? "/" : "", path) == -1)
1169 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1171 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1172 8505ab66 2023-07-10 thomas if (ie) {
1173 8505ab66 2023-07-10 thomas err = report_single_file_status(path, ondisk_path,
1174 8505ab66 2023-07-10 thomas fileindex, status_cb, status_arg, repo,
1175 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1176 8505ab66 2023-07-10 thomas goto done;
1177 8505ab66 2023-07-10 thomas } else {
1178 8505ab66 2023-07-10 thomas struct find_missing_children_args fmca;
1179 8505ab66 2023-07-10 thomas fmca.parent_path = path;
1180 8505ab66 2023-07-10 thomas fmca.parent_len = strlen(path);
1181 8505ab66 2023-07-10 thomas fmca.children = &missing_children;
1182 8505ab66 2023-07-10 thomas fmca.cancel_cb = cancel_cb;
1183 8505ab66 2023-07-10 thomas fmca.cancel_arg = cancel_arg;
1184 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex,
1185 8505ab66 2023-07-10 thomas find_missing_children, &fmca);
1186 8505ab66 2023-07-10 thomas if (err)
1187 8505ab66 2023-07-10 thomas goto done;
1190 8505ab66 2023-07-10 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1191 8505ab66 2023-07-10 thomas if (fd == -1) {
1192 8505ab66 2023-07-10 thomas if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1193 8505ab66 2023-07-10 thomas !got_err_open_nofollow_on_symlink())
1194 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open", ondisk_path);
1196 8505ab66 2023-07-10 thomas if (!no_ignores) {
1197 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1198 8505ab66 2023-07-10 thomas worktree->root_path, ondisk_path);
1199 8505ab66 2023-07-10 thomas if (err)
1200 8505ab66 2023-07-10 thomas goto done;
1202 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&missing_children)) {
1203 8505ab66 2023-07-10 thomas err = report_single_file_status(path,
1204 8505ab66 2023-07-10 thomas ondisk_path, fileindex,
1205 8505ab66 2023-07-10 thomas status_cb, status_arg, repo,
1206 8505ab66 2023-07-10 thomas report_unchanged, &ignores, no_ignores);
1207 8505ab66 2023-07-10 thomas if (err)
1208 8505ab66 2023-07-10 thomas goto done;
1209 8505ab66 2023-07-10 thomas } else {
1210 8505ab66 2023-07-10 thomas err = report_children(&missing_children,
1211 8505ab66 2023-07-10 thomas worktree, fileindex, repo,
1212 8505ab66 2023-07-10 thomas (path[0] == '\0'), report_unchanged,
1213 8505ab66 2023-07-10 thomas &ignores, no_ignores,
1214 8505ab66 2023-07-10 thomas status_cb, status_arg,
1215 8505ab66 2023-07-10 thomas cancel_cb, cancel_arg);
1216 8505ab66 2023-07-10 thomas if (err)
1217 8505ab66 2023-07-10 thomas goto done;
1220 8505ab66 2023-07-10 thomas } else {
1221 8505ab66 2023-07-10 thomas fdiff_cb.diff_old_new = status_old_new;
1222 8505ab66 2023-07-10 thomas fdiff_cb.diff_old = status_old;
1223 8505ab66 2023-07-10 thomas fdiff_cb.diff_new = status_new;
1224 8505ab66 2023-07-10 thomas fdiff_cb.diff_traverse = status_traverse;
1225 8505ab66 2023-07-10 thomas arg.fileindex = fileindex;
1226 8505ab66 2023-07-10 thomas arg.worktree = worktree;
1227 8505ab66 2023-07-10 thomas arg.status_path = path;
1228 8505ab66 2023-07-10 thomas arg.status_path_len = strlen(path);
1229 8505ab66 2023-07-10 thomas arg.repo = repo;
1230 8505ab66 2023-07-10 thomas arg.status_cb = status_cb;
1231 8505ab66 2023-07-10 thomas arg.status_arg = status_arg;
1232 8505ab66 2023-07-10 thomas arg.cancel_cb = cancel_cb;
1233 8505ab66 2023-07-10 thomas arg.cancel_arg = cancel_arg;
1234 8505ab66 2023-07-10 thomas arg.report_unchanged = report_unchanged;
1235 8505ab66 2023-07-10 thomas arg.no_ignores = no_ignores;
1236 8505ab66 2023-07-10 thomas if (!no_ignores) {
1237 8505ab66 2023-07-10 thomas err = add_ignores_from_parent_paths(&ignores,
1238 8505ab66 2023-07-10 thomas worktree->root_path, path);
1239 8505ab66 2023-07-10 thomas if (err)
1240 8505ab66 2023-07-10 thomas goto done;
1242 8505ab66 2023-07-10 thomas arg.ignores = &ignores;
1243 8505ab66 2023-07-10 thomas err = got_fileindex_diff_dir(fileindex, fd,
1244 8505ab66 2023-07-10 thomas worktree->root_path, path, repo, &fdiff_cb, &arg);
1247 8505ab66 2023-07-10 thomas free_ignores(&ignores);
1248 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1249 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1250 8505ab66 2023-07-10 thomas free(ondisk_path);
1251 8505ab66 2023-07-10 thomas return err;
1254 8505ab66 2023-07-10 thomas static void
1255 8505ab66 2023-07-10 thomas free_commitable(struct got_commitable *ct)
1257 8505ab66 2023-07-10 thomas free(ct->path);
1258 8505ab66 2023-07-10 thomas free(ct->in_repo_path);
1259 8505ab66 2023-07-10 thomas free(ct->ondisk_path);
1260 8505ab66 2023-07-10 thomas free(ct->blob_id);
1261 8505ab66 2023-07-10 thomas free(ct->base_blob_id);
1262 8505ab66 2023-07-10 thomas free(ct->staged_blob_id);
1263 8505ab66 2023-07-10 thomas free(ct->base_commit_id);
1264 8505ab66 2023-07-10 thomas free(ct);
1267 8505ab66 2023-07-10 thomas struct collect_commitables_arg {
1268 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths;
1269 8505ab66 2023-07-10 thomas struct got_repository *repo;
1270 8505ab66 2023-07-10 thomas struct got_worktree *worktree;
1271 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex;
1272 8505ab66 2023-07-10 thomas int have_staged_files;
1273 8505ab66 2023-07-10 thomas int allow_bad_symlinks;
1274 8505ab66 2023-07-10 thomas int diff_header_shown;
1275 8505ab66 2023-07-10 thomas int commit_conflicts;
1276 8505ab66 2023-07-10 thomas FILE *diff_outfile;
1277 8505ab66 2023-07-10 thomas FILE *f1;
1278 8505ab66 2023-07-10 thomas FILE *f2;
1282 8505ab66 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
1283 8505ab66 2023-07-10 thomas * it as content to the diff engine.
1285 8505ab66 2023-07-10 thomas static const struct got_error *
1286 8505ab66 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1287 8505ab66 2023-07-10 thomas const char *abspath)
1289 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1290 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1291 8505ab66 2023-07-10 thomas ssize_t target_len, outlen;
1293 8505ab66 2023-07-10 thomas *fd = -1;
1295 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1296 8505ab66 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1297 8505ab66 2023-07-10 thomas if (target_len == -1)
1298 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
1299 8505ab66 2023-07-10 thomas } else {
1300 8505ab66 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
1301 8505ab66 2023-07-10 thomas if (target_len == -1)
1302 8505ab66 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
1305 8505ab66 2023-07-10 thomas *fd = got_opentempfd();
1306 8505ab66 2023-07-10 thomas if (*fd == -1)
1307 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
1309 8505ab66 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
1310 8505ab66 2023-07-10 thomas if (outlen == -1) {
1311 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1312 8505ab66 2023-07-10 thomas goto done;
1315 8505ab66 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
1316 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
1317 8505ab66 2023-07-10 thomas goto done;
1320 8505ab66 2023-07-10 thomas if (err) {
1321 8505ab66 2023-07-10 thomas close(*fd);
1322 8505ab66 2023-07-10 thomas *fd = -1;
1324 8505ab66 2023-07-10 thomas return err;
1327 8505ab66 2023-07-10 thomas static const struct got_error *
1328 8505ab66 2023-07-10 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1329 8505ab66 2023-07-10 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1330 8505ab66 2023-07-10 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1332 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1333 8505ab66 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
1334 8505ab66 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
1335 8505ab66 2023-07-10 thomas FILE *ondisk_file = NULL;
1336 8505ab66 2023-07-10 thomas char *label1 = NULL;
1337 8505ab66 2023-07-10 thomas struct stat sb;
1338 8505ab66 2023-07-10 thomas off_t size1 = 0;
1339 8505ab66 2023-07-10 thomas int f2_exists = 0;
1340 8505ab66 2023-07-10 thomas char *id_str = NULL;
1342 8505ab66 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
1344 8505ab66 2023-07-10 thomas if (diff_staged) {
1345 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1346 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD &&
1347 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1348 8505ab66 2023-07-10 thomas return NULL;
1349 8505ab66 2023-07-10 thomas } else {
1350 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1351 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_ADD &&
1352 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1353 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1354 8505ab66 2023-07-10 thomas return NULL;
1357 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f1);
1358 8505ab66 2023-07-10 thomas if (err)
1359 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1360 8505ab66 2023-07-10 thomas err = got_opentemp_truncate(f2);
1361 8505ab66 2023-07-10 thomas if (err)
1362 8505ab66 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
1364 8505ab66 2023-07-10 thomas if (!*diff_header_shown) {
1365 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
1366 8505ab66 2023-07-10 thomas if (err)
1367 8505ab66 2023-07-10 thomas return err;
1368 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1369 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree));
1370 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
1371 8505ab66 2023-07-10 thomas fprintf(diff_outfile, "path + %s%s\n",
1372 8505ab66 2023-07-10 thomas got_worktree_get_root_path(worktree),
1373 8505ab66 2023-07-10 thomas diff_staged ? " (staged changes)" : "");
1374 8505ab66 2023-07-10 thomas *diff_header_shown = 1;
1377 8505ab66 2023-07-10 thomas if (diff_staged) {
1378 8505ab66 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
1379 8505ab66 2023-07-10 thomas switch (ct->staged_status) {
1380 8505ab66 2023-07-10 thomas case GOT_STATUS_MODIFY:
1381 8505ab66 2023-07-10 thomas label1 = ct->path;
1382 8505ab66 2023-07-10 thomas label2 = ct->path;
1384 8505ab66 2023-07-10 thomas case GOT_STATUS_ADD:
1385 8505ab66 2023-07-10 thomas label2 = ct->path;
1387 8505ab66 2023-07-10 thomas case GOT_STATUS_DELETE:
1388 8505ab66 2023-07-10 thomas label1 = ct->path;
1390 8505ab66 2023-07-10 thomas default:
1391 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
1393 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1394 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1395 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1396 8505ab66 2023-07-10 thomas goto done;
1398 8505ab66 2023-07-10 thomas fd2 = got_opentempfd();
1399 8505ab66 2023-07-10 thomas if (fd2 == -1) {
1400 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1401 8505ab66 2023-07-10 thomas goto done;
1403 8505ab66 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1404 8505ab66 2023-07-10 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1405 8505ab66 2023-07-10 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1406 8505ab66 2023-07-10 thomas NULL, repo, diff_outfile);
1407 8505ab66 2023-07-10 thomas goto done;
1410 8505ab66 2023-07-10 thomas fd1 = got_opentempfd();
1411 8505ab66 2023-07-10 thomas if (fd1 == -1) {
1412 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
1413 8505ab66 2023-07-10 thomas goto done;
1416 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD) {
1417 8505ab66 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1418 8505ab66 2023-07-10 thomas 8192, fd1);
1419 8505ab66 2023-07-10 thomas if (err)
1420 8505ab66 2023-07-10 thomas goto done;
1423 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_DELETE) {
1424 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1425 8505ab66 2023-07-10 thomas fd = openat(dirfd, de_name,
1426 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1427 8505ab66 2023-07-10 thomas if (fd == -1) {
1428 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1429 8505ab66 2023-07-10 thomas err = got_error_from_errno2("openat",
1430 8505ab66 2023-07-10 thomas ct->ondisk_path);
1431 8505ab66 2023-07-10 thomas goto done;
1433 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1434 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1435 8505ab66 2023-07-10 thomas if (err)
1436 8505ab66 2023-07-10 thomas goto done;
1438 8505ab66 2023-07-10 thomas } else {
1439 8505ab66 2023-07-10 thomas fd = open(ct->ondisk_path,
1440 8505ab66 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1441 8505ab66 2023-07-10 thomas if (fd == -1) {
1442 8505ab66 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
1443 8505ab66 2023-07-10 thomas err = got_error_from_errno2("open",
1444 8505ab66 2023-07-10 thomas ct->ondisk_path);
1445 8505ab66 2023-07-10 thomas goto done;
1447 8505ab66 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
1448 8505ab66 2023-07-10 thomas de_name, ct->ondisk_path);
1449 8505ab66 2023-07-10 thomas if (err)
1450 8505ab66 2023-07-10 thomas goto done;
1453 8505ab66 2023-07-10 thomas if (fstatat(fd, ct->ondisk_path, &sb,
1454 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1455 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
1456 8505ab66 2023-07-10 thomas goto done;
1458 8505ab66 2023-07-10 thomas ondisk_file = fdopen(fd, "r");
1459 8505ab66 2023-07-10 thomas if (ondisk_file == NULL) {
1460 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
1461 8505ab66 2023-07-10 thomas goto done;
1463 8505ab66 2023-07-10 thomas fd = -1;
1464 8505ab66 2023-07-10 thomas f2_exists = 1;
1467 8505ab66 2023-07-10 thomas if (blob1) {
1468 8505ab66 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1469 8505ab66 2023-07-10 thomas f1, blob1);
1470 8505ab66 2023-07-10 thomas if (err)
1471 8505ab66 2023-07-10 thomas goto done;
1474 8505ab66 2023-07-10 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
1475 8505ab66 2023-07-10 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1476 8505ab66 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1478 8505ab66 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1479 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1480 8505ab66 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1481 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1482 8505ab66 2023-07-10 thomas if (blob1)
1483 8505ab66 2023-07-10 thomas got_object_blob_close(blob1);
1484 8505ab66 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1485 8505ab66 2023-07-10 thomas err = got_error_from_errno("close");
1486 8505ab66 2023-07-10 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1487 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
1488 8505ab66 2023-07-10 thomas return err;
1491 8505ab66 2023-07-10 thomas static const struct got_error *
1492 8505ab66 2023-07-10 thomas collect_commitables(void *arg, unsigned char status,
1493 8505ab66 2023-07-10 thomas unsigned char staged_status, const char *relpath,
1494 8505ab66 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1495 8505ab66 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
1497 8505ab66 2023-07-10 thomas struct collect_commitables_arg *a = arg;
1498 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1499 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
1500 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new = NULL;
1501 8505ab66 2023-07-10 thomas char *parent_path = NULL, *path = NULL;
1502 8505ab66 2023-07-10 thomas struct stat sb;
1504 8505ab66 2023-07-10 thomas if (a->have_staged_files) {
1505 8505ab66 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
1506 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
1507 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
1508 8505ab66 2023-07-10 thomas return NULL;
1509 8505ab66 2023-07-10 thomas } else {
1510 8505ab66 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1511 8505ab66 2023-07-10 thomas printf("C %s\n", relpath);
1512 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_CONFLICT);
1515 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
1516 8505ab66 2023-07-10 thomas status != GOT_STATUS_MODE_CHANGE &&
1517 8505ab66 2023-07-10 thomas status != GOT_STATUS_ADD &&
1518 8505ab66 2023-07-10 thomas status != GOT_STATUS_DELETE &&
1519 8505ab66 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
1520 8505ab66 2023-07-10 thomas return NULL;
1523 8505ab66 2023-07-10 thomas if (asprintf(&path, "/%s", relpath) == -1) {
1524 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1525 8505ab66 2023-07-10 thomas goto done;
1527 8505ab66 2023-07-10 thomas if (strcmp(path, "/") == 0) {
1528 8505ab66 2023-07-10 thomas parent_path = strdup("");
1529 8505ab66 2023-07-10 thomas if (parent_path == NULL)
1530 8505ab66 2023-07-10 thomas return got_error_from_errno("strdup");
1531 8505ab66 2023-07-10 thomas } else {
1532 8505ab66 2023-07-10 thomas err = got_path_dirname(&parent_path, path);
1533 8505ab66 2023-07-10 thomas if (err)
1534 8505ab66 2023-07-10 thomas return err;
1537 8505ab66 2023-07-10 thomas ct = calloc(1, sizeof(*ct));
1538 8505ab66 2023-07-10 thomas if (ct == NULL) {
1539 8505ab66 2023-07-10 thomas err = got_error_from_errno("calloc");
1540 8505ab66 2023-07-10 thomas goto done;
1543 8505ab66 2023-07-10 thomas if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1544 8505ab66 2023-07-10 thomas relpath) == -1) {
1545 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1546 8505ab66 2023-07-10 thomas goto done;
1549 8505ab66 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
1550 8505ab66 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
1551 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
1552 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1553 8505ab66 2023-07-10 thomas switch (got_fileindex_entry_staged_filetype_get(ie)) {
1554 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_REGULAR_FILE:
1555 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_BAD_SYMLINK:
1556 8505ab66 2023-07-10 thomas ct->mode = S_IFREG;
1558 8505ab66 2023-07-10 thomas case GOT_FILEIDX_MODE_SYMLINK:
1559 8505ab66 2023-07-10 thomas ct->mode = S_IFLNK;
1561 8505ab66 2023-07-10 thomas default:
1562 8505ab66 2023-07-10 thomas err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1563 8505ab66 2023-07-10 thomas goto done;
1565 8505ab66 2023-07-10 thomas ct->mode |= got_fileindex_entry_perms_get(ie);
1566 8505ab66 2023-07-10 thomas } else if (status != GOT_STATUS_DELETE &&
1567 8505ab66 2023-07-10 thomas staged_status != GOT_STATUS_DELETE) {
1568 8505ab66 2023-07-10 thomas if (dirfd != -1) {
1569 8505ab66 2023-07-10 thomas if (fstatat(dirfd, de_name, &sb,
1570 8505ab66 2023-07-10 thomas AT_SYMLINK_NOFOLLOW) == -1) {
1571 8505ab66 2023-07-10 thomas err = got_error_from_errno2("fstatat",
1572 8505ab66 2023-07-10 thomas ct->ondisk_path);
1573 8505ab66 2023-07-10 thomas goto done;
1575 8505ab66 2023-07-10 thomas } else if (lstat(ct->ondisk_path, &sb) == -1) {
1576 8505ab66 2023-07-10 thomas err = got_error_from_errno2("lstat", ct->ondisk_path);
1577 8505ab66 2023-07-10 thomas goto done;
1579 8505ab66 2023-07-10 thomas ct->mode = sb.st_mode;
1582 8505ab66 2023-07-10 thomas if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1583 8505ab66 2023-07-10 thomas got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1584 8505ab66 2023-07-10 thomas relpath) == -1) {
1585 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
1586 8505ab66 2023-07-10 thomas goto done;
1589 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1590 8505ab66 2023-07-10 thomas status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1591 8505ab66 2023-07-10 thomas int is_bad_symlink;
1592 8505ab66 2023-07-10 thomas char target_path[PATH_MAX];
1593 8505ab66 2023-07-10 thomas ssize_t target_len;
1594 8505ab66 2023-07-10 thomas target_len = readlink(ct->ondisk_path, target_path,
1595 8505ab66 2023-07-10 thomas sizeof(target_path));
1596 8505ab66 2023-07-10 thomas if (target_len == -1) {
1597 8505ab66 2023-07-10 thomas err = got_error_from_errno2("readlink",
1598 8505ab66 2023-07-10 thomas ct->ondisk_path);
1599 8505ab66 2023-07-10 thomas goto done;
1601 8505ab66 2023-07-10 thomas err = is_bad_symlink_target(&is_bad_symlink, target_path,
1602 8505ab66 2023-07-10 thomas target_len, ct->ondisk_path, a->worktree->root_path);
1603 8505ab66 2023-07-10 thomas if (err)
1604 8505ab66 2023-07-10 thomas goto done;
1605 8505ab66 2023-07-10 thomas if (is_bad_symlink) {
1606 8505ab66 2023-07-10 thomas err = got_error_path(ct->ondisk_path,
1607 8505ab66 2023-07-10 thomas GOT_ERR_BAD_SYMLINK);
1608 8505ab66 2023-07-10 thomas goto done;
1612 8505ab66 2023-07-10 thomas ct->status = status;
1613 8505ab66 2023-07-10 thomas ct->staged_status = staged_status;
1614 8505ab66 2023-07-10 thomas ct->blob_id = NULL; /* will be filled in when blob gets created */
1615 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
1616 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) {
1617 8505ab66 2023-07-10 thomas ct->base_blob_id = got_object_id_dup(blob_id);
1618 8505ab66 2023-07-10 thomas if (ct->base_blob_id == NULL) {
1619 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1620 8505ab66 2023-07-10 thomas goto done;
1622 8505ab66 2023-07-10 thomas ct->base_commit_id = got_object_id_dup(commit_id);
1623 8505ab66 2023-07-10 thomas if (ct->base_commit_id == NULL) {
1624 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1625 8505ab66 2023-07-10 thomas goto done;
1628 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
1629 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
1630 8505ab66 2023-07-10 thomas ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1631 8505ab66 2023-07-10 thomas if (ct->staged_blob_id == NULL) {
1632 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_object_id_dup");
1633 8505ab66 2023-07-10 thomas goto done;
1636 8505ab66 2023-07-10 thomas ct->path = strdup(path);
1637 8505ab66 2023-07-10 thomas if (ct->path == NULL) {
1638 8505ab66 2023-07-10 thomas err = got_error_from_errno("strdup");
1639 8505ab66 2023-07-10 thomas goto done;
1641 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1642 8505ab66 2023-07-10 thomas if (err)
1643 8505ab66 2023-07-10 thomas goto done;
1645 8505ab66 2023-07-10 thomas if (a->diff_outfile && ct && new != NULL) {
1646 8505ab66 2023-07-10 thomas err = append_ct_diff(ct, &a->diff_header_shown,
1647 8505ab66 2023-07-10 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1648 8505ab66 2023-07-10 thomas a->have_staged_files, a->repo, a->worktree);
1649 8505ab66 2023-07-10 thomas if (err)
1650 8505ab66 2023-07-10 thomas goto done;
1653 8505ab66 2023-07-10 thomas if (ct && (err || new == NULL))
1654 8505ab66 2023-07-10 thomas free_commitable(ct);
1655 8505ab66 2023-07-10 thomas free(parent_path);
1656 8505ab66 2023-07-10 thomas free(path);
1657 8505ab66 2023-07-10 thomas return err;
1660 8505ab66 2023-07-10 thomas static const struct got_error *write_tree(struct got_object_id **, int *,
1661 8505ab66 2023-07-10 thomas struct got_tree_object *, const char *, struct got_pathlist_head *,
1662 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1663 8505ab66 2023-07-10 thomas struct got_repository *);
1665 8505ab66 2023-07-10 thomas static const struct got_error *
1666 8505ab66 2023-07-10 thomas write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1667 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *parent_path,
1668 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1669 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1670 8505ab66 2023-07-10 thomas struct got_repository *repo)
1672 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1673 8505ab66 2023-07-10 thomas struct got_tree_object *subtree;
1674 8505ab66 2023-07-10 thomas char *subpath;
1676 8505ab66 2023-07-10 thomas if (asprintf(&subpath, "%s%s%s", parent_path,
1677 8505ab66 2023-07-10 thomas got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1678 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1680 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&subtree, repo, &te->id);
1681 8505ab66 2023-07-10 thomas if (err)
1682 8505ab66 2023-07-10 thomas return err;
1684 8505ab66 2023-07-10 thomas err = write_tree(new_subtree_id, nentries, subtree, subpath,
1685 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1686 8505ab66 2023-07-10 thomas got_object_tree_close(subtree);
1687 8505ab66 2023-07-10 thomas free(subpath);
1688 8505ab66 2023-07-10 thomas return err;
1691 8505ab66 2023-07-10 thomas static const struct got_error *
1692 8505ab66 2023-07-10 thomas match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1694 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1695 8505ab66 2023-07-10 thomas char *ct_parent_path = NULL;
1697 8505ab66 2023-07-10 thomas *match = 0;
1699 8505ab66 2023-07-10 thomas if (strchr(ct->in_repo_path, '/') == NULL) {
1700 8505ab66 2023-07-10 thomas *match = got_path_is_root_dir(path);
1701 8505ab66 2023-07-10 thomas return NULL;
1704 8505ab66 2023-07-10 thomas err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1705 8505ab66 2023-07-10 thomas if (err)
1706 8505ab66 2023-07-10 thomas return err;
1707 8505ab66 2023-07-10 thomas *match = (strcmp(path, ct_parent_path) == 0);
1708 8505ab66 2023-07-10 thomas free(ct_parent_path);
1709 8505ab66 2023-07-10 thomas return err;
1712 8505ab66 2023-07-10 thomas static mode_t
1713 8505ab66 2023-07-10 thomas get_ct_file_mode(struct got_commitable *ct)
1715 8505ab66 2023-07-10 thomas if (S_ISLNK(ct->mode))
1716 8505ab66 2023-07-10 thomas return S_IFLNK;
1718 8505ab66 2023-07-10 thomas return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1721 8505ab66 2023-07-10 thomas static const struct got_error *
1722 8505ab66 2023-07-10 thomas alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1723 8505ab66 2023-07-10 thomas struct got_tree_entry *te, struct got_commitable *ct)
1725 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1727 8505ab66 2023-07-10 thomas *new_te = NULL;
1729 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(new_te, te);
1730 8505ab66 2023-07-10 thomas if (err)
1731 8505ab66 2023-07-10 thomas goto done;
1733 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1735 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_MODIFY)
1736 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1737 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1739 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1741 8505ab66 2023-07-10 thomas if (err && *new_te) {
1742 8505ab66 2023-07-10 thomas free(*new_te);
1743 8505ab66 2023-07-10 thomas *new_te = NULL;
1745 8505ab66 2023-07-10 thomas return err;
1748 8505ab66 2023-07-10 thomas static const struct got_error *
1749 8505ab66 2023-07-10 thomas alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1750 8505ab66 2023-07-10 thomas struct got_commitable *ct)
1752 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1753 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1755 8505ab66 2023-07-10 thomas *new_te = NULL;
1757 8505ab66 2023-07-10 thomas *new_te = calloc(1, sizeof(**new_te));
1758 8505ab66 2023-07-10 thomas if (*new_te == NULL)
1759 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1761 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, ct->path);
1762 8505ab66 2023-07-10 thomas if (err)
1763 8505ab66 2023-07-10 thomas goto done;
1764 8505ab66 2023-07-10 thomas if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1765 8505ab66 2023-07-10 thomas sizeof((*new_te)->name)) {
1766 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1767 8505ab66 2023-07-10 thomas goto done;
1770 8505ab66 2023-07-10 thomas (*new_te)->mode = get_ct_file_mode(ct);
1772 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD)
1773 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->staged_blob_id,
1774 8505ab66 2023-07-10 thomas sizeof((*new_te)->id));
1776 8505ab66 2023-07-10 thomas memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1778 8505ab66 2023-07-10 thomas free(ct_name);
1779 8505ab66 2023-07-10 thomas if (err && *new_te) {
1780 8505ab66 2023-07-10 thomas free(*new_te);
1781 8505ab66 2023-07-10 thomas *new_te = NULL;
1783 8505ab66 2023-07-10 thomas return err;
1786 8505ab66 2023-07-10 thomas static const struct got_error *
1787 8505ab66 2023-07-10 thomas insert_tree_entry(struct got_tree_entry *new_te,
1788 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
1790 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1791 8505ab66 2023-07-10 thomas struct got_pathlist_entry *new_pe;
1793 8505ab66 2023-07-10 thomas err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1794 8505ab66 2023-07-10 thomas if (err)
1795 8505ab66 2023-07-10 thomas return err;
1796 8505ab66 2023-07-10 thomas if (new_pe == NULL)
1797 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_TREE_DUP_ENTRY);
1798 8505ab66 2023-07-10 thomas return NULL;
1801 8505ab66 2023-07-10 thomas static const struct got_error *
1802 8505ab66 2023-07-10 thomas report_ct_status(struct got_commitable *ct,
1803 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg)
1805 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
1806 8505ab66 2023-07-10 thomas unsigned char status;
1808 8505ab66 2023-07-10 thomas if (status_cb == NULL) /* no commit progress output desired */
1809 8505ab66 2023-07-10 thomas return NULL;
1811 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
1812 8505ab66 2023-07-10 thomas ct_path++;
1814 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1815 8505ab66 2023-07-10 thomas status = ct->staged_status;
1817 8505ab66 2023-07-10 thomas status = ct->status;
1819 8505ab66 2023-07-10 thomas return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1820 8505ab66 2023-07-10 thomas ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1823 8505ab66 2023-07-10 thomas static const struct got_error *
1824 8505ab66 2023-07-10 thomas match_modified_subtree(int *modified, struct got_tree_entry *te,
1825 8505ab66 2023-07-10 thomas const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1827 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1828 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1829 8505ab66 2023-07-10 thomas char *te_path;
1831 8505ab66 2023-07-10 thomas *modified = 0;
1833 8505ab66 2023-07-10 thomas if (asprintf(&te_path, "%s%s%s", base_tree_path,
1834 8505ab66 2023-07-10 thomas got_path_is_root_dir(base_tree_path) ? "" : "/",
1835 8505ab66 2023-07-10 thomas te->name) == -1)
1836 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1838 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1839 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1840 8505ab66 2023-07-10 thomas *modified = got_path_is_child(ct->in_repo_path, te_path,
1841 8505ab66 2023-07-10 thomas strlen(te_path));
1842 8505ab66 2023-07-10 thomas if (*modified)
1846 8505ab66 2023-07-10 thomas free(te_path);
1847 8505ab66 2023-07-10 thomas return err;
1850 8505ab66 2023-07-10 thomas static const struct got_error *
1851 8505ab66 2023-07-10 thomas match_deleted_or_modified_ct(struct got_commitable **ctp,
1852 8505ab66 2023-07-10 thomas struct got_tree_entry *te, const char *base_tree_path,
1853 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
1855 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1856 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1858 8505ab66 2023-07-10 thomas *ctp = NULL;
1860 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1861 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1862 8505ab66 2023-07-10 thomas char *ct_name = NULL;
1863 8505ab66 2023-07-10 thomas int path_matches;
1865 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1866 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_MODIFY &&
1867 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
1868 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_DELETE &&
1869 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
1870 8505ab66 2023-07-10 thomas continue;
1871 8505ab66 2023-07-10 thomas } else {
1872 8505ab66 2023-07-10 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
1873 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_DELETE)
1874 8505ab66 2023-07-10 thomas continue;
1877 8505ab66 2023-07-10 thomas if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1878 8505ab66 2023-07-10 thomas continue;
1880 8505ab66 2023-07-10 thomas err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1881 8505ab66 2023-07-10 thomas if (err)
1882 8505ab66 2023-07-10 thomas return err;
1883 8505ab66 2023-07-10 thomas if (!path_matches)
1884 8505ab66 2023-07-10 thomas continue;
1886 8505ab66 2023-07-10 thomas err = got_path_basename(&ct_name, pe->path);
1887 8505ab66 2023-07-10 thomas if (err)
1888 8505ab66 2023-07-10 thomas return err;
1890 8505ab66 2023-07-10 thomas if (strcmp(te->name, ct_name) != 0) {
1891 8505ab66 2023-07-10 thomas free(ct_name);
1892 8505ab66 2023-07-10 thomas continue;
1894 8505ab66 2023-07-10 thomas free(ct_name);
1896 8505ab66 2023-07-10 thomas *ctp = ct;
1900 8505ab66 2023-07-10 thomas return err;
1903 8505ab66 2023-07-10 thomas static const struct got_error *
1904 8505ab66 2023-07-10 thomas make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1905 8505ab66 2023-07-10 thomas const char *child_path, const char *path_base_tree,
1906 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1907 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1908 8505ab66 2023-07-10 thomas struct got_repository *repo)
1910 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1911 8505ab66 2023-07-10 thomas struct got_tree_entry *new_te;
1912 8505ab66 2023-07-10 thomas char *subtree_path;
1913 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
1914 8505ab66 2023-07-10 thomas int nentries;
1916 8505ab66 2023-07-10 thomas *new_tep = NULL;
1918 8505ab66 2023-07-10 thomas if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1919 8505ab66 2023-07-10 thomas got_path_is_root_dir(path_base_tree) ? "" : "/",
1920 8505ab66 2023-07-10 thomas child_path) == -1)
1921 8505ab66 2023-07-10 thomas return got_error_from_errno("asprintf");
1923 8505ab66 2023-07-10 thomas new_te = calloc(1, sizeof(*new_te));
1924 8505ab66 2023-07-10 thomas if (new_te == NULL)
1925 8505ab66 2023-07-10 thomas return got_error_from_errno("calloc");
1926 8505ab66 2023-07-10 thomas new_te->mode = S_IFDIR;
1928 8505ab66 2023-07-10 thomas if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1929 8505ab66 2023-07-10 thomas sizeof(new_te->name)) {
1930 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
1931 8505ab66 2023-07-10 thomas goto done;
1933 8505ab66 2023-07-10 thomas err = write_tree(&id, &nentries, NULL, subtree_path,
1934 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
1935 8505ab66 2023-07-10 thomas if (err) {
1936 8505ab66 2023-07-10 thomas free(new_te);
1937 8505ab66 2023-07-10 thomas goto done;
1939 8505ab66 2023-07-10 thomas memcpy(&new_te->id, id, sizeof(new_te->id));
1941 8505ab66 2023-07-10 thomas free(id);
1942 8505ab66 2023-07-10 thomas free(subtree_path);
1943 8505ab66 2023-07-10 thomas if (err == NULL)
1944 8505ab66 2023-07-10 thomas *new_tep = new_te;
1945 8505ab66 2023-07-10 thomas return err;
1948 8505ab66 2023-07-10 thomas static const struct got_error *
1949 8505ab66 2023-07-10 thomas write_tree(struct got_object_id **new_tree_id, int *nentries,
1950 8505ab66 2023-07-10 thomas struct got_tree_object *base_tree, const char *path_base_tree,
1951 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
1952 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
1953 8505ab66 2023-07-10 thomas struct got_repository *repo)
1955 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
1956 8505ab66 2023-07-10 thomas struct got_pathlist_head paths;
1957 8505ab66 2023-07-10 thomas struct got_tree_entry *te, *new_te = NULL;
1958 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
1960 8505ab66 2023-07-10 thomas TAILQ_INIT(&paths);
1961 8505ab66 2023-07-10 thomas *nentries = 0;
1963 8505ab66 2023-07-10 thomas /* Insert, and recurse into, newly added entries first. */
1964 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
1965 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
1966 8505ab66 2023-07-10 thomas char *child_path = NULL, *slash;
1968 8505ab66 2023-07-10 thomas if ((ct->status != GOT_STATUS_ADD &&
1969 8505ab66 2023-07-10 thomas ct->staged_status != GOT_STATUS_ADD) ||
1970 8505ab66 2023-07-10 thomas (ct->flags & GOT_COMMITABLE_ADDED))
1971 8505ab66 2023-07-10 thomas continue;
1973 8505ab66 2023-07-10 thomas if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1974 8505ab66 2023-07-10 thomas strlen(path_base_tree)))
1975 8505ab66 2023-07-10 thomas continue;
1977 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1978 8505ab66 2023-07-10 thomas ct->in_repo_path);
1979 8505ab66 2023-07-10 thomas if (err)
1980 8505ab66 2023-07-10 thomas goto done;
1982 8505ab66 2023-07-10 thomas slash = strchr(child_path, '/');
1983 8505ab66 2023-07-10 thomas if (slash == NULL) {
1984 8505ab66 2023-07-10 thomas err = alloc_added_blob_tree_entry(&new_te, ct);
1985 8505ab66 2023-07-10 thomas if (err)
1986 8505ab66 2023-07-10 thomas goto done;
1987 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb, status_arg);
1988 8505ab66 2023-07-10 thomas if (err)
1989 8505ab66 2023-07-10 thomas goto done;
1990 8505ab66 2023-07-10 thomas ct->flags |= GOT_COMMITABLE_ADDED;
1991 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
1992 8505ab66 2023-07-10 thomas if (err)
1993 8505ab66 2023-07-10 thomas goto done;
1994 8505ab66 2023-07-10 thomas (*nentries)++;
1995 8505ab66 2023-07-10 thomas } else {
1996 8505ab66 2023-07-10 thomas *slash = '\0'; /* trim trailing path components */
1997 8505ab66 2023-07-10 thomas if (base_tree == NULL ||
1998 8505ab66 2023-07-10 thomas got_object_tree_find_entry(base_tree, child_path)
1999 8505ab66 2023-07-10 thomas == NULL) {
2000 8505ab66 2023-07-10 thomas err = make_subtree_for_added_blob(&new_te,
2001 8505ab66 2023-07-10 thomas child_path, path_base_tree,
2002 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg,
2004 8505ab66 2023-07-10 thomas if (err)
2005 8505ab66 2023-07-10 thomas goto done;
2006 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2007 8505ab66 2023-07-10 thomas if (err)
2008 8505ab66 2023-07-10 thomas goto done;
2009 8505ab66 2023-07-10 thomas (*nentries)++;
2014 8505ab66 2023-07-10 thomas if (base_tree) {
2015 8505ab66 2023-07-10 thomas int i, nbase_entries;
2016 8505ab66 2023-07-10 thomas /* Handle modified and deleted entries. */
2017 8505ab66 2023-07-10 thomas nbase_entries = got_object_tree_get_nentries(base_tree);
2018 8505ab66 2023-07-10 thomas for (i = 0; i < nbase_entries; i++) {
2019 8505ab66 2023-07-10 thomas struct got_commitable *ct = NULL;
2021 8505ab66 2023-07-10 thomas te = got_object_tree_get_entry(base_tree, i);
2022 8505ab66 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te)) {
2023 8505ab66 2023-07-10 thomas /* Entry is a submodule; just copy it. */
2024 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2025 8505ab66 2023-07-10 thomas if (err)
2026 8505ab66 2023-07-10 thomas goto done;
2027 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2028 8505ab66 2023-07-10 thomas if (err)
2029 8505ab66 2023-07-10 thomas goto done;
2030 8505ab66 2023-07-10 thomas (*nentries)++;
2031 8505ab66 2023-07-10 thomas continue;
2034 8505ab66 2023-07-10 thomas if (S_ISDIR(te->mode)) {
2035 8505ab66 2023-07-10 thomas int modified;
2036 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2037 8505ab66 2023-07-10 thomas if (err)
2038 8505ab66 2023-07-10 thomas goto done;
2039 8505ab66 2023-07-10 thomas err = match_modified_subtree(&modified, te,
2040 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2041 8505ab66 2023-07-10 thomas if (err)
2042 8505ab66 2023-07-10 thomas goto done;
2043 8505ab66 2023-07-10 thomas /* Avoid recursion into unmodified subtrees. */
2044 8505ab66 2023-07-10 thomas if (modified) {
2045 8505ab66 2023-07-10 thomas struct got_object_id *new_id;
2046 8505ab66 2023-07-10 thomas int nsubentries;
2047 8505ab66 2023-07-10 thomas err = write_subtree(&new_id,
2048 8505ab66 2023-07-10 thomas &nsubentries, te,
2049 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths,
2050 8505ab66 2023-07-10 thomas status_cb, status_arg, repo);
2051 8505ab66 2023-07-10 thomas if (err)
2052 8505ab66 2023-07-10 thomas goto done;
2053 8505ab66 2023-07-10 thomas if (nsubentries == 0) {
2054 8505ab66 2023-07-10 thomas /* All entries were deleted. */
2055 8505ab66 2023-07-10 thomas free(new_id);
2056 8505ab66 2023-07-10 thomas continue;
2058 8505ab66 2023-07-10 thomas memcpy(&new_te->id, new_id,
2059 8505ab66 2023-07-10 thomas sizeof(new_te->id));
2060 8505ab66 2023-07-10 thomas free(new_id);
2062 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2063 8505ab66 2023-07-10 thomas if (err)
2064 8505ab66 2023-07-10 thomas goto done;
2065 8505ab66 2023-07-10 thomas (*nentries)++;
2066 8505ab66 2023-07-10 thomas continue;
2069 8505ab66 2023-07-10 thomas err = match_deleted_or_modified_ct(&ct, te,
2070 8505ab66 2023-07-10 thomas path_base_tree, commitable_paths);
2071 8505ab66 2023-07-10 thomas if (err)
2072 8505ab66 2023-07-10 thomas goto done;
2073 8505ab66 2023-07-10 thomas if (ct) {
2074 8505ab66 2023-07-10 thomas /* NB: Deleted entries get dropped here. */
2075 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_MODIFY ||
2076 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_MODE_CHANGE ||
2077 8505ab66 2023-07-10 thomas ct->status == GOT_STATUS_CONFLICT ||
2078 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2079 8505ab66 2023-07-10 thomas err = alloc_modified_blob_tree_entry(
2080 8505ab66 2023-07-10 thomas &new_te, te, ct);
2081 8505ab66 2023-07-10 thomas if (err)
2082 8505ab66 2023-07-10 thomas goto done;
2083 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2084 8505ab66 2023-07-10 thomas if (err)
2085 8505ab66 2023-07-10 thomas goto done;
2086 8505ab66 2023-07-10 thomas (*nentries)++;
2088 8505ab66 2023-07-10 thomas err = report_ct_status(ct, status_cb,
2089 8505ab66 2023-07-10 thomas status_arg);
2090 8505ab66 2023-07-10 thomas if (err)
2091 8505ab66 2023-07-10 thomas goto done;
2092 8505ab66 2023-07-10 thomas } else {
2093 8505ab66 2023-07-10 thomas /* Entry is unchanged; just copy it. */
2094 8505ab66 2023-07-10 thomas err = got_object_tree_entry_dup(&new_te, te);
2095 8505ab66 2023-07-10 thomas if (err)
2096 8505ab66 2023-07-10 thomas goto done;
2097 8505ab66 2023-07-10 thomas err = insert_tree_entry(new_te, &paths);
2098 8505ab66 2023-07-10 thomas if (err)
2099 8505ab66 2023-07-10 thomas goto done;
2100 8505ab66 2023-07-10 thomas (*nentries)++;
2105 8505ab66 2023-07-10 thomas /* Write new list of entries; deleted entries have been dropped. */
2106 8505ab66 2023-07-10 thomas err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2108 8505ab66 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2109 8505ab66 2023-07-10 thomas return err;
2112 8505ab66 2023-07-10 thomas static const struct got_error *
2113 8505ab66 2023-07-10 thomas update_fileindex_after_commit(struct got_worktree *worktree,
2114 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2115 8505ab66 2023-07-10 thomas struct got_object_id *new_base_commit_id,
2116 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex, int have_staged_files)
2118 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2119 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2120 8505ab66 2023-07-10 thomas char *relpath = NULL;
2122 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2123 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2124 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2126 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2128 8505ab66 2023-07-10 thomas err = got_path_skip_common_ancestor(&relpath,
2129 8505ab66 2023-07-10 thomas worktree->root_path, ct->ondisk_path);
2130 8505ab66 2023-07-10 thomas if (err)
2131 8505ab66 2023-07-10 thomas goto done;
2133 8505ab66 2023-07-10 thomas if (ie) {
2134 8505ab66 2023-07-10 thomas if (ct->status == GOT_STATUS_DELETE ||
2135 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_DELETE) {
2136 8505ab66 2023-07-10 thomas got_fileindex_entry_remove(fileindex, ie);
2137 8505ab66 2023-07-10 thomas } else if (ct->staged_status == GOT_STATUS_ADD ||
2138 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY) {
2139 8505ab66 2023-07-10 thomas got_fileindex_entry_stage_set(ie,
2140 8505ab66 2023-07-10 thomas GOT_FILEIDX_STAGE_NONE);
2141 8505ab66 2023-07-10 thomas got_fileindex_entry_staged_filetype_set(ie, 0);
2143 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2144 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2145 8505ab66 2023-07-10 thomas ct->staged_blob_id->sha1,
2146 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2147 8505ab66 2023-07-10 thomas !have_staged_files);
2149 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2150 8505ab66 2023-07-10 thomas worktree->root_fd, relpath,
2151 8505ab66 2023-07-10 thomas ct->blob_id->sha1,
2152 8505ab66 2023-07-10 thomas new_base_commit_id->sha1,
2153 8505ab66 2023-07-10 thomas !have_staged_files);
2154 8505ab66 2023-07-10 thomas } else {
2155 8505ab66 2023-07-10 thomas err = got_fileindex_entry_alloc(&ie, pe->path);
2156 8505ab66 2023-07-10 thomas if (err)
2157 8505ab66 2023-07-10 thomas goto done;
2158 8505ab66 2023-07-10 thomas err = got_fileindex_entry_update(ie,
2159 8505ab66 2023-07-10 thomas worktree->root_fd, relpath, ct->blob_id->sha1,
2160 8505ab66 2023-07-10 thomas new_base_commit_id->sha1, 1);
2161 8505ab66 2023-07-10 thomas if (err) {
2162 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2163 8505ab66 2023-07-10 thomas goto done;
2165 8505ab66 2023-07-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2166 8505ab66 2023-07-10 thomas if (err) {
2167 8505ab66 2023-07-10 thomas got_fileindex_entry_free(ie);
2168 8505ab66 2023-07-10 thomas goto done;
2171 8505ab66 2023-07-10 thomas free(relpath);
2172 8505ab66 2023-07-10 thomas relpath = NULL;
2175 8505ab66 2023-07-10 thomas free(relpath);
2176 8505ab66 2023-07-10 thomas return err;
2179 8505ab66 2023-07-10 thomas static const struct got_error *
2180 8505ab66 2023-07-10 thomas check_out_of_date(const char *in_repo_path, unsigned char status,
2181 8505ab66 2023-07-10 thomas unsigned char staged_status, struct got_object_id *base_blob_id,
2182 8505ab66 2023-07-10 thomas struct got_object_id *base_commit_id,
2183 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id, struct got_repository *repo,
2184 8505ab66 2023-07-10 thomas int ood_errcode)
2186 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2187 8505ab66 2023-07-10 thomas struct got_commit_object *commit = NULL;
2188 8505ab66 2023-07-10 thomas struct got_object_id *id = NULL;
2190 8505ab66 2023-07-10 thomas if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2191 8505ab66 2023-07-10 thomas /* Trivial case: base commit == head commit */
2192 8505ab66 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2193 8505ab66 2023-07-10 thomas return NULL;
2195 8505ab66 2023-07-10 thomas * Ensure file content which local changes were based
2196 8505ab66 2023-07-10 thomas * on matches file content in the branch head.
2198 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2199 8505ab66 2023-07-10 thomas if (err)
2200 8505ab66 2023-07-10 thomas goto done;
2201 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2202 8505ab66 2023-07-10 thomas if (err) {
2203 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NO_TREE_ENTRY)
2204 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2205 8505ab66 2023-07-10 thomas goto done;
2206 8505ab66 2023-07-10 thomas } else if (got_object_id_cmp(id, base_blob_id) != 0)
2207 8505ab66 2023-07-10 thomas err = got_error(ood_errcode);
2208 8505ab66 2023-07-10 thomas } else {
2209 8505ab66 2023-07-10 thomas /* Require that added files don't exist in the branch head. */
2210 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
2211 8505ab66 2023-07-10 thomas if (err)
2212 8505ab66 2023-07-10 thomas goto done;
2213 8505ab66 2023-07-10 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2214 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2215 8505ab66 2023-07-10 thomas goto done;
2216 8505ab66 2023-07-10 thomas err = id ? got_error(ood_errcode) : NULL;
2219 8505ab66 2023-07-10 thomas free(id);
2220 8505ab66 2023-07-10 thomas if (commit)
2221 8505ab66 2023-07-10 thomas got_object_commit_close(commit);
2222 8505ab66 2023-07-10 thomas return err;
2225 8505ab66 2023-07-10 thomas static const struct got_error *
2226 8505ab66 2023-07-10 thomas commit_worktree(struct got_object_id **new_commit_id,
2227 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths,
2228 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id,
2229 8505ab66 2023-07-10 thomas struct got_object_id *parent_id2,
2230 8505ab66 2023-07-10 thomas struct got_worktree *worktree,
2231 8505ab66 2023-07-10 thomas const char *author, const char *committer, char *diff_path,
2232 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2233 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2234 8505ab66 2023-07-10 thomas struct got_repository *repo)
2236 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2237 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2238 8505ab66 2023-07-10 thomas struct got_commit_object *head_commit = NULL;
2239 8505ab66 2023-07-10 thomas struct got_tree_object *head_tree = NULL;
2240 8505ab66 2023-07-10 thomas struct got_object_id *new_tree_id = NULL;
2241 8505ab66 2023-07-10 thomas int nentries, nparents = 0;
2242 8505ab66 2023-07-10 thomas struct got_object_id_queue parent_ids;
2243 8505ab66 2023-07-10 thomas struct got_object_qid *pid = NULL;
2244 8505ab66 2023-07-10 thomas char *logmsg = NULL;
2245 8505ab66 2023-07-10 thomas time_t timestamp;
2247 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2249 8505ab66 2023-07-10 thomas STAILQ_INIT(&parent_ids);
2251 8505ab66 2023-07-10 thomas err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2252 8505ab66 2023-07-10 thomas if (err)
2253 8505ab66 2023-07-10 thomas goto done;
2255 8505ab66 2023-07-10 thomas err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2256 8505ab66 2023-07-10 thomas if (err)
2257 8505ab66 2023-07-10 thomas goto done;
2259 8505ab66 2023-07-10 thomas if (commit_msg_cb != NULL) {
2260 8505ab66 2023-07-10 thomas err = commit_msg_cb(commitable_paths, diff_path,
2261 8505ab66 2023-07-10 thomas &logmsg, commit_arg);
2262 8505ab66 2023-07-10 thomas if (err)
2263 8505ab66 2023-07-10 thomas goto done;
2266 8505ab66 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0) {
2267 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2268 8505ab66 2023-07-10 thomas goto done;
2271 8505ab66 2023-07-10 thomas /* Create blobs from added and modified files and record their IDs. */
2272 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
2273 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
2274 8505ab66 2023-07-10 thomas char *ondisk_path;
2276 8505ab66 2023-07-10 thomas /* Blobs for staged files already exist. */
2277 8505ab66 2023-07-10 thomas if (ct->staged_status == GOT_STATUS_ADD ||
2278 8505ab66 2023-07-10 thomas ct->staged_status == GOT_STATUS_MODIFY)
2279 8505ab66 2023-07-10 thomas continue;
2281 8505ab66 2023-07-10 thomas if (ct->status != GOT_STATUS_ADD &&
2282 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODIFY &&
2283 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
2284 8505ab66 2023-07-10 thomas ct->status != GOT_STATUS_CONFLICT)
2285 8505ab66 2023-07-10 thomas continue;
2287 8505ab66 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
2288 8505ab66 2023-07-10 thomas worktree->root_path, pe->path) == -1) {
2289 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2290 8505ab66 2023-07-10 thomas goto done;
2292 8505ab66 2023-07-10 thomas err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2293 8505ab66 2023-07-10 thomas free(ondisk_path);
2294 8505ab66 2023-07-10 thomas if (err)
2295 8505ab66 2023-07-10 thomas goto done;
2298 8505ab66 2023-07-10 thomas /* Recursively write new tree objects. */
2299 8505ab66 2023-07-10 thomas err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2300 8505ab66 2023-07-10 thomas commitable_paths, status_cb, status_arg, repo);
2301 8505ab66 2023-07-10 thomas if (err)
2302 8505ab66 2023-07-10 thomas goto done;
2304 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, head_commit_id);
2305 8505ab66 2023-07-10 thomas if (err)
2306 8505ab66 2023-07-10 thomas goto done;
2307 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2308 8505ab66 2023-07-10 thomas nparents++;
2309 8505ab66 2023-07-10 thomas if (parent_id2) {
2310 8505ab66 2023-07-10 thomas err = got_object_qid_alloc(&pid, parent_id2);
2311 8505ab66 2023-07-10 thomas if (err)
2312 8505ab66 2023-07-10 thomas goto done;
2313 8505ab66 2023-07-10 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2314 8505ab66 2023-07-10 thomas nparents++;
2316 8505ab66 2023-07-10 thomas timestamp = time(NULL);
2317 8505ab66 2023-07-10 thomas err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2318 8505ab66 2023-07-10 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
2319 8505ab66 2023-07-10 thomas if (logmsg != NULL)
2320 8505ab66 2023-07-10 thomas free(logmsg);
2321 8505ab66 2023-07-10 thomas if (err)
2322 8505ab66 2023-07-10 thomas goto done;
2324 8505ab66 2023-07-10 thomas got_object_id_queue_free(&parent_ids);
2325 8505ab66 2023-07-10 thomas if (head_tree)
2326 8505ab66 2023-07-10 thomas got_object_tree_close(head_tree);
2327 8505ab66 2023-07-10 thomas if (head_commit)
2328 8505ab66 2023-07-10 thomas got_object_commit_close(head_commit);
2329 8505ab66 2023-07-10 thomas return err;
2332 8505ab66 2023-07-10 thomas static const struct got_error *
2333 8505ab66 2023-07-10 thomas check_path_is_commitable(const char *path,
2334 8505ab66 2023-07-10 thomas struct got_pathlist_head *commitable_paths)
2336 8505ab66 2023-07-10 thomas struct got_pathlist_entry *cpe = NULL;
2337 8505ab66 2023-07-10 thomas size_t path_len = strlen(path);
2339 8505ab66 2023-07-10 thomas TAILQ_FOREACH(cpe, commitable_paths, entry) {
2340 8505ab66 2023-07-10 thomas struct got_commitable *ct = cpe->data;
2341 8505ab66 2023-07-10 thomas const char *ct_path = ct->path;
2343 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
2344 8505ab66 2023-07-10 thomas ct_path++;
2346 8505ab66 2023-07-10 thomas if (strcmp(path, ct_path) == 0 ||
2347 8505ab66 2023-07-10 thomas got_path_is_child(ct_path, path, path_len))
2351 8505ab66 2023-07-10 thomas if (cpe == NULL)
2352 8505ab66 2023-07-10 thomas return got_error_path(path, GOT_ERR_BAD_PATH);
2354 8505ab66 2023-07-10 thomas return NULL;
2357 8505ab66 2023-07-10 thomas static const struct got_error *
2358 8505ab66 2023-07-10 thomas check_staged_file(void *arg, struct got_fileindex_entry *ie)
2360 8505ab66 2023-07-10 thomas int *have_staged_files = arg;
2362 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2363 8505ab66 2023-07-10 thomas *have_staged_files = 1;
2364 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2367 8505ab66 2023-07-10 thomas return NULL;
2370 8505ab66 2023-07-10 thomas static const struct got_error *
2371 8505ab66 2023-07-10 thomas check_non_staged_files(struct got_fileindex *fileindex,
2372 8505ab66 2023-07-10 thomas struct got_pathlist_head *paths)
2374 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2375 8505ab66 2023-07-10 thomas struct got_fileindex_entry *ie;
2377 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
2378 8505ab66 2023-07-10 thomas if (pe->path[0] == '\0')
2379 8505ab66 2023-07-10 thomas continue;
2380 8505ab66 2023-07-10 thomas ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2381 8505ab66 2023-07-10 thomas if (ie == NULL)
2382 8505ab66 2023-07-10 thomas return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2383 8505ab66 2023-07-10 thomas if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2384 8505ab66 2023-07-10 thomas return got_error_path(pe->path,
2385 8505ab66 2023-07-10 thomas GOT_ERR_FILE_NOT_STAGED);
2388 8505ab66 2023-07-10 thomas return NULL;
2391 8505ab66 2023-07-10 thomas static void
2392 8505ab66 2023-07-10 thomas print_load_info(int print_colored, int print_found, int print_trees,
2393 8505ab66 2023-07-10 thomas int ncolored, int nfound, int ntrees)
2395 8505ab66 2023-07-10 thomas if (print_colored) {
2396 8505ab66 2023-07-10 thomas printf("%d commit%s colored", ncolored,
2397 8505ab66 2023-07-10 thomas ncolored == 1 ? "" : "s");
2399 8505ab66 2023-07-10 thomas if (print_found) {
2400 8505ab66 2023-07-10 thomas printf("%s%d object%s found",
2401 8505ab66 2023-07-10 thomas ncolored > 0 ? "; " : "",
2402 8505ab66 2023-07-10 thomas nfound, nfound == 1 ? "" : "s");
2404 8505ab66 2023-07-10 thomas if (print_trees) {
2405 8505ab66 2023-07-10 thomas printf("; %d tree%s scanned", ntrees,
2406 8505ab66 2023-07-10 thomas ntrees == 1 ? "" : "s");
2410 8505ab66 2023-07-10 thomas struct got_send_progress_arg {
2411 8505ab66 2023-07-10 thomas char last_scaled_packsize[FMT_SCALED_STRSIZE];
2412 8505ab66 2023-07-10 thomas int verbosity;
2413 8505ab66 2023-07-10 thomas int last_ncolored;
2414 8505ab66 2023-07-10 thomas int last_nfound;
2415 8505ab66 2023-07-10 thomas int last_ntrees;
2416 8505ab66 2023-07-10 thomas int loading_done;
2417 8505ab66 2023-07-10 thomas int last_ncommits;
2418 8505ab66 2023-07-10 thomas int last_nobj_total;
2419 8505ab66 2023-07-10 thomas int last_p_deltify;
2420 8505ab66 2023-07-10 thomas int last_p_written;
2421 8505ab66 2023-07-10 thomas int last_p_sent;
2422 8505ab66 2023-07-10 thomas int printed_something;
2423 8505ab66 2023-07-10 thomas int sent_something;
2424 8505ab66 2023-07-10 thomas struct got_pathlist_head *delete_branches;
2427 8505ab66 2023-07-10 thomas static const struct got_error *
2428 8505ab66 2023-07-10 thomas send_progress(void *arg, int ncolored, int nfound, int ntrees,
2429 8505ab66 2023-07-10 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2430 8505ab66 2023-07-10 thomas int nobj_written, off_t bytes_sent, const char *refname,
2431 8505ab66 2023-07-10 thomas const char *errmsg, int success)
2433 8505ab66 2023-07-10 thomas struct got_send_progress_arg *a = arg;
2434 8505ab66 2023-07-10 thomas char scaled_packsize[FMT_SCALED_STRSIZE];
2435 8505ab66 2023-07-10 thomas char scaled_sent[FMT_SCALED_STRSIZE];
2436 8505ab66 2023-07-10 thomas int p_deltify = 0, p_written = 0, p_sent = 0;
2437 8505ab66 2023-07-10 thomas int print_colored = 0, print_found = 0, print_trees = 0;
2438 8505ab66 2023-07-10 thomas int print_searching = 0, print_total = 0;
2439 8505ab66 2023-07-10 thomas int print_deltify = 0, print_written = 0, print_sent = 0;
2441 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2442 8505ab66 2023-07-10 thomas return NULL;
2444 8505ab66 2023-07-10 thomas if (refname) {
2445 8505ab66 2023-07-10 thomas const char *status = success ? "accepted" : "rejected";
2447 8505ab66 2023-07-10 thomas if (success) {
2448 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2449 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, a->delete_branches, entry) {
2450 8505ab66 2023-07-10 thomas const char *branchname = pe->path;
2451 8505ab66 2023-07-10 thomas if (got_path_cmp(branchname, refname,
2452 8505ab66 2023-07-10 thomas strlen(branchname), strlen(refname)) == 0) {
2453 8505ab66 2023-07-10 thomas status = "deleted";
2454 8505ab66 2023-07-10 thomas a->sent_something = 1;
2460 8505ab66 2023-07-10 thomas if (a->printed_something)
2461 8505ab66 2023-07-10 thomas putchar('\n');
2462 8505ab66 2023-07-10 thomas printf("Server has %s %s", status, refname);
2463 8505ab66 2023-07-10 thomas if (errmsg)
2464 8505ab66 2023-07-10 thomas printf(": %s", errmsg);
2465 8505ab66 2023-07-10 thomas a->printed_something = 1;
2466 8505ab66 2023-07-10 thomas return NULL;
2469 8505ab66 2023-07-10 thomas if (a->last_ncolored != ncolored) {
2470 8505ab66 2023-07-10 thomas print_colored = 1;
2471 8505ab66 2023-07-10 thomas a->last_ncolored = ncolored;
2474 8505ab66 2023-07-10 thomas if (a->last_nfound != nfound) {
2475 8505ab66 2023-07-10 thomas print_colored = 1;
2476 8505ab66 2023-07-10 thomas print_found = 1;
2477 8505ab66 2023-07-10 thomas a->last_nfound = nfound;
2480 8505ab66 2023-07-10 thomas if (a->last_ntrees != ntrees) {
2481 8505ab66 2023-07-10 thomas print_colored = 1;
2482 8505ab66 2023-07-10 thomas print_found = 1;
2483 8505ab66 2023-07-10 thomas print_trees = 1;
2484 8505ab66 2023-07-10 thomas a->last_ntrees = ntrees;
2487 8505ab66 2023-07-10 thomas if ((print_colored || print_found || print_trees) &&
2488 8505ab66 2023-07-10 thomas !a->loading_done) {
2489 8505ab66 2023-07-10 thomas printf("\r");
2490 8505ab66 2023-07-10 thomas print_load_info(print_colored, print_found, print_trees,
2491 8505ab66 2023-07-10 thomas ncolored, nfound, ntrees);
2492 8505ab66 2023-07-10 thomas a->printed_something = 1;
2493 8505ab66 2023-07-10 thomas fflush(stdout);
2494 8505ab66 2023-07-10 thomas return NULL;
2495 8505ab66 2023-07-10 thomas } else if (!a->loading_done) {
2496 8505ab66 2023-07-10 thomas printf("\r");
2497 8505ab66 2023-07-10 thomas print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2498 8505ab66 2023-07-10 thomas printf("\n");
2499 8505ab66 2023-07-10 thomas a->loading_done = 1;
2502 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2503 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2504 8505ab66 2023-07-10 thomas if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2505 8505ab66 2023-07-10 thomas return got_error_from_errno("fmt_scaled");
2507 8505ab66 2023-07-10 thomas if (a->last_ncommits != ncommits) {
2508 8505ab66 2023-07-10 thomas print_searching = 1;
2509 8505ab66 2023-07-10 thomas a->last_ncommits = ncommits;
2512 8505ab66 2023-07-10 thomas if (a->last_nobj_total != nobj_total) {
2513 8505ab66 2023-07-10 thomas print_searching = 1;
2514 8505ab66 2023-07-10 thomas print_total = 1;
2515 8505ab66 2023-07-10 thomas a->last_nobj_total = nobj_total;
2518 8505ab66 2023-07-10 thomas if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2519 8505ab66 2023-07-10 thomas strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2520 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2521 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2522 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2525 8505ab66 2023-07-10 thomas if (nobj_deltify > 0 || nobj_written > 0) {
2526 8505ab66 2023-07-10 thomas if (nobj_deltify > 0) {
2527 8505ab66 2023-07-10 thomas p_deltify = (nobj_deltify * 100) / nobj_total;
2528 8505ab66 2023-07-10 thomas if (p_deltify != a->last_p_deltify) {
2529 8505ab66 2023-07-10 thomas a->last_p_deltify = p_deltify;
2530 8505ab66 2023-07-10 thomas print_searching = 1;
2531 8505ab66 2023-07-10 thomas print_total = 1;
2532 8505ab66 2023-07-10 thomas print_deltify = 1;
2535 8505ab66 2023-07-10 thomas if (nobj_written > 0) {
2536 8505ab66 2023-07-10 thomas p_written = (nobj_written * 100) / nobj_total;
2537 8505ab66 2023-07-10 thomas if (p_written != a->last_p_written) {
2538 8505ab66 2023-07-10 thomas a->last_p_written = p_written;
2539 8505ab66 2023-07-10 thomas print_searching = 1;
2540 8505ab66 2023-07-10 thomas print_total = 1;
2541 8505ab66 2023-07-10 thomas print_deltify = 1;
2542 8505ab66 2023-07-10 thomas print_written = 1;
2547 8505ab66 2023-07-10 thomas if (bytes_sent > 0) {
2548 8505ab66 2023-07-10 thomas p_sent = (bytes_sent * 100) / packfile_size;
2549 8505ab66 2023-07-10 thomas if (p_sent != a->last_p_sent) {
2550 8505ab66 2023-07-10 thomas a->last_p_sent = p_sent;
2551 8505ab66 2023-07-10 thomas print_searching = 1;
2552 8505ab66 2023-07-10 thomas print_total = 1;
2553 8505ab66 2023-07-10 thomas print_deltify = 1;
2554 8505ab66 2023-07-10 thomas print_written = 1;
2555 8505ab66 2023-07-10 thomas print_sent = 1;
2557 8505ab66 2023-07-10 thomas a->sent_something = 1;
2560 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify || print_written ||
2561 8505ab66 2023-07-10 thomas print_sent)
2562 8505ab66 2023-07-10 thomas printf("\r");
2563 8505ab66 2023-07-10 thomas if (print_searching)
2564 8505ab66 2023-07-10 thomas printf("packing %d reference%s", ncommits,
2565 8505ab66 2023-07-10 thomas ncommits == 1 ? "" : "s");
2566 8505ab66 2023-07-10 thomas if (print_total)
2567 8505ab66 2023-07-10 thomas printf("; %d object%s", nobj_total,
2568 8505ab66 2023-07-10 thomas nobj_total == 1 ? "" : "s");
2569 8505ab66 2023-07-10 thomas if (print_deltify)
2570 8505ab66 2023-07-10 thomas printf("; deltify: %d%%", p_deltify);
2571 8505ab66 2023-07-10 thomas if (print_sent)
2572 8505ab66 2023-07-10 thomas printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2573 8505ab66 2023-07-10 thomas scaled_packsize, p_sent);
2574 8505ab66 2023-07-10 thomas else if (print_written)
2575 8505ab66 2023-07-10 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2576 8505ab66 2023-07-10 thomas scaled_packsize, p_written);
2577 8505ab66 2023-07-10 thomas if (print_searching || print_total || print_deltify ||
2578 8505ab66 2023-07-10 thomas print_written || print_sent) {
2579 8505ab66 2023-07-10 thomas a->printed_something = 1;
2580 8505ab66 2023-07-10 thomas fflush(stdout);
2582 8505ab66 2023-07-10 thomas return NULL;
2585 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg {
2586 8505ab66 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
2587 8505ab66 2023-07-10 thomas int last_p_indexed;
2588 8505ab66 2023-07-10 thomas int last_p_resolved;
2589 8505ab66 2023-07-10 thomas int verbosity;
2591 8505ab66 2023-07-10 thomas struct got_repository *repo;
2594 8505ab66 2023-07-10 thomas static const struct got_error *
2595 8505ab66 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
2596 8505ab66 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2598 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
2599 8505ab66 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
2600 8505ab66 2023-07-10 thomas int p_indexed, p_resolved;
2601 8505ab66 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
2603 8505ab66 2023-07-10 thomas if (a->verbosity < 0)
2604 8505ab66 2023-07-10 thomas return NULL;
2606 8505ab66 2023-07-10 thomas if (message && message[0] != '\0') {
2607 8505ab66 2023-07-10 thomas printf("\rserver: %s", message);
2608 8505ab66 2023-07-10 thomas fflush(stdout);
2609 8505ab66 2023-07-10 thomas return NULL;
2612 8505ab66 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
2613 8505ab66 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2614 8505ab66 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
2615 8505ab66 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
2616 8505ab66 2023-07-10 thomas print_size = 1;
2617 8505ab66 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
2618 8505ab66 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2619 8505ab66 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
2621 8505ab66 2023-07-10 thomas if (nobj_indexed > 0) {
2622 8505ab66 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
2623 8505ab66 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
2624 8505ab66 2023-07-10 thomas a->last_p_indexed = p_indexed;
2625 8505ab66 2023-07-10 thomas print_indexed = 1;
2626 8505ab66 2023-07-10 thomas print_size = 1;
2629 8505ab66 2023-07-10 thomas if (nobj_resolved > 0) {
2630 8505ab66 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
2631 8505ab66 2023-07-10 thomas (nobj_total - nobj_loose);
2632 8505ab66 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
2633 8505ab66 2023-07-10 thomas a->last_p_resolved = p_resolved;
2634 8505ab66 2023-07-10 thomas print_resolved = 1;
2635 8505ab66 2023-07-10 thomas print_indexed = 1;
2636 8505ab66 2023-07-10 thomas print_size = 1;
2641 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
2642 8505ab66 2023-07-10 thomas printf("\r");
2643 8505ab66 2023-07-10 thomas if (print_size)
2644 8505ab66 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2645 8505ab66 2023-07-10 thomas if (print_indexed)
2646 8505ab66 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
2647 8505ab66 2023-07-10 thomas if (print_resolved)
2648 8505ab66 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
2649 8505ab66 2023-07-10 thomas if (print_size || print_indexed || print_resolved) {
2650 8505ab66 2023-07-10 thomas putchar('\n');
2651 8505ab66 2023-07-10 thomas fflush(stdout);
2654 8505ab66 2023-07-10 thomas return NULL;
2657 8505ab66 2023-07-10 thomas static const struct got_error *
2658 8505ab66 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
2659 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2661 8505ab66 2023-07-10 thomas const struct got_error *err;
2662 8505ab66 2023-07-10 thomas struct got_reference *head_symref;
2664 8505ab66 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2665 8505ab66 2023-07-10 thomas if (err)
2666 8505ab66 2023-07-10 thomas return err;
2668 8505ab66 2023-07-10 thomas err = got_ref_write(head_symref, repo);
2669 8505ab66 2023-07-10 thomas if (err == NULL && verbosity > 0) {
2670 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
2671 8505ab66 2023-07-10 thomas got_ref_get_name(target_ref));
2673 8505ab66 2023-07-10 thomas got_ref_close(head_symref);
2674 8505ab66 2023-07-10 thomas return err;
2677 8505ab66 2023-07-10 thomas static const struct got_error *
2678 8505ab66 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
2679 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2681 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2682 8505ab66 2023-07-10 thomas struct got_reference *ref;
2683 8505ab66 2023-07-10 thomas char *id_str;
2685 8505ab66 2023-07-10 thomas err = got_object_id_str(&id_str, id);
2686 8505ab66 2023-07-10 thomas if (err)
2687 8505ab66 2023-07-10 thomas return err;
2689 8505ab66 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
2690 8505ab66 2023-07-10 thomas if (err)
2691 8505ab66 2023-07-10 thomas goto done;
2693 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2694 8505ab66 2023-07-10 thomas got_ref_close(ref);
2696 8505ab66 2023-07-10 thomas if (err == NULL && verbosity >= 0)
2697 8505ab66 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
2699 8505ab66 2023-07-10 thomas free(id_str);
2700 8505ab66 2023-07-10 thomas return err;
2703 8505ab66 2023-07-10 thomas static const struct got_error *
2704 8505ab66 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
2705 8505ab66 2023-07-10 thomas int verbosity, struct got_repository *repo)
2707 8505ab66 2023-07-10 thomas const struct got_error *err = NULL;
2708 8505ab66 2023-07-10 thomas char *new_id_str = NULL;
2709 8505ab66 2023-07-10 thomas struct got_object_id *old_id = NULL;
2711 8505ab66 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
2712 8505ab66 2023-07-10 thomas if (err)
2713 8505ab66 2023-07-10 thomas goto done;
2715 8505ab66 2023-07-10 thomas if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2716 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2717 8505ab66 2023-07-10 thomas if (err)
2718 8505ab66 2023-07-10 thomas goto done;
2719 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2720 8505ab66 2023-07-10 thomas goto done;
2721 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2722 8505ab66 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
2723 8505ab66 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
2725 8505ab66 2023-07-10 thomas goto done;
2728 8505ab66 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
2729 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
2730 8505ab66 2023-07-10 thomas printf("Replacing reference %s: %s\n",
2731 8505ab66 2023-07-10 thomas got_ref_get_name(ref),
2732 8505ab66 2023-07-10 thomas got_ref_get_symref_target(ref));
2734 8505ab66 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
2735 8505ab66 2023-07-10 thomas if (err)
2736 8505ab66 2023-07-10 thomas goto done;
2737 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2738 8505ab66 2023-07-10 thomas if (err)
2739 8505ab66 2023-07-10 thomas goto done;
2740 8505ab66 2023-07-10 thomas } else {
2741 8505ab66 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
2742 8505ab66 2023-07-10 thomas if (err)
2743 8505ab66 2023-07-10 thomas goto done;
2744 8505ab66 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
2745 8505ab66 2023-07-10 thomas goto done;
2747 8505ab66 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
2748 8505ab66 2023-07-10 thomas if (err)
2749 8505ab66 2023-07-10 thomas goto done;
2750 8505ab66 2023-07-10 thomas err = got_ref_write(ref, repo);
2751 8505ab66 2023-07-10 thomas if (err)
2752 8505ab66 2023-07-10 thomas goto done;
2755 8505ab66 2023-07-10 thomas if (verbosity >= 0)
2756 8505ab66 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
2757 8505ab66 2023-07-10 thomas new_id_str);
2759 8505ab66 2023-07-10 thomas free(old_id);
2760 8505ab66 2023-07-10 thomas free(new_id_str);
2761 8505ab66 2023-07-10 thomas return err;
2764 8505ab66 2023-07-10 thomas static const struct got_error *
2765 8505ab66 2023-07-10 thomas fetch_updated_remote(const char *proto, const char *host, const char *port,
2766 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2767 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote, struct got_repository *repo,
2768 8505ab66 2023-07-10 thomas struct got_reference *head_ref, const char *head_refname)
2770 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlock_err = NULL;
2771 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2772 8505ab66 2023-07-10 thomas struct got_pathlist_head learned_refs;
2773 8505ab66 2023-07-10 thomas struct got_pathlist_head symrefs;
2774 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_branches;
2775 8505ab66 2023-07-10 thomas struct got_pathlist_head wanted_refs;
2776 8505ab66 2023-07-10 thomas struct got_object_id *pack_hash;
2777 8505ab66 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2778 8505ab66 2023-07-10 thomas int fetchfd = -1;
2779 8505ab66 2023-07-10 thomas pid_t fetchpid = -1;
2781 8505ab66 2023-07-10 thomas TAILQ_INIT(&learned_refs);
2782 8505ab66 2023-07-10 thomas TAILQ_INIT(&symrefs);
2783 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2784 8505ab66 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2786 8505ab66 2023-07-10 thomas err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2788 8505ab66 2023-07-10 thomas if (err)
2789 8505ab66 2023-07-10 thomas goto done;
2791 8505ab66 2023-07-10 thomas err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2792 8505ab66 2023-07-10 thomas port, server_path, verbosity);
2793 8505ab66 2023-07-10 thomas if (err)
2794 8505ab66 2023-07-10 thomas goto done;
2796 8505ab66 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2797 8505ab66 2023-07-10 thomas fpa.last_p_indexed = -1;
2798 8505ab66 2023-07-10 thomas fpa.last_p_resolved = -1;
2799 8505ab66 2023-07-10 thomas fpa.verbosity = verbosity;
2800 8505ab66 2023-07-10 thomas fpa.repo = repo;
2802 8505ab66 2023-07-10 thomas err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2803 8505ab66 2023-07-10 thomas remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2804 8505ab66 2023-07-10 thomas fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2805 8505ab66 2023-07-10 thomas if (err)
2806 8505ab66 2023-07-10 thomas goto done;
2808 8505ab66 2023-07-10 thomas /* Update references provided with the pack file. */
2809 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &learned_refs, entry) {
2810 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2811 8505ab66 2023-07-10 thomas struct got_object_id *id = pe->data;
2812 8505ab66 2023-07-10 thomas struct got_reference *ref;
2814 8505ab66 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
2815 8505ab66 2023-07-10 thomas if (err) {
2816 8505ab66 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
2817 8505ab66 2023-07-10 thomas goto done;
2818 8505ab66 2023-07-10 thomas err = create_ref(refname, id, verbosity, repo);
2819 8505ab66 2023-07-10 thomas if (err)
2820 8505ab66 2023-07-10 thomas goto done;
2821 8505ab66 2023-07-10 thomas } else {
2822 8505ab66 2023-07-10 thomas err = update_ref(ref, id, verbosity, repo);
2823 8505ab66 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2824 8505ab66 2023-07-10 thomas if (unlock_err && err == NULL)
2825 8505ab66 2023-07-10 thomas err = unlock_err;
2826 8505ab66 2023-07-10 thomas got_ref_close(ref);
2827 8505ab66 2023-07-10 thomas if (err)
2828 8505ab66 2023-07-10 thomas goto done;
2832 8505ab66 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
2833 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
2834 8505ab66 2023-07-10 thomas struct got_reference *target_ref;
2835 8505ab66 2023-07-10 thomas const char *refname = pe->path;
2836 8505ab66 2023-07-10 thomas const char *target = pe->data;
2837 8505ab66 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
2839 8505ab66 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
2840 8505ab66 2023-07-10 thomas continue;
2842 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, target, 0);
2843 8505ab66 2023-07-10 thomas if (err) {
2844 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2845 8505ab66 2023-07-10 thomas err = NULL;
2846 8505ab66 2023-07-10 thomas continue;
2848 8505ab66 2023-07-10 thomas goto done;
2851 8505ab66 2023-07-10 thomas err = create_symref(refname, target_ref, verbosity, repo);
2852 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2853 8505ab66 2023-07-10 thomas if (err)
2854 8505ab66 2023-07-10 thomas goto done;
2856 8505ab66 2023-07-10 thomas if (remote->mirror_references)
2857 8505ab66 2023-07-10 thomas continue;
2859 8505ab66 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
2860 8505ab66 2023-07-10 thomas continue;
2862 8505ab66 2023-07-10 thomas if (asprintf(&remote_refname,
2863 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2864 8505ab66 2023-07-10 thomas refname) == -1) {
2865 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2866 8505ab66 2023-07-10 thomas goto done;
2868 8505ab66 2023-07-10 thomas if (asprintf(&remote_target,
2869 8505ab66 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2870 8505ab66 2023-07-10 thomas target + 11) == -1) {
2871 8505ab66 2023-07-10 thomas err = got_error_from_errno("asprintf");
2872 8505ab66 2023-07-10 thomas free(remote_refname);
2873 8505ab66 2023-07-10 thomas goto done;
2875 8505ab66 2023-07-10 thomas err = got_ref_open(&target_ref, repo, remote_target, 0);
2876 8505ab66 2023-07-10 thomas if (err) {
2877 8505ab66 2023-07-10 thomas free(remote_refname);
2878 8505ab66 2023-07-10 thomas free(remote_target);
2879 8505ab66 2023-07-10 thomas if (err->code == GOT_ERR_NOT_REF) {
2880 8505ab66 2023-07-10 thomas err = NULL;
2881 8505ab66 2023-07-10 thomas continue;
2883 8505ab66 2023-07-10 thomas goto done;
2885 8505ab66 2023-07-10 thomas err = create_symref(remote_refname, target_ref,
2886 8505ab66 2023-07-10 thomas verbosity - 1, repo);
2887 8505ab66 2023-07-10 thomas free(remote_refname);
2888 8505ab66 2023-07-10 thomas free(remote_target);
2889 8505ab66 2023-07-10 thomas got_ref_close(target_ref);
2890 8505ab66 2023-07-10 thomas if (err)
2891 8505ab66 2023-07-10 thomas goto done;
2895 8505ab66 2023-07-10 thomas got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2896 8505ab66 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2897 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2898 8505ab66 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2899 8505ab66 2023-07-10 thomas return err;
2903 8505ab66 2023-07-10 thomas const struct got_error *
2904 8505ab66 2023-07-10 thomas got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2905 8505ab66 2023-07-10 thomas struct got_worktree *worktree, struct got_pathlist_head *paths,
2906 8505ab66 2023-07-10 thomas const char *author, const char *committer, int allow_bad_symlinks,
2907 8505ab66 2023-07-10 thomas int show_diff, int commit_conflicts,
2908 8505ab66 2023-07-10 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2909 8505ab66 2023-07-10 thomas got_worktree_status_cb status_cb, void *status_arg,
2910 8505ab66 2023-07-10 thomas const char *proto, const char *host, const char *port,
2911 8505ab66 2023-07-10 thomas const char *server_path, int verbosity,
2912 8505ab66 2023-07-10 thomas const struct got_remote_repo *remote,
2913 8505ab66 2023-07-10 thomas got_cancel_cb check_cancelled,
2914 8505ab66 2023-07-10 thomas struct got_repository *repo)
2916 8505ab66 2023-07-10 thomas const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2917 8505ab66 2023-07-10 thomas struct got_fileindex *fileindex = NULL;
2918 8505ab66 2023-07-10 thomas char *fileindex_path = NULL;
2919 8505ab66 2023-07-10 thomas struct got_pathlist_head commitable_paths;
2920 8505ab66 2023-07-10 thomas struct collect_commitables_arg cc_arg;
2921 8505ab66 2023-07-10 thomas struct got_pathlist_entry *pe;
2922 8505ab66 2023-07-10 thomas struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2923 8505ab66 2023-07-10 thomas struct got_reference *commit_ref = NULL;
2924 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2925 8505ab66 2023-07-10 thomas struct got_object_id *head_commit_id2 = NULL;
2926 8505ab66 2023-07-10 thomas char *head_refname = NULL;
2927 8505ab66 2023-07-10 thomas char *commit_refname = NULL;
2928 8505ab66 2023-07-10 thomas char *diff_path = NULL;
2929 8505ab66 2023-07-10 thomas int have_staged_files = 0;
2930 8505ab66 2023-07-10 thomas int sendfd = -1;
2931 8505ab66 2023-07-10 thomas pid_t sendpid = -1;
2932 8505ab66 2023-07-10 thomas struct got_send_progress_arg spa;
2933 8505ab66 2023-07-10 thomas struct got_pathlist_head commit_reflist;
2934 8505ab66 2023-07-10 thomas struct got_pathlist_head tag_names;
2935 8505ab66 2023-07-10 thomas struct got_pathlist_head delete_branches;
2937 8505ab66 2023-07-10 thomas *new_commit_id = NULL;
2939 8505ab66 2023-07-10 thomas memset(&cc_arg, 0, sizeof(cc_arg));
2940 8505ab66 2023-07-10 thomas TAILQ_INIT(&commitable_paths);
2941 8505ab66 2023-07-10 thomas TAILQ_INIT(&commit_reflist);
2942 8505ab66 2023-07-10 thomas TAILQ_INIT(&tag_names);
2943 8505ab66 2023-07-10 thomas TAILQ_INIT(&delete_branches);
2945 8505ab66 2023-07-10 thomas err = lock_worktree(worktree, LOCK_EX);
2946 8505ab66 2023-07-10 thomas if (err)
2947 8505ab66 2023-07-10 thomas goto done;
2949 8505ab66 2023-07-10 thomas err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2950 8505ab66 2023-07-10 thomas worktree);
2951 8505ab66 2023-07-10 thomas if (err)
2952 8505ab66 2023-07-10 thomas goto done;
2954 8505ab66 2023-07-10 thomas head_refname = worktree->head_ref_name;
2955 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref, repo, head_refname, 0);
2956 8505ab66 2023-07-10 thomas if (err)
2957 8505ab66 2023-07-10 thomas goto done;
2958 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2959 8505ab66 2023-07-10 thomas if (err)
2960 8505ab66 2023-07-10 thomas goto done;
2962 8505ab66 2023-07-10 thomas err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2963 8505ab66 2023-07-10 thomas if (err)
2964 8505ab66 2023-07-10 thomas goto done;
2965 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
2966 8505ab66 2023-07-10 thomas if (err)
2967 8505ab66 2023-07-10 thomas goto done;
2969 8505ab66 2023-07-10 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
2970 8505ab66 2023-07-10 thomas if (err)
2971 8505ab66 2023-07-10 thomas goto done;
2973 8505ab66 2023-07-10 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2974 8505ab66 2023-07-10 thomas &have_staged_files);
2975 8505ab66 2023-07-10 thomas if (err && err->code != GOT_ERR_CANCELLED)
2976 8505ab66 2023-07-10 thomas goto done;
2977 8505ab66 2023-07-10 thomas if (have_staged_files) {
2978 8505ab66 2023-07-10 thomas err = check_non_staged_files(fileindex, paths);
2979 8505ab66 2023-07-10 thomas if (err)
2980 8505ab66 2023-07-10 thomas goto done;
2983 8505ab66 2023-07-10 thomas cc_arg.commitable_paths = &commitable_paths;
2984 8505ab66 2023-07-10 thomas cc_arg.worktree = worktree;
2985 8505ab66 2023-07-10 thomas cc_arg.fileindex = fileindex;
2986 8505ab66 2023-07-10 thomas cc_arg.repo = repo;
2987 8505ab66 2023-07-10 thomas cc_arg.have_staged_files = have_staged_files;
2988 8505ab66 2023-07-10 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2989 8505ab66 2023-07-10 thomas cc_arg.diff_header_shown = 0;
2990 8505ab66 2023-07-10 thomas cc_arg.commit_conflicts = commit_conflicts;
2991 8505ab66 2023-07-10 thomas if (show_diff) {
2992 8505ab66 2023-07-10 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2993 8505ab66 2023-07-10 thomas GOT_TMPDIR_STR "/got", ".diff");
2994 8505ab66 2023-07-10 thomas if (err)
2995 8505ab66 2023-07-10 thomas goto done;
2996 8505ab66 2023-07-10 thomas cc_arg.f1 = got_opentemp();
2997 8505ab66 2023-07-10 thomas if (cc_arg.f1 == NULL) {
2998 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
2999 8505ab66 2023-07-10 thomas goto done;
3001 8505ab66 2023-07-10 thomas cc_arg.f2 = got_opentemp();
3002 8505ab66 2023-07-10 thomas if (cc_arg.f2 == NULL) {
3003 8505ab66 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3004 8505ab66 2023-07-10 thomas goto done;
3008 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
3009 8505ab66 2023-07-10 thomas err = worktree_status(worktree, pe->path, fileindex, repo,
3010 8505ab66 2023-07-10 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3011 8505ab66 2023-07-10 thomas if (err)
3012 8505ab66 2023-07-10 thomas goto done;
3015 8505ab66 2023-07-10 thomas if (show_diff) {
3016 8505ab66 2023-07-10 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
3017 8505ab66 2023-07-10 thomas err = got_error_from_errno("fflush");
3018 8505ab66 2023-07-10 thomas goto done;
3022 8505ab66 2023-07-10 thomas if (TAILQ_EMPTY(&commitable_paths)) {
3023 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3024 8505ab66 2023-07-10 thomas goto done;
3027 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
3028 8505ab66 2023-07-10 thomas err = check_path_is_commitable(pe->path, &commitable_paths);
3029 8505ab66 2023-07-10 thomas if (err)
3030 8505ab66 2023-07-10 thomas goto done;
3033 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3034 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3035 8505ab66 2023-07-10 thomas const char *ct_path = ct->in_repo_path;
3037 8505ab66 2023-07-10 thomas while (ct_path[0] == '/')
3038 8505ab66 2023-07-10 thomas ct_path++;
3039 8505ab66 2023-07-10 thomas err = check_out_of_date(ct_path, ct->status,
3040 8505ab66 2023-07-10 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3041 8505ab66 2023-07-10 thomas head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3042 8505ab66 2023-07-10 thomas if (err)
3043 8505ab66 2023-07-10 thomas goto done;
3046 8505ab66 2023-07-10 thomas err = commit_worktree(new_commit_id, &commitable_paths,
3047 8505ab66 2023-07-10 thomas head_commit_id, NULL, worktree, author, committer,
3048 8505ab66 2023-07-10 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3049 8505ab66 2023-07-10 thomas commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3050 8505ab66 2023-07-10 thomas if (err)
3051 8505ab66 2023-07-10 thomas goto done;
3054 8505ab66 2023-07-10 thomas * Check if a concurrent commit to our branch has occurred.
3055 8505ab66 2023-07-10 thomas * Lock the reference here to prevent concurrent modification.
3057 8505ab66 2023-07-10 thomas err = got_ref_open(&head_ref2, repo, head_refname, 1);
3058 8505ab66 2023-07-10 thomas if (err)
3059 8505ab66 2023-07-10 thomas goto done;
3060 8505ab66 2023-07-10 thomas err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3061 8505ab66 2023-07-10 thomas if (err)
3062 8505ab66 2023-07-10 thomas goto done;
3063 8505ab66 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3064 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3065 8505ab66 2023-07-10 thomas goto done;
3068 8505ab66 2023-07-10 thomas err = got_pathlist_append(&commit_reflist, commit_refname,
3069 8505ab66 2023-07-10 thomas head_refname);
3070 8505ab66 2023-07-10 thomas if (err)
3071 8505ab66 2023-07-10 thomas goto done;
3073 8505ab66 2023-07-10 thomas /* Update commit ref in repository. */
3074 8505ab66 2023-07-10 thomas err = got_ref_change_ref(commit_ref, *new_commit_id);
3075 8505ab66 2023-07-10 thomas if (err)
3076 8505ab66 2023-07-10 thomas goto done;
3077 8505ab66 2023-07-10 thomas err = got_ref_write(commit_ref, repo);
3078 8505ab66 2023-07-10 thomas if (err)
3079 8505ab66 2023-07-10 thomas goto done;
3081 8505ab66 2023-07-10 thomas if (verbosity >= 0) {
3082 8505ab66 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3083 8505ab66 2023-07-10 thomas remote->name, proto, host,
3084 8505ab66 2023-07-10 thomas port ? ":" : "", port ? port : "",
3085 8505ab66 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
3088 8505ab66 2023-07-10 thomas /* Attempt send to remote branch. */
3089 8505ab66 2023-07-10 thomas err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3090 8505ab66 2023-07-10 thomas server_path, verbosity);
3091 8505ab66 2023-07-10 thomas if (err)
3092 8505ab66 2023-07-10 thomas goto done;
3094 8505ab66 2023-07-10 thomas memset(&spa, 0, sizeof(spa));
3095 8505ab66 2023-07-10 thomas spa.last_scaled_packsize[0] = '\0';
3096 8505ab66 2023-07-10 thomas spa.last_p_deltify = -1;
3097 8505ab66 2023-07-10 thomas spa.last_p_written = -1;
3098 8505ab66 2023-07-10 thomas spa.verbosity = verbosity;
3099 8505ab66 2023-07-10 thomas spa.delete_branches = &delete_branches;
3100 8505ab66 2023-07-10 thomas err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3101 8505ab66 2023-07-10 thomas &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3102 8505ab66 2023-07-10 thomas check_cancelled, NULL);
3103 8505ab66 2023-07-10 thomas if (spa.printed_something)
3104 8505ab66 2023-07-10 thomas putchar('\n');
3105 8505ab66 2023-07-10 thomas if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3107 8505ab66 2023-07-10 thomas * Fetch new changes since remote has diverged.
3108 8505ab66 2023-07-10 thomas * No trivial-rebase yet; require update to be run manually.
3110 8505ab66 2023-07-10 thomas err = fetch_updated_remote(proto, host, port, server_path,
3111 8505ab66 2023-07-10 thomas verbosity, remote, repo, head_ref, head_refname);
3112 8505ab66 2023-07-10 thomas if (err == NULL)
3113 8505ab66 2023-07-10 thomas goto done;
3114 8505ab66 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3115 8505ab66 2023-07-10 thomas goto done;
3116 8505ab66 2023-07-10 thomas /* XXX: Rebase commit over fetched remote branch. */
3118 8505ab66 2023-07-10 thomas if (err) {
3119 8505ab66 2023-07-10 thomas goto done;
3122 8505ab66 2023-07-10 thomas /* Update branch head in repository. */
3123 8505ab66 2023-07-10 thomas err = got_ref_change_ref(head_ref2, *new_commit_id);
3124 8505ab66 2023-07-10 thomas if (err)
3125 8505ab66 2023-07-10 thomas goto done;
3126 8505ab66 2023-07-10 thomas err = got_ref_write(head_ref2, repo);
3127 8505ab66 2023-07-10 thomas if (err)
3128 8505ab66 2023-07-10 thomas goto done;
3130 8505ab66 2023-07-10 thomas err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3131 8505ab66 2023-07-10 thomas if (err)
3132 8505ab66 2023-07-10 thomas goto done;
3134 8505ab66 2023-07-10 thomas err = ref_base_commit(worktree, repo);
3135 8505ab66 2023-07-10 thomas if (err)
3136 8505ab66 2023-07-10 thomas goto done;
3138 8505ab66 2023-07-10 thomas /* XXX: fileindex must be updated for other fetched changes? */
3139 8505ab66 2023-07-10 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
3140 8505ab66 2023-07-10 thomas *new_commit_id, fileindex, have_staged_files);
3141 8505ab66 2023-07-10 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
3142 8505ab66 2023-07-10 thomas if (sync_err && err == NULL)
3143 8505ab66 2023-07-10 thomas err = sync_err;
3145 8505ab66 2023-07-10 thomas if (head_ref2) {
3146 8505ab66 2023-07-10 thomas unlockerr = got_ref_unlock(head_ref2);
3147 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3148 8505ab66 2023-07-10 thomas err = unlockerr;
3149 8505ab66 2023-07-10 thomas got_ref_close(head_ref2);
3151 8505ab66 2023-07-10 thomas if (commit_ref)
3152 8505ab66 2023-07-10 thomas got_ref_close(commit_ref);
3153 8505ab66 2023-07-10 thomas if (fileindex)
3154 8505ab66 2023-07-10 thomas got_fileindex_free(fileindex);
3155 8505ab66 2023-07-10 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
3156 8505ab66 2023-07-10 thomas if (unlockerr && err == NULL)
3157 8505ab66 2023-07-10 thomas err = unlockerr;
3158 8505ab66 2023-07-10 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
3159 8505ab66 2023-07-10 thomas struct got_commitable *ct = pe->data;
3161 8505ab66 2023-07-10 thomas free_commitable(ct);
3163 8505ab66 2023-07-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3164 8505ab66 2023-07-10 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
3165 8505ab66 2023-07-10 thomas err = got_error_from_errno2("unlink", diff_path);
3166 8505ab66 2023-07-10 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3167 8505ab66 2023-07-10 thomas err == NULL)
3168 8505ab66 2023-07-10 thomas err = got_error_from_errno("fclose");
3169 8505ab66 2023-07-10 thomas free(head_commit_id);
3170 8505ab66 2023-07-10 thomas free(head_commit_id2);
3171 8505ab66 2023-07-10 thomas free(commit_refname);
3172 8505ab66 2023-07-10 thomas free(fileindex_path);
3173 8505ab66 2023-07-10 thomas free(diff_path);
3174 8505ab66 2023-07-10 thomas return err;
3177 8505ab66 2023-07-10 thomas const struct got_error *
3178 8505ab66 2023-07-10 thomas got_worktree_cvg_get_commit_ref_name(char **refname,
3179 8505ab66 2023-07-10 thomas struct got_worktree *worktree)
3181 8505ab66 2023-07-10 thomas return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);