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 <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_worktree_cvg.h"
47 #include "got_opentemp.h"
48 #include "got_diff.h"
49 #include "got_send.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"
63 #ifndef MIN
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #endif
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)
72 {
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)));
77 return NULL;
78 }
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)
83 {
84 const struct got_error *err = NULL;
85 char canonpath[PATH_MAX];
86 char *path_got = NULL;
88 *is_bad_symlink = 0;
90 if (target_len >= sizeof(canonpath)) {
91 *is_bad_symlink = 1;
92 return NULL;
93 }
95 /*
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.
101 */
102 if (!got_path_is_absolute(target_path)) {
103 char *abspath, *parent;
104 err = got_path_dirname(&parent, ondisk_path);
105 if (err)
106 return err;
107 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
108 free(parent);
109 return got_error_from_errno("asprintf");
111 free(parent);
112 if (strlen(abspath) >= sizeof(canonpath)) {
113 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
114 free(abspath);
115 return err;
117 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
118 free(abspath);
119 if (err)
120 return err;
121 } else {
122 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
123 if (err)
124 return err;
127 /* Only allow symlinks pointing at paths within the work tree. */
128 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
129 *is_bad_symlink = 1;
130 return NULL;
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)))
138 *is_bad_symlink = 1;
140 free(path_got);
141 return NULL;
144 /*
145 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
146 * conflict marker is found in newly added lines only.
147 */
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,
151 FILE *ondisk_file)
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
158 };
159 FILE *f1 = NULL;
160 struct got_diffreg_result *diffreg_result = NULL;
161 struct diff_result *r;
162 int nchunks_parsed, n, i = 0, ln = 0;
163 char *line = NULL;
164 size_t linesize = 0;
165 ssize_t linelen;
167 if (*status != GOT_STATUS_MODIFY)
168 return NULL;
170 f1 = got_opentemp();
171 if (f1 == NULL)
172 return got_error_from_errno("got_opentemp");
174 if (blob) {
175 got_object_blob_rewind(blob);
176 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
177 if (err)
178 goto done;
181 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
182 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
183 if (err)
184 goto done;
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 = {};
191 off_t pos;
193 /*
194 * We can optimise a little by advancing straight
195 * to the next chunk if this one has no added lines.
196 */
197 c = diff_chunk_get(r, n);
199 if (diff_chunk_type(c) != CHUNK_PLUS) {
200 nchunks_parsed = 1;
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);
207 goto done;
210 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
211 ln = cc.right.start;
213 while (ln < cc.right.end) {
214 linelen = getline(&line, &linesize, ondisk_file);
215 if (linelen == -1) {
216 if (feof(ondisk_file))
217 break;
218 err = got_ferror(ondisk_file, GOT_ERR_IO);
219 break;
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;
227 goto done;
228 } else
229 i++;
231 ++ln;
235 done:
236 free(line);
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);
240 if (err == NULL)
241 err = free_err;
243 return err;
246 static int
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));
253 static int
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));
264 static unsigned char
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;
274 default:
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];
287 ssize_t elen;
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. */
295 do {
296 err = got_object_blob_read_block(&len, blob);
297 if (err)
298 return err;
299 if (len + target_len >= sizeof(target_path)) {
300 /*
301 * Should not happen. The blob contents were OK
302 * when this symlink was installed.
303 */
304 return got_error(GOT_ERR_NO_SPACE);
306 if (len > 0) {
307 /* Skip blob object header first time around. */
308 memcpy(target_path + target_len, buf + hdrlen,
309 len - hdrlen);
310 target_len += len - hdrlen;
311 hdrlen = 0;
313 } while (len != 0);
314 target_path[target_len] = '\0';
316 if (dirfd != -1) {
317 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
318 if (elen == -1)
319 return got_error_from_errno2("readlinkat", abspath);
320 } else {
321 elen = readlink(abspath, etarget, sizeof(etarget));
322 if (elen == -1)
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;
329 return NULL;
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;
339 size_t hdrlen;
340 int fd = -1, fd1 = -1;
341 FILE *f = NULL;
342 uint8_t fbuf[8192];
343 struct got_blob_object *blob = NULL;
344 size_t flen, blen;
345 unsigned char staged_status;
347 staged_status = get_staged_status(ie);
348 *status = GOT_STATUS_NO_CHANGE;
349 memset(sb, 0, sizeof(*sb));
351 /*
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.
355 */
356 if (dirfd != -1) {
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;
361 else
362 *status = GOT_STATUS_DELETE;
363 goto done;
365 err = got_error_from_errno2("fstatat", abspath);
366 goto done;
368 } else {
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;
380 else
381 *status = GOT_STATUS_DELETE;
382 goto done;
384 err = got_error_from_errno2("fstat", abspath);
385 goto done;
389 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
390 *status = GOT_STATUS_OBSTRUCTED;
391 goto done;
394 if (!got_fileindex_entry_has_file_on_disk(ie)) {
395 *status = GOT_STATUS_DELETE;
396 goto done;
397 } else if (!got_fileindex_entry_has_blob(ie) &&
398 staged_status != GOT_STATUS_ADD) {
399 *status = GOT_STATUS_ADD;
400 goto done;
403 if (!stat_info_differs(ie, sb))
404 goto done;
406 if (S_ISLNK(sb->st_mode) &&
407 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
408 *status = GOT_STATUS_MODIFY;
409 goto done;
412 if (staged_status == GOT_STATUS_MODIFY ||
413 staged_status == GOT_STATUS_ADD)
414 got_fileindex_entry_get_staged_blob_id(&id, ie);
415 else
416 got_fileindex_entry_get_blob_id(&id, ie);
418 fd1 = got_opentempfd();
419 if (fd1 == -1) {
420 err = got_error_from_errno("got_opentempfd");
421 goto done;
423 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
424 if (err)
425 goto done;
427 if (S_ISLNK(sb->st_mode)) {
428 err = get_symlink_modification_status(status, ie,
429 abspath, dirfd, de_name, blob);
430 goto done;
433 if (dirfd != -1) {
434 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
435 if (fd == -1) {
436 err = got_error_from_errno2("openat", abspath);
437 goto done;
441 f = fdopen(fd, "r");
442 if (f == NULL) {
443 err = got_error_from_errno2("fdopen", abspath);
444 goto done;
446 fd = -1;
447 hdrlen = got_object_blob_get_hdrlen(blob);
448 for (;;) {
449 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
450 err = got_object_blob_read_block(&blen, blob);
451 if (err)
452 goto done;
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");
457 goto done;
459 if (blen - hdrlen == 0) {
460 if (flen != 0)
461 *status = GOT_STATUS_MODIFY;
462 break;
463 } else if (flen == 0) {
464 if (blen - hdrlen != 0)
465 *status = GOT_STATUS_MODIFY;
466 break;
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;
471 break;
473 } else {
474 *status = GOT_STATUS_MODIFY;
475 break;
477 hdrlen = 0;
480 if (*status == GOT_STATUS_MODIFY) {
481 rewind(f);
482 err = get_modified_file_content_status(status, blob, ie->path,
483 sb, f);
484 } else if (xbit_differs(ie, sb->st_mode))
485 *status = GOT_STATUS_MODE_CHANGE;
486 done:
487 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
488 err = got_error_from_errno("close");
489 if (blob)
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);
495 return err;
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;
504 *refname = NULL;
506 err = got_worktree_get_uuid(&uuidstr, worktree);
507 if (err)
508 return err;
510 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
511 err = got_error_from_errno("asprintf");
512 *refname = NULL;
514 free(uuidstr);
515 return err;
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);
524 /*
525 * Prevent Git's garbage collector from deleting our base commit by
526 * setting a reference to our base commit's ID.
527 */
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;
533 char *refname;
535 err = get_base_ref_name(&refname, worktree);
536 if (err)
537 return err;
539 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
540 if (err)
541 goto done;
543 err = got_ref_write(ref, repo);
544 done:
545 free(refname);
546 if (ref)
547 got_ref_close(ref);
548 return err;
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;
561 return err;
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;
569 FILE *index = 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);
577 if (err)
578 goto done;
580 index = fopen(*fileindex_path, "rbe");
581 if (index == NULL) {
582 if (errno != ENOENT)
583 err = got_error_from_errno2("fopen", *fileindex_path);
584 } else {
585 err = got_fileindex_read(*fileindex, index);
586 if (fclose(index) == EOF && err == NULL)
587 err = got_error_from_errno("fclose");
589 done:
590 if (err) {
591 free(*fileindex_path);
592 *fileindex_path = NULL;
593 got_fileindex_free(*fileindex);
594 *fileindex = NULL;
596 return err;
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,
608 fileindex_path, "");
609 if (err)
610 goto done;
612 err = got_fileindex_write(fileindex, new_index);
613 if (err)
614 goto done;
616 if (rename(new_fileindex_path, fileindex_path) != 0) {
617 err = got_error_from_errno3("rename", new_fileindex_path,
618 fileindex_path);
619 unlink(new_fileindex_path);
622 /*
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.
626 */
627 timeout.tv_sec = 0;
628 timeout.tv_nsec = 1;
629 nanosleep(&timeout, NULL);
630 done:
631 if (new_index)
632 fclose(new_index);
633 free(new_fileindex_path);
634 return err;
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;
644 void *status_arg;
645 got_cancel_cb cancel_cb;
646 void *cancel_arg;
647 /* A pathlist containing per-directory pathlists of ignore patterns. */
648 struct got_pathlist_head *ignores;
649 int report_unchanged;
650 int no_ignores;
651 };
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;
662 struct stat sb;
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);
669 if (err)
670 return err;
672 if (status == GOT_STATUS_NO_CHANGE &&
673 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
674 return NULL;
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;
696 char *abspath;
698 if (a->cancel_cb) {
699 err = a->cancel_cb(a->cancel_arg);
700 if (err)
701 return err;
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))
707 return NULL;
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");
713 } else {
714 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
715 de->d_name) == -1)
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);
721 free(abspath);
722 return err;
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;
733 if (a->cancel_cb) {
734 err = a->cancel_cb(a->cancel_arg);
735 if (err)
736 return err;
739 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
740 return NULL;
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;
746 else
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);
752 static void
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;
772 size_t linesize = 0;
773 ssize_t linelen;
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. */
785 if (line[0] == '#')
786 continue;
788 /* Git's negated patterns are not (yet?) supported. */
789 if (line[0] == '!')
790 continue;
792 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
793 line) == -1) {
794 err = got_error_from_errno("asprintf");
795 goto done;
797 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
798 if (err)
799 goto done;
801 if (ferror(f)) {
802 err = got_error_from_errno("getline");
803 goto done;
806 dirpath = strdup(path);
807 if (dirpath == NULL) {
808 err = got_error_from_errno("strdup");
809 goto done;
811 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
812 done:
813 free(line);
814 if (err || pe == NULL) {
815 free(dirpath);
816 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
818 return err;
821 static int
822 match_path(const char *pattern, size_t pattern_len, const char *path,
823 int flags)
825 char buf[PATH_MAX];
827 /*
828 * Trailing slashes signify directories.
829 * Append a * to make such patterns conform to fnmatch rules.
830 */
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);
841 static int
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) {
852 const char *p;
854 if (pi->path_len < 3 ||
855 strncmp(pi->path, "**/", 3) != 0)
856 continue;
857 p = path;
858 while (*p) {
859 if (match_path(pi->path + 3,
860 pi->path_len - 3, p,
861 FNM_PATHNAME | FNM_LEADING_DIR)) {
862 /* Retry in next directory. */
863 while (*p && *p != '/')
864 p++;
865 while (*p == '/')
866 p++;
867 continue;
869 return 1;
874 /*
875 * The ignores pathlist contains ignore lists from children before
876 * parents, so we can find the most specific ignorelist by walking
877 * ignores backwards.
878 */
879 pe = TAILQ_LAST(ignores, got_pathlist_head);
880 while (pe) {
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,
889 path, flags))
890 continue;
891 return 1;
894 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
897 return 0;
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;
905 char *ignorespath;
906 int fd = -1;
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");
913 if (dirfd != -1) {
914 fd = openat(dirfd, ignores_filename,
915 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
916 if (fd == -1) {
917 if (errno != ENOENT && errno != EACCES)
918 err = got_error_from_errno2("openat",
919 ignorespath);
920 } else {
921 ignoresfile = fdopen(fd, "r");
922 if (ignoresfile == NULL)
923 err = got_error_from_errno2("fdopen",
924 ignorespath);
925 else {
926 fd = -1;
927 err = read_ignores(ignores, path, ignoresfile);
930 } else {
931 ignoresfile = fopen(ignorespath, "re");
932 if (ignoresfile == NULL) {
933 if (errno != ENOENT && errno != EACCES)
934 err = got_error_from_errno2("fopen",
935 ignorespath);
936 } else
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);
944 free(ignorespath);
945 return err;
948 static const struct got_error *
949 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
950 int dirfd)
952 const struct got_error *err = NULL;
953 struct diff_dir_cb_arg *a = arg;
954 char *path = NULL;
956 if (ignore != NULL)
957 *ignore = 0;
959 if (a->cancel_cb) {
960 err = a->cancel_cb(a->cancel_arg);
961 if (err)
962 return err;
965 if (parent_path[0]) {
966 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
967 return got_error_from_errno("asprintf");
968 } else {
969 path = de->d_name;
972 if (de->d_type == DT_DIR) {
973 if (!a->no_ignores && ignore != NULL &&
974 match_ignores(a->ignores, path))
975 *ignore = 1;
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);
980 if (parent_path[0])
981 free(path);
982 return err;
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;
991 if (a->no_ignores)
992 return NULL;
994 err = add_ignores(a->ignores, a->worktree->root_path,
995 path, dirfd, ".cvsignore");
996 if (err)
997 return err;
999 err = add_ignores(a->ignores, a->worktree->root_path, path,
1000 dirfd, ".gitignore");
1002 return err;
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;
1012 struct stat sb;
1014 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1015 if (ie)
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))
1027 return NULL;
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);
1033 return 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,
1044 ".cvsignore");
1045 if (err)
1046 return err;
1048 err = add_ignores(ignores, root_path, "", -1,
1049 ".gitignore");
1050 if (err)
1051 return err;
1053 err = got_path_dirname(&parent_path, path);
1054 if (err) {
1055 if (err->code == GOT_ERR_BAD_PATH)
1056 return NULL; /* cannot traverse parent */
1057 return err;
1059 for (;;) {
1060 err = add_ignores(ignores, root_path, parent_path, -1,
1061 ".cvsignore");
1062 if (err)
1063 break;
1064 err = add_ignores(ignores, root_path, parent_path, -1,
1065 ".gitignore");
1066 if (err)
1067 break;
1068 err = got_path_dirname(&next_parent_path, parent_path);
1069 if (err) {
1070 if (err->code == GOT_ERR_BAD_PATH)
1071 err = NULL; /* traversed everything */
1072 break;
1074 if (got_path_is_root_dir(parent_path))
1075 break;
1076 free(parent_path);
1077 parent_path = next_parent_path;
1078 next_parent_path = NULL;
1081 free(parent_path);
1082 free(next_parent_path);
1083 return err;
1086 struct find_missing_children_args {
1087 const char *parent_path;
1088 size_t parent_len;
1089 struct got_pathlist_head *children;
1090 got_cancel_cb cancel_cb;
1091 void *cancel_arg;
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;
1100 if (a->cancel_cb) {
1101 err = a->cancel_cb(a->cancel_arg);
1102 if (err)
1103 return err;
1106 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1107 err = got_pathlist_append(a->children, ie->path, NULL);
1109 return err;
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) {
1125 if (cancel_cb) {
1126 err = cancel_cb(cancel_arg);
1127 if (err)
1128 break;
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");
1134 ondisk_path = NULL;
1135 break;
1138 err = report_single_file_status(pe->path, ondisk_path,
1139 fileindex, status_cb, status_arg, repo, report_unchanged,
1140 ignores, no_ignores);
1141 if (err)
1142 break;
1144 free(ondisk_path);
1145 ondisk_path = NULL;
1148 free(ondisk_path);
1149 return err;
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;
1160 int fd = -1;
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));
1175 if (ie) {
1176 err = report_single_file_status(path, ondisk_path,
1177 fileindex, status_cb, status_arg, repo,
1178 report_unchanged, &ignores, no_ignores);
1179 goto done;
1180 } else {
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);
1189 if (err)
1190 goto done;
1193 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1194 if (fd == -1) {
1195 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1196 !got_err_open_nofollow_on_symlink())
1197 err = got_error_from_errno2("open", ondisk_path);
1198 else {
1199 if (!no_ignores) {
1200 err = add_ignores_from_parent_paths(&ignores,
1201 worktree->root_path, ondisk_path);
1202 if (err)
1203 goto done;
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);
1210 if (err)
1211 goto done;
1212 } else {
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);
1219 if (err)
1220 goto done;
1223 } else {
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);
1232 arg.repo = repo;
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;
1239 if (!no_ignores) {
1240 err = add_ignores_from_parent_paths(&ignores,
1241 worktree->root_path, path);
1242 if (err)
1243 goto done;
1245 arg.ignores = &ignores;
1246 err = got_fileindex_diff_dir(fileindex, fd,
1247 worktree->root_path, path, repo, &fdiff_cb, &arg);
1249 done:
1250 free_ignores(&ignores);
1251 if (fd != -1 && close(fd) == -1 && err == NULL)
1252 err = got_error_from_errno("close");
1253 free(ondisk_path);
1254 return err;
1257 static void
1258 free_commitable(struct got_commitable *ct)
1260 free(ct->path);
1261 free(ct->in_repo_path);
1262 free(ct->ondisk_path);
1263 free(ct->blob_id);
1264 free(ct->base_blob_id);
1265 free(ct->staged_blob_id);
1266 free(ct->base_commit_id);
1267 free(ct);
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;
1279 FILE *diff_outfile;
1280 FILE *f1;
1281 FILE *f2;
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;
1296 *fd = -1;
1298 if (dirfd != -1) {
1299 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1300 if (target_len == -1)
1301 return got_error_from_errno2("readlinkat", abspath);
1302 } else {
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();
1309 if (*fd == -1)
1310 return got_error_from_errno("got_opentempfd");
1312 outlen = write(*fd, target_path, target_len);
1313 if (outlen == -1) {
1314 err = got_error_from_errno("got_opentempfd");
1315 goto done;
1318 if (lseek(*fd, 0, SEEK_SET) == -1) {
1319 err = got_error_from_errno2("lseek", abspath);
1320 goto done;
1322 done:
1323 if (err) {
1324 close(*fd);
1325 *fd = -1;
1327 return err;
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;
1340 struct stat sb;
1341 off_t size1 = 0;
1342 int f2_exists = 0;
1343 char *id_str = NULL;
1345 memset(&sb, 0, sizeof(sb));
1347 if (diff_staged) {
1348 if (ct->staged_status != GOT_STATUS_MODIFY &&
1349 ct->staged_status != GOT_STATUS_ADD &&
1350 ct->staged_status != GOT_STATUS_DELETE)
1351 return NULL;
1352 } else {
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)
1357 return NULL;
1360 err = got_opentemp_truncate(f1);
1361 if (err)
1362 return got_error_from_errno("got_opentemp_truncate");
1363 err = got_opentemp_truncate(f2);
1364 if (err)
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);
1369 if (err)
1370 return err;
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;
1380 if (diff_staged) {
1381 const char *label1 = NULL, *label2 = NULL;
1382 switch (ct->staged_status) {
1383 case GOT_STATUS_MODIFY:
1384 label1 = ct->path;
1385 label2 = ct->path;
1386 break;
1387 case GOT_STATUS_ADD:
1388 label2 = ct->path;
1389 break;
1390 case GOT_STATUS_DELETE:
1391 label1 = ct->path;
1392 break;
1393 default:
1394 return got_error(GOT_ERR_FILE_STATUS);
1396 fd1 = got_opentempfd();
1397 if (fd1 == -1) {
1398 err = got_error_from_errno("got_opentempfd");
1399 goto done;
1401 fd2 = got_opentempfd();
1402 if (fd2 == -1) {
1403 err = got_error_from_errno("got_opentempfd");
1404 goto done;
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);
1410 goto done;
1413 fd1 = got_opentempfd();
1414 if (fd1 == -1) {
1415 err = got_error_from_errno("got_opentempfd");
1416 goto done;
1419 if (ct->status != GOT_STATUS_ADD) {
1420 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1421 8192, fd1);
1422 if (err)
1423 goto done;
1426 if (ct->status != GOT_STATUS_DELETE) {
1427 if (dirfd != -1) {
1428 fd = openat(dirfd, de_name,
1429 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1430 if (fd == -1) {
1431 if (!got_err_open_nofollow_on_symlink()) {
1432 err = got_error_from_errno2("openat",
1433 ct->ondisk_path);
1434 goto done;
1436 err = get_symlink_target_file(&fd, dirfd,
1437 de_name, ct->ondisk_path);
1438 if (err)
1439 goto done;
1441 } else {
1442 fd = open(ct->ondisk_path,
1443 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1444 if (fd == -1) {
1445 if (!got_err_open_nofollow_on_symlink()) {
1446 err = got_error_from_errno2("open",
1447 ct->ondisk_path);
1448 goto done;
1450 err = get_symlink_target_file(&fd, dirfd,
1451 de_name, ct->ondisk_path);
1452 if (err)
1453 goto done;
1456 if (fstatat(fd, ct->ondisk_path, &sb,
1457 AT_SYMLINK_NOFOLLOW) == -1) {
1458 err = got_error_from_errno2("fstatat", ct->ondisk_path);
1459 goto done;
1461 ondisk_file = fdopen(fd, "r");
1462 if (ondisk_file == NULL) {
1463 err = got_error_from_errno2("fdopen", ct->ondisk_path);
1464 goto done;
1466 fd = -1;
1467 f2_exists = 1;
1470 if (blob1) {
1471 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1472 f1, blob1);
1473 if (err)
1474 goto done;
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);
1480 done:
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");
1485 if (blob1)
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");
1491 return err;
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;
1505 struct stat sb;
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)
1511 return NULL;
1512 } else {
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)
1523 return NULL;
1526 if (asprintf(&path, "/%s", relpath) == -1) {
1527 err = got_error_from_errno("asprintf");
1528 goto done;
1530 if (strcmp(path, "/") == 0) {
1531 parent_path = strdup("");
1532 if (parent_path == NULL)
1533 return got_error_from_errno("strdup");
1534 } else {
1535 err = got_path_dirname(&parent_path, path);
1536 if (err)
1537 return err;
1540 ct = calloc(1, sizeof(*ct));
1541 if (ct == NULL) {
1542 err = got_error_from_errno("calloc");
1543 goto done;
1546 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1547 relpath) == -1) {
1548 err = got_error_from_errno("asprintf");
1549 goto done;
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:
1559 ct->mode = S_IFREG;
1560 break;
1561 case GOT_FILEIDX_MODE_SYMLINK:
1562 ct->mode = S_IFLNK;
1563 break;
1564 default:
1565 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1566 goto done;
1568 ct->mode |= got_fileindex_entry_perms_get(ie);
1569 } else if (status != GOT_STATUS_DELETE &&
1570 staged_status != GOT_STATUS_DELETE) {
1571 if (dirfd != -1) {
1572 if (fstatat(dirfd, de_name, &sb,
1573 AT_SYMLINK_NOFOLLOW) == -1) {
1574 err = got_error_from_errno2("fstatat",
1575 ct->ondisk_path);
1576 goto done;
1578 } else if (lstat(ct->ondisk_path, &sb) == -1) {
1579 err = got_error_from_errno2("lstat", ct->ondisk_path);
1580 goto done;
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) ? "" : "/",
1587 relpath) == -1) {
1588 err = got_error_from_errno("asprintf");
1589 goto done;
1592 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1593 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1594 int is_bad_symlink;
1595 char target_path[PATH_MAX];
1596 ssize_t target_len;
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",
1601 ct->ondisk_path);
1602 goto done;
1604 err = is_bad_symlink_target(&is_bad_symlink, target_path,
1605 target_len, ct->ondisk_path, a->worktree->root_path);
1606 if (err)
1607 goto done;
1608 if (is_bad_symlink) {
1609 err = got_error_path(ct->ondisk_path,
1610 GOT_ERR_BAD_SYMLINK);
1611 goto done;
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");
1623 goto done;
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");
1628 goto done;
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");
1636 goto done;
1639 ct->path = strdup(path);
1640 if (ct->path == NULL) {
1641 err = got_error_from_errno("strdup");
1642 goto done;
1644 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1645 if (err)
1646 goto done;
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);
1652 if (err)
1653 goto done;
1655 done:
1656 if (ct && (err || new == NULL))
1657 free_commitable(ct);
1658 free(parent_path);
1659 free(path);
1660 return err;
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;
1677 char *subpath;
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);
1684 if (err)
1685 return err;
1687 err = write_tree(new_subtree_id, nentries, subtree, subpath,
1688 commitable_paths, status_cb, status_arg, repo);
1689 got_object_tree_close(subtree);
1690 free(subpath);
1691 return err;
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;
1700 *match = 0;
1702 if (strchr(ct->in_repo_path, '/') == NULL) {
1703 *match = got_path_is_root_dir(path);
1704 return NULL;
1707 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1708 if (err)
1709 return err;
1710 *match = (strcmp(path, ct_parent_path) == 0);
1711 free(ct_parent_path);
1712 return err;
1715 static mode_t
1716 get_ct_file_mode(struct got_commitable *ct)
1718 if (S_ISLNK(ct->mode))
1719 return S_IFLNK;
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;
1730 *new_te = NULL;
1732 err = got_object_tree_entry_dup(new_te, te);
1733 if (err)
1734 goto done;
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));
1741 else
1742 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1743 done:
1744 if (err && *new_te) {
1745 free(*new_te);
1746 *new_te = NULL;
1748 return err;
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;
1758 *new_te = 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);
1765 if (err)
1766 goto done;
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);
1770 goto done;
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));
1778 else
1779 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1780 done:
1781 free(ct_name);
1782 if (err && *new_te) {
1783 free(*new_te);
1784 *new_te = NULL;
1786 return err;
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);
1797 if (err)
1798 return err;
1799 if (new_pe == NULL)
1800 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1801 return NULL;
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 */
1812 return NULL;
1814 while (ct_path[0] == '/')
1815 ct_path++;
1817 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1818 status = ct->staged_status;
1819 else
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;
1832 char *te_path;
1834 *modified = 0;
1836 if (asprintf(&te_path, "%s%s%s", base_tree_path,
1837 got_path_is_root_dir(base_tree_path) ? "" : "/",
1838 te->name) == -1)
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,
1844 strlen(te_path));
1845 if (*modified)
1846 break;
1849 free(te_path);
1850 return err;
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;
1861 *ctp = NULL;
1863 TAILQ_FOREACH(pe, commitable_paths, entry) {
1864 struct got_commitable *ct = pe->data;
1865 char *ct_name = NULL;
1866 int path_matches;
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)
1873 continue;
1874 } else {
1875 if (ct->staged_status != GOT_STATUS_MODIFY &&
1876 ct->staged_status != GOT_STATUS_DELETE)
1877 continue;
1880 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1881 continue;
1883 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1884 if (err)
1885 return err;
1886 if (!path_matches)
1887 continue;
1889 err = got_path_basename(&ct_name, pe->path);
1890 if (err)
1891 return err;
1893 if (strcmp(te->name, ct_name) != 0) {
1894 free(ct_name);
1895 continue;
1897 free(ct_name);
1899 *ctp = ct;
1900 break;
1903 return err;
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;
1915 char *subtree_path;
1916 struct got_object_id *id = NULL;
1917 int nentries;
1919 *new_tep = NULL;
1921 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1922 got_path_is_root_dir(path_base_tree) ? "" : "/",
1923 child_path) == -1)
1924 return got_error_from_errno("asprintf");
1926 new_te = calloc(1, sizeof(*new_te));
1927 if (new_te == NULL)
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);
1934 goto done;
1936 err = write_tree(&id, &nentries, NULL, subtree_path,
1937 commitable_paths, status_cb, status_arg, repo);
1938 if (err) {
1939 free(new_te);
1940 goto done;
1942 memcpy(&new_te->id, id, sizeof(new_te->id));
1943 done:
1944 free(id);
1945 free(subtree_path);
1946 if (err == NULL)
1947 *new_tep = new_te;
1948 return err;
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;
1963 TAILQ_INIT(&paths);
1964 *nentries = 0;
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))
1974 continue;
1976 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1977 strlen(path_base_tree)))
1978 continue;
1980 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1981 ct->in_repo_path);
1982 if (err)
1983 goto done;
1985 slash = strchr(child_path, '/');
1986 if (slash == NULL) {
1987 err = alloc_added_blob_tree_entry(&new_te, ct);
1988 if (err)
1989 goto done;
1990 err = report_ct_status(ct, status_cb, status_arg);
1991 if (err)
1992 goto done;
1993 ct->flags |= GOT_COMMITABLE_ADDED;
1994 err = insert_tree_entry(new_te, &paths);
1995 if (err)
1996 goto done;
1997 (*nentries)++;
1998 } else {
1999 *slash = '\0'; /* trim trailing path components */
2000 if (base_tree == NULL ||
2001 got_object_tree_find_entry(base_tree, child_path)
2002 == NULL) {
2003 err = make_subtree_for_added_blob(&new_te,
2004 child_path, path_base_tree,
2005 commitable_paths, status_cb, status_arg,
2006 repo);
2007 if (err)
2008 goto done;
2009 err = insert_tree_entry(new_te, &paths);
2010 if (err)
2011 goto done;
2012 (*nentries)++;
2017 if (base_tree) {
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);
2028 if (err)
2029 goto done;
2030 err = insert_tree_entry(new_te, &paths);
2031 if (err)
2032 goto done;
2033 (*nentries)++;
2034 continue;
2037 if (S_ISDIR(te->mode)) {
2038 int modified;
2039 err = got_object_tree_entry_dup(&new_te, te);
2040 if (err)
2041 goto done;
2042 err = match_modified_subtree(&modified, te,
2043 path_base_tree, commitable_paths);
2044 if (err)
2045 goto done;
2046 /* Avoid recursion into unmodified subtrees. */
2047 if (modified) {
2048 struct got_object_id *new_id;
2049 int nsubentries;
2050 err = write_subtree(&new_id,
2051 &nsubentries, te,
2052 path_base_tree, commitable_paths,
2053 status_cb, status_arg, repo);
2054 if (err)
2055 goto done;
2056 if (nsubentries == 0) {
2057 /* All entries were deleted. */
2058 free(new_id);
2059 continue;
2061 memcpy(&new_te->id, new_id,
2062 sizeof(new_te->id));
2063 free(new_id);
2065 err = insert_tree_entry(new_te, &paths);
2066 if (err)
2067 goto done;
2068 (*nentries)++;
2069 continue;
2072 err = match_deleted_or_modified_ct(&ct, te,
2073 path_base_tree, commitable_paths);
2074 if (err)
2075 goto done;
2076 if (ct) {
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(
2083 &new_te, te, ct);
2084 if (err)
2085 goto done;
2086 err = insert_tree_entry(new_te, &paths);
2087 if (err)
2088 goto done;
2089 (*nentries)++;
2091 err = report_ct_status(ct, status_cb,
2092 status_arg);
2093 if (err)
2094 goto done;
2095 } else {
2096 /* Entry is unchanged; just copy it. */
2097 err = got_object_tree_entry_dup(&new_te, te);
2098 if (err)
2099 goto done;
2100 err = insert_tree_entry(new_te, &paths);
2101 if (err)
2102 goto done;
2103 (*nentries)++;
2108 /* Write new list of entries; deleted entries have been dropped. */
2109 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2110 done:
2111 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2112 return err;
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);
2133 if (err)
2134 goto done;
2136 if (ie) {
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);
2151 } else
2152 err = got_fileindex_entry_update(ie,
2153 worktree->root_fd, relpath,
2154 ct->blob_id->sha1,
2155 new_base_commit_id->sha1,
2156 !have_staged_files);
2157 } else {
2158 err = got_fileindex_entry_alloc(&ie, pe->path);
2159 if (err)
2160 goto done;
2161 err = got_fileindex_entry_update(ie,
2162 worktree->root_fd, relpath, ct->blob_id->sha1,
2163 new_base_commit_id->sha1, 1);
2164 if (err) {
2165 got_fileindex_entry_free(ie);
2166 goto done;
2168 err = got_fileindex_entry_add(fileindex, ie);
2169 if (err) {
2170 got_fileindex_entry_free(ie);
2171 goto done;
2174 free(relpath);
2175 relpath = NULL;
2177 done:
2178 free(relpath);
2179 return err;
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,
2187 int ood_errcode)
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)
2196 return NULL;
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);
2202 if (err)
2203 goto done;
2204 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2205 if (err) {
2206 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2207 err = got_error(ood_errcode);
2208 goto done;
2209 } else if (got_object_id_cmp(id, base_blob_id) != 0)
2210 err = got_error(ood_errcode);
2211 } else {
2212 /* Require that added files don't exist in the branch head. */
2213 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2214 if (err)
2215 goto done;
2216 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2217 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2218 goto done;
2219 err = id ? got_error(ood_errcode) : NULL;
2221 done:
2222 free(id);
2223 if (commit)
2224 got_object_commit_close(commit);
2225 return err;
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;
2248 time_t timestamp;
2250 *new_commit_id = NULL;
2252 STAILQ_INIT(&parent_ids);
2254 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2255 if (err)
2256 goto done;
2258 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2259 if (err)
2260 goto done;
2262 if (commit_msg_cb != NULL) {
2263 err = commit_msg_cb(commitable_paths, diff_path,
2264 &logmsg, commit_arg);
2265 if (err)
2266 goto done;
2269 if (logmsg == NULL || strlen(logmsg) == 0) {
2270 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2271 goto done;
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;
2277 char *ondisk_path;
2279 /* Blobs for staged files already exist. */
2280 if (ct->staged_status == GOT_STATUS_ADD ||
2281 ct->staged_status == GOT_STATUS_MODIFY)
2282 continue;
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)
2288 continue;
2290 if (asprintf(&ondisk_path, "%s/%s",
2291 worktree->root_path, pe->path) == -1) {
2292 err = got_error_from_errno("asprintf");
2293 goto done;
2295 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2296 free(ondisk_path);
2297 if (err)
2298 goto done;
2301 /* Recursively write new tree objects. */
2302 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2303 commitable_paths, status_cb, status_arg, repo);
2304 if (err)
2305 goto done;
2307 err = got_object_qid_alloc(&pid, head_commit_id);
2308 if (err)
2309 goto done;
2310 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2311 nparents++;
2312 if (parent_id2) {
2313 err = got_object_qid_alloc(&pid, parent_id2);
2314 if (err)
2315 goto done;
2316 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2317 nparents++;
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);
2322 if (logmsg != NULL)
2323 free(logmsg);
2324 if (err)
2325 goto done;
2326 done:
2327 got_object_id_queue_free(&parent_ids);
2328 if (head_tree)
2329 got_object_tree_close(head_tree);
2330 if (head_commit)
2331 got_object_commit_close(head_commit);
2332 return err;
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] == '/')
2347 ct_path++;
2349 if (strcmp(path, ct_path) == 0 ||
2350 got_path_is_child(ct_path, path, path_len))
2351 break;
2354 if (cpe == NULL)
2355 return got_error_path(path, GOT_ERR_BAD_PATH);
2357 return NULL;
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);
2370 return NULL;
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')
2382 continue;
2383 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2384 if (ie == NULL)
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);
2391 return NULL;
2394 static void
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");
2402 if (print_found) {
2403 printf("%s%d object%s found",
2404 ncolored > 0 ? "; " : "",
2405 nfound, nfound == 1 ? "" : "s");
2407 if (print_trees) {
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];
2415 int verbosity;
2416 int last_ncolored;
2417 int last_nfound;
2418 int last_ntrees;
2419 int loading_done;
2420 int last_ncommits;
2421 int last_nobj_total;
2422 int last_p_deltify;
2423 int last_p_written;
2424 int last_p_sent;
2425 int printed_something;
2426 int sent_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)
2445 return NULL;
2447 if (refname) {
2448 const char *status = success ? "accepted" : "rejected";
2450 if (success) {
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) {
2456 status = "deleted";
2457 a->sent_something = 1;
2458 break;
2463 if (a->printed_something)
2464 putchar('\n');
2465 printf("Server has %s %s", status, refname);
2466 if (errmsg)
2467 printf(": %s", errmsg);
2468 a->printed_something = 1;
2469 return NULL;
2472 if (a->last_ncolored != ncolored) {
2473 print_colored = 1;
2474 a->last_ncolored = ncolored;
2477 if (a->last_nfound != nfound) {
2478 print_colored = 1;
2479 print_found = 1;
2480 a->last_nfound = nfound;
2483 if (a->last_ntrees != ntrees) {
2484 print_colored = 1;
2485 print_found = 1;
2486 print_trees = 1;
2487 a->last_ntrees = ntrees;
2490 if ((print_colored || print_found || print_trees) &&
2491 !a->loading_done) {
2492 printf("\r");
2493 print_load_info(print_colored, print_found, print_trees,
2494 ncolored, nfound, ntrees);
2495 a->printed_something = 1;
2496 fflush(stdout);
2497 return NULL;
2498 } else if (!a->loading_done) {
2499 printf("\r");
2500 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2501 printf("\n");
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;
2517 print_total = 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;
2534 print_total = 1;
2535 print_deltify = 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;
2543 print_total = 1;
2544 print_deltify = 1;
2545 print_written = 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;
2555 print_total = 1;
2556 print_deltify = 1;
2557 print_written = 1;
2558 print_sent = 1;
2560 a->sent_something = 1;
2563 if (print_searching || print_total || print_deltify || print_written ||
2564 print_sent)
2565 printf("\r");
2566 if (print_searching)
2567 printf("packing %d reference%s", ncommits,
2568 ncommits == 1 ? "" : "s");
2569 if (print_total)
2570 printf("; %d object%s", nobj_total,
2571 nobj_total == 1 ? "" : "s");
2572 if (print_deltify)
2573 printf("; deltify: %d%%", p_deltify);
2574 if (print_sent)
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;
2583 fflush(stdout);
2585 return NULL;
2588 struct got_fetch_progress_arg {
2589 char last_scaled_size[FMT_SCALED_STRSIZE];
2590 int last_p_indexed;
2591 int last_p_resolved;
2592 int verbosity;
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)
2607 return NULL;
2609 if (message && message[0] != '\0') {
2610 printf("\rserver: %s", message);
2611 fflush(stdout);
2612 return NULL;
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) {
2619 print_size = 1;
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;
2628 print_indexed = 1;
2629 print_size = 1;
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;
2637 print_resolved = 1;
2638 print_indexed = 1;
2639 print_size = 1;
2644 if (print_size || print_indexed || print_resolved)
2645 printf("\r");
2646 if (print_size)
2647 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2648 if (print_indexed)
2649 printf("; indexing %d%%", p_indexed);
2650 if (print_resolved)
2651 printf("; resolving deltas %d%%", p_resolved);
2652 if (print_size || print_indexed || print_resolved) {
2653 putchar('\n');
2654 fflush(stdout);
2657 return NULL;
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);
2668 if (err)
2669 return err;
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);
2677 return err;
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;
2686 char *id_str;
2688 err = got_object_id_str(&id_str, id);
2689 if (err)
2690 return err;
2692 err = got_ref_alloc(&ref, refname, id);
2693 if (err)
2694 goto done;
2696 err = got_ref_write(ref, repo);
2697 got_ref_close(ref);
2699 if (err == NULL && verbosity >= 0)
2700 printf("Created reference %s: %s\n", refname, id_str);
2701 done:
2702 free(id_str);
2703 return err;
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);
2715 if (err)
2716 goto done;
2718 if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2719 err = got_ref_resolve(&old_id, repo, ref);
2720 if (err)
2721 goto done;
2722 if (got_object_id_cmp(old_id, new_id) == 0)
2723 goto done;
2724 if (verbosity >= 0) {
2725 printf("Rejecting update of existing tag %s: %s\n",
2726 got_ref_get_name(ref), new_id_str);
2728 goto done;
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);
2738 if (err)
2739 goto done;
2740 err = got_ref_write(ref, repo);
2741 if (err)
2742 goto done;
2743 } else {
2744 err = got_ref_resolve(&old_id, repo, ref);
2745 if (err)
2746 goto done;
2747 if (got_object_id_cmp(old_id, new_id) == 0)
2748 goto done;
2750 err = got_ref_change_ref(ref, new_id);
2751 if (err)
2752 goto done;
2753 err = got_ref_write(ref, repo);
2754 if (err)
2755 goto done;
2758 if (verbosity >= 0)
2759 printf("Updated %s: %s\n", got_ref_get_name(ref),
2760 new_id_str);
2761 done:
2762 free(old_id);
2763 free(new_id_str);
2764 return err;
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;
2781 int fetchfd = -1;
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,
2790 NULL);
2791 if (err)
2792 goto done;
2794 err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2795 port, server_path, verbosity);
2796 if (err)
2797 goto done;
2799 fpa.last_scaled_size[0] = '\0';
2800 fpa.last_p_indexed = -1;
2801 fpa.last_p_resolved = -1;
2802 fpa.verbosity = verbosity;
2803 fpa.repo = repo;
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);
2808 if (err)
2809 goto done;
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);
2818 if (err) {
2819 if (err->code != GOT_ERR_NOT_REF)
2820 goto done;
2821 err = create_ref(refname, id, verbosity, repo);
2822 if (err)
2823 goto done;
2824 } else {
2825 err = update_ref(ref, id, verbosity, repo);
2826 unlock_err = got_ref_unlock(ref);
2827 if (unlock_err && err == NULL)
2828 err = unlock_err;
2829 got_ref_close(ref);
2830 if (err)
2831 goto done;
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)
2843 continue;
2845 err = got_ref_open(&target_ref, repo, target, 0);
2846 if (err) {
2847 if (err->code == GOT_ERR_NOT_REF) {
2848 err = NULL;
2849 continue;
2851 goto done;
2854 err = create_symref(refname, target_ref, verbosity, repo);
2855 got_ref_close(target_ref);
2856 if (err)
2857 goto done;
2859 if (remote->mirror_references)
2860 continue;
2862 if (strncmp("refs/heads/", target, 11) != 0)
2863 continue;
2865 if (asprintf(&remote_refname,
2866 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2867 refname) == -1) {
2868 err = got_error_from_errno("asprintf");
2869 goto done;
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);
2876 goto done;
2878 err = got_ref_open(&target_ref, repo, remote_target, 0);
2879 if (err) {
2880 free(remote_refname);
2881 free(remote_target);
2882 if (err->code == GOT_ERR_NOT_REF) {
2883 err = NULL;
2884 continue;
2886 goto done;
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);
2893 if (err)
2894 goto done;
2897 done:
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);
2902 return err;
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;
2933 int sendfd = -1;
2934 pid_t sendpid = -1;
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);
2949 if (err)
2950 goto done;
2952 err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2953 worktree);
2954 if (err)
2955 goto done;
2957 head_refname = worktree->head_ref_name;
2958 err = got_ref_open(&head_ref, repo, head_refname, 0);
2959 if (err)
2960 goto done;
2961 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2962 if (err)
2963 goto done;
2965 err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2966 if (err)
2967 goto done;
2968 err = got_ref_write(commit_ref, repo);
2969 if (err)
2970 goto done;
2972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2973 if (err)
2974 goto done;
2976 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2977 &have_staged_files);
2978 if (err && err->code != GOT_ERR_CANCELLED)
2979 goto done;
2980 if (have_staged_files) {
2981 err = check_non_staged_files(fileindex, paths);
2982 if (err)
2983 goto done;
2986 cc_arg.commitable_paths = &commitable_paths;
2987 cc_arg.worktree = worktree;
2988 cc_arg.fileindex = fileindex;
2989 cc_arg.repo = repo;
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;
2994 if (show_diff) {
2995 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2996 GOT_TMPDIR_STR "/got", ".diff");
2997 if (err)
2998 goto done;
2999 cc_arg.f1 = got_opentemp();
3000 if (cc_arg.f1 == NULL) {
3001 err = got_error_from_errno("got_opentemp");
3002 goto done;
3004 cc_arg.f2 = got_opentemp();
3005 if (cc_arg.f2 == NULL) {
3006 err = got_error_from_errno("got_opentemp");
3007 goto done;
3011 TAILQ_FOREACH(pe, paths, entry) {
3012 err = worktree_status(worktree, pe->path, fileindex, repo,
3013 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3014 if (err)
3015 goto done;
3018 if (show_diff) {
3019 if (fflush(cc_arg.diff_outfile) == EOF) {
3020 err = got_error_from_errno("fflush");
3021 goto done;
3025 if (TAILQ_EMPTY(&commitable_paths)) {
3026 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3027 goto done;
3030 TAILQ_FOREACH(pe, paths, entry) {
3031 err = check_path_is_commitable(pe->path, &commitable_paths);
3032 if (err)
3033 goto done;
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] == '/')
3041 ct_path++;
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);
3045 if (err)
3046 goto done;
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);
3053 if (err)
3054 goto done;
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);
3061 if (err)
3062 goto done;
3063 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3064 if (err)
3065 goto done;
3066 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3067 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3068 goto done;
3071 err = got_pathlist_append(&commit_reflist, commit_refname,
3072 head_refname);
3073 if (err)
3074 goto done;
3076 /* Update commit ref in repository. */
3077 err = got_ref_change_ref(commit_ref, *new_commit_id);
3078 if (err)
3079 goto done;
3080 err = got_ref_write(commit_ref, repo);
3081 if (err)
3082 goto done;
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);
3094 if (err)
3095 goto done;
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)
3107 putchar('\n');
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);
3115 if (err == NULL)
3116 goto done;
3117 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3118 goto done;
3119 /* XXX: Rebase commit over fetched remote branch. */
3121 if (err) {
3122 goto done;
3125 /* Update branch head in repository. */
3126 err = got_ref_change_ref(head_ref2, *new_commit_id);
3127 if (err)
3128 goto done;
3129 err = got_ref_write(head_ref2, repo);
3130 if (err)
3131 goto done;
3133 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3134 if (err)
3135 goto done;
3137 err = ref_base_commit(worktree, repo);
3138 if (err)
3139 goto done;
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)
3146 err = sync_err;
3147 done:
3148 if (head_ref2) {
3149 unlockerr = got_ref_unlock(head_ref2);
3150 if (unlockerr && err == NULL)
3151 err = unlockerr;
3152 got_ref_close(head_ref2);
3154 if (commit_ref)
3155 got_ref_close(commit_ref);
3156 if (fileindex)
3157 got_fileindex_free(fileindex);
3158 unlockerr = lock_worktree(worktree, LOCK_SH);
3159 if (unlockerr && err == NULL)
3160 err = unlockerr;
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 &&
3170 err == NULL)
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);
3176 free(diff_path);
3177 return err;
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);