Blob


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