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;
699 err = a->cancel_cb(a->cancel_arg);
704 if (got_path_cmp(parent_path, a->status_path,
705 strlen(parent_path), a->status_path_len) != 0 &&
706 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
709 if (parent_path[0]) {
710 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
711 parent_path, de->d_name) == -1)
712 return got_error_from_errno("asprintf");
714 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
716 return got_error_from_errno("asprintf");
719 err = report_file_status(ie, abspath, dirfd, de->d_name,
720 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
725 static const struct got_error *
726 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
728 const struct got_error *err = NULL;
729 struct diff_dir_cb_arg *a = arg;
730 struct got_object_id blob_id, commit_id;
731 unsigned char status;
734 err = a->cancel_cb(a->cancel_arg);
739 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
742 got_fileindex_entry_get_blob_id(&blob_id, ie);
743 got_fileindex_entry_get_commit_id(&commit_id, ie);
744 if (got_fileindex_entry_has_file_on_disk(ie))
745 status = GOT_STATUS_MISSING;
747 status = GOT_STATUS_DELETE;
748 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
749 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
753 free_ignores(struct got_pathlist_head *ignores)
755 struct got_pathlist_entry *pe;
757 TAILQ_FOREACH(pe, ignores, entry) {
758 struct got_pathlist_head *ignorelist = pe->data;
760 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
762 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
765 static const struct got_error *
766 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
768 const struct got_error *err = NULL;
769 struct got_pathlist_entry *pe = NULL;
770 struct got_pathlist_head *ignorelist;
771 char *line = NULL, *pattern, *dirpath = NULL;
775 ignorelist = calloc(1, sizeof(*ignorelist));
776 if (ignorelist == NULL)
777 return got_error_from_errno("calloc");
778 TAILQ_INIT(ignorelist);
780 while ((linelen = getline(&line, &linesize, f)) != -1) {
781 if (linelen > 0 && line[linelen - 1] == '\n')
782 line[linelen - 1] = '\0';
784 /* Git's ignores may contain comments. */
788 /* Git's negated patterns are not (yet?) supported. */
792 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
794 err = got_error_from_errno("asprintf");
797 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
802 err = got_error_from_errno("getline");
806 dirpath = strdup(path);
807 if (dirpath == NULL) {
808 err = got_error_from_errno("strdup");
811 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
814 if (err || pe == NULL) {
816 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
822 match_path(const char *pattern, size_t pattern_len, const char *path,
828 * Trailing slashes signify directories.
829 * Append a * to make such patterns conform to fnmatch rules.
831 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
832 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
833 return FNM_NOMATCH; /* XXX */
835 return fnmatch(buf, path, flags);
838 return fnmatch(pattern, path, flags);
842 match_ignores(struct got_pathlist_head *ignores, const char *path)
844 struct got_pathlist_entry *pe;
846 /* Handle patterns which match in all directories. */
847 TAILQ_FOREACH(pe, ignores, entry) {
848 struct got_pathlist_head *ignorelist = pe->data;
849 struct got_pathlist_entry *pi;
851 TAILQ_FOREACH(pi, ignorelist, entry) {
854 if (pi->path_len < 3 ||
855 strncmp(pi->path, "**/", 3) != 0)
859 if (match_path(pi->path + 3,
861 FNM_PATHNAME | FNM_LEADING_DIR)) {
862 /* Retry in next directory. */
863 while (*p && *p != '/')
875 * The ignores pathlist contains ignore lists from children before
876 * parents, so we can find the most specific ignorelist by walking
879 pe = TAILQ_LAST(ignores, got_pathlist_head);
881 if (got_path_is_child(path, pe->path, pe->path_len)) {
882 struct got_pathlist_head *ignorelist = pe->data;
883 struct got_pathlist_entry *pi;
884 TAILQ_FOREACH(pi, ignorelist, entry) {
885 int flags = FNM_LEADING_DIR;
886 if (strstr(pi->path, "/**/") == NULL)
887 flags |= FNM_PATHNAME;
888 if (match_path(pi->path, pi->path_len,
894 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
900 static const struct got_error *
901 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
902 const char *path, int dirfd, const char *ignores_filename)
904 const struct got_error *err = NULL;
907 FILE *ignoresfile = NULL;
909 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
910 path[0] ? "/" : "", ignores_filename) == -1)
911 return got_error_from_errno("asprintf");
914 fd = openat(dirfd, ignores_filename,
915 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
917 if (errno != ENOENT && errno != EACCES)
918 err = got_error_from_errno2("openat",
921 ignoresfile = fdopen(fd, "r");
922 if (ignoresfile == NULL)
923 err = got_error_from_errno2("fdopen",
927 err = read_ignores(ignores, path, ignoresfile);
931 ignoresfile = fopen(ignorespath, "re");
932 if (ignoresfile == NULL) {
933 if (errno != ENOENT && errno != EACCES)
934 err = got_error_from_errno2("fopen",
937 err = read_ignores(ignores, path, ignoresfile);
940 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
941 err = got_error_from_errno2("fclose", path);
942 if (fd != -1 && close(fd) == -1 && err == NULL)
943 err = got_error_from_errno2("close", path);
948 static const struct got_error *
949 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
952 const struct got_error *err = NULL;
953 struct diff_dir_cb_arg *a = arg;
960 err = a->cancel_cb(a->cancel_arg);
965 if (parent_path[0]) {
966 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
967 return got_error_from_errno("asprintf");
972 if (de->d_type == DT_DIR) {
973 if (!a->no_ignores && ignore != NULL &&
974 match_ignores(a->ignores, path))
976 } else if (!match_ignores(a->ignores, path) &&
977 got_path_is_child(path, a->status_path, a->status_path_len))
978 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
979 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
985 static const struct got_error *
986 status_traverse(void *arg, const char *path, int dirfd)
988 const struct got_error *err = NULL;
989 struct diff_dir_cb_arg *a = arg;
994 err = add_ignores(a->ignores, a->worktree->root_path,
995 path, dirfd, ".cvsignore");
999 err = add_ignores(a->ignores, a->worktree->root_path, path,
1000 dirfd, ".gitignore");
1005 static const struct got_error *
1006 report_single_file_status(const char *path, const char *ondisk_path,
1007 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
1008 void *status_arg, struct got_repository *repo, int report_unchanged,
1009 struct got_pathlist_head *ignores, int no_ignores)
1011 struct got_fileindex_entry *ie;
1014 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1016 return report_file_status(ie, ondisk_path, -1, NULL,
1017 status_cb, status_arg, repo, report_unchanged);
1019 if (lstat(ondisk_path, &sb) == -1) {
1020 if (errno != ENOENT)
1021 return got_error_from_errno2("lstat", ondisk_path);
1022 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1023 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1026 if (!no_ignores && match_ignores(ignores, path))
1029 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1030 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1031 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1036 static const struct got_error *
1037 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1038 const char *root_path, const char *path)
1040 const struct got_error *err;
1041 char *parent_path, *next_parent_path = NULL;
1043 err = add_ignores(ignores, root_path, "", -1,
1048 err = add_ignores(ignores, root_path, "", -1,
1053 err = got_path_dirname(&parent_path, path);
1055 if (err->code == GOT_ERR_BAD_PATH)
1056 return NULL; /* cannot traverse parent */
1060 err = add_ignores(ignores, root_path, parent_path, -1,
1064 err = add_ignores(ignores, root_path, parent_path, -1,
1068 err = got_path_dirname(&next_parent_path, parent_path);
1070 if (err->code == GOT_ERR_BAD_PATH)
1071 err = NULL; /* traversed everything */
1074 if (got_path_is_root_dir(parent_path))
1077 parent_path = next_parent_path;
1078 next_parent_path = NULL;
1082 free(next_parent_path);
1086 struct find_missing_children_args {
1087 const char *parent_path;
1089 struct got_pathlist_head *children;
1090 got_cancel_cb cancel_cb;
1094 static const struct got_error *
1095 find_missing_children(void *arg, struct got_fileindex_entry *ie)
1097 const struct got_error *err = NULL;
1098 struct find_missing_children_args *a = arg;
1101 err = a->cancel_cb(a->cancel_arg);
1106 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1107 err = got_pathlist_append(a->children, ie->path, NULL);
1112 static const struct got_error *
1113 report_children(struct got_pathlist_head *children,
1114 struct got_worktree *worktree, struct got_fileindex *fileindex,
1115 struct got_repository *repo, int is_root_dir, int report_unchanged,
1116 struct got_pathlist_head *ignores, int no_ignores,
1117 got_worktree_status_cb status_cb, void *status_arg,
1118 got_cancel_cb cancel_cb, void *cancel_arg)
1120 const struct got_error *err = NULL;
1121 struct got_pathlist_entry *pe;
1122 char *ondisk_path = NULL;
1124 TAILQ_FOREACH(pe, children, entry) {
1126 err = cancel_cb(cancel_arg);
1131 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1132 !is_root_dir ? "/" : "", pe->path) == -1) {
1133 err = got_error_from_errno("asprintf");
1138 err = report_single_file_status(pe->path, ondisk_path,
1139 fileindex, status_cb, status_arg, repo, report_unchanged,
1140 ignores, no_ignores);
1152 static const struct got_error *
1153 worktree_status(struct got_worktree *worktree, const char *path,
1154 struct got_fileindex *fileindex, struct got_repository *repo,
1155 got_worktree_status_cb status_cb, void *status_arg,
1156 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1157 int report_unchanged)
1159 const struct got_error *err = NULL;
1161 struct got_fileindex_diff_dir_cb fdiff_cb;
1162 struct diff_dir_cb_arg arg;
1163 char *ondisk_path = NULL;
1164 struct got_pathlist_head ignores, missing_children;
1165 struct got_fileindex_entry *ie;
1167 TAILQ_INIT(&ignores);
1168 TAILQ_INIT(&missing_children);
1170 if (asprintf(&ondisk_path, "%s%s%s",
1171 worktree->root_path, path[0] ? "/" : "", path) == -1)
1172 return got_error_from_errno("asprintf");
1174 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1176 err = report_single_file_status(path, ondisk_path,
1177 fileindex, status_cb, status_arg, repo,
1178 report_unchanged, &ignores, no_ignores);
1181 struct find_missing_children_args fmca;
1182 fmca.parent_path = path;
1183 fmca.parent_len = strlen(path);
1184 fmca.children = &missing_children;
1185 fmca.cancel_cb = cancel_cb;
1186 fmca.cancel_arg = cancel_arg;
1187 err = got_fileindex_for_each_entry_safe(fileindex,
1188 find_missing_children, &fmca);
1193 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1195 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1196 !got_err_open_nofollow_on_symlink())
1197 err = got_error_from_errno2("open", ondisk_path);
1200 err = add_ignores_from_parent_paths(&ignores,
1201 worktree->root_path, ondisk_path);
1205 if (TAILQ_EMPTY(&missing_children)) {
1206 err = report_single_file_status(path,
1207 ondisk_path, fileindex,
1208 status_cb, status_arg, repo,
1209 report_unchanged, &ignores, no_ignores);
1213 err = report_children(&missing_children,
1214 worktree, fileindex, repo,
1215 (path[0] == '\0'), report_unchanged,
1216 &ignores, no_ignores,
1217 status_cb, status_arg,
1218 cancel_cb, cancel_arg);
1224 fdiff_cb.diff_old_new = status_old_new;
1225 fdiff_cb.diff_old = status_old;
1226 fdiff_cb.diff_new = status_new;
1227 fdiff_cb.diff_traverse = status_traverse;
1228 arg.fileindex = fileindex;
1229 arg.worktree = worktree;
1230 arg.status_path = path;
1231 arg.status_path_len = strlen(path);
1233 arg.status_cb = status_cb;
1234 arg.status_arg = status_arg;
1235 arg.cancel_cb = cancel_cb;
1236 arg.cancel_arg = cancel_arg;
1237 arg.report_unchanged = report_unchanged;
1238 arg.no_ignores = no_ignores;
1240 err = add_ignores_from_parent_paths(&ignores,
1241 worktree->root_path, path);
1245 arg.ignores = &ignores;
1246 err = got_fileindex_diff_dir(fileindex, fd,
1247 worktree->root_path, path, repo, &fdiff_cb, &arg);
1250 free_ignores(&ignores);
1251 if (fd != -1 && close(fd) == -1 && err == NULL)
1252 err = got_error_from_errno("close");
1258 free_commitable(struct got_commitable *ct)
1261 free(ct->in_repo_path);
1262 free(ct->ondisk_path);
1264 free(ct->base_blob_id);
1265 free(ct->staged_blob_id);
1266 free(ct->base_commit_id);
1270 struct collect_commitables_arg {
1271 struct got_pathlist_head *commitable_paths;
1272 struct got_repository *repo;
1273 struct got_worktree *worktree;
1274 struct got_fileindex *fileindex;
1275 int have_staged_files;
1276 int allow_bad_symlinks;
1277 int diff_header_shown;
1278 int commit_conflicts;
1285 * Create a file which contains the target path of a symlink so we can feed
1286 * it as content to the diff engine.
1288 static const struct got_error *
1289 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1290 const char *abspath)
1292 const struct got_error *err = NULL;
1293 char target_path[PATH_MAX];
1294 ssize_t target_len, outlen;
1299 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1300 if (target_len == -1)
1301 return got_error_from_errno2("readlinkat", abspath);
1303 target_len = readlink(abspath, target_path, PATH_MAX);
1304 if (target_len == -1)
1305 return got_error_from_errno2("readlink", abspath);
1308 *fd = got_opentempfd();
1310 return got_error_from_errno("got_opentempfd");
1312 outlen = write(*fd, target_path, target_len);
1314 err = got_error_from_errno("got_opentempfd");
1318 if (lseek(*fd, 0, SEEK_SET) == -1) {
1319 err = got_error_from_errno2("lseek", abspath);
1330 static const struct got_error *
1331 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1332 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1333 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1335 const struct got_error *err = NULL;
1336 struct got_blob_object *blob1 = NULL;
1337 int fd = -1, fd1 = -1, fd2 = -1;
1338 FILE *ondisk_file = NULL;
1339 char *label1 = NULL;
1343 char *id_str = NULL;
1345 memset(&sb, 0, sizeof(sb));
1348 if (ct->staged_status != GOT_STATUS_MODIFY &&
1349 ct->staged_status != GOT_STATUS_ADD &&
1350 ct->staged_status != GOT_STATUS_DELETE)
1353 if (ct->status != GOT_STATUS_MODIFY &&
1354 ct->status != GOT_STATUS_ADD &&
1355 ct->status != GOT_STATUS_DELETE &&
1356 ct->status != GOT_STATUS_CONFLICT)
1360 err = got_opentemp_truncate(f1);
1362 return got_error_from_errno("got_opentemp_truncate");
1363 err = got_opentemp_truncate(f2);
1365 return got_error_from_errno("got_opentemp_truncate");
1367 if (!*diff_header_shown) {
1368 err = got_object_id_str(&id_str, worktree->base_commit_id);
1371 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1372 got_worktree_get_root_path(worktree));
1373 fprintf(diff_outfile, "commit - %s\n", id_str);
1374 fprintf(diff_outfile, "path + %s%s\n",
1375 got_worktree_get_root_path(worktree),
1376 diff_staged ? " (staged changes)" : "");
1377 *diff_header_shown = 1;
1381 const char *label1 = NULL, *label2 = NULL;
1382 switch (ct->staged_status) {
1383 case GOT_STATUS_MODIFY:
1387 case GOT_STATUS_ADD:
1390 case GOT_STATUS_DELETE:
1394 return got_error(GOT_ERR_FILE_STATUS);
1396 fd1 = got_opentempfd();
1398 err = got_error_from_errno("got_opentempfd");
1401 fd2 = got_opentempfd();
1403 err = got_error_from_errno("got_opentempfd");
1406 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1407 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1408 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1409 NULL, repo, diff_outfile);
1413 fd1 = got_opentempfd();
1415 err = got_error_from_errno("got_opentempfd");
1419 if (ct->status != GOT_STATUS_ADD) {
1420 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1426 if (ct->status != GOT_STATUS_DELETE) {
1428 fd = openat(dirfd, de_name,
1429 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1431 if (!got_err_open_nofollow_on_symlink()) {
1432 err = got_error_from_errno2("openat",
1436 err = get_symlink_target_file(&fd, dirfd,
1437 de_name, ct->ondisk_path);
1442 fd = open(ct->ondisk_path,
1443 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1445 if (!got_err_open_nofollow_on_symlink()) {
1446 err = got_error_from_errno2("open",
1450 err = get_symlink_target_file(&fd, dirfd,
1451 de_name, ct->ondisk_path);
1456 if (fstatat(fd, ct->ondisk_path, &sb,
1457 AT_SYMLINK_NOFOLLOW) == -1) {
1458 err = got_error_from_errno2("fstatat", ct->ondisk_path);
1461 ondisk_file = fdopen(fd, "r");
1462 if (ondisk_file == NULL) {
1463 err = got_error_from_errno2("fdopen", ct->ondisk_path);
1471 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1477 err = got_diff_blob_file(blob1, f1, size1, label1,
1478 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1479 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1481 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1482 err = got_error_from_errno("close");
1483 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1484 err = got_error_from_errno("close");
1486 got_object_blob_close(blob1);
1487 if (fd != -1 && close(fd) == -1 && err == NULL)
1488 err = got_error_from_errno("close");
1489 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1490 err = got_error_from_errno("fclose");
1494 static const struct got_error *
1495 collect_commitables(void *arg, unsigned char status,
1496 unsigned char staged_status, const char *relpath,
1497 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1498 struct got_object_id *commit_id, int dirfd, const char *de_name)
1500 struct collect_commitables_arg *a = arg;
1501 const struct got_error *err = NULL;
1502 struct got_commitable *ct = NULL;
1503 struct got_pathlist_entry *new = NULL;
1504 char *parent_path = NULL, *path = NULL;
1507 if (a->have_staged_files) {
1508 if (staged_status != GOT_STATUS_MODIFY &&
1509 staged_status != GOT_STATUS_ADD &&
1510 staged_status != GOT_STATUS_DELETE)
1513 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1514 printf("C %s\n", relpath);
1515 return got_error(GOT_ERR_COMMIT_CONFLICT);
1518 if (status != GOT_STATUS_MODIFY &&
1519 status != GOT_STATUS_MODE_CHANGE &&
1520 status != GOT_STATUS_ADD &&
1521 status != GOT_STATUS_DELETE &&
1522 status != GOT_STATUS_CONFLICT)
1526 if (asprintf(&path, "/%s", relpath) == -1) {
1527 err = got_error_from_errno("asprintf");
1530 if (strcmp(path, "/") == 0) {
1531 parent_path = strdup("");
1532 if (parent_path == NULL)
1533 return got_error_from_errno("strdup");
1535 err = got_path_dirname(&parent_path, path);
1540 ct = calloc(1, sizeof(*ct));
1542 err = got_error_from_errno("calloc");
1546 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1548 err = got_error_from_errno("asprintf");
1552 if (staged_status == GOT_STATUS_ADD ||
1553 staged_status == GOT_STATUS_MODIFY) {
1554 struct got_fileindex_entry *ie;
1555 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1556 switch (got_fileindex_entry_staged_filetype_get(ie)) {
1557 case GOT_FILEIDX_MODE_REGULAR_FILE:
1558 case GOT_FILEIDX_MODE_BAD_SYMLINK:
1561 case GOT_FILEIDX_MODE_SYMLINK:
1565 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1568 ct->mode |= got_fileindex_entry_perms_get(ie);
1569 } else if (status != GOT_STATUS_DELETE &&
1570 staged_status != GOT_STATUS_DELETE) {
1572 if (fstatat(dirfd, de_name, &sb,
1573 AT_SYMLINK_NOFOLLOW) == -1) {
1574 err = got_error_from_errno2("fstatat",
1578 } else if (lstat(ct->ondisk_path, &sb) == -1) {
1579 err = got_error_from_errno2("lstat", ct->ondisk_path);
1582 ct->mode = sb.st_mode;
1585 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1586 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1588 err = got_error_from_errno("asprintf");
1592 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1593 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1595 char target_path[PATH_MAX];
1597 target_len = readlink(ct->ondisk_path, target_path,
1598 sizeof(target_path));
1599 if (target_len == -1) {
1600 err = got_error_from_errno2("readlink",
1604 err = is_bad_symlink_target(&is_bad_symlink, target_path,
1605 target_len, ct->ondisk_path, a->worktree->root_path);
1608 if (is_bad_symlink) {
1609 err = got_error_path(ct->ondisk_path,
1610 GOT_ERR_BAD_SYMLINK);
1615 ct->status = status;
1616 ct->staged_status = staged_status;
1617 ct->blob_id = NULL; /* will be filled in when blob gets created */
1618 if (ct->status != GOT_STATUS_ADD &&
1619 ct->staged_status != GOT_STATUS_ADD) {
1620 ct->base_blob_id = got_object_id_dup(blob_id);
1621 if (ct->base_blob_id == NULL) {
1622 err = got_error_from_errno("got_object_id_dup");
1625 ct->base_commit_id = got_object_id_dup(commit_id);
1626 if (ct->base_commit_id == NULL) {
1627 err = got_error_from_errno("got_object_id_dup");
1631 if (ct->staged_status == GOT_STATUS_ADD ||
1632 ct->staged_status == GOT_STATUS_MODIFY) {
1633 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1634 if (ct->staged_blob_id == NULL) {
1635 err = got_error_from_errno("got_object_id_dup");
1639 ct->path = strdup(path);
1640 if (ct->path == NULL) {
1641 err = got_error_from_errno("strdup");
1644 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1648 if (a->diff_outfile && ct && new != NULL) {
1649 err = append_ct_diff(ct, &a->diff_header_shown,
1650 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1651 a->have_staged_files, a->repo, a->worktree);
1656 if (ct && (err || new == NULL))
1657 free_commitable(ct);
1663 static const struct got_error *write_tree(struct got_object_id **, int *,
1664 struct got_tree_object *, const char *, struct got_pathlist_head *,
1665 got_worktree_status_cb status_cb, void *status_arg,
1666 struct got_repository *);
1668 static const struct got_error *
1669 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1670 struct got_tree_entry *te, const char *parent_path,
1671 struct got_pathlist_head *commitable_paths,
1672 got_worktree_status_cb status_cb, void *status_arg,
1673 struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 struct got_tree_object *subtree;
1679 if (asprintf(&subpath, "%s%s%s", parent_path,
1680 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1681 return got_error_from_errno("asprintf");
1683 err = got_object_open_as_tree(&subtree, repo, &te->id);
1687 err = write_tree(new_subtree_id, nentries, subtree, subpath,
1688 commitable_paths, status_cb, status_arg, repo);
1689 got_object_tree_close(subtree);
1694 static const struct got_error *
1695 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1697 const struct got_error *err = NULL;
1698 char *ct_parent_path = NULL;
1702 if (strchr(ct->in_repo_path, '/') == NULL) {
1703 *match = got_path_is_root_dir(path);
1707 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1710 *match = (strcmp(path, ct_parent_path) == 0);
1711 free(ct_parent_path);
1716 get_ct_file_mode(struct got_commitable *ct)
1718 if (S_ISLNK(ct->mode))
1721 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1724 static const struct got_error *
1725 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1726 struct got_tree_entry *te, struct got_commitable *ct)
1728 const struct got_error *err = NULL;
1732 err = got_object_tree_entry_dup(new_te, te);
1736 (*new_te)->mode = get_ct_file_mode(ct);
1738 if (ct->staged_status == GOT_STATUS_MODIFY)
1739 memcpy(&(*new_te)->id, ct->staged_blob_id,
1740 sizeof((*new_te)->id));
1742 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1744 if (err && *new_te) {
1751 static const struct got_error *
1752 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1753 struct got_commitable *ct)
1755 const struct got_error *err = NULL;
1756 char *ct_name = NULL;
1760 *new_te = calloc(1, sizeof(**new_te));
1761 if (*new_te == NULL)
1762 return got_error_from_errno("calloc");
1764 err = got_path_basename(&ct_name, ct->path);
1767 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1768 sizeof((*new_te)->name)) {
1769 err = got_error(GOT_ERR_NO_SPACE);
1773 (*new_te)->mode = get_ct_file_mode(ct);
1775 if (ct->staged_status == GOT_STATUS_ADD)
1776 memcpy(&(*new_te)->id, ct->staged_blob_id,
1777 sizeof((*new_te)->id));
1779 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1782 if (err && *new_te) {
1789 static const struct got_error *
1790 insert_tree_entry(struct got_tree_entry *new_te,
1791 struct got_pathlist_head *paths)
1793 const struct got_error *err = NULL;
1794 struct got_pathlist_entry *new_pe;
1796 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1800 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1804 static const struct got_error *
1805 report_ct_status(struct got_commitable *ct,
1806 got_worktree_status_cb status_cb, void *status_arg)
1808 const char *ct_path = ct->path;
1809 unsigned char status;
1811 if (status_cb == NULL) /* no commit progress output desired */
1814 while (ct_path[0] == '/')
1817 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1818 status = ct->staged_status;
1820 status = ct->status;
1822 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1823 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1826 static const struct got_error *
1827 match_modified_subtree(int *modified, struct got_tree_entry *te,
1828 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1830 const struct got_error *err = NULL;
1831 struct got_pathlist_entry *pe;
1836 if (asprintf(&te_path, "%s%s%s", base_tree_path,
1837 got_path_is_root_dir(base_tree_path) ? "" : "/",
1839 return got_error_from_errno("asprintf");
1841 TAILQ_FOREACH(pe, commitable_paths, entry) {
1842 struct got_commitable *ct = pe->data;
1843 *modified = got_path_is_child(ct->in_repo_path, te_path,
1853 static const struct got_error *
1854 match_deleted_or_modified_ct(struct got_commitable **ctp,
1855 struct got_tree_entry *te, const char *base_tree_path,
1856 struct got_pathlist_head *commitable_paths)
1858 const struct got_error *err = NULL;
1859 struct got_pathlist_entry *pe;
1863 TAILQ_FOREACH(pe, commitable_paths, entry) {
1864 struct got_commitable *ct = pe->data;
1865 char *ct_name = NULL;
1868 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1869 if (ct->status != GOT_STATUS_MODIFY &&
1870 ct->status != GOT_STATUS_MODE_CHANGE &&
1871 ct->status != GOT_STATUS_DELETE &&
1872 ct->status != GOT_STATUS_CONFLICT)
1875 if (ct->staged_status != GOT_STATUS_MODIFY &&
1876 ct->staged_status != GOT_STATUS_DELETE)
1880 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1883 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1889 err = got_path_basename(&ct_name, pe->path);
1893 if (strcmp(te->name, ct_name) != 0) {
1906 static const struct got_error *
1907 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1908 const char *child_path, const char *path_base_tree,
1909 struct got_pathlist_head *commitable_paths,
1910 got_worktree_status_cb status_cb, void *status_arg,
1911 struct got_repository *repo)
1913 const struct got_error *err = NULL;
1914 struct got_tree_entry *new_te;
1916 struct got_object_id *id = NULL;
1921 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1922 got_path_is_root_dir(path_base_tree) ? "" : "/",
1924 return got_error_from_errno("asprintf");
1926 new_te = calloc(1, sizeof(*new_te));
1928 return got_error_from_errno("calloc");
1929 new_te->mode = S_IFDIR;
1931 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1932 sizeof(new_te->name)) {
1933 err = got_error(GOT_ERR_NO_SPACE);
1936 err = write_tree(&id, &nentries, NULL, subtree_path,
1937 commitable_paths, status_cb, status_arg, repo);
1942 memcpy(&new_te->id, id, sizeof(new_te->id));
1951 static const struct got_error *
1952 write_tree(struct got_object_id **new_tree_id, int *nentries,
1953 struct got_tree_object *base_tree, const char *path_base_tree,
1954 struct got_pathlist_head *commitable_paths,
1955 got_worktree_status_cb status_cb, void *status_arg,
1956 struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_pathlist_head paths;
1960 struct got_tree_entry *te, *new_te = NULL;
1961 struct got_pathlist_entry *pe;
1966 /* Insert, and recurse into, newly added entries first. */
1967 TAILQ_FOREACH(pe, commitable_paths, entry) {
1968 struct got_commitable *ct = pe->data;
1969 char *child_path = NULL, *slash;
1971 if ((ct->status != GOT_STATUS_ADD &&
1972 ct->staged_status != GOT_STATUS_ADD) ||
1973 (ct->flags & GOT_COMMITABLE_ADDED))
1976 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1977 strlen(path_base_tree)))
1980 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1985 slash = strchr(child_path, '/');
1986 if (slash == NULL) {
1987 err = alloc_added_blob_tree_entry(&new_te, ct);
1990 err = report_ct_status(ct, status_cb, status_arg);
1993 ct->flags |= GOT_COMMITABLE_ADDED;
1994 err = insert_tree_entry(new_te, &paths);
1999 *slash = '\0'; /* trim trailing path components */
2000 if (base_tree == NULL ||
2001 got_object_tree_find_entry(base_tree, child_path)
2003 err = make_subtree_for_added_blob(&new_te,
2004 child_path, path_base_tree,
2005 commitable_paths, status_cb, status_arg,
2009 err = insert_tree_entry(new_te, &paths);
2018 int i, nbase_entries;
2019 /* Handle modified and deleted entries. */
2020 nbase_entries = got_object_tree_get_nentries(base_tree);
2021 for (i = 0; i < nbase_entries; i++) {
2022 struct got_commitable *ct = NULL;
2024 te = got_object_tree_get_entry(base_tree, i);
2025 if (got_object_tree_entry_is_submodule(te)) {
2026 /* Entry is a submodule; just copy it. */
2027 err = got_object_tree_entry_dup(&new_te, te);
2030 err = insert_tree_entry(new_te, &paths);
2037 if (S_ISDIR(te->mode)) {
2039 err = got_object_tree_entry_dup(&new_te, te);
2042 err = match_modified_subtree(&modified, te,
2043 path_base_tree, commitable_paths);
2046 /* Avoid recursion into unmodified subtrees. */
2048 struct got_object_id *new_id;
2050 err = write_subtree(&new_id,
2052 path_base_tree, commitable_paths,
2053 status_cb, status_arg, repo);
2056 if (nsubentries == 0) {
2057 /* All entries were deleted. */
2061 memcpy(&new_te->id, new_id,
2062 sizeof(new_te->id));
2065 err = insert_tree_entry(new_te, &paths);
2072 err = match_deleted_or_modified_ct(&ct, te,
2073 path_base_tree, commitable_paths);
2077 /* NB: Deleted entries get dropped here. */
2078 if (ct->status == GOT_STATUS_MODIFY ||
2079 ct->status == GOT_STATUS_MODE_CHANGE ||
2080 ct->status == GOT_STATUS_CONFLICT ||
2081 ct->staged_status == GOT_STATUS_MODIFY) {
2082 err = alloc_modified_blob_tree_entry(
2086 err = insert_tree_entry(new_te, &paths);
2091 err = report_ct_status(ct, status_cb,
2096 /* Entry is unchanged; just copy it. */
2097 err = got_object_tree_entry_dup(&new_te, te);
2100 err = insert_tree_entry(new_te, &paths);
2108 /* Write new list of entries; deleted entries have been dropped. */
2109 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2111 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2115 static const struct got_error *
2116 update_fileindex_after_commit(struct got_worktree *worktree,
2117 struct got_pathlist_head *commitable_paths,
2118 struct got_object_id *new_base_commit_id,
2119 struct got_fileindex *fileindex, int have_staged_files)
2121 const struct got_error *err = NULL;
2122 struct got_pathlist_entry *pe;
2123 char *relpath = NULL;
2125 TAILQ_FOREACH(pe, commitable_paths, entry) {
2126 struct got_fileindex_entry *ie;
2127 struct got_commitable *ct = pe->data;
2129 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2131 err = got_path_skip_common_ancestor(&relpath,
2132 worktree->root_path, ct->ondisk_path);
2137 if (ct->status == GOT_STATUS_DELETE ||
2138 ct->staged_status == GOT_STATUS_DELETE) {
2139 got_fileindex_entry_remove(fileindex, ie);
2140 } else if (ct->staged_status == GOT_STATUS_ADD ||
2141 ct->staged_status == GOT_STATUS_MODIFY) {
2142 got_fileindex_entry_stage_set(ie,
2143 GOT_FILEIDX_STAGE_NONE);
2144 got_fileindex_entry_staged_filetype_set(ie, 0);
2146 err = got_fileindex_entry_update(ie,
2147 worktree->root_fd, relpath,
2148 ct->staged_blob_id->sha1,
2149 new_base_commit_id->sha1,
2150 !have_staged_files);
2152 err = got_fileindex_entry_update(ie,
2153 worktree->root_fd, relpath,
2155 new_base_commit_id->sha1,
2156 !have_staged_files);
2158 err = got_fileindex_entry_alloc(&ie, pe->path);
2161 err = got_fileindex_entry_update(ie,
2162 worktree->root_fd, relpath, ct->blob_id->sha1,
2163 new_base_commit_id->sha1, 1);
2165 got_fileindex_entry_free(ie);
2168 err = got_fileindex_entry_add(fileindex, ie);
2170 got_fileindex_entry_free(ie);
2182 static const struct got_error *
2183 check_out_of_date(const char *in_repo_path, unsigned char status,
2184 unsigned char staged_status, struct got_object_id *base_blob_id,
2185 struct got_object_id *base_commit_id,
2186 struct got_object_id *head_commit_id, struct got_repository *repo,
2189 const struct got_error *err = NULL;
2190 struct got_commit_object *commit = NULL;
2191 struct got_object_id *id = NULL;
2193 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2194 /* Trivial case: base commit == head commit */
2195 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2198 * Ensure file content which local changes were based
2199 * on matches file content in the branch head.
2201 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2204 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2206 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2207 err = got_error(ood_errcode);
2209 } else if (got_object_id_cmp(id, base_blob_id) != 0)
2210 err = got_error(ood_errcode);
2212 /* Require that added files don't exist in the branch head. */
2213 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2216 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2217 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2219 err = id ? got_error(ood_errcode) : NULL;
2224 got_object_commit_close(commit);
2228 static const struct got_error *
2229 commit_worktree(struct got_object_id **new_commit_id,
2230 struct got_pathlist_head *commitable_paths,
2231 struct got_object_id *head_commit_id,
2232 struct got_object_id *parent_id2,
2233 struct got_worktree *worktree,
2234 const char *author, const char *committer, char *diff_path,
2235 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2236 got_worktree_status_cb status_cb, void *status_arg,
2237 struct got_repository *repo)
2239 const struct got_error *err = NULL;
2240 struct got_pathlist_entry *pe;
2241 struct got_commit_object *head_commit = NULL;
2242 struct got_tree_object *head_tree = NULL;
2243 struct got_object_id *new_tree_id = NULL;
2244 int nentries, nparents = 0;
2245 struct got_object_id_queue parent_ids;
2246 struct got_object_qid *pid = NULL;
2247 char *logmsg = NULL;
2250 *new_commit_id = NULL;
2252 STAILQ_INIT(&parent_ids);
2254 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2258 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2262 if (commit_msg_cb != NULL) {
2263 err = commit_msg_cb(commitable_paths, diff_path,
2264 &logmsg, commit_arg);
2269 if (logmsg == NULL || strlen(logmsg) == 0) {
2270 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2274 /* Create blobs from added and modified files and record their IDs. */
2275 TAILQ_FOREACH(pe, commitable_paths, entry) {
2276 struct got_commitable *ct = pe->data;
2279 /* Blobs for staged files already exist. */
2280 if (ct->staged_status == GOT_STATUS_ADD ||
2281 ct->staged_status == GOT_STATUS_MODIFY)
2284 if (ct->status != GOT_STATUS_ADD &&
2285 ct->status != GOT_STATUS_MODIFY &&
2286 ct->status != GOT_STATUS_MODE_CHANGE &&
2287 ct->status != GOT_STATUS_CONFLICT)
2290 if (asprintf(&ondisk_path, "%s/%s",
2291 worktree->root_path, pe->path) == -1) {
2292 err = got_error_from_errno("asprintf");
2295 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2301 /* Recursively write new tree objects. */
2302 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2303 commitable_paths, status_cb, status_arg, repo);
2307 err = got_object_qid_alloc(&pid, head_commit_id);
2310 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2313 err = got_object_qid_alloc(&pid, parent_id2);
2316 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2319 timestamp = time(NULL);
2320 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2321 nparents, author, timestamp, committer, timestamp, logmsg, repo);
2327 got_object_id_queue_free(&parent_ids);
2329 got_object_tree_close(head_tree);
2331 got_object_commit_close(head_commit);
2335 static const struct got_error *
2336 check_path_is_commitable(const char *path,
2337 struct got_pathlist_head *commitable_paths)
2339 struct got_pathlist_entry *cpe = NULL;
2340 size_t path_len = strlen(path);
2342 TAILQ_FOREACH(cpe, commitable_paths, entry) {
2343 struct got_commitable *ct = cpe->data;
2344 const char *ct_path = ct->path;
2346 while (ct_path[0] == '/')
2349 if (strcmp(path, ct_path) == 0 ||
2350 got_path_is_child(ct_path, path, path_len))
2355 return got_error_path(path, GOT_ERR_BAD_PATH);
2360 static const struct got_error *
2361 check_staged_file(void *arg, struct got_fileindex_entry *ie)
2363 int *have_staged_files = arg;
2365 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2366 *have_staged_files = 1;
2367 return got_error(GOT_ERR_CANCELLED);
2373 static const struct got_error *
2374 check_non_staged_files(struct got_fileindex *fileindex,
2375 struct got_pathlist_head *paths)
2377 struct got_pathlist_entry *pe;
2378 struct got_fileindex_entry *ie;
2380 TAILQ_FOREACH(pe, paths, entry) {
2381 if (pe->path[0] == '\0')
2383 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2385 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2386 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2387 return got_error_path(pe->path,
2388 GOT_ERR_FILE_NOT_STAGED);
2395 print_load_info(int print_colored, int print_found, int print_trees,
2396 int ncolored, int nfound, int ntrees)
2398 if (print_colored) {
2399 printf("%d commit%s colored", ncolored,
2400 ncolored == 1 ? "" : "s");
2403 printf("%s%d object%s found",
2404 ncolored > 0 ? "; " : "",
2405 nfound, nfound == 1 ? "" : "s");
2408 printf("; %d tree%s scanned", ntrees,
2409 ntrees == 1 ? "" : "s");
2413 struct got_send_progress_arg {
2414 char last_scaled_packsize[FMT_SCALED_STRSIZE];
2421 int last_nobj_total;
2425 int printed_something;
2427 struct got_pathlist_head *delete_branches;
2430 static const struct got_error *
2431 send_progress(void *arg, int ncolored, int nfound, int ntrees,
2432 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2433 int nobj_written, off_t bytes_sent, const char *refname,
2434 const char *errmsg, int success)
2436 struct got_send_progress_arg *a = arg;
2437 char scaled_packsize[FMT_SCALED_STRSIZE];
2438 char scaled_sent[FMT_SCALED_STRSIZE];
2439 int p_deltify = 0, p_written = 0, p_sent = 0;
2440 int print_colored = 0, print_found = 0, print_trees = 0;
2441 int print_searching = 0, print_total = 0;
2442 int print_deltify = 0, print_written = 0, print_sent = 0;
2444 if (a->verbosity < 0)
2448 const char *status = success ? "accepted" : "rejected";
2451 struct got_pathlist_entry *pe;
2452 TAILQ_FOREACH(pe, a->delete_branches, entry) {
2453 const char *branchname = pe->path;
2454 if (got_path_cmp(branchname, refname,
2455 strlen(branchname), strlen(refname)) == 0) {
2457 a->sent_something = 1;
2463 if (a->printed_something)
2465 printf("Server has %s %s", status, refname);
2467 printf(": %s", errmsg);
2468 a->printed_something = 1;
2472 if (a->last_ncolored != ncolored) {
2474 a->last_ncolored = ncolored;
2477 if (a->last_nfound != nfound) {
2480 a->last_nfound = nfound;
2483 if (a->last_ntrees != ntrees) {
2487 a->last_ntrees = ntrees;
2490 if ((print_colored || print_found || print_trees) &&
2493 print_load_info(print_colored, print_found, print_trees,
2494 ncolored, nfound, ntrees);
2495 a->printed_something = 1;
2498 } else if (!a->loading_done) {
2500 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2502 a->loading_done = 1;
2505 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2506 return got_error_from_errno("fmt_scaled");
2507 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2508 return got_error_from_errno("fmt_scaled");
2510 if (a->last_ncommits != ncommits) {
2511 print_searching = 1;
2512 a->last_ncommits = ncommits;
2515 if (a->last_nobj_total != nobj_total) {
2516 print_searching = 1;
2518 a->last_nobj_total = nobj_total;
2521 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2522 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2523 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2524 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2525 return got_error(GOT_ERR_NO_SPACE);
2528 if (nobj_deltify > 0 || nobj_written > 0) {
2529 if (nobj_deltify > 0) {
2530 p_deltify = (nobj_deltify * 100) / nobj_total;
2531 if (p_deltify != a->last_p_deltify) {
2532 a->last_p_deltify = p_deltify;
2533 print_searching = 1;
2538 if (nobj_written > 0) {
2539 p_written = (nobj_written * 100) / nobj_total;
2540 if (p_written != a->last_p_written) {
2541 a->last_p_written = p_written;
2542 print_searching = 1;
2550 if (bytes_sent > 0) {
2551 p_sent = (bytes_sent * 100) / packfile_size;
2552 if (p_sent != a->last_p_sent) {
2553 a->last_p_sent = p_sent;
2554 print_searching = 1;
2560 a->sent_something = 1;
2563 if (print_searching || print_total || print_deltify || print_written ||
2566 if (print_searching)
2567 printf("packing %d reference%s", ncommits,
2568 ncommits == 1 ? "" : "s");
2570 printf("; %d object%s", nobj_total,
2571 nobj_total == 1 ? "" : "s");
2573 printf("; deltify: %d%%", p_deltify);
2575 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2576 scaled_packsize, p_sent);
2577 else if (print_written)
2578 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2579 scaled_packsize, p_written);
2580 if (print_searching || print_total || print_deltify ||
2581 print_written || print_sent) {
2582 a->printed_something = 1;
2588 struct got_fetch_progress_arg {
2589 char last_scaled_size[FMT_SCALED_STRSIZE];
2591 int last_p_resolved;
2594 struct got_repository *repo;
2597 static const struct got_error *
2598 fetch_progress(void *arg, const char *message, off_t packfile_size,
2599 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2601 struct got_fetch_progress_arg *a = arg;
2602 char scaled_size[FMT_SCALED_STRSIZE];
2603 int p_indexed, p_resolved;
2604 int print_size = 0, print_indexed = 0, print_resolved = 0;
2606 if (a->verbosity < 0)
2609 if (message && message[0] != '\0') {
2610 printf("\rserver: %s", message);
2615 if (packfile_size > 0 || nobj_indexed > 0) {
2616 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2617 (a->last_scaled_size[0] == '\0' ||
2618 strcmp(scaled_size, a->last_scaled_size)) != 0) {
2620 if (strlcpy(a->last_scaled_size, scaled_size,
2621 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2622 return got_error(GOT_ERR_NO_SPACE);
2624 if (nobj_indexed > 0) {
2625 p_indexed = (nobj_indexed * 100) / nobj_total;
2626 if (p_indexed != a->last_p_indexed) {
2627 a->last_p_indexed = p_indexed;
2632 if (nobj_resolved > 0) {
2633 p_resolved = (nobj_resolved * 100) /
2634 (nobj_total - nobj_loose);
2635 if (p_resolved != a->last_p_resolved) {
2636 a->last_p_resolved = p_resolved;
2644 if (print_size || print_indexed || print_resolved)
2647 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2649 printf("; indexing %d%%", p_indexed);
2651 printf("; resolving deltas %d%%", p_resolved);
2652 if (print_size || print_indexed || print_resolved) {
2660 static const struct got_error *
2661 create_symref(const char *refname, struct got_reference *target_ref,
2662 int verbosity, struct got_repository *repo)
2664 const struct got_error *err;
2665 struct got_reference *head_symref;
2667 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2671 err = got_ref_write(head_symref, repo);
2672 if (err == NULL && verbosity > 0) {
2673 printf("Created reference %s: %s\n", GOT_REF_HEAD,
2674 got_ref_get_name(target_ref));
2676 got_ref_close(head_symref);
2680 static const struct got_error *
2681 create_ref(const char *refname, struct got_object_id *id,
2682 int verbosity, struct got_repository *repo)
2684 const struct got_error *err = NULL;
2685 struct got_reference *ref;
2688 err = got_object_id_str(&id_str, id);
2692 err = got_ref_alloc(&ref, refname, id);
2696 err = got_ref_write(ref, repo);
2699 if (err == NULL && verbosity >= 0)
2700 printf("Created reference %s: %s\n", refname, id_str);
2706 static const struct got_error *
2707 update_ref(struct got_reference *ref, struct got_object_id *new_id,
2708 int verbosity, struct got_repository *repo)
2710 const struct got_error *err = NULL;
2711 char *new_id_str = NULL;
2712 struct got_object_id *old_id = NULL;
2714 err = got_object_id_str(&new_id_str, new_id);
2718 if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2719 err = got_ref_resolve(&old_id, repo, ref);
2722 if (got_object_id_cmp(old_id, new_id) == 0)
2724 if (verbosity >= 0) {
2725 printf("Rejecting update of existing tag %s: %s\n",
2726 got_ref_get_name(ref), new_id_str);
2731 if (got_ref_is_symbolic(ref)) {
2732 if (verbosity >= 0) {
2733 printf("Replacing reference %s: %s\n",
2734 got_ref_get_name(ref),
2735 got_ref_get_symref_target(ref));
2737 err = got_ref_change_symref_to_ref(ref, new_id);
2740 err = got_ref_write(ref, repo);
2744 err = got_ref_resolve(&old_id, repo, ref);
2747 if (got_object_id_cmp(old_id, new_id) == 0)
2750 err = got_ref_change_ref(ref, new_id);
2753 err = got_ref_write(ref, repo);
2759 printf("Updated %s: %s\n", got_ref_get_name(ref),
2767 static const struct got_error *
2768 fetch_updated_remote(const char *proto, const char *host, const char *port,
2769 const char *server_path, int verbosity,
2770 const struct got_remote_repo *remote, struct got_repository *repo,
2771 struct got_reference *head_ref, const char *head_refname)
2773 const struct got_error *err = NULL, *unlock_err = NULL;
2774 struct got_pathlist_entry *pe;
2775 struct got_pathlist_head learned_refs;
2776 struct got_pathlist_head symrefs;
2777 struct got_pathlist_head wanted_branches;
2778 struct got_pathlist_head wanted_refs;
2779 struct got_object_id *pack_hash;
2780 struct got_fetch_progress_arg fpa;
2782 pid_t fetchpid = -1;
2784 TAILQ_INIT(&learned_refs);
2785 TAILQ_INIT(&symrefs);
2786 TAILQ_INIT(&wanted_branches);
2787 TAILQ_INIT(&wanted_refs);
2789 err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2794 err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2795 port, server_path, verbosity);
2799 fpa.last_scaled_size[0] = '\0';
2800 fpa.last_p_indexed = -1;
2801 fpa.last_p_resolved = -1;
2802 fpa.verbosity = verbosity;
2805 err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2806 remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2807 fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2811 /* Update references provided with the pack file. */
2812 TAILQ_FOREACH(pe, &learned_refs, entry) {
2813 const char *refname = pe->path;
2814 struct got_object_id *id = pe->data;
2815 struct got_reference *ref;
2817 err = got_ref_open(&ref, repo, refname, 0);
2819 if (err->code != GOT_ERR_NOT_REF)
2821 err = create_ref(refname, id, verbosity, repo);
2825 err = update_ref(ref, id, verbosity, repo);
2826 unlock_err = got_ref_unlock(ref);
2827 if (unlock_err && err == NULL)
2835 /* Set the HEAD reference if the server provided one. */
2836 TAILQ_FOREACH(pe, &symrefs, entry) {
2837 struct got_reference *target_ref;
2838 const char *refname = pe->path;
2839 const char *target = pe->data;
2840 char *remote_refname = NULL, *remote_target = NULL;
2842 if (strcmp(refname, GOT_REF_HEAD) != 0)
2845 err = got_ref_open(&target_ref, repo, target, 0);
2847 if (err->code == GOT_ERR_NOT_REF) {
2854 err = create_symref(refname, target_ref, verbosity, repo);
2855 got_ref_close(target_ref);
2859 if (remote->mirror_references)
2862 if (strncmp("refs/heads/", target, 11) != 0)
2865 if (asprintf(&remote_refname,
2866 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2868 err = got_error_from_errno("asprintf");
2871 if (asprintf(&remote_target,
2872 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2873 target + 11) == -1) {
2874 err = got_error_from_errno("asprintf");
2875 free(remote_refname);
2878 err = got_ref_open(&target_ref, repo, remote_target, 0);
2880 free(remote_refname);
2881 free(remote_target);
2882 if (err->code == GOT_ERR_NOT_REF) {
2888 err = create_symref(remote_refname, target_ref,
2889 verbosity - 1, repo);
2890 free(remote_refname);
2891 free(remote_target);
2892 got_ref_close(target_ref);
2898 got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2899 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2900 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2901 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2906 const struct got_error *
2907 got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2908 struct got_worktree *worktree, struct got_pathlist_head *paths,
2909 const char *author, const char *committer, int allow_bad_symlinks,
2910 int show_diff, int commit_conflicts,
2911 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2912 got_worktree_status_cb status_cb, void *status_arg,
2913 const char *proto, const char *host, const char *port,
2914 const char *server_path, int verbosity,
2915 const struct got_remote_repo *remote,
2916 got_cancel_cb check_cancelled,
2917 struct got_repository *repo)
2919 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2920 struct got_fileindex *fileindex = NULL;
2921 char *fileindex_path = NULL;
2922 struct got_pathlist_head commitable_paths;
2923 struct collect_commitables_arg cc_arg;
2924 struct got_pathlist_entry *pe;
2925 struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2926 struct got_reference *commit_ref = NULL;
2927 struct got_object_id *head_commit_id = NULL;
2928 struct got_object_id *head_commit_id2 = NULL;
2929 char *head_refname = NULL;
2930 char *commit_refname = NULL;
2931 char *diff_path = NULL;
2932 int have_staged_files = 0;
2935 struct got_send_progress_arg spa;
2936 struct got_pathlist_head commit_reflist;
2937 struct got_pathlist_head tag_names;
2938 struct got_pathlist_head delete_branches;
2940 *new_commit_id = NULL;
2942 memset(&cc_arg, 0, sizeof(cc_arg));
2943 TAILQ_INIT(&commitable_paths);
2944 TAILQ_INIT(&commit_reflist);
2945 TAILQ_INIT(&tag_names);
2946 TAILQ_INIT(&delete_branches);
2948 err = lock_worktree(worktree, LOCK_EX);
2952 err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2957 head_refname = worktree->head_ref_name;
2958 err = got_ref_open(&head_ref, repo, head_refname, 0);
2961 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2965 err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2968 err = got_ref_write(commit_ref, repo);
2972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2976 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2977 &have_staged_files);
2978 if (err && err->code != GOT_ERR_CANCELLED)
2980 if (have_staged_files) {
2981 err = check_non_staged_files(fileindex, paths);
2986 cc_arg.commitable_paths = &commitable_paths;
2987 cc_arg.worktree = worktree;
2988 cc_arg.fileindex = fileindex;
2990 cc_arg.have_staged_files = have_staged_files;
2991 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2992 cc_arg.diff_header_shown = 0;
2993 cc_arg.commit_conflicts = commit_conflicts;
2995 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2996 GOT_TMPDIR_STR "/got", ".diff");
2999 cc_arg.f1 = got_opentemp();
3000 if (cc_arg.f1 == NULL) {
3001 err = got_error_from_errno("got_opentemp");
3004 cc_arg.f2 = got_opentemp();
3005 if (cc_arg.f2 == NULL) {
3006 err = got_error_from_errno("got_opentemp");
3011 TAILQ_FOREACH(pe, paths, entry) {
3012 err = worktree_status(worktree, pe->path, fileindex, repo,
3013 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3019 if (fflush(cc_arg.diff_outfile) == EOF) {
3020 err = got_error_from_errno("fflush");
3025 if (TAILQ_EMPTY(&commitable_paths)) {
3026 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3030 TAILQ_FOREACH(pe, paths, entry) {
3031 err = check_path_is_commitable(pe->path, &commitable_paths);
3036 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3037 struct got_commitable *ct = pe->data;
3038 const char *ct_path = ct->in_repo_path;
3040 while (ct_path[0] == '/')
3042 err = check_out_of_date(ct_path, ct->status,
3043 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3044 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3049 err = commit_worktree(new_commit_id, &commitable_paths,
3050 head_commit_id, NULL, worktree, author, committer,
3051 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3052 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3057 * Check if a concurrent commit to our branch has occurred.
3058 * Lock the reference here to prevent concurrent modification.
3060 err = got_ref_open(&head_ref2, repo, head_refname, 1);
3063 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3066 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3067 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3071 err = got_pathlist_append(&commit_reflist, commit_refname,
3076 /* Update commit ref in repository. */
3077 err = got_ref_change_ref(commit_ref, *new_commit_id);
3080 err = got_ref_write(commit_ref, repo);
3084 if (verbosity >= 0) {
3085 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3086 remote->name, proto, host,
3087 port ? ":" : "", port ? port : "",
3088 *server_path == '/' ? "" : "/", server_path);
3091 /* Attempt send to remote branch. */
3092 err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3093 server_path, verbosity);
3097 memset(&spa, 0, sizeof(spa));
3098 spa.last_scaled_packsize[0] = '\0';
3099 spa.last_p_deltify = -1;
3100 spa.last_p_written = -1;
3101 spa.verbosity = verbosity;
3102 spa.delete_branches = &delete_branches;
3103 err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3104 &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3105 check_cancelled, NULL);
3106 if (spa.printed_something)
3108 if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3110 * Fetch new changes since remote has diverged.
3111 * No trivial-rebase yet; require update to be run manually.
3113 err = fetch_updated_remote(proto, host, port, server_path,
3114 verbosity, remote, repo, head_ref, head_refname);
3117 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3119 /* XXX: Rebase commit over fetched remote branch. */
3125 /* Update branch head in repository. */
3126 err = got_ref_change_ref(head_ref2, *new_commit_id);
3129 err = got_ref_write(head_ref2, repo);
3133 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3137 err = ref_base_commit(worktree, repo);
3141 /* XXX: fileindex must be updated for other fetched changes? */
3142 err = update_fileindex_after_commit(worktree, &commitable_paths,
3143 *new_commit_id, fileindex, have_staged_files);
3144 sync_err = sync_fileindex(fileindex, fileindex_path);
3145 if (sync_err && err == NULL)
3149 unlockerr = got_ref_unlock(head_ref2);
3150 if (unlockerr && err == NULL)
3152 got_ref_close(head_ref2);
3155 got_ref_close(commit_ref);
3157 got_fileindex_free(fileindex);
3158 unlockerr = lock_worktree(worktree, LOCK_SH);
3159 if (unlockerr && err == NULL)
3161 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3162 struct got_commitable *ct = pe->data;
3164 free_commitable(ct);
3166 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3167 if (diff_path && unlink(diff_path) == -1 && err == NULL)
3168 err = got_error_from_errno2("unlink", diff_path);
3169 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3171 err = got_error_from_errno("fclose");
3172 free(head_commit_id);
3173 free(head_commit_id2);
3174 free(commit_refname);
3175 free(fileindex_path);
3180 const struct got_error *
3181 got_worktree_cvg_get_commit_ref_name(char **refname,
3182 struct got_worktree *worktree)
3184 return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);