2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_worktree_cvg.h"
47 #include "got_opentemp.h"
50 #include "got_fetch.h"
52 #include "got_lib_worktree.h"
53 #include "got_lib_hash.h"
54 #include "got_lib_fileindex.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_object_idset.h"
61 #include "got_lib_diff.h"
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define GOT_MERGE_LABEL_MERGED "merged change"
68 #define GOT_MERGE_LABEL_BASE "3-way merge base"
70 static const struct got_error *
71 lock_worktree(struct got_worktree *worktree, int operation)
73 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
74 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
75 : got_error_from_errno2("flock",
76 got_worktree_get_root_path(worktree)));
80 static const struct got_error *
81 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
82 size_t target_len, const char *ondisk_path, const char *wtroot_path)
84 const struct got_error *err = NULL;
85 char canonpath[PATH_MAX];
86 char *path_got = NULL;
90 if (target_len >= sizeof(canonpath)) {
96 * We do not use realpath(3) to resolve the symlink's target
97 * path because we don't want to resolve symlinks recursively.
98 * Instead we make the path absolute and then canonicalize it.
99 * Relative symlink target lookup should begin at the directory
100 * in which the blob object is being installed.
102 if (!got_path_is_absolute(target_path)) {
103 char *abspath, *parent;
104 err = got_path_dirname(&parent, ondisk_path);
107 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
109 return got_error_from_errno("asprintf");
112 if (strlen(abspath) >= sizeof(canonpath)) {
113 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
117 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
122 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
127 /* Only allow symlinks pointing at paths within the work tree. */
128 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
133 /* Do not allow symlinks pointing into the .got directory. */
134 if (asprintf(&path_got, "%s/%s", wtroot_path,
135 GOT_WORKTREE_GOT_DIR) == -1)
136 return got_error_from_errno("asprintf");
137 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
145 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
146 * conflict marker is found in newly added lines only.
148 static const struct got_error *
149 get_modified_file_content_status(unsigned char *status,
150 struct got_blob_object *blob, const char *path, struct stat *sb,
153 const struct got_error *err, *free_err;
154 const char *markers[3] = {
155 GOT_DIFF_CONFLICT_MARKER_BEGIN,
156 GOT_DIFF_CONFLICT_MARKER_SEP,
157 GOT_DIFF_CONFLICT_MARKER_END
160 struct got_diffreg_result *diffreg_result = NULL;
161 struct diff_result *r;
162 int nchunks_parsed, n, i = 0, ln = 0;
167 if (*status != GOT_STATUS_MODIFY)
172 return got_error_from_errno("got_opentemp");
175 got_object_blob_rewind(blob);
176 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
181 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
182 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
186 r = diffreg_result->result;
188 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
189 struct diff_chunk *c;
190 struct diff_chunk_context cc = {};
194 * We can optimise a little by advancing straight
195 * to the next chunk if this one has no added lines.
197 c = diff_chunk_get(r, n);
199 if (diff_chunk_type(c) != CHUNK_PLUS) {
201 continue; /* removed or unchanged lines */
204 pos = diff_chunk_get_right_start_pos(c);
205 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
206 err = got_ferror(ondisk_file, GOT_ERR_IO);
210 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
213 while (ln < cc.right.end) {
214 linelen = getline(&line, &linesize, ondisk_file);
216 if (feof(ondisk_file))
218 err = got_ferror(ondisk_file, GOT_ERR_IO);
222 if (line && strncmp(line, markers[i],
223 strlen(markers[i])) == 0) {
224 if (strcmp(markers[i],
225 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
226 *status = GOT_STATUS_CONFLICT;
237 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
238 err = got_error_from_errno("fclose");
239 free_err = got_diffreg_result_free(diffreg_result);
247 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
249 mode_t ie_mode = got_fileindex_perms_to_st(ie);
250 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
254 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
256 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
257 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
258 ie->mtime_sec == sb->st_mtim.tv_sec &&
259 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
260 ie->size == (sb->st_size & 0xffffffff) &&
261 !xbit_differs(ie, sb->st_mode));
265 get_staged_status(struct got_fileindex_entry *ie)
267 switch (got_fileindex_entry_stage_get(ie)) {
268 case GOT_FILEIDX_STAGE_ADD:
269 return GOT_STATUS_ADD;
270 case GOT_FILEIDX_STAGE_DELETE:
271 return GOT_STATUS_DELETE;
272 case GOT_FILEIDX_STAGE_MODIFY:
273 return GOT_STATUS_MODIFY;
275 return GOT_STATUS_NO_CHANGE;
279 static const struct got_error *
280 get_symlink_modification_status(unsigned char *status,
281 struct got_fileindex_entry *ie, const char *abspath,
282 int dirfd, const char *de_name, struct got_blob_object *blob)
284 const struct got_error *err = NULL;
285 char target_path[PATH_MAX];
286 char etarget[PATH_MAX];
288 size_t len, target_len = 0;
289 const uint8_t *buf = got_object_blob_get_read_buf(blob);
290 size_t hdrlen = got_object_blob_get_hdrlen(blob);
292 *status = GOT_STATUS_NO_CHANGE;
294 /* Blob object content specifies the target path of the link. */
296 err = got_object_blob_read_block(&len, blob);
299 if (len + target_len >= sizeof(target_path)) {
301 * Should not happen. The blob contents were OK
302 * when this symlink was installed.
304 return got_error(GOT_ERR_NO_SPACE);
307 /* Skip blob object header first time around. */
308 memcpy(target_path + target_len, buf + hdrlen,
310 target_len += len - hdrlen;
314 target_path[target_len] = '\0';
317 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
319 return got_error_from_errno2("readlinkat", abspath);
321 elen = readlink(abspath, etarget, sizeof(etarget));
323 return got_error_from_errno2("readlink", abspath);
326 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
327 *status = GOT_STATUS_MODIFY;
332 static const struct got_error *
333 get_file_status(unsigned char *status, struct stat *sb,
334 struct got_fileindex_entry *ie, const char *abspath,
335 int dirfd, const char *de_name, struct got_repository *repo)
337 const struct got_error *err = NULL;
338 struct got_object_id id;
340 int fd = -1, fd1 = -1;
343 struct got_blob_object *blob = NULL;
345 unsigned char staged_status;
347 staged_status = get_staged_status(ie);
348 *status = GOT_STATUS_NO_CHANGE;
349 memset(sb, 0, sizeof(*sb));
352 * Whenever the caller provides a directory descriptor and a
353 * directory entry name for the file, use them! This prevents
354 * race conditions if filesystem paths change beneath our feet.
357 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
358 if (errno == ENOENT) {
359 if (got_fileindex_entry_has_file_on_disk(ie))
360 *status = GOT_STATUS_MISSING;
362 *status = GOT_STATUS_DELETE;
365 err = got_error_from_errno2("fstatat", abspath);
369 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
370 if (fd == -1 && errno != ENOENT &&
371 !got_err_open_nofollow_on_symlink())
372 return got_error_from_errno2("open", abspath);
373 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
374 if (lstat(abspath, sb) == -1)
375 return got_error_from_errno2("lstat", abspath);
376 } else if (fd == -1 || fstat(fd, sb) == -1) {
377 if (errno == ENOENT) {
378 if (got_fileindex_entry_has_file_on_disk(ie))
379 *status = GOT_STATUS_MISSING;
381 *status = GOT_STATUS_DELETE;
384 err = got_error_from_errno2("fstat", abspath);
389 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
390 *status = GOT_STATUS_OBSTRUCTED;
394 if (!got_fileindex_entry_has_file_on_disk(ie)) {
395 *status = GOT_STATUS_DELETE;
397 } else if (!got_fileindex_entry_has_blob(ie) &&
398 staged_status != GOT_STATUS_ADD) {
399 *status = GOT_STATUS_ADD;
403 if (!stat_info_differs(ie, sb))
406 if (S_ISLNK(sb->st_mode) &&
407 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
408 *status = GOT_STATUS_MODIFY;
412 if (staged_status == GOT_STATUS_MODIFY ||
413 staged_status == GOT_STATUS_ADD)
414 got_fileindex_entry_get_staged_blob_id(&id, ie);
416 got_fileindex_entry_get_blob_id(&id, ie);
418 fd1 = got_opentempfd();
420 err = got_error_from_errno("got_opentempfd");
423 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
427 if (S_ISLNK(sb->st_mode)) {
428 err = get_symlink_modification_status(status, ie,
429 abspath, dirfd, de_name, blob);
434 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
436 err = got_error_from_errno2("openat", abspath);
443 err = got_error_from_errno2("fdopen", abspath);
447 hdrlen = got_object_blob_get_hdrlen(blob);
449 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
450 err = got_object_blob_read_block(&blen, blob);
453 /* Skip length of blob object header first time around. */
454 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
455 if (flen == 0 && ferror(f)) {
456 err = got_error_from_errno("fread");
459 if (blen - hdrlen == 0) {
461 *status = GOT_STATUS_MODIFY;
463 } else if (flen == 0) {
464 if (blen - hdrlen != 0)
465 *status = GOT_STATUS_MODIFY;
467 } else if (blen - hdrlen == flen) {
468 /* Skip blob object header first time around. */
469 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
470 *status = GOT_STATUS_MODIFY;
474 *status = GOT_STATUS_MODIFY;
480 if (*status == GOT_STATUS_MODIFY) {
482 err = get_modified_file_content_status(status, blob, ie->path,
484 } else if (xbit_differs(ie, sb->st_mode))
485 *status = GOT_STATUS_MODE_CHANGE;
487 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
488 err = got_error_from_errno("close");
490 got_object_blob_close(blob);
491 if (f != NULL && fclose(f) == EOF && err == NULL)
492 err = got_error_from_errno2("fclose", abspath);
493 if (fd != -1 && close(fd) == -1 && err == NULL)
494 err = got_error_from_errno2("close", abspath);
498 static const struct got_error *
499 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
501 const struct got_error *err = NULL;
502 char *uuidstr = NULL;
506 err = got_worktree_get_uuid(&uuidstr, worktree);
510 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
511 err = got_error_from_errno("asprintf");
518 static const struct got_error *
519 get_base_ref_name(char **refname, struct got_worktree *worktree)
521 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
525 * Prevent Git's garbage collector from deleting our base commit by
526 * setting a reference to our base commit's ID.
528 static const struct got_error *
529 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
531 const struct got_error *err = NULL;
532 struct got_reference *ref = NULL;
535 err = get_base_ref_name(&refname, worktree);
539 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
543 err = got_ref_write(ref, repo);
551 static const struct got_error *
552 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
554 const struct got_error *err = NULL;
556 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
557 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
558 err = got_error_from_errno("asprintf");
559 *fileindex_path = NULL;
564 static const struct got_error *
565 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
566 struct got_worktree *worktree)
568 const struct got_error *err = NULL;
571 *fileindex_path = NULL;
572 *fileindex = got_fileindex_alloc();
573 if (*fileindex == NULL)
574 return got_error_from_errno("got_fileindex_alloc");
576 err = get_fileindex_path(fileindex_path, worktree);
580 index = fopen(*fileindex_path, "rbe");
583 err = got_error_from_errno2("fopen", *fileindex_path);
585 err = got_fileindex_read(*fileindex, index);
586 if (fclose(index) == EOF && err == NULL)
587 err = got_error_from_errno("fclose");
591 free(*fileindex_path);
592 *fileindex_path = NULL;
593 got_fileindex_free(*fileindex);
599 static const struct got_error *
600 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
602 const struct got_error *err = NULL;
603 char *new_fileindex_path = NULL;
604 FILE *new_index = NULL;
605 struct timespec timeout;
607 err = got_opentemp_named(&new_fileindex_path, &new_index,
612 err = got_fileindex_write(fileindex, new_index);
616 if (rename(new_fileindex_path, fileindex_path) != 0) {
617 err = got_error_from_errno3("rename", new_fileindex_path,
619 unlink(new_fileindex_path);
623 * Sleep for a short amount of time to ensure that files modified after
624 * this program exits have a different time stamp from the one which
625 * was recorded in the file index.
629 nanosleep(&timeout, NULL);
633 free(new_fileindex_path);
637 struct diff_dir_cb_arg {
638 struct got_fileindex *fileindex;
639 struct got_worktree *worktree;
640 const char *status_path;
641 size_t status_path_len;
642 struct got_repository *repo;
643 got_worktree_status_cb status_cb;
645 got_cancel_cb cancel_cb;
647 /* A pathlist containing per-directory pathlists of ignore patterns. */
648 struct got_pathlist_head *ignores;
649 int report_unchanged;
653 static const struct got_error *
654 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
655 int dirfd, const char *de_name,
656 got_worktree_status_cb status_cb, void *status_arg,
657 struct got_repository *repo, int report_unchanged)
659 const struct got_error *err = NULL;
660 unsigned char status = GOT_STATUS_NO_CHANGE;
661 unsigned char staged_status;
663 struct got_object_id blob_id, commit_id, staged_blob_id;
664 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
665 struct got_object_id *staged_blob_idp = NULL;
667 staged_status = get_staged_status(ie);
668 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
672 if (status == GOT_STATUS_NO_CHANGE &&
673 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
676 if (got_fileindex_entry_has_blob(ie))
677 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
678 if (got_fileindex_entry_has_commit(ie))
679 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
680 if (staged_status == GOT_STATUS_ADD ||
681 staged_status == GOT_STATUS_MODIFY) {
682 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
683 &staged_blob_id, ie);
686 return (*status_cb)(status_arg, status, staged_status,
687 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
690 static const struct got_error *
691 status_old_new(void *arg, struct got_fileindex_entry *ie,
692 struct dirent *de, const char *parent_path, int dirfd)
694 const struct got_error *err = NULL;
695 struct diff_dir_cb_arg *a = arg;
698 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
699 return got_error(GOT_ERR_CANCELLED);
701 if (got_path_cmp(parent_path, a->status_path,
702 strlen(parent_path), a->status_path_len) != 0 &&
703 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
706 if (parent_path[0]) {
707 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
708 parent_path, de->d_name) == -1)
709 return got_error_from_errno("asprintf");
711 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
713 return got_error_from_errno("asprintf");
716 err = report_file_status(ie, abspath, dirfd, de->d_name,
717 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
722 static const struct got_error *
723 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
725 struct diff_dir_cb_arg *a = arg;
726 struct got_object_id blob_id, commit_id;
727 unsigned char status;
729 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
730 return got_error(GOT_ERR_CANCELLED);
732 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
735 got_fileindex_entry_get_blob_id(&blob_id, ie);
736 got_fileindex_entry_get_commit_id(&commit_id, ie);
737 if (got_fileindex_entry_has_file_on_disk(ie))
738 status = GOT_STATUS_MISSING;
740 status = GOT_STATUS_DELETE;
741 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
742 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
746 free_ignores(struct got_pathlist_head *ignores)
748 struct got_pathlist_entry *pe;
750 TAILQ_FOREACH(pe, ignores, entry) {
751 struct got_pathlist_head *ignorelist = pe->data;
753 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
755 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
758 static const struct got_error *
759 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
761 const struct got_error *err = NULL;
762 struct got_pathlist_entry *pe = NULL;
763 struct got_pathlist_head *ignorelist;
764 char *line = NULL, *pattern, *dirpath = NULL;
768 ignorelist = calloc(1, sizeof(*ignorelist));
769 if (ignorelist == NULL)
770 return got_error_from_errno("calloc");
771 TAILQ_INIT(ignorelist);
773 while ((linelen = getline(&line, &linesize, f)) != -1) {
774 if (linelen > 0 && line[linelen - 1] == '\n')
775 line[linelen - 1] = '\0';
777 /* Git's ignores may contain comments. */
781 /* Git's negated patterns are not (yet?) supported. */
785 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
787 err = got_error_from_errno("asprintf");
790 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
795 err = got_error_from_errno("getline");
799 dirpath = strdup(path);
800 if (dirpath == NULL) {
801 err = got_error_from_errno("strdup");
804 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
807 if (err || pe == NULL) {
809 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
815 match_path(const char *pattern, size_t pattern_len, const char *path,
821 * Trailing slashes signify directories.
822 * Append a * to make such patterns conform to fnmatch rules.
824 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
825 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
826 return FNM_NOMATCH; /* XXX */
828 return fnmatch(buf, path, flags);
831 return fnmatch(pattern, path, flags);
835 match_ignores(struct got_pathlist_head *ignores, const char *path)
837 struct got_pathlist_entry *pe;
839 /* Handle patterns which match in all directories. */
840 TAILQ_FOREACH(pe, ignores, entry) {
841 struct got_pathlist_head *ignorelist = pe->data;
842 struct got_pathlist_entry *pi;
844 TAILQ_FOREACH(pi, ignorelist, entry) {
847 if (pi->path_len < 3 ||
848 strncmp(pi->path, "**/", 3) != 0)
852 if (match_path(pi->path + 3,
854 FNM_PATHNAME | FNM_LEADING_DIR)) {
855 /* Retry in next directory. */
856 while (*p && *p != '/')
868 * The ignores pathlist contains ignore lists from children before
869 * parents, so we can find the most specific ignorelist by walking
872 pe = TAILQ_LAST(ignores, got_pathlist_head);
874 if (got_path_is_child(path, pe->path, pe->path_len)) {
875 struct got_pathlist_head *ignorelist = pe->data;
876 struct got_pathlist_entry *pi;
877 TAILQ_FOREACH(pi, ignorelist, entry) {
878 int flags = FNM_LEADING_DIR;
879 if (strstr(pi->path, "/**/") == NULL)
880 flags |= FNM_PATHNAME;
881 if (match_path(pi->path, pi->path_len,
887 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
893 static const struct got_error *
894 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
895 const char *path, int dirfd, const char *ignores_filename)
897 const struct got_error *err = NULL;
900 FILE *ignoresfile = NULL;
902 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
903 path[0] ? "/" : "", ignores_filename) == -1)
904 return got_error_from_errno("asprintf");
907 fd = openat(dirfd, ignores_filename,
908 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
910 if (errno != ENOENT && errno != EACCES)
911 err = got_error_from_errno2("openat",
914 ignoresfile = fdopen(fd, "r");
915 if (ignoresfile == NULL)
916 err = got_error_from_errno2("fdopen",
920 err = read_ignores(ignores, path, ignoresfile);
924 ignoresfile = fopen(ignorespath, "re");
925 if (ignoresfile == NULL) {
926 if (errno != ENOENT && errno != EACCES)
927 err = got_error_from_errno2("fopen",
930 err = read_ignores(ignores, path, ignoresfile);
933 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
934 err = got_error_from_errno2("fclose", path);
935 if (fd != -1 && close(fd) == -1 && err == NULL)
936 err = got_error_from_errno2("close", path);
941 static const struct got_error *
942 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
945 const struct got_error *err = NULL;
946 struct diff_dir_cb_arg *a = arg;
952 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
953 return got_error(GOT_ERR_CANCELLED);
955 if (parent_path[0]) {
956 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
957 return got_error_from_errno("asprintf");
962 if (de->d_type == DT_DIR) {
963 if (!a->no_ignores && ignore != NULL &&
964 match_ignores(a->ignores, path))
966 } else if (!match_ignores(a->ignores, path) &&
967 got_path_is_child(path, a->status_path, a->status_path_len))
968 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
969 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
975 static const struct got_error *
976 status_traverse(void *arg, const char *path, int dirfd)
978 const struct got_error *err = NULL;
979 struct diff_dir_cb_arg *a = arg;
984 err = add_ignores(a->ignores, a->worktree->root_path,
985 path, dirfd, ".cvsignore");
989 err = add_ignores(a->ignores, a->worktree->root_path, path,
990 dirfd, ".gitignore");
995 static const struct got_error *
996 report_single_file_status(const char *path, const char *ondisk_path,
997 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
998 void *status_arg, struct got_repository *repo, int report_unchanged,
999 struct got_pathlist_head *ignores, int no_ignores)
1001 struct got_fileindex_entry *ie;
1004 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1006 return report_file_status(ie, ondisk_path, -1, NULL,
1007 status_cb, status_arg, repo, report_unchanged);
1009 if (lstat(ondisk_path, &sb) == -1) {
1010 if (errno != ENOENT)
1011 return got_error_from_errno2("lstat", ondisk_path);
1012 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1013 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1016 if (!no_ignores && match_ignores(ignores, path))
1019 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1020 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1021 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1026 static const struct got_error *
1027 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1028 const char *root_path, const char *path)
1030 const struct got_error *err;
1031 char *parent_path, *next_parent_path = NULL;
1033 err = add_ignores(ignores, root_path, "", -1,
1038 err = add_ignores(ignores, root_path, "", -1,
1043 err = got_path_dirname(&parent_path, path);
1045 if (err->code == GOT_ERR_BAD_PATH)
1046 return NULL; /* cannot traverse parent */
1050 err = add_ignores(ignores, root_path, parent_path, -1,
1054 err = add_ignores(ignores, root_path, parent_path, -1,
1058 err = got_path_dirname(&next_parent_path, parent_path);
1060 if (err->code == GOT_ERR_BAD_PATH)
1061 err = NULL; /* traversed everything */
1064 if (got_path_is_root_dir(parent_path))
1067 parent_path = next_parent_path;
1068 next_parent_path = NULL;
1072 free(next_parent_path);
1076 struct find_missing_children_args {
1077 const char *parent_path;
1079 struct got_pathlist_head *children;
1080 got_cancel_cb cancel_cb;
1084 static const struct got_error *
1085 find_missing_children(void *arg, struct got_fileindex_entry *ie)
1087 const struct got_error *err = NULL;
1088 struct find_missing_children_args *a = arg;
1091 err = a->cancel_cb(a->cancel_arg);
1096 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1097 err = got_pathlist_append(a->children, ie->path, NULL);
1102 static const struct got_error *
1103 report_children(struct got_pathlist_head *children,
1104 struct got_worktree *worktree, struct got_fileindex *fileindex,
1105 struct got_repository *repo, int is_root_dir, int report_unchanged,
1106 struct got_pathlist_head *ignores, int no_ignores,
1107 got_worktree_status_cb status_cb, void *status_arg,
1108 got_cancel_cb cancel_cb, void *cancel_arg)
1110 const struct got_error *err = NULL;
1111 struct got_pathlist_entry *pe;
1112 char *ondisk_path = NULL;
1114 TAILQ_FOREACH(pe, children, entry) {
1116 err = cancel_cb(cancel_arg);
1121 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1122 !is_root_dir ? "/" : "", pe->path) == -1) {
1123 err = got_error_from_errno("asprintf");
1128 err = report_single_file_status(pe->path, ondisk_path,
1129 fileindex, status_cb, status_arg, repo, report_unchanged,
1130 ignores, no_ignores);
1142 static const struct got_error *
1143 worktree_status(struct got_worktree *worktree, const char *path,
1144 struct got_fileindex *fileindex, struct got_repository *repo,
1145 got_worktree_status_cb status_cb, void *status_arg,
1146 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1147 int report_unchanged)
1149 const struct got_error *err = NULL;
1151 struct got_fileindex_diff_dir_cb fdiff_cb;
1152 struct diff_dir_cb_arg arg;
1153 char *ondisk_path = NULL;
1154 struct got_pathlist_head ignores, missing_children;
1155 struct got_fileindex_entry *ie;
1157 TAILQ_INIT(&ignores);
1158 TAILQ_INIT(&missing_children);
1160 if (asprintf(&ondisk_path, "%s%s%s",
1161 worktree->root_path, path[0] ? "/" : "", path) == -1)
1162 return got_error_from_errno("asprintf");
1164 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1166 err = report_single_file_status(path, ondisk_path,
1167 fileindex, status_cb, status_arg, repo,
1168 report_unchanged, &ignores, no_ignores);
1171 struct find_missing_children_args fmca;
1172 fmca.parent_path = path;
1173 fmca.parent_len = strlen(path);
1174 fmca.children = &missing_children;
1175 fmca.cancel_cb = cancel_cb;
1176 fmca.cancel_arg = cancel_arg;
1177 err = got_fileindex_for_each_entry_safe(fileindex,
1178 find_missing_children, &fmca);
1183 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1185 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1186 !got_err_open_nofollow_on_symlink())
1187 err = got_error_from_errno2("open", ondisk_path);
1190 err = add_ignores_from_parent_paths(&ignores,
1191 worktree->root_path, ondisk_path);
1195 if (TAILQ_EMPTY(&missing_children)) {
1196 err = report_single_file_status(path,
1197 ondisk_path, fileindex,
1198 status_cb, status_arg, repo,
1199 report_unchanged, &ignores, no_ignores);
1203 err = report_children(&missing_children,
1204 worktree, fileindex, repo,
1205 (path[0] == '\0'), report_unchanged,
1206 &ignores, no_ignores,
1207 status_cb, status_arg,
1208 cancel_cb, cancel_arg);
1214 fdiff_cb.diff_old_new = status_old_new;
1215 fdiff_cb.diff_old = status_old;
1216 fdiff_cb.diff_new = status_new;
1217 fdiff_cb.diff_traverse = status_traverse;
1218 arg.fileindex = fileindex;
1219 arg.worktree = worktree;
1220 arg.status_path = path;
1221 arg.status_path_len = strlen(path);
1223 arg.status_cb = status_cb;
1224 arg.status_arg = status_arg;
1225 arg.cancel_cb = cancel_cb;
1226 arg.cancel_arg = cancel_arg;
1227 arg.report_unchanged = report_unchanged;
1228 arg.no_ignores = no_ignores;
1230 err = add_ignores_from_parent_paths(&ignores,
1231 worktree->root_path, path);
1235 arg.ignores = &ignores;
1236 err = got_fileindex_diff_dir(fileindex, fd,
1237 worktree->root_path, path, repo, &fdiff_cb, &arg);
1240 free_ignores(&ignores);
1241 if (fd != -1 && close(fd) == -1 && err == NULL)
1242 err = got_error_from_errno("close");
1248 free_commitable(struct got_commitable *ct)
1251 free(ct->in_repo_path);
1252 free(ct->ondisk_path);
1254 free(ct->base_blob_id);
1255 free(ct->staged_blob_id);
1256 free(ct->base_commit_id);
1260 struct collect_commitables_arg {
1261 struct got_pathlist_head *commitable_paths;
1262 struct got_repository *repo;
1263 struct got_worktree *worktree;
1264 struct got_fileindex *fileindex;
1265 int have_staged_files;
1266 int allow_bad_symlinks;
1267 int diff_header_shown;
1268 int commit_conflicts;
1275 * Create a file which contains the target path of a symlink so we can feed
1276 * it as content to the diff engine.
1278 static const struct got_error *
1279 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1280 const char *abspath)
1282 const struct got_error *err = NULL;
1283 char target_path[PATH_MAX];
1284 ssize_t target_len, outlen;
1289 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1290 if (target_len == -1)
1291 return got_error_from_errno2("readlinkat", abspath);
1293 target_len = readlink(abspath, target_path, PATH_MAX);
1294 if (target_len == -1)
1295 return got_error_from_errno2("readlink", abspath);
1298 *fd = got_opentempfd();
1300 return got_error_from_errno("got_opentempfd");
1302 outlen = write(*fd, target_path, target_len);
1304 err = got_error_from_errno("got_opentempfd");
1308 if (lseek(*fd, 0, SEEK_SET) == -1) {
1309 err = got_error_from_errno2("lseek", abspath);
1320 static const struct got_error *
1321 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1322 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1323 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1325 const struct got_error *err = NULL;
1326 struct got_blob_object *blob1 = NULL;
1327 int fd = -1, fd1 = -1, fd2 = -1;
1328 FILE *ondisk_file = NULL;
1329 char *label1 = NULL;
1333 char *id_str = NULL;
1335 memset(&sb, 0, sizeof(sb));
1338 if (ct->staged_status != GOT_STATUS_MODIFY &&
1339 ct->staged_status != GOT_STATUS_ADD &&
1340 ct->staged_status != GOT_STATUS_DELETE)
1343 if (ct->status != GOT_STATUS_MODIFY &&
1344 ct->status != GOT_STATUS_ADD &&
1345 ct->status != GOT_STATUS_DELETE &&
1346 ct->status != GOT_STATUS_CONFLICT)
1350 err = got_opentemp_truncate(f1);
1352 return got_error_from_errno("got_opentemp_truncate");
1353 err = got_opentemp_truncate(f2);
1355 return got_error_from_errno("got_opentemp_truncate");
1357 if (!*diff_header_shown) {
1358 err = got_object_id_str(&id_str, worktree->base_commit_id);
1361 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1362 got_worktree_get_root_path(worktree));
1363 fprintf(diff_outfile, "commit - %s\n", id_str);
1364 fprintf(diff_outfile, "path + %s%s\n",
1365 got_worktree_get_root_path(worktree),
1366 diff_staged ? " (staged changes)" : "");
1367 *diff_header_shown = 1;
1371 const char *label1 = NULL, *label2 = NULL;
1372 switch (ct->staged_status) {
1373 case GOT_STATUS_MODIFY:
1377 case GOT_STATUS_ADD:
1380 case GOT_STATUS_DELETE:
1384 return got_error(GOT_ERR_FILE_STATUS);
1386 fd1 = got_opentempfd();
1388 err = got_error_from_errno("got_opentempfd");
1391 fd2 = got_opentempfd();
1393 err = got_error_from_errno("got_opentempfd");
1396 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1397 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1398 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1399 NULL, repo, diff_outfile);
1403 fd1 = got_opentempfd();
1405 err = got_error_from_errno("got_opentempfd");
1409 if (ct->status != GOT_STATUS_ADD) {
1410 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1416 if (ct->status != GOT_STATUS_DELETE) {
1418 fd = openat(dirfd, de_name,
1419 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1421 if (!got_err_open_nofollow_on_symlink()) {
1422 err = got_error_from_errno2("openat",
1426 err = get_symlink_target_file(&fd, dirfd,
1427 de_name, ct->ondisk_path);
1432 fd = open(ct->ondisk_path,
1433 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1435 if (!got_err_open_nofollow_on_symlink()) {
1436 err = got_error_from_errno2("open",
1440 err = get_symlink_target_file(&fd, dirfd,
1441 de_name, ct->ondisk_path);
1446 if (fstatat(fd, ct->ondisk_path, &sb,
1447 AT_SYMLINK_NOFOLLOW) == -1) {
1448 err = got_error_from_errno2("fstatat", ct->ondisk_path);
1451 ondisk_file = fdopen(fd, "r");
1452 if (ondisk_file == NULL) {
1453 err = got_error_from_errno2("fdopen", ct->ondisk_path);
1461 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1467 err = got_diff_blob_file(blob1, f1, size1, label1,
1468 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1469 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1471 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1472 err = got_error_from_errno("close");
1473 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1474 err = got_error_from_errno("close");
1476 got_object_blob_close(blob1);
1477 if (fd != -1 && close(fd) == -1 && err == NULL)
1478 err = got_error_from_errno("close");
1479 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1480 err = got_error_from_errno("fclose");
1484 static const struct got_error *
1485 collect_commitables(void *arg, unsigned char status,
1486 unsigned char staged_status, const char *relpath,
1487 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1488 struct got_object_id *commit_id, int dirfd, const char *de_name)
1490 struct collect_commitables_arg *a = arg;
1491 const struct got_error *err = NULL;
1492 struct got_commitable *ct = NULL;
1493 struct got_pathlist_entry *new = NULL;
1494 char *parent_path = NULL, *path = NULL;
1497 if (a->have_staged_files) {
1498 if (staged_status != GOT_STATUS_MODIFY &&
1499 staged_status != GOT_STATUS_ADD &&
1500 staged_status != GOT_STATUS_DELETE)
1503 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1504 printf("C %s\n", relpath);
1505 return got_error(GOT_ERR_COMMIT_CONFLICT);
1508 if (status != GOT_STATUS_MODIFY &&
1509 status != GOT_STATUS_MODE_CHANGE &&
1510 status != GOT_STATUS_ADD &&
1511 status != GOT_STATUS_DELETE &&
1512 status != GOT_STATUS_CONFLICT)
1516 if (asprintf(&path, "/%s", relpath) == -1) {
1517 err = got_error_from_errno("asprintf");
1520 if (strcmp(path, "/") == 0) {
1521 parent_path = strdup("");
1522 if (parent_path == NULL)
1523 return got_error_from_errno("strdup");
1525 err = got_path_dirname(&parent_path, path);
1530 ct = calloc(1, sizeof(*ct));
1532 err = got_error_from_errno("calloc");
1536 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1538 err = got_error_from_errno("asprintf");
1542 if (staged_status == GOT_STATUS_ADD ||
1543 staged_status == GOT_STATUS_MODIFY) {
1544 struct got_fileindex_entry *ie;
1545 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1546 switch (got_fileindex_entry_staged_filetype_get(ie)) {
1547 case GOT_FILEIDX_MODE_REGULAR_FILE:
1548 case GOT_FILEIDX_MODE_BAD_SYMLINK:
1551 case GOT_FILEIDX_MODE_SYMLINK:
1555 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1558 ct->mode |= got_fileindex_entry_perms_get(ie);
1559 } else if (status != GOT_STATUS_DELETE &&
1560 staged_status != GOT_STATUS_DELETE) {
1562 if (fstatat(dirfd, de_name, &sb,
1563 AT_SYMLINK_NOFOLLOW) == -1) {
1564 err = got_error_from_errno2("fstatat",
1568 } else if (lstat(ct->ondisk_path, &sb) == -1) {
1569 err = got_error_from_errno2("lstat", ct->ondisk_path);
1572 ct->mode = sb.st_mode;
1575 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1576 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1578 err = got_error_from_errno("asprintf");
1582 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1583 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1585 char target_path[PATH_MAX];
1587 target_len = readlink(ct->ondisk_path, target_path,
1588 sizeof(target_path));
1589 if (target_len == -1) {
1590 err = got_error_from_errno2("readlink",
1594 err = is_bad_symlink_target(&is_bad_symlink, target_path,
1595 target_len, ct->ondisk_path, a->worktree->root_path);
1598 if (is_bad_symlink) {
1599 err = got_error_path(ct->ondisk_path,
1600 GOT_ERR_BAD_SYMLINK);
1605 ct->status = status;
1606 ct->staged_status = staged_status;
1607 ct->blob_id = NULL; /* will be filled in when blob gets created */
1608 if (ct->status != GOT_STATUS_ADD &&
1609 ct->staged_status != GOT_STATUS_ADD) {
1610 ct->base_blob_id = got_object_id_dup(blob_id);
1611 if (ct->base_blob_id == NULL) {
1612 err = got_error_from_errno("got_object_id_dup");
1615 ct->base_commit_id = got_object_id_dup(commit_id);
1616 if (ct->base_commit_id == NULL) {
1617 err = got_error_from_errno("got_object_id_dup");
1621 if (ct->staged_status == GOT_STATUS_ADD ||
1622 ct->staged_status == GOT_STATUS_MODIFY) {
1623 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1624 if (ct->staged_blob_id == NULL) {
1625 err = got_error_from_errno("got_object_id_dup");
1629 ct->path = strdup(path);
1630 if (ct->path == NULL) {
1631 err = got_error_from_errno("strdup");
1634 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1638 if (a->diff_outfile && ct && new != NULL) {
1639 err = append_ct_diff(ct, &a->diff_header_shown,
1640 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1641 a->have_staged_files, a->repo, a->worktree);
1646 if (ct && (err || new == NULL))
1647 free_commitable(ct);
1653 static const struct got_error *write_tree(struct got_object_id **, int *,
1654 struct got_tree_object *, const char *, struct got_pathlist_head *,
1655 got_worktree_status_cb status_cb, void *status_arg,
1656 struct got_repository *);
1658 static const struct got_error *
1659 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1660 struct got_tree_entry *te, const char *parent_path,
1661 struct got_pathlist_head *commitable_paths,
1662 got_worktree_status_cb status_cb, void *status_arg,
1663 struct got_repository *repo)
1665 const struct got_error *err = NULL;
1666 struct got_tree_object *subtree;
1669 if (asprintf(&subpath, "%s%s%s", parent_path,
1670 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1671 return got_error_from_errno("asprintf");
1673 err = got_object_open_as_tree(&subtree, repo, &te->id);
1677 err = write_tree(new_subtree_id, nentries, subtree, subpath,
1678 commitable_paths, status_cb, status_arg, repo);
1679 got_object_tree_close(subtree);
1684 static const struct got_error *
1685 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1687 const struct got_error *err = NULL;
1688 char *ct_parent_path = NULL;
1692 if (strchr(ct->in_repo_path, '/') == NULL) {
1693 *match = got_path_is_root_dir(path);
1697 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1700 *match = (strcmp(path, ct_parent_path) == 0);
1701 free(ct_parent_path);
1706 get_ct_file_mode(struct got_commitable *ct)
1708 if (S_ISLNK(ct->mode))
1711 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1714 static const struct got_error *
1715 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1716 struct got_tree_entry *te, struct got_commitable *ct)
1718 const struct got_error *err = NULL;
1722 err = got_object_tree_entry_dup(new_te, te);
1726 (*new_te)->mode = get_ct_file_mode(ct);
1728 if (ct->staged_status == GOT_STATUS_MODIFY)
1729 memcpy(&(*new_te)->id, ct->staged_blob_id,
1730 sizeof((*new_te)->id));
1732 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1734 if (err && *new_te) {
1741 static const struct got_error *
1742 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1743 struct got_commitable *ct)
1745 const struct got_error *err = NULL;
1746 char *ct_name = NULL;
1750 *new_te = calloc(1, sizeof(**new_te));
1751 if (*new_te == NULL)
1752 return got_error_from_errno("calloc");
1754 err = got_path_basename(&ct_name, ct->path);
1757 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1758 sizeof((*new_te)->name)) {
1759 err = got_error(GOT_ERR_NO_SPACE);
1763 (*new_te)->mode = get_ct_file_mode(ct);
1765 if (ct->staged_status == GOT_STATUS_ADD)
1766 memcpy(&(*new_te)->id, ct->staged_blob_id,
1767 sizeof((*new_te)->id));
1769 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1772 if (err && *new_te) {
1779 static const struct got_error *
1780 insert_tree_entry(struct got_tree_entry *new_te,
1781 struct got_pathlist_head *paths)
1783 const struct got_error *err = NULL;
1784 struct got_pathlist_entry *new_pe;
1786 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1790 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1794 static const struct got_error *
1795 report_ct_status(struct got_commitable *ct,
1796 got_worktree_status_cb status_cb, void *status_arg)
1798 const char *ct_path = ct->path;
1799 unsigned char status;
1801 if (status_cb == NULL) /* no commit progress output desired */
1804 while (ct_path[0] == '/')
1807 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1808 status = ct->staged_status;
1810 status = ct->status;
1812 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1813 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1816 static const struct got_error *
1817 match_modified_subtree(int *modified, struct got_tree_entry *te,
1818 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1820 const struct got_error *err = NULL;
1821 struct got_pathlist_entry *pe;
1826 if (asprintf(&te_path, "%s%s%s", base_tree_path,
1827 got_path_is_root_dir(base_tree_path) ? "" : "/",
1829 return got_error_from_errno("asprintf");
1831 TAILQ_FOREACH(pe, commitable_paths, entry) {
1832 struct got_commitable *ct = pe->data;
1833 *modified = got_path_is_child(ct->in_repo_path, te_path,
1843 static const struct got_error *
1844 match_deleted_or_modified_ct(struct got_commitable **ctp,
1845 struct got_tree_entry *te, const char *base_tree_path,
1846 struct got_pathlist_head *commitable_paths)
1848 const struct got_error *err = NULL;
1849 struct got_pathlist_entry *pe;
1853 TAILQ_FOREACH(pe, commitable_paths, entry) {
1854 struct got_commitable *ct = pe->data;
1855 char *ct_name = NULL;
1858 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1859 if (ct->status != GOT_STATUS_MODIFY &&
1860 ct->status != GOT_STATUS_MODE_CHANGE &&
1861 ct->status != GOT_STATUS_DELETE &&
1862 ct->status != GOT_STATUS_CONFLICT)
1865 if (ct->staged_status != GOT_STATUS_MODIFY &&
1866 ct->staged_status != GOT_STATUS_DELETE)
1870 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1873 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1879 err = got_path_basename(&ct_name, pe->path);
1883 if (strcmp(te->name, ct_name) != 0) {
1896 static const struct got_error *
1897 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1898 const char *child_path, const char *path_base_tree,
1899 struct got_pathlist_head *commitable_paths,
1900 got_worktree_status_cb status_cb, void *status_arg,
1901 struct got_repository *repo)
1903 const struct got_error *err = NULL;
1904 struct got_tree_entry *new_te;
1906 struct got_object_id *id = NULL;
1911 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1912 got_path_is_root_dir(path_base_tree) ? "" : "/",
1914 return got_error_from_errno("asprintf");
1916 new_te = calloc(1, sizeof(*new_te));
1918 return got_error_from_errno("calloc");
1919 new_te->mode = S_IFDIR;
1921 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1922 sizeof(new_te->name)) {
1923 err = got_error(GOT_ERR_NO_SPACE);
1926 err = write_tree(&id, &nentries, NULL, subtree_path,
1927 commitable_paths, status_cb, status_arg, repo);
1932 memcpy(&new_te->id, id, sizeof(new_te->id));
1941 static const struct got_error *
1942 write_tree(struct got_object_id **new_tree_id, int *nentries,
1943 struct got_tree_object *base_tree, const char *path_base_tree,
1944 struct got_pathlist_head *commitable_paths,
1945 got_worktree_status_cb status_cb, void *status_arg,
1946 struct got_repository *repo)
1948 const struct got_error *err = NULL;
1949 struct got_pathlist_head paths;
1950 struct got_tree_entry *te, *new_te = NULL;
1951 struct got_pathlist_entry *pe;
1956 /* Insert, and recurse into, newly added entries first. */
1957 TAILQ_FOREACH(pe, commitable_paths, entry) {
1958 struct got_commitable *ct = pe->data;
1959 char *child_path = NULL, *slash;
1961 if ((ct->status != GOT_STATUS_ADD &&
1962 ct->staged_status != GOT_STATUS_ADD) ||
1963 (ct->flags & GOT_COMMITABLE_ADDED))
1966 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1967 strlen(path_base_tree)))
1970 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1975 slash = strchr(child_path, '/');
1976 if (slash == NULL) {
1977 err = alloc_added_blob_tree_entry(&new_te, ct);
1980 err = report_ct_status(ct, status_cb, status_arg);
1983 ct->flags |= GOT_COMMITABLE_ADDED;
1984 err = insert_tree_entry(new_te, &paths);
1989 *slash = '\0'; /* trim trailing path components */
1990 if (base_tree == NULL ||
1991 got_object_tree_find_entry(base_tree, child_path)
1993 err = make_subtree_for_added_blob(&new_te,
1994 child_path, path_base_tree,
1995 commitable_paths, status_cb, status_arg,
1999 err = insert_tree_entry(new_te, &paths);
2008 int i, nbase_entries;
2009 /* Handle modified and deleted entries. */
2010 nbase_entries = got_object_tree_get_nentries(base_tree);
2011 for (i = 0; i < nbase_entries; i++) {
2012 struct got_commitable *ct = NULL;
2014 te = got_object_tree_get_entry(base_tree, i);
2015 if (got_object_tree_entry_is_submodule(te)) {
2016 /* Entry is a submodule; just copy it. */
2017 err = got_object_tree_entry_dup(&new_te, te);
2020 err = insert_tree_entry(new_te, &paths);
2027 if (S_ISDIR(te->mode)) {
2029 err = got_object_tree_entry_dup(&new_te, te);
2032 err = match_modified_subtree(&modified, te,
2033 path_base_tree, commitable_paths);
2036 /* Avoid recursion into unmodified subtrees. */
2038 struct got_object_id *new_id;
2040 err = write_subtree(&new_id,
2042 path_base_tree, commitable_paths,
2043 status_cb, status_arg, repo);
2046 if (nsubentries == 0) {
2047 /* All entries were deleted. */
2051 memcpy(&new_te->id, new_id,
2052 sizeof(new_te->id));
2055 err = insert_tree_entry(new_te, &paths);
2062 err = match_deleted_or_modified_ct(&ct, te,
2063 path_base_tree, commitable_paths);
2067 /* NB: Deleted entries get dropped here. */
2068 if (ct->status == GOT_STATUS_MODIFY ||
2069 ct->status == GOT_STATUS_MODE_CHANGE ||
2070 ct->status == GOT_STATUS_CONFLICT ||
2071 ct->staged_status == GOT_STATUS_MODIFY) {
2072 err = alloc_modified_blob_tree_entry(
2076 err = insert_tree_entry(new_te, &paths);
2081 err = report_ct_status(ct, status_cb,
2086 /* Entry is unchanged; just copy it. */
2087 err = got_object_tree_entry_dup(&new_te, te);
2090 err = insert_tree_entry(new_te, &paths);
2098 /* Write new list of entries; deleted entries have been dropped. */
2099 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2101 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2105 static const struct got_error *
2106 update_fileindex_after_commit(struct got_worktree *worktree,
2107 struct got_pathlist_head *commitable_paths,
2108 struct got_object_id *new_base_commit_id,
2109 struct got_fileindex *fileindex, int have_staged_files)
2111 const struct got_error *err = NULL;
2112 struct got_pathlist_entry *pe;
2113 char *relpath = NULL;
2115 TAILQ_FOREACH(pe, commitable_paths, entry) {
2116 struct got_fileindex_entry *ie;
2117 struct got_commitable *ct = pe->data;
2119 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2121 err = got_path_skip_common_ancestor(&relpath,
2122 worktree->root_path, ct->ondisk_path);
2127 if (ct->status == GOT_STATUS_DELETE ||
2128 ct->staged_status == GOT_STATUS_DELETE) {
2129 got_fileindex_entry_remove(fileindex, ie);
2130 } else if (ct->staged_status == GOT_STATUS_ADD ||
2131 ct->staged_status == GOT_STATUS_MODIFY) {
2132 got_fileindex_entry_stage_set(ie,
2133 GOT_FILEIDX_STAGE_NONE);
2134 got_fileindex_entry_staged_filetype_set(ie, 0);
2136 err = got_fileindex_entry_update(ie,
2137 worktree->root_fd, relpath,
2138 ct->staged_blob_id->sha1,
2139 new_base_commit_id->sha1,
2140 !have_staged_files);
2142 err = got_fileindex_entry_update(ie,
2143 worktree->root_fd, relpath,
2145 new_base_commit_id->sha1,
2146 !have_staged_files);
2148 err = got_fileindex_entry_alloc(&ie, pe->path);
2151 err = got_fileindex_entry_update(ie,
2152 worktree->root_fd, relpath, ct->blob_id->sha1,
2153 new_base_commit_id->sha1, 1);
2155 got_fileindex_entry_free(ie);
2158 err = got_fileindex_entry_add(fileindex, ie);
2160 got_fileindex_entry_free(ie);
2172 static const struct got_error *
2173 check_out_of_date(const char *in_repo_path, unsigned char status,
2174 unsigned char staged_status, struct got_object_id *base_blob_id,
2175 struct got_object_id *base_commit_id,
2176 struct got_object_id *head_commit_id, struct got_repository *repo,
2179 const struct got_error *err = NULL;
2180 struct got_commit_object *commit = NULL;
2181 struct got_object_id *id = NULL;
2183 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2184 /* Trivial case: base commit == head commit */
2185 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2188 * Ensure file content which local changes were based
2189 * on matches file content in the branch head.
2191 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2194 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2196 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2197 err = got_error(ood_errcode);
2199 } else if (got_object_id_cmp(id, base_blob_id) != 0)
2200 err = got_error(ood_errcode);
2202 /* Require that added files don't exist in the branch head. */
2203 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2206 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2207 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2209 err = id ? got_error(ood_errcode) : NULL;
2214 got_object_commit_close(commit);
2218 static const struct got_error *
2219 commit_worktree(struct got_object_id **new_commit_id,
2220 struct got_pathlist_head *commitable_paths,
2221 struct got_object_id *head_commit_id,
2222 struct got_object_id *parent_id2,
2223 struct got_worktree *worktree,
2224 const char *author, const char *committer, char *diff_path,
2225 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2226 got_worktree_status_cb status_cb, void *status_arg,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 struct got_pathlist_entry *pe;
2231 struct got_commit_object *head_commit = NULL;
2232 struct got_tree_object *head_tree = NULL;
2233 struct got_object_id *new_tree_id = NULL;
2234 int nentries, nparents = 0;
2235 struct got_object_id_queue parent_ids;
2236 struct got_object_qid *pid = NULL;
2237 char *logmsg = NULL;
2240 *new_commit_id = NULL;
2242 STAILQ_INIT(&parent_ids);
2244 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2248 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2252 if (commit_msg_cb != NULL) {
2253 err = commit_msg_cb(commitable_paths, diff_path,
2254 &logmsg, commit_arg);
2259 if (logmsg == NULL || strlen(logmsg) == 0) {
2260 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2264 /* Create blobs from added and modified files and record their IDs. */
2265 TAILQ_FOREACH(pe, commitable_paths, entry) {
2266 struct got_commitable *ct = pe->data;
2269 /* Blobs for staged files already exist. */
2270 if (ct->staged_status == GOT_STATUS_ADD ||
2271 ct->staged_status == GOT_STATUS_MODIFY)
2274 if (ct->status != GOT_STATUS_ADD &&
2275 ct->status != GOT_STATUS_MODIFY &&
2276 ct->status != GOT_STATUS_MODE_CHANGE &&
2277 ct->status != GOT_STATUS_CONFLICT)
2280 if (asprintf(&ondisk_path, "%s/%s",
2281 worktree->root_path, pe->path) == -1) {
2282 err = got_error_from_errno("asprintf");
2285 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2291 /* Recursively write new tree objects. */
2292 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2293 commitable_paths, status_cb, status_arg, repo);
2297 err = got_object_qid_alloc(&pid, head_commit_id);
2300 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2303 err = got_object_qid_alloc(&pid, parent_id2);
2306 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2309 timestamp = time(NULL);
2310 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2311 nparents, author, timestamp, committer, timestamp, logmsg, repo);
2317 got_object_id_queue_free(&parent_ids);
2319 got_object_tree_close(head_tree);
2321 got_object_commit_close(head_commit);
2325 static const struct got_error *
2326 check_path_is_commitable(const char *path,
2327 struct got_pathlist_head *commitable_paths)
2329 struct got_pathlist_entry *cpe = NULL;
2330 size_t path_len = strlen(path);
2332 TAILQ_FOREACH(cpe, commitable_paths, entry) {
2333 struct got_commitable *ct = cpe->data;
2334 const char *ct_path = ct->path;
2336 while (ct_path[0] == '/')
2339 if (strcmp(path, ct_path) == 0 ||
2340 got_path_is_child(ct_path, path, path_len))
2345 return got_error_path(path, GOT_ERR_BAD_PATH);
2350 static const struct got_error *
2351 check_staged_file(void *arg, struct got_fileindex_entry *ie)
2353 int *have_staged_files = arg;
2355 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2356 *have_staged_files = 1;
2357 return got_error(GOT_ERR_CANCELLED);
2363 static const struct got_error *
2364 check_non_staged_files(struct got_fileindex *fileindex,
2365 struct got_pathlist_head *paths)
2367 struct got_pathlist_entry *pe;
2368 struct got_fileindex_entry *ie;
2370 TAILQ_FOREACH(pe, paths, entry) {
2371 if (pe->path[0] == '\0')
2373 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2375 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2376 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2377 return got_error_path(pe->path,
2378 GOT_ERR_FILE_NOT_STAGED);
2385 print_load_info(int print_colored, int print_found, int print_trees,
2386 int ncolored, int nfound, int ntrees)
2388 if (print_colored) {
2389 printf("%d commit%s colored", ncolored,
2390 ncolored == 1 ? "" : "s");
2393 printf("%s%d object%s found",
2394 ncolored > 0 ? "; " : "",
2395 nfound, nfound == 1 ? "" : "s");
2398 printf("; %d tree%s scanned", ntrees,
2399 ntrees == 1 ? "" : "s");
2403 struct got_send_progress_arg {
2404 char last_scaled_packsize[FMT_SCALED_STRSIZE];
2411 int last_nobj_total;
2415 int printed_something;
2417 struct got_pathlist_head *delete_branches;
2420 static const struct got_error *
2421 send_progress(void *arg, int ncolored, int nfound, int ntrees,
2422 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2423 int nobj_written, off_t bytes_sent, const char *refname,
2424 const char *errmsg, int success)
2426 struct got_send_progress_arg *a = arg;
2427 char scaled_packsize[FMT_SCALED_STRSIZE];
2428 char scaled_sent[FMT_SCALED_STRSIZE];
2429 int p_deltify = 0, p_written = 0, p_sent = 0;
2430 int print_colored = 0, print_found = 0, print_trees = 0;
2431 int print_searching = 0, print_total = 0;
2432 int print_deltify = 0, print_written = 0, print_sent = 0;
2434 if (a->verbosity < 0)
2438 const char *status = success ? "accepted" : "rejected";
2441 struct got_pathlist_entry *pe;
2442 TAILQ_FOREACH(pe, a->delete_branches, entry) {
2443 const char *branchname = pe->path;
2444 if (got_path_cmp(branchname, refname,
2445 strlen(branchname), strlen(refname)) == 0) {
2447 a->sent_something = 1;
2453 if (a->printed_something)
2455 printf("Server has %s %s", status, refname);
2457 printf(": %s", errmsg);
2458 a->printed_something = 1;
2462 if (a->last_ncolored != ncolored) {
2464 a->last_ncolored = ncolored;
2467 if (a->last_nfound != nfound) {
2470 a->last_nfound = nfound;
2473 if (a->last_ntrees != ntrees) {
2477 a->last_ntrees = ntrees;
2480 if ((print_colored || print_found || print_trees) &&
2483 print_load_info(print_colored, print_found, print_trees,
2484 ncolored, nfound, ntrees);
2485 a->printed_something = 1;
2488 } else if (!a->loading_done) {
2490 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2492 a->loading_done = 1;
2495 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2496 return got_error_from_errno("fmt_scaled");
2497 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2498 return got_error_from_errno("fmt_scaled");
2500 if (a->last_ncommits != ncommits) {
2501 print_searching = 1;
2502 a->last_ncommits = ncommits;
2505 if (a->last_nobj_total != nobj_total) {
2506 print_searching = 1;
2508 a->last_nobj_total = nobj_total;
2511 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2512 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2513 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2514 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2515 return got_error(GOT_ERR_NO_SPACE);
2518 if (nobj_deltify > 0 || nobj_written > 0) {
2519 if (nobj_deltify > 0) {
2520 p_deltify = (nobj_deltify * 100) / nobj_total;
2521 if (p_deltify != a->last_p_deltify) {
2522 a->last_p_deltify = p_deltify;
2523 print_searching = 1;
2528 if (nobj_written > 0) {
2529 p_written = (nobj_written * 100) / nobj_total;
2530 if (p_written != a->last_p_written) {
2531 a->last_p_written = p_written;
2532 print_searching = 1;
2540 if (bytes_sent > 0) {
2541 p_sent = (bytes_sent * 100) / packfile_size;
2542 if (p_sent != a->last_p_sent) {
2543 a->last_p_sent = p_sent;
2544 print_searching = 1;
2550 a->sent_something = 1;
2553 if (print_searching || print_total || print_deltify || print_written ||
2556 if (print_searching)
2557 printf("packing %d reference%s", ncommits,
2558 ncommits == 1 ? "" : "s");
2560 printf("; %d object%s", nobj_total,
2561 nobj_total == 1 ? "" : "s");
2563 printf("; deltify: %d%%", p_deltify);
2565 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2566 scaled_packsize, p_sent);
2567 else if (print_written)
2568 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2569 scaled_packsize, p_written);
2570 if (print_searching || print_total || print_deltify ||
2571 print_written || print_sent) {
2572 a->printed_something = 1;
2578 struct got_fetch_progress_arg {
2579 char last_scaled_size[FMT_SCALED_STRSIZE];
2581 int last_p_resolved;
2584 struct got_repository *repo;
2587 static const struct got_error *
2588 fetch_progress(void *arg, const char *message, off_t packfile_size,
2589 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2591 struct got_fetch_progress_arg *a = arg;
2592 char scaled_size[FMT_SCALED_STRSIZE];
2593 int p_indexed, p_resolved;
2594 int print_size = 0, print_indexed = 0, print_resolved = 0;
2596 if (a->verbosity < 0)
2599 if (message && message[0] != '\0') {
2600 printf("\rserver: %s", message);
2605 if (packfile_size > 0 || nobj_indexed > 0) {
2606 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2607 (a->last_scaled_size[0] == '\0' ||
2608 strcmp(scaled_size, a->last_scaled_size)) != 0) {
2610 if (strlcpy(a->last_scaled_size, scaled_size,
2611 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2612 return got_error(GOT_ERR_NO_SPACE);
2614 if (nobj_indexed > 0) {
2615 p_indexed = (nobj_indexed * 100) / nobj_total;
2616 if (p_indexed != a->last_p_indexed) {
2617 a->last_p_indexed = p_indexed;
2622 if (nobj_resolved > 0) {
2623 p_resolved = (nobj_resolved * 100) /
2624 (nobj_total - nobj_loose);
2625 if (p_resolved != a->last_p_resolved) {
2626 a->last_p_resolved = p_resolved;
2634 if (print_size || print_indexed || print_resolved)
2637 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2639 printf("; indexing %d%%", p_indexed);
2641 printf("; resolving deltas %d%%", p_resolved);
2642 if (print_size || print_indexed || print_resolved) {
2650 static const struct got_error *
2651 create_symref(const char *refname, struct got_reference *target_ref,
2652 int verbosity, struct got_repository *repo)
2654 const struct got_error *err;
2655 struct got_reference *head_symref;
2657 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2661 err = got_ref_write(head_symref, repo);
2662 if (err == NULL && verbosity > 0) {
2663 printf("Created reference %s: %s\n", GOT_REF_HEAD,
2664 got_ref_get_name(target_ref));
2666 got_ref_close(head_symref);
2670 static const struct got_error *
2671 create_ref(const char *refname, struct got_object_id *id,
2672 int verbosity, struct got_repository *repo)
2674 const struct got_error *err = NULL;
2675 struct got_reference *ref;
2678 err = got_object_id_str(&id_str, id);
2682 err = got_ref_alloc(&ref, refname, id);
2686 err = got_ref_write(ref, repo);
2689 if (err == NULL && verbosity >= 0)
2690 printf("Created reference %s: %s\n", refname, id_str);
2696 static const struct got_error *
2697 update_ref(struct got_reference *ref, struct got_object_id *new_id,
2698 int verbosity, struct got_repository *repo)
2700 const struct got_error *err = NULL;
2701 char *new_id_str = NULL;
2702 struct got_object_id *old_id = NULL;
2704 err = got_object_id_str(&new_id_str, new_id);
2708 if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2709 err = got_ref_resolve(&old_id, repo, ref);
2712 if (got_object_id_cmp(old_id, new_id) == 0)
2714 if (verbosity >= 0) {
2715 printf("Rejecting update of existing tag %s: %s\n",
2716 got_ref_get_name(ref), new_id_str);
2721 if (got_ref_is_symbolic(ref)) {
2722 if (verbosity >= 0) {
2723 printf("Replacing reference %s: %s\n",
2724 got_ref_get_name(ref),
2725 got_ref_get_symref_target(ref));
2727 err = got_ref_change_symref_to_ref(ref, new_id);
2730 err = got_ref_write(ref, repo);
2734 err = got_ref_resolve(&old_id, repo, ref);
2737 if (got_object_id_cmp(old_id, new_id) == 0)
2740 err = got_ref_change_ref(ref, new_id);
2743 err = got_ref_write(ref, repo);
2749 printf("Updated %s: %s\n", got_ref_get_name(ref),
2757 static const struct got_error *
2758 fetch_updated_remote(const char *proto, const char *host, const char *port,
2759 const char *server_path, int verbosity,
2760 const struct got_remote_repo *remote, struct got_repository *repo,
2761 struct got_reference *head_ref, const char *head_refname)
2763 const struct got_error *err = NULL, *unlock_err = NULL;
2764 struct got_pathlist_entry *pe;
2765 struct got_pathlist_head learned_refs;
2766 struct got_pathlist_head symrefs;
2767 struct got_pathlist_head wanted_branches;
2768 struct got_pathlist_head wanted_refs;
2769 struct got_object_id *pack_hash;
2770 struct got_fetch_progress_arg fpa;
2772 pid_t fetchpid = -1;
2774 TAILQ_INIT(&learned_refs);
2775 TAILQ_INIT(&symrefs);
2776 TAILQ_INIT(&wanted_branches);
2777 TAILQ_INIT(&wanted_refs);
2779 err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2784 err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2785 port, server_path, verbosity);
2789 fpa.last_scaled_size[0] = '\0';
2790 fpa.last_p_indexed = -1;
2791 fpa.last_p_resolved = -1;
2792 fpa.verbosity = verbosity;
2795 err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2796 remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2797 fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2801 /* Update references provided with the pack file. */
2802 TAILQ_FOREACH(pe, &learned_refs, entry) {
2803 const char *refname = pe->path;
2804 struct got_object_id *id = pe->data;
2805 struct got_reference *ref;
2807 err = got_ref_open(&ref, repo, refname, 0);
2809 if (err->code != GOT_ERR_NOT_REF)
2811 err = create_ref(refname, id, verbosity, repo);
2815 err = update_ref(ref, id, verbosity, repo);
2816 unlock_err = got_ref_unlock(ref);
2817 if (unlock_err && err == NULL)
2825 /* Set the HEAD reference if the server provided one. */
2826 TAILQ_FOREACH(pe, &symrefs, entry) {
2827 struct got_reference *target_ref;
2828 const char *refname = pe->path;
2829 const char *target = pe->data;
2830 char *remote_refname = NULL, *remote_target = NULL;
2832 if (strcmp(refname, GOT_REF_HEAD) != 0)
2835 err = got_ref_open(&target_ref, repo, target, 0);
2837 if (err->code == GOT_ERR_NOT_REF) {
2844 err = create_symref(refname, target_ref, verbosity, repo);
2845 got_ref_close(target_ref);
2849 if (remote->mirror_references)
2852 if (strncmp("refs/heads/", target, 11) != 0)
2855 if (asprintf(&remote_refname,
2856 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2858 err = got_error_from_errno("asprintf");
2861 if (asprintf(&remote_target,
2862 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2863 target + 11) == -1) {
2864 err = got_error_from_errno("asprintf");
2865 free(remote_refname);
2868 err = got_ref_open(&target_ref, repo, remote_target, 0);
2870 free(remote_refname);
2871 free(remote_target);
2872 if (err->code == GOT_ERR_NOT_REF) {
2878 err = create_symref(remote_refname, target_ref,
2879 verbosity - 1, repo);
2880 free(remote_refname);
2881 free(remote_target);
2882 got_ref_close(target_ref);
2888 got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2889 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2890 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2891 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2896 const struct got_error *
2897 got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2898 struct got_worktree *worktree, struct got_pathlist_head *paths,
2899 const char *author, const char *committer, int allow_bad_symlinks,
2900 int show_diff, int commit_conflicts,
2901 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2902 got_worktree_status_cb status_cb, void *status_arg,
2903 const char *proto, const char *host, const char *port,
2904 const char *server_path, int verbosity,
2905 const struct got_remote_repo *remote,
2906 got_cancel_cb check_cancelled,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2910 struct got_fileindex *fileindex = NULL;
2911 char *fileindex_path = NULL;
2912 struct got_pathlist_head commitable_paths;
2913 struct collect_commitables_arg cc_arg;
2914 struct got_pathlist_entry *pe;
2915 struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2916 struct got_reference *commit_ref = NULL;
2917 struct got_object_id *head_commit_id = NULL;
2918 struct got_object_id *head_commit_id2 = NULL;
2919 char *head_refname = NULL;
2920 char *commit_refname = NULL;
2921 char *diff_path = NULL;
2922 int have_staged_files = 0;
2925 struct got_send_progress_arg spa;
2926 struct got_pathlist_head commit_reflist;
2927 struct got_pathlist_head tag_names;
2928 struct got_pathlist_head delete_branches;
2930 *new_commit_id = NULL;
2932 memset(&cc_arg, 0, sizeof(cc_arg));
2933 TAILQ_INIT(&commitable_paths);
2934 TAILQ_INIT(&commit_reflist);
2935 TAILQ_INIT(&tag_names);
2936 TAILQ_INIT(&delete_branches);
2938 err = lock_worktree(worktree, LOCK_EX);
2942 err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2947 head_refname = worktree->head_ref_name;
2948 err = got_ref_open(&head_ref, repo, head_refname, 0);
2951 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2955 err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2958 err = got_ref_write(commit_ref, repo);
2962 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2966 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2967 &have_staged_files);
2968 if (err && err->code != GOT_ERR_CANCELLED)
2970 if (have_staged_files) {
2971 err = check_non_staged_files(fileindex, paths);
2976 cc_arg.commitable_paths = &commitable_paths;
2977 cc_arg.worktree = worktree;
2978 cc_arg.fileindex = fileindex;
2980 cc_arg.have_staged_files = have_staged_files;
2981 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2982 cc_arg.diff_header_shown = 0;
2983 cc_arg.commit_conflicts = commit_conflicts;
2985 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2986 GOT_TMPDIR_STR "/got", ".diff");
2989 cc_arg.f1 = got_opentemp();
2990 if (cc_arg.f1 == NULL) {
2991 err = got_error_from_errno("got_opentemp");
2994 cc_arg.f2 = got_opentemp();
2995 if (cc_arg.f2 == NULL) {
2996 err = got_error_from_errno("got_opentemp");
3001 TAILQ_FOREACH(pe, paths, entry) {
3002 err = worktree_status(worktree, pe->path, fileindex, repo,
3003 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3009 if (fflush(cc_arg.diff_outfile) == EOF) {
3010 err = got_error_from_errno("fflush");
3015 if (TAILQ_EMPTY(&commitable_paths)) {
3016 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3020 TAILQ_FOREACH(pe, paths, entry) {
3021 err = check_path_is_commitable(pe->path, &commitable_paths);
3026 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3027 struct got_commitable *ct = pe->data;
3028 const char *ct_path = ct->in_repo_path;
3030 while (ct_path[0] == '/')
3032 err = check_out_of_date(ct_path, ct->status,
3033 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3034 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3039 err = commit_worktree(new_commit_id, &commitable_paths,
3040 head_commit_id, NULL, worktree, author, committer,
3041 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3042 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3047 * Check if a concurrent commit to our branch has occurred.
3048 * Lock the reference here to prevent concurrent modification.
3050 err = got_ref_open(&head_ref2, repo, head_refname, 1);
3053 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3056 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3057 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3061 err = got_pathlist_append(&commit_reflist, commit_refname,
3066 /* Update commit ref in repository. */
3067 err = got_ref_change_ref(commit_ref, *new_commit_id);
3070 err = got_ref_write(commit_ref, repo);
3074 if (verbosity >= 0) {
3075 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3076 remote->name, proto, host,
3077 port ? ":" : "", port ? port : "",
3078 *server_path == '/' ? "" : "/", server_path);
3081 /* Attempt send to remote branch. */
3082 err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3083 server_path, verbosity);
3087 memset(&spa, 0, sizeof(spa));
3088 spa.last_scaled_packsize[0] = '\0';
3089 spa.last_p_deltify = -1;
3090 spa.last_p_written = -1;
3091 spa.verbosity = verbosity;
3092 spa.delete_branches = &delete_branches;
3093 err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3094 &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3095 check_cancelled, NULL);
3096 if (spa.printed_something)
3098 if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3100 * Fetch new changes since remote has diverged.
3101 * No trivial-rebase yet; require update to be run manually.
3103 err = fetch_updated_remote(proto, host, port, server_path,
3104 verbosity, remote, repo, head_ref, head_refname);
3107 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3109 /* XXX: Rebase commit over fetched remote branch. */
3115 /* Update branch head in repository. */
3116 err = got_ref_change_ref(head_ref2, *new_commit_id);
3119 err = got_ref_write(head_ref2, repo);
3123 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3127 err = ref_base_commit(worktree, repo);
3131 /* XXX: fileindex must be updated for other fetched changes? */
3132 err = update_fileindex_after_commit(worktree, &commitable_paths,
3133 *new_commit_id, fileindex, have_staged_files);
3134 sync_err = sync_fileindex(fileindex, fileindex_path);
3135 if (sync_err && err == NULL)
3139 unlockerr = got_ref_unlock(head_ref2);
3140 if (unlockerr && err == NULL)
3142 got_ref_close(head_ref2);
3145 got_ref_close(commit_ref);
3147 got_fileindex_free(fileindex);
3148 unlockerr = lock_worktree(worktree, LOCK_SH);
3149 if (unlockerr && err == NULL)
3151 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3152 struct got_commitable *ct = pe->data;
3154 free_commitable(ct);
3156 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3157 if (diff_path && unlink(diff_path) == -1 && err == NULL)
3158 err = got_error_from_errno2("unlink", diff_path);
3159 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3161 err = got_error_from_errno("fclose");
3162 free(head_commit_id);
3163 free(head_commit_id2);
3164 free(commit_refname);
3165 free(fileindex_path);
3170 const struct got_error *
3171 got_worktree_cvg_get_commit_ref_name(char **refname,
3172 struct got_worktree *worktree)
3174 return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);