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 && a->cancel_cb(a->cancel_arg))
699 return got_error(GOT_ERR_CANCELLED);
701 if (got_path_cmp(parent_path, a->status_path,
702 strlen(parent_path), a->status_path_len) != 0 &&
703 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
704 return NULL;
706 if (parent_path[0]) {
707 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
708 parent_path, de->d_name) == -1)
709 return got_error_from_errno("asprintf");
710 } else {
711 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
712 de->d_name) == -1)
713 return got_error_from_errno("asprintf");
716 err = report_file_status(ie, abspath, dirfd, de->d_name,
717 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
718 free(abspath);
719 return err;
722 static const struct got_error *
723 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
725 struct diff_dir_cb_arg *a = arg;
726 struct got_object_id blob_id, commit_id;
727 unsigned char status;
729 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
730 return got_error(GOT_ERR_CANCELLED);
732 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
733 return NULL;
735 got_fileindex_entry_get_blob_id(&blob_id, ie);
736 got_fileindex_entry_get_commit_id(&commit_id, ie);
737 if (got_fileindex_entry_has_file_on_disk(ie))
738 status = GOT_STATUS_MISSING;
739 else
740 status = GOT_STATUS_DELETE;
741 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
742 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
745 static void
746 free_ignores(struct got_pathlist_head *ignores)
748 struct got_pathlist_entry *pe;
750 TAILQ_FOREACH(pe, ignores, entry) {
751 struct got_pathlist_head *ignorelist = pe->data;
753 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
755 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
758 static const struct got_error *
759 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
761 const struct got_error *err = NULL;
762 struct got_pathlist_entry *pe = NULL;
763 struct got_pathlist_head *ignorelist;
764 char *line = NULL, *pattern, *dirpath = NULL;
765 size_t linesize = 0;
766 ssize_t linelen;
768 ignorelist = calloc(1, sizeof(*ignorelist));
769 if (ignorelist == NULL)
770 return got_error_from_errno("calloc");
771 TAILQ_INIT(ignorelist);
773 while ((linelen = getline(&line, &linesize, f)) != -1) {
774 if (linelen > 0 && line[linelen - 1] == '\n')
775 line[linelen - 1] = '\0';
777 /* Git's ignores may contain comments. */
778 if (line[0] == '#')
779 continue;
781 /* Git's negated patterns are not (yet?) supported. */
782 if (line[0] == '!')
783 continue;
785 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
786 line) == -1) {
787 err = got_error_from_errno("asprintf");
788 goto done;
790 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
791 if (err)
792 goto done;
794 if (ferror(f)) {
795 err = got_error_from_errno("getline");
796 goto done;
799 dirpath = strdup(path);
800 if (dirpath == NULL) {
801 err = got_error_from_errno("strdup");
802 goto done;
804 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
805 done:
806 free(line);
807 if (err || pe == NULL) {
808 free(dirpath);
809 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
811 return err;
814 static int
815 match_path(const char *pattern, size_t pattern_len, const char *path,
816 int flags)
818 char buf[PATH_MAX];
820 /*
821 * Trailing slashes signify directories.
822 * Append a * to make such patterns conform to fnmatch rules.
823 */
824 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
825 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
826 return FNM_NOMATCH; /* XXX */
828 return fnmatch(buf, path, flags);
831 return fnmatch(pattern, path, flags);
834 static int
835 match_ignores(struct got_pathlist_head *ignores, const char *path)
837 struct got_pathlist_entry *pe;
839 /* Handle patterns which match in all directories. */
840 TAILQ_FOREACH(pe, ignores, entry) {
841 struct got_pathlist_head *ignorelist = pe->data;
842 struct got_pathlist_entry *pi;
844 TAILQ_FOREACH(pi, ignorelist, entry) {
845 const char *p;
847 if (pi->path_len < 3 ||
848 strncmp(pi->path, "**/", 3) != 0)
849 continue;
850 p = path;
851 while (*p) {
852 if (match_path(pi->path + 3,
853 pi->path_len - 3, p,
854 FNM_PATHNAME | FNM_LEADING_DIR)) {
855 /* Retry in next directory. */
856 while (*p && *p != '/')
857 p++;
858 while (*p == '/')
859 p++;
860 continue;
862 return 1;
867 /*
868 * The ignores pathlist contains ignore lists from children before
869 * parents, so we can find the most specific ignorelist by walking
870 * ignores backwards.
871 */
872 pe = TAILQ_LAST(ignores, got_pathlist_head);
873 while (pe) {
874 if (got_path_is_child(path, pe->path, pe->path_len)) {
875 struct got_pathlist_head *ignorelist = pe->data;
876 struct got_pathlist_entry *pi;
877 TAILQ_FOREACH(pi, ignorelist, entry) {
878 int flags = FNM_LEADING_DIR;
879 if (strstr(pi->path, "/**/") == NULL)
880 flags |= FNM_PATHNAME;
881 if (match_path(pi->path, pi->path_len,
882 path, flags))
883 continue;
884 return 1;
887 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
890 return 0;
893 static const struct got_error *
894 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
895 const char *path, int dirfd, const char *ignores_filename)
897 const struct got_error *err = NULL;
898 char *ignorespath;
899 int fd = -1;
900 FILE *ignoresfile = NULL;
902 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
903 path[0] ? "/" : "", ignores_filename) == -1)
904 return got_error_from_errno("asprintf");
906 if (dirfd != -1) {
907 fd = openat(dirfd, ignores_filename,
908 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
909 if (fd == -1) {
910 if (errno != ENOENT && errno != EACCES)
911 err = got_error_from_errno2("openat",
912 ignorespath);
913 } else {
914 ignoresfile = fdopen(fd, "r");
915 if (ignoresfile == NULL)
916 err = got_error_from_errno2("fdopen",
917 ignorespath);
918 else {
919 fd = -1;
920 err = read_ignores(ignores, path, ignoresfile);
923 } else {
924 ignoresfile = fopen(ignorespath, "re");
925 if (ignoresfile == NULL) {
926 if (errno != ENOENT && errno != EACCES)
927 err = got_error_from_errno2("fopen",
928 ignorespath);
929 } else
930 err = read_ignores(ignores, path, ignoresfile);
933 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
934 err = got_error_from_errno2("fclose", path);
935 if (fd != -1 && close(fd) == -1 && err == NULL)
936 err = got_error_from_errno2("close", path);
937 free(ignorespath);
938 return err;
941 static const struct got_error *
942 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
943 int dirfd)
945 const struct got_error *err = NULL;
946 struct diff_dir_cb_arg *a = arg;
947 char *path = NULL;
949 if (ignore != NULL)
950 *ignore = 0;
952 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
953 return got_error(GOT_ERR_CANCELLED);
955 if (parent_path[0]) {
956 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
957 return got_error_from_errno("asprintf");
958 } else {
959 path = de->d_name;
962 if (de->d_type == DT_DIR) {
963 if (!a->no_ignores && ignore != NULL &&
964 match_ignores(a->ignores, path))
965 *ignore = 1;
966 } else if (!match_ignores(a->ignores, path) &&
967 got_path_is_child(path, a->status_path, a->status_path_len))
968 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
969 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
970 if (parent_path[0])
971 free(path);
972 return err;
975 static const struct got_error *
976 status_traverse(void *arg, const char *path, int dirfd)
978 const struct got_error *err = NULL;
979 struct diff_dir_cb_arg *a = arg;
981 if (a->no_ignores)
982 return NULL;
984 err = add_ignores(a->ignores, a->worktree->root_path,
985 path, dirfd, ".cvsignore");
986 if (err)
987 return err;
989 err = add_ignores(a->ignores, a->worktree->root_path, path,
990 dirfd, ".gitignore");
992 return err;
995 static const struct got_error *
996 report_single_file_status(const char *path, const char *ondisk_path,
997 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
998 void *status_arg, struct got_repository *repo, int report_unchanged,
999 struct got_pathlist_head *ignores, int no_ignores)
1001 struct got_fileindex_entry *ie;
1002 struct stat sb;
1004 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1005 if (ie)
1006 return report_file_status(ie, ondisk_path, -1, NULL,
1007 status_cb, status_arg, repo, report_unchanged);
1009 if (lstat(ondisk_path, &sb) == -1) {
1010 if (errno != ENOENT)
1011 return got_error_from_errno2("lstat", ondisk_path);
1012 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
1013 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1016 if (!no_ignores && match_ignores(ignores, path))
1017 return NULL;
1019 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1020 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
1021 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
1023 return NULL;
1026 static const struct got_error *
1027 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
1028 const char *root_path, const char *path)
1030 const struct got_error *err;
1031 char *parent_path, *next_parent_path = NULL;
1033 err = add_ignores(ignores, root_path, "", -1,
1034 ".cvsignore");
1035 if (err)
1036 return err;
1038 err = add_ignores(ignores, root_path, "", -1,
1039 ".gitignore");
1040 if (err)
1041 return err;
1043 err = got_path_dirname(&parent_path, path);
1044 if (err) {
1045 if (err->code == GOT_ERR_BAD_PATH)
1046 return NULL; /* cannot traverse parent */
1047 return err;
1049 for (;;) {
1050 err = add_ignores(ignores, root_path, parent_path, -1,
1051 ".cvsignore");
1052 if (err)
1053 break;
1054 err = add_ignores(ignores, root_path, parent_path, -1,
1055 ".gitignore");
1056 if (err)
1057 break;
1058 err = got_path_dirname(&next_parent_path, parent_path);
1059 if (err) {
1060 if (err->code == GOT_ERR_BAD_PATH)
1061 err = NULL; /* traversed everything */
1062 break;
1064 if (got_path_is_root_dir(parent_path))
1065 break;
1066 free(parent_path);
1067 parent_path = next_parent_path;
1068 next_parent_path = NULL;
1071 free(parent_path);
1072 free(next_parent_path);
1073 return err;
1076 struct find_missing_children_args {
1077 const char *parent_path;
1078 size_t parent_len;
1079 struct got_pathlist_head *children;
1080 got_cancel_cb cancel_cb;
1081 void *cancel_arg;
1084 static const struct got_error *
1085 find_missing_children(void *arg, struct got_fileindex_entry *ie)
1087 const struct got_error *err = NULL;
1088 struct find_missing_children_args *a = arg;
1090 if (a->cancel_cb) {
1091 err = a->cancel_cb(a->cancel_arg);
1092 if (err)
1093 return err;
1096 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
1097 err = got_pathlist_append(a->children, ie->path, NULL);
1099 return err;
1102 static const struct got_error *
1103 report_children(struct got_pathlist_head *children,
1104 struct got_worktree *worktree, struct got_fileindex *fileindex,
1105 struct got_repository *repo, int is_root_dir, int report_unchanged,
1106 struct got_pathlist_head *ignores, int no_ignores,
1107 got_worktree_status_cb status_cb, void *status_arg,
1108 got_cancel_cb cancel_cb, void *cancel_arg)
1110 const struct got_error *err = NULL;
1111 struct got_pathlist_entry *pe;
1112 char *ondisk_path = NULL;
1114 TAILQ_FOREACH(pe, children, entry) {
1115 if (cancel_cb) {
1116 err = cancel_cb(cancel_arg);
1117 if (err)
1118 break;
1121 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
1122 !is_root_dir ? "/" : "", pe->path) == -1) {
1123 err = got_error_from_errno("asprintf");
1124 ondisk_path = NULL;
1125 break;
1128 err = report_single_file_status(pe->path, ondisk_path,
1129 fileindex, status_cb, status_arg, repo, report_unchanged,
1130 ignores, no_ignores);
1131 if (err)
1132 break;
1134 free(ondisk_path);
1135 ondisk_path = NULL;
1138 free(ondisk_path);
1139 return err;
1142 static const struct got_error *
1143 worktree_status(struct got_worktree *worktree, const char *path,
1144 struct got_fileindex *fileindex, struct got_repository *repo,
1145 got_worktree_status_cb status_cb, void *status_arg,
1146 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
1147 int report_unchanged)
1149 const struct got_error *err = NULL;
1150 int fd = -1;
1151 struct got_fileindex_diff_dir_cb fdiff_cb;
1152 struct diff_dir_cb_arg arg;
1153 char *ondisk_path = NULL;
1154 struct got_pathlist_head ignores, missing_children;
1155 struct got_fileindex_entry *ie;
1157 TAILQ_INIT(&ignores);
1158 TAILQ_INIT(&missing_children);
1160 if (asprintf(&ondisk_path, "%s%s%s",
1161 worktree->root_path, path[0] ? "/" : "", path) == -1)
1162 return got_error_from_errno("asprintf");
1164 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
1165 if (ie) {
1166 err = report_single_file_status(path, ondisk_path,
1167 fileindex, status_cb, status_arg, repo,
1168 report_unchanged, &ignores, no_ignores);
1169 goto done;
1170 } else {
1171 struct find_missing_children_args fmca;
1172 fmca.parent_path = path;
1173 fmca.parent_len = strlen(path);
1174 fmca.children = &missing_children;
1175 fmca.cancel_cb = cancel_cb;
1176 fmca.cancel_arg = cancel_arg;
1177 err = got_fileindex_for_each_entry_safe(fileindex,
1178 find_missing_children, &fmca);
1179 if (err)
1180 goto done;
1183 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1184 if (fd == -1) {
1185 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
1186 !got_err_open_nofollow_on_symlink())
1187 err = got_error_from_errno2("open", ondisk_path);
1188 else {
1189 if (!no_ignores) {
1190 err = add_ignores_from_parent_paths(&ignores,
1191 worktree->root_path, ondisk_path);
1192 if (err)
1193 goto done;
1195 if (TAILQ_EMPTY(&missing_children)) {
1196 err = report_single_file_status(path,
1197 ondisk_path, fileindex,
1198 status_cb, status_arg, repo,
1199 report_unchanged, &ignores, no_ignores);
1200 if (err)
1201 goto done;
1202 } else {
1203 err = report_children(&missing_children,
1204 worktree, fileindex, repo,
1205 (path[0] == '\0'), report_unchanged,
1206 &ignores, no_ignores,
1207 status_cb, status_arg,
1208 cancel_cb, cancel_arg);
1209 if (err)
1210 goto done;
1213 } else {
1214 fdiff_cb.diff_old_new = status_old_new;
1215 fdiff_cb.diff_old = status_old;
1216 fdiff_cb.diff_new = status_new;
1217 fdiff_cb.diff_traverse = status_traverse;
1218 arg.fileindex = fileindex;
1219 arg.worktree = worktree;
1220 arg.status_path = path;
1221 arg.status_path_len = strlen(path);
1222 arg.repo = repo;
1223 arg.status_cb = status_cb;
1224 arg.status_arg = status_arg;
1225 arg.cancel_cb = cancel_cb;
1226 arg.cancel_arg = cancel_arg;
1227 arg.report_unchanged = report_unchanged;
1228 arg.no_ignores = no_ignores;
1229 if (!no_ignores) {
1230 err = add_ignores_from_parent_paths(&ignores,
1231 worktree->root_path, path);
1232 if (err)
1233 goto done;
1235 arg.ignores = &ignores;
1236 err = got_fileindex_diff_dir(fileindex, fd,
1237 worktree->root_path, path, repo, &fdiff_cb, &arg);
1239 done:
1240 free_ignores(&ignores);
1241 if (fd != -1 && close(fd) == -1 && err == NULL)
1242 err = got_error_from_errno("close");
1243 free(ondisk_path);
1244 return err;
1247 static void
1248 free_commitable(struct got_commitable *ct)
1250 free(ct->path);
1251 free(ct->in_repo_path);
1252 free(ct->ondisk_path);
1253 free(ct->blob_id);
1254 free(ct->base_blob_id);
1255 free(ct->staged_blob_id);
1256 free(ct->base_commit_id);
1257 free(ct);
1260 struct collect_commitables_arg {
1261 struct got_pathlist_head *commitable_paths;
1262 struct got_repository *repo;
1263 struct got_worktree *worktree;
1264 struct got_fileindex *fileindex;
1265 int have_staged_files;
1266 int allow_bad_symlinks;
1267 int diff_header_shown;
1268 int commit_conflicts;
1269 FILE *diff_outfile;
1270 FILE *f1;
1271 FILE *f2;
1275 * Create a file which contains the target path of a symlink so we can feed
1276 * it as content to the diff engine.
1278 static const struct got_error *
1279 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
1280 const char *abspath)
1282 const struct got_error *err = NULL;
1283 char target_path[PATH_MAX];
1284 ssize_t target_len, outlen;
1286 *fd = -1;
1288 if (dirfd != -1) {
1289 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
1290 if (target_len == -1)
1291 return got_error_from_errno2("readlinkat", abspath);
1292 } else {
1293 target_len = readlink(abspath, target_path, PATH_MAX);
1294 if (target_len == -1)
1295 return got_error_from_errno2("readlink", abspath);
1298 *fd = got_opentempfd();
1299 if (*fd == -1)
1300 return got_error_from_errno("got_opentempfd");
1302 outlen = write(*fd, target_path, target_len);
1303 if (outlen == -1) {
1304 err = got_error_from_errno("got_opentempfd");
1305 goto done;
1308 if (lseek(*fd, 0, SEEK_SET) == -1) {
1309 err = got_error_from_errno2("lseek", abspath);
1310 goto done;
1312 done:
1313 if (err) {
1314 close(*fd);
1315 *fd = -1;
1317 return err;
1320 static const struct got_error *
1321 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
1322 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
1323 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
1325 const struct got_error *err = NULL;
1326 struct got_blob_object *blob1 = NULL;
1327 int fd = -1, fd1 = -1, fd2 = -1;
1328 FILE *ondisk_file = NULL;
1329 char *label1 = NULL;
1330 struct stat sb;
1331 off_t size1 = 0;
1332 int f2_exists = 0;
1333 char *id_str = NULL;
1335 memset(&sb, 0, sizeof(sb));
1337 if (diff_staged) {
1338 if (ct->staged_status != GOT_STATUS_MODIFY &&
1339 ct->staged_status != GOT_STATUS_ADD &&
1340 ct->staged_status != GOT_STATUS_DELETE)
1341 return NULL;
1342 } else {
1343 if (ct->status != GOT_STATUS_MODIFY &&
1344 ct->status != GOT_STATUS_ADD &&
1345 ct->status != GOT_STATUS_DELETE &&
1346 ct->status != GOT_STATUS_CONFLICT)
1347 return NULL;
1350 err = got_opentemp_truncate(f1);
1351 if (err)
1352 return got_error_from_errno("got_opentemp_truncate");
1353 err = got_opentemp_truncate(f2);
1354 if (err)
1355 return got_error_from_errno("got_opentemp_truncate");
1357 if (!*diff_header_shown) {
1358 err = got_object_id_str(&id_str, worktree->base_commit_id);
1359 if (err)
1360 return err;
1361 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
1362 got_worktree_get_root_path(worktree));
1363 fprintf(diff_outfile, "commit - %s\n", id_str);
1364 fprintf(diff_outfile, "path + %s%s\n",
1365 got_worktree_get_root_path(worktree),
1366 diff_staged ? " (staged changes)" : "");
1367 *diff_header_shown = 1;
1370 if (diff_staged) {
1371 const char *label1 = NULL, *label2 = NULL;
1372 switch (ct->staged_status) {
1373 case GOT_STATUS_MODIFY:
1374 label1 = ct->path;
1375 label2 = ct->path;
1376 break;
1377 case GOT_STATUS_ADD:
1378 label2 = ct->path;
1379 break;
1380 case GOT_STATUS_DELETE:
1381 label1 = ct->path;
1382 break;
1383 default:
1384 return got_error(GOT_ERR_FILE_STATUS);
1386 fd1 = got_opentempfd();
1387 if (fd1 == -1) {
1388 err = got_error_from_errno("got_opentempfd");
1389 goto done;
1391 fd2 = got_opentempfd();
1392 if (fd2 == -1) {
1393 err = got_error_from_errno("got_opentempfd");
1394 goto done;
1396 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
1397 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
1398 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
1399 NULL, repo, diff_outfile);
1400 goto done;
1403 fd1 = got_opentempfd();
1404 if (fd1 == -1) {
1405 err = got_error_from_errno("got_opentempfd");
1406 goto done;
1409 if (ct->status != GOT_STATUS_ADD) {
1410 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
1411 8192, fd1);
1412 if (err)
1413 goto done;
1416 if (ct->status != GOT_STATUS_DELETE) {
1417 if (dirfd != -1) {
1418 fd = openat(dirfd, de_name,
1419 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1420 if (fd == -1) {
1421 if (!got_err_open_nofollow_on_symlink()) {
1422 err = got_error_from_errno2("openat",
1423 ct->ondisk_path);
1424 goto done;
1426 err = get_symlink_target_file(&fd, dirfd,
1427 de_name, ct->ondisk_path);
1428 if (err)
1429 goto done;
1431 } else {
1432 fd = open(ct->ondisk_path,
1433 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1434 if (fd == -1) {
1435 if (!got_err_open_nofollow_on_symlink()) {
1436 err = got_error_from_errno2("open",
1437 ct->ondisk_path);
1438 goto done;
1440 err = get_symlink_target_file(&fd, dirfd,
1441 de_name, ct->ondisk_path);
1442 if (err)
1443 goto done;
1446 if (fstatat(fd, ct->ondisk_path, &sb,
1447 AT_SYMLINK_NOFOLLOW) == -1) {
1448 err = got_error_from_errno2("fstatat", ct->ondisk_path);
1449 goto done;
1451 ondisk_file = fdopen(fd, "r");
1452 if (ondisk_file == NULL) {
1453 err = got_error_from_errno2("fdopen", ct->ondisk_path);
1454 goto done;
1456 fd = -1;
1457 f2_exists = 1;
1460 if (blob1) {
1461 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
1462 f1, blob1);
1463 if (err)
1464 goto done;
1467 err = got_diff_blob_file(blob1, f1, size1, label1,
1468 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
1469 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
1470 done:
1471 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1472 err = got_error_from_errno("close");
1473 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
1474 err = got_error_from_errno("close");
1475 if (blob1)
1476 got_object_blob_close(blob1);
1477 if (fd != -1 && close(fd) == -1 && err == NULL)
1478 err = got_error_from_errno("close");
1479 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
1480 err = got_error_from_errno("fclose");
1481 return err;
1484 static const struct got_error *
1485 collect_commitables(void *arg, unsigned char status,
1486 unsigned char staged_status, const char *relpath,
1487 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
1488 struct got_object_id *commit_id, int dirfd, const char *de_name)
1490 struct collect_commitables_arg *a = arg;
1491 const struct got_error *err = NULL;
1492 struct got_commitable *ct = NULL;
1493 struct got_pathlist_entry *new = NULL;
1494 char *parent_path = NULL, *path = NULL;
1495 struct stat sb;
1497 if (a->have_staged_files) {
1498 if (staged_status != GOT_STATUS_MODIFY &&
1499 staged_status != GOT_STATUS_ADD &&
1500 staged_status != GOT_STATUS_DELETE)
1501 return NULL;
1502 } else {
1503 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
1504 printf("C %s\n", relpath);
1505 return got_error(GOT_ERR_COMMIT_CONFLICT);
1508 if (status != GOT_STATUS_MODIFY &&
1509 status != GOT_STATUS_MODE_CHANGE &&
1510 status != GOT_STATUS_ADD &&
1511 status != GOT_STATUS_DELETE &&
1512 status != GOT_STATUS_CONFLICT)
1513 return NULL;
1516 if (asprintf(&path, "/%s", relpath) == -1) {
1517 err = got_error_from_errno("asprintf");
1518 goto done;
1520 if (strcmp(path, "/") == 0) {
1521 parent_path = strdup("");
1522 if (parent_path == NULL)
1523 return got_error_from_errno("strdup");
1524 } else {
1525 err = got_path_dirname(&parent_path, path);
1526 if (err)
1527 return err;
1530 ct = calloc(1, sizeof(*ct));
1531 if (ct == NULL) {
1532 err = got_error_from_errno("calloc");
1533 goto done;
1536 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
1537 relpath) == -1) {
1538 err = got_error_from_errno("asprintf");
1539 goto done;
1542 if (staged_status == GOT_STATUS_ADD ||
1543 staged_status == GOT_STATUS_MODIFY) {
1544 struct got_fileindex_entry *ie;
1545 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
1546 switch (got_fileindex_entry_staged_filetype_get(ie)) {
1547 case GOT_FILEIDX_MODE_REGULAR_FILE:
1548 case GOT_FILEIDX_MODE_BAD_SYMLINK:
1549 ct->mode = S_IFREG;
1550 break;
1551 case GOT_FILEIDX_MODE_SYMLINK:
1552 ct->mode = S_IFLNK;
1553 break;
1554 default:
1555 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
1556 goto done;
1558 ct->mode |= got_fileindex_entry_perms_get(ie);
1559 } else if (status != GOT_STATUS_DELETE &&
1560 staged_status != GOT_STATUS_DELETE) {
1561 if (dirfd != -1) {
1562 if (fstatat(dirfd, de_name, &sb,
1563 AT_SYMLINK_NOFOLLOW) == -1) {
1564 err = got_error_from_errno2("fstatat",
1565 ct->ondisk_path);
1566 goto done;
1568 } else if (lstat(ct->ondisk_path, &sb) == -1) {
1569 err = got_error_from_errno2("lstat", ct->ondisk_path);
1570 goto done;
1572 ct->mode = sb.st_mode;
1575 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
1576 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
1577 relpath) == -1) {
1578 err = got_error_from_errno("asprintf");
1579 goto done;
1582 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
1583 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
1584 int is_bad_symlink;
1585 char target_path[PATH_MAX];
1586 ssize_t target_len;
1587 target_len = readlink(ct->ondisk_path, target_path,
1588 sizeof(target_path));
1589 if (target_len == -1) {
1590 err = got_error_from_errno2("readlink",
1591 ct->ondisk_path);
1592 goto done;
1594 err = is_bad_symlink_target(&is_bad_symlink, target_path,
1595 target_len, ct->ondisk_path, a->worktree->root_path);
1596 if (err)
1597 goto done;
1598 if (is_bad_symlink) {
1599 err = got_error_path(ct->ondisk_path,
1600 GOT_ERR_BAD_SYMLINK);
1601 goto done;
1605 ct->status = status;
1606 ct->staged_status = staged_status;
1607 ct->blob_id = NULL; /* will be filled in when blob gets created */
1608 if (ct->status != GOT_STATUS_ADD &&
1609 ct->staged_status != GOT_STATUS_ADD) {
1610 ct->base_blob_id = got_object_id_dup(blob_id);
1611 if (ct->base_blob_id == NULL) {
1612 err = got_error_from_errno("got_object_id_dup");
1613 goto done;
1615 ct->base_commit_id = got_object_id_dup(commit_id);
1616 if (ct->base_commit_id == NULL) {
1617 err = got_error_from_errno("got_object_id_dup");
1618 goto done;
1621 if (ct->staged_status == GOT_STATUS_ADD ||
1622 ct->staged_status == GOT_STATUS_MODIFY) {
1623 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
1624 if (ct->staged_blob_id == NULL) {
1625 err = got_error_from_errno("got_object_id_dup");
1626 goto done;
1629 ct->path = strdup(path);
1630 if (ct->path == NULL) {
1631 err = got_error_from_errno("strdup");
1632 goto done;
1634 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
1635 if (err)
1636 goto done;
1638 if (a->diff_outfile && ct && new != NULL) {
1639 err = append_ct_diff(ct, &a->diff_header_shown,
1640 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
1641 a->have_staged_files, a->repo, a->worktree);
1642 if (err)
1643 goto done;
1645 done:
1646 if (ct && (err || new == NULL))
1647 free_commitable(ct);
1648 free(parent_path);
1649 free(path);
1650 return err;
1653 static const struct got_error *write_tree(struct got_object_id **, int *,
1654 struct got_tree_object *, const char *, struct got_pathlist_head *,
1655 got_worktree_status_cb status_cb, void *status_arg,
1656 struct got_repository *);
1658 static const struct got_error *
1659 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
1660 struct got_tree_entry *te, const char *parent_path,
1661 struct got_pathlist_head *commitable_paths,
1662 got_worktree_status_cb status_cb, void *status_arg,
1663 struct got_repository *repo)
1665 const struct got_error *err = NULL;
1666 struct got_tree_object *subtree;
1667 char *subpath;
1669 if (asprintf(&subpath, "%s%s%s", parent_path,
1670 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
1671 return got_error_from_errno("asprintf");
1673 err = got_object_open_as_tree(&subtree, repo, &te->id);
1674 if (err)
1675 return err;
1677 err = write_tree(new_subtree_id, nentries, subtree, subpath,
1678 commitable_paths, status_cb, status_arg, repo);
1679 got_object_tree_close(subtree);
1680 free(subpath);
1681 return err;
1684 static const struct got_error *
1685 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
1687 const struct got_error *err = NULL;
1688 char *ct_parent_path = NULL;
1690 *match = 0;
1692 if (strchr(ct->in_repo_path, '/') == NULL) {
1693 *match = got_path_is_root_dir(path);
1694 return NULL;
1697 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
1698 if (err)
1699 return err;
1700 *match = (strcmp(path, ct_parent_path) == 0);
1701 free(ct_parent_path);
1702 return err;
1705 static mode_t
1706 get_ct_file_mode(struct got_commitable *ct)
1708 if (S_ISLNK(ct->mode))
1709 return S_IFLNK;
1711 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1714 static const struct got_error *
1715 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
1716 struct got_tree_entry *te, struct got_commitable *ct)
1718 const struct got_error *err = NULL;
1720 *new_te = NULL;
1722 err = got_object_tree_entry_dup(new_te, te);
1723 if (err)
1724 goto done;
1726 (*new_te)->mode = get_ct_file_mode(ct);
1728 if (ct->staged_status == GOT_STATUS_MODIFY)
1729 memcpy(&(*new_te)->id, ct->staged_blob_id,
1730 sizeof((*new_te)->id));
1731 else
1732 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1733 done:
1734 if (err && *new_te) {
1735 free(*new_te);
1736 *new_te = NULL;
1738 return err;
1741 static const struct got_error *
1742 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1743 struct got_commitable *ct)
1745 const struct got_error *err = NULL;
1746 char *ct_name = NULL;
1748 *new_te = NULL;
1750 *new_te = calloc(1, sizeof(**new_te));
1751 if (*new_te == NULL)
1752 return got_error_from_errno("calloc");
1754 err = got_path_basename(&ct_name, ct->path);
1755 if (err)
1756 goto done;
1757 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
1758 sizeof((*new_te)->name)) {
1759 err = got_error(GOT_ERR_NO_SPACE);
1760 goto done;
1763 (*new_te)->mode = get_ct_file_mode(ct);
1765 if (ct->staged_status == GOT_STATUS_ADD)
1766 memcpy(&(*new_te)->id, ct->staged_blob_id,
1767 sizeof((*new_te)->id));
1768 else
1769 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
1770 done:
1771 free(ct_name);
1772 if (err && *new_te) {
1773 free(*new_te);
1774 *new_te = NULL;
1776 return err;
1779 static const struct got_error *
1780 insert_tree_entry(struct got_tree_entry *new_te,
1781 struct got_pathlist_head *paths)
1783 const struct got_error *err = NULL;
1784 struct got_pathlist_entry *new_pe;
1786 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1787 if (err)
1788 return err;
1789 if (new_pe == NULL)
1790 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1791 return NULL;
1794 static const struct got_error *
1795 report_ct_status(struct got_commitable *ct,
1796 got_worktree_status_cb status_cb, void *status_arg)
1798 const char *ct_path = ct->path;
1799 unsigned char status;
1801 if (status_cb == NULL) /* no commit progress output desired */
1802 return NULL;
1804 while (ct_path[0] == '/')
1805 ct_path++;
1807 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
1808 status = ct->staged_status;
1809 else
1810 status = ct->status;
1812 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
1813 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
1816 static const struct got_error *
1817 match_modified_subtree(int *modified, struct got_tree_entry *te,
1818 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
1820 const struct got_error *err = NULL;
1821 struct got_pathlist_entry *pe;
1822 char *te_path;
1824 *modified = 0;
1826 if (asprintf(&te_path, "%s%s%s", base_tree_path,
1827 got_path_is_root_dir(base_tree_path) ? "" : "/",
1828 te->name) == -1)
1829 return got_error_from_errno("asprintf");
1831 TAILQ_FOREACH(pe, commitable_paths, entry) {
1832 struct got_commitable *ct = pe->data;
1833 *modified = got_path_is_child(ct->in_repo_path, te_path,
1834 strlen(te_path));
1835 if (*modified)
1836 break;
1839 free(te_path);
1840 return err;
1843 static const struct got_error *
1844 match_deleted_or_modified_ct(struct got_commitable **ctp,
1845 struct got_tree_entry *te, const char *base_tree_path,
1846 struct got_pathlist_head *commitable_paths)
1848 const struct got_error *err = NULL;
1849 struct got_pathlist_entry *pe;
1851 *ctp = NULL;
1853 TAILQ_FOREACH(pe, commitable_paths, entry) {
1854 struct got_commitable *ct = pe->data;
1855 char *ct_name = NULL;
1856 int path_matches;
1858 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
1859 if (ct->status != GOT_STATUS_MODIFY &&
1860 ct->status != GOT_STATUS_MODE_CHANGE &&
1861 ct->status != GOT_STATUS_DELETE &&
1862 ct->status != GOT_STATUS_CONFLICT)
1863 continue;
1864 } else {
1865 if (ct->staged_status != GOT_STATUS_MODIFY &&
1866 ct->staged_status != GOT_STATUS_DELETE)
1867 continue;
1870 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
1871 continue;
1873 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
1874 if (err)
1875 return err;
1876 if (!path_matches)
1877 continue;
1879 err = got_path_basename(&ct_name, pe->path);
1880 if (err)
1881 return err;
1883 if (strcmp(te->name, ct_name) != 0) {
1884 free(ct_name);
1885 continue;
1887 free(ct_name);
1889 *ctp = ct;
1890 break;
1893 return err;
1896 static const struct got_error *
1897 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
1898 const char *child_path, const char *path_base_tree,
1899 struct got_pathlist_head *commitable_paths,
1900 got_worktree_status_cb status_cb, void *status_arg,
1901 struct got_repository *repo)
1903 const struct got_error *err = NULL;
1904 struct got_tree_entry *new_te;
1905 char *subtree_path;
1906 struct got_object_id *id = NULL;
1907 int nentries;
1909 *new_tep = NULL;
1911 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
1912 got_path_is_root_dir(path_base_tree) ? "" : "/",
1913 child_path) == -1)
1914 return got_error_from_errno("asprintf");
1916 new_te = calloc(1, sizeof(*new_te));
1917 if (new_te == NULL)
1918 return got_error_from_errno("calloc");
1919 new_te->mode = S_IFDIR;
1921 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
1922 sizeof(new_te->name)) {
1923 err = got_error(GOT_ERR_NO_SPACE);
1924 goto done;
1926 err = write_tree(&id, &nentries, NULL, subtree_path,
1927 commitable_paths, status_cb, status_arg, repo);
1928 if (err) {
1929 free(new_te);
1930 goto done;
1932 memcpy(&new_te->id, id, sizeof(new_te->id));
1933 done:
1934 free(id);
1935 free(subtree_path);
1936 if (err == NULL)
1937 *new_tep = new_te;
1938 return err;
1941 static const struct got_error *
1942 write_tree(struct got_object_id **new_tree_id, int *nentries,
1943 struct got_tree_object *base_tree, const char *path_base_tree,
1944 struct got_pathlist_head *commitable_paths,
1945 got_worktree_status_cb status_cb, void *status_arg,
1946 struct got_repository *repo)
1948 const struct got_error *err = NULL;
1949 struct got_pathlist_head paths;
1950 struct got_tree_entry *te, *new_te = NULL;
1951 struct got_pathlist_entry *pe;
1953 TAILQ_INIT(&paths);
1954 *nentries = 0;
1956 /* Insert, and recurse into, newly added entries first. */
1957 TAILQ_FOREACH(pe, commitable_paths, entry) {
1958 struct got_commitable *ct = pe->data;
1959 char *child_path = NULL, *slash;
1961 if ((ct->status != GOT_STATUS_ADD &&
1962 ct->staged_status != GOT_STATUS_ADD) ||
1963 (ct->flags & GOT_COMMITABLE_ADDED))
1964 continue;
1966 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
1967 strlen(path_base_tree)))
1968 continue;
1970 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
1971 ct->in_repo_path);
1972 if (err)
1973 goto done;
1975 slash = strchr(child_path, '/');
1976 if (slash == NULL) {
1977 err = alloc_added_blob_tree_entry(&new_te, ct);
1978 if (err)
1979 goto done;
1980 err = report_ct_status(ct, status_cb, status_arg);
1981 if (err)
1982 goto done;
1983 ct->flags |= GOT_COMMITABLE_ADDED;
1984 err = insert_tree_entry(new_te, &paths);
1985 if (err)
1986 goto done;
1987 (*nentries)++;
1988 } else {
1989 *slash = '\0'; /* trim trailing path components */
1990 if (base_tree == NULL ||
1991 got_object_tree_find_entry(base_tree, child_path)
1992 == NULL) {
1993 err = make_subtree_for_added_blob(&new_te,
1994 child_path, path_base_tree,
1995 commitable_paths, status_cb, status_arg,
1996 repo);
1997 if (err)
1998 goto done;
1999 err = insert_tree_entry(new_te, &paths);
2000 if (err)
2001 goto done;
2002 (*nentries)++;
2007 if (base_tree) {
2008 int i, nbase_entries;
2009 /* Handle modified and deleted entries. */
2010 nbase_entries = got_object_tree_get_nentries(base_tree);
2011 for (i = 0; i < nbase_entries; i++) {
2012 struct got_commitable *ct = NULL;
2014 te = got_object_tree_get_entry(base_tree, i);
2015 if (got_object_tree_entry_is_submodule(te)) {
2016 /* Entry is a submodule; just copy it. */
2017 err = got_object_tree_entry_dup(&new_te, te);
2018 if (err)
2019 goto done;
2020 err = insert_tree_entry(new_te, &paths);
2021 if (err)
2022 goto done;
2023 (*nentries)++;
2024 continue;
2027 if (S_ISDIR(te->mode)) {
2028 int modified;
2029 err = got_object_tree_entry_dup(&new_te, te);
2030 if (err)
2031 goto done;
2032 err = match_modified_subtree(&modified, te,
2033 path_base_tree, commitable_paths);
2034 if (err)
2035 goto done;
2036 /* Avoid recursion into unmodified subtrees. */
2037 if (modified) {
2038 struct got_object_id *new_id;
2039 int nsubentries;
2040 err = write_subtree(&new_id,
2041 &nsubentries, te,
2042 path_base_tree, commitable_paths,
2043 status_cb, status_arg, repo);
2044 if (err)
2045 goto done;
2046 if (nsubentries == 0) {
2047 /* All entries were deleted. */
2048 free(new_id);
2049 continue;
2051 memcpy(&new_te->id, new_id,
2052 sizeof(new_te->id));
2053 free(new_id);
2055 err = insert_tree_entry(new_te, &paths);
2056 if (err)
2057 goto done;
2058 (*nentries)++;
2059 continue;
2062 err = match_deleted_or_modified_ct(&ct, te,
2063 path_base_tree, commitable_paths);
2064 if (err)
2065 goto done;
2066 if (ct) {
2067 /* NB: Deleted entries get dropped here. */
2068 if (ct->status == GOT_STATUS_MODIFY ||
2069 ct->status == GOT_STATUS_MODE_CHANGE ||
2070 ct->status == GOT_STATUS_CONFLICT ||
2071 ct->staged_status == GOT_STATUS_MODIFY) {
2072 err = alloc_modified_blob_tree_entry(
2073 &new_te, te, ct);
2074 if (err)
2075 goto done;
2076 err = insert_tree_entry(new_te, &paths);
2077 if (err)
2078 goto done;
2079 (*nentries)++;
2081 err = report_ct_status(ct, status_cb,
2082 status_arg);
2083 if (err)
2084 goto done;
2085 } else {
2086 /* Entry is unchanged; just copy it. */
2087 err = got_object_tree_entry_dup(&new_te, te);
2088 if (err)
2089 goto done;
2090 err = insert_tree_entry(new_te, &paths);
2091 if (err)
2092 goto done;
2093 (*nentries)++;
2098 /* Write new list of entries; deleted entries have been dropped. */
2099 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
2100 done:
2101 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2102 return err;
2105 static const struct got_error *
2106 update_fileindex_after_commit(struct got_worktree *worktree,
2107 struct got_pathlist_head *commitable_paths,
2108 struct got_object_id *new_base_commit_id,
2109 struct got_fileindex *fileindex, int have_staged_files)
2111 const struct got_error *err = NULL;
2112 struct got_pathlist_entry *pe;
2113 char *relpath = NULL;
2115 TAILQ_FOREACH(pe, commitable_paths, entry) {
2116 struct got_fileindex_entry *ie;
2117 struct got_commitable *ct = pe->data;
2119 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2121 err = got_path_skip_common_ancestor(&relpath,
2122 worktree->root_path, ct->ondisk_path);
2123 if (err)
2124 goto done;
2126 if (ie) {
2127 if (ct->status == GOT_STATUS_DELETE ||
2128 ct->staged_status == GOT_STATUS_DELETE) {
2129 got_fileindex_entry_remove(fileindex, ie);
2130 } else if (ct->staged_status == GOT_STATUS_ADD ||
2131 ct->staged_status == GOT_STATUS_MODIFY) {
2132 got_fileindex_entry_stage_set(ie,
2133 GOT_FILEIDX_STAGE_NONE);
2134 got_fileindex_entry_staged_filetype_set(ie, 0);
2136 err = got_fileindex_entry_update(ie,
2137 worktree->root_fd, relpath,
2138 ct->staged_blob_id->sha1,
2139 new_base_commit_id->sha1,
2140 !have_staged_files);
2141 } else
2142 err = got_fileindex_entry_update(ie,
2143 worktree->root_fd, relpath,
2144 ct->blob_id->sha1,
2145 new_base_commit_id->sha1,
2146 !have_staged_files);
2147 } else {
2148 err = got_fileindex_entry_alloc(&ie, pe->path);
2149 if (err)
2150 goto done;
2151 err = got_fileindex_entry_update(ie,
2152 worktree->root_fd, relpath, ct->blob_id->sha1,
2153 new_base_commit_id->sha1, 1);
2154 if (err) {
2155 got_fileindex_entry_free(ie);
2156 goto done;
2158 err = got_fileindex_entry_add(fileindex, ie);
2159 if (err) {
2160 got_fileindex_entry_free(ie);
2161 goto done;
2164 free(relpath);
2165 relpath = NULL;
2167 done:
2168 free(relpath);
2169 return err;
2172 static const struct got_error *
2173 check_out_of_date(const char *in_repo_path, unsigned char status,
2174 unsigned char staged_status, struct got_object_id *base_blob_id,
2175 struct got_object_id *base_commit_id,
2176 struct got_object_id *head_commit_id, struct got_repository *repo,
2177 int ood_errcode)
2179 const struct got_error *err = NULL;
2180 struct got_commit_object *commit = NULL;
2181 struct got_object_id *id = NULL;
2183 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
2184 /* Trivial case: base commit == head commit */
2185 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
2186 return NULL;
2188 * Ensure file content which local changes were based
2189 * on matches file content in the branch head.
2191 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2192 if (err)
2193 goto done;
2194 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2195 if (err) {
2196 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2197 err = got_error(ood_errcode);
2198 goto done;
2199 } else if (got_object_id_cmp(id, base_blob_id) != 0)
2200 err = got_error(ood_errcode);
2201 } else {
2202 /* Require that added files don't exist in the branch head. */
2203 err = got_object_open_as_commit(&commit, repo, head_commit_id);
2204 if (err)
2205 goto done;
2206 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
2207 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2208 goto done;
2209 err = id ? got_error(ood_errcode) : NULL;
2211 done:
2212 free(id);
2213 if (commit)
2214 got_object_commit_close(commit);
2215 return err;
2218 static const struct got_error *
2219 commit_worktree(struct got_object_id **new_commit_id,
2220 struct got_pathlist_head *commitable_paths,
2221 struct got_object_id *head_commit_id,
2222 struct got_object_id *parent_id2,
2223 struct got_worktree *worktree,
2224 const char *author, const char *committer, char *diff_path,
2225 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2226 got_worktree_status_cb status_cb, void *status_arg,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 struct got_pathlist_entry *pe;
2231 struct got_commit_object *head_commit = NULL;
2232 struct got_tree_object *head_tree = NULL;
2233 struct got_object_id *new_tree_id = NULL;
2234 int nentries, nparents = 0;
2235 struct got_object_id_queue parent_ids;
2236 struct got_object_qid *pid = NULL;
2237 char *logmsg = NULL;
2238 time_t timestamp;
2240 *new_commit_id = NULL;
2242 STAILQ_INIT(&parent_ids);
2244 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2245 if (err)
2246 goto done;
2248 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2249 if (err)
2250 goto done;
2252 if (commit_msg_cb != NULL) {
2253 err = commit_msg_cb(commitable_paths, diff_path,
2254 &logmsg, commit_arg);
2255 if (err)
2256 goto done;
2259 if (logmsg == NULL || strlen(logmsg) == 0) {
2260 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2261 goto done;
2264 /* Create blobs from added and modified files and record their IDs. */
2265 TAILQ_FOREACH(pe, commitable_paths, entry) {
2266 struct got_commitable *ct = pe->data;
2267 char *ondisk_path;
2269 /* Blobs for staged files already exist. */
2270 if (ct->staged_status == GOT_STATUS_ADD ||
2271 ct->staged_status == GOT_STATUS_MODIFY)
2272 continue;
2274 if (ct->status != GOT_STATUS_ADD &&
2275 ct->status != GOT_STATUS_MODIFY &&
2276 ct->status != GOT_STATUS_MODE_CHANGE &&
2277 ct->status != GOT_STATUS_CONFLICT)
2278 continue;
2280 if (asprintf(&ondisk_path, "%s/%s",
2281 worktree->root_path, pe->path) == -1) {
2282 err = got_error_from_errno("asprintf");
2283 goto done;
2285 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2286 free(ondisk_path);
2287 if (err)
2288 goto done;
2291 /* Recursively write new tree objects. */
2292 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
2293 commitable_paths, status_cb, status_arg, repo);
2294 if (err)
2295 goto done;
2297 err = got_object_qid_alloc(&pid, head_commit_id);
2298 if (err)
2299 goto done;
2300 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2301 nparents++;
2302 if (parent_id2) {
2303 err = got_object_qid_alloc(&pid, parent_id2);
2304 if (err)
2305 goto done;
2306 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
2307 nparents++;
2309 timestamp = time(NULL);
2310 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2311 nparents, author, timestamp, committer, timestamp, logmsg, repo);
2312 if (logmsg != NULL)
2313 free(logmsg);
2314 if (err)
2315 goto done;
2316 done:
2317 got_object_id_queue_free(&parent_ids);
2318 if (head_tree)
2319 got_object_tree_close(head_tree);
2320 if (head_commit)
2321 got_object_commit_close(head_commit);
2322 return err;
2325 static const struct got_error *
2326 check_path_is_commitable(const char *path,
2327 struct got_pathlist_head *commitable_paths)
2329 struct got_pathlist_entry *cpe = NULL;
2330 size_t path_len = strlen(path);
2332 TAILQ_FOREACH(cpe, commitable_paths, entry) {
2333 struct got_commitable *ct = cpe->data;
2334 const char *ct_path = ct->path;
2336 while (ct_path[0] == '/')
2337 ct_path++;
2339 if (strcmp(path, ct_path) == 0 ||
2340 got_path_is_child(ct_path, path, path_len))
2341 break;
2344 if (cpe == NULL)
2345 return got_error_path(path, GOT_ERR_BAD_PATH);
2347 return NULL;
2350 static const struct got_error *
2351 check_staged_file(void *arg, struct got_fileindex_entry *ie)
2353 int *have_staged_files = arg;
2355 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
2356 *have_staged_files = 1;
2357 return got_error(GOT_ERR_CANCELLED);
2360 return NULL;
2363 static const struct got_error *
2364 check_non_staged_files(struct got_fileindex *fileindex,
2365 struct got_pathlist_head *paths)
2367 struct got_pathlist_entry *pe;
2368 struct got_fileindex_entry *ie;
2370 TAILQ_FOREACH(pe, paths, entry) {
2371 if (pe->path[0] == '\0')
2372 continue;
2373 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
2374 if (ie == NULL)
2375 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
2376 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
2377 return got_error_path(pe->path,
2378 GOT_ERR_FILE_NOT_STAGED);
2381 return NULL;
2384 static void
2385 print_load_info(int print_colored, int print_found, int print_trees,
2386 int ncolored, int nfound, int ntrees)
2388 if (print_colored) {
2389 printf("%d commit%s colored", ncolored,
2390 ncolored == 1 ? "" : "s");
2392 if (print_found) {
2393 printf("%s%d object%s found",
2394 ncolored > 0 ? "; " : "",
2395 nfound, nfound == 1 ? "" : "s");
2397 if (print_trees) {
2398 printf("; %d tree%s scanned", ntrees,
2399 ntrees == 1 ? "" : "s");
2403 struct got_send_progress_arg {
2404 char last_scaled_packsize[FMT_SCALED_STRSIZE];
2405 int verbosity;
2406 int last_ncolored;
2407 int last_nfound;
2408 int last_ntrees;
2409 int loading_done;
2410 int last_ncommits;
2411 int last_nobj_total;
2412 int last_p_deltify;
2413 int last_p_written;
2414 int last_p_sent;
2415 int printed_something;
2416 int sent_something;
2417 struct got_pathlist_head *delete_branches;
2420 static const struct got_error *
2421 send_progress(void *arg, int ncolored, int nfound, int ntrees,
2422 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
2423 int nobj_written, off_t bytes_sent, const char *refname,
2424 const char *errmsg, int success)
2426 struct got_send_progress_arg *a = arg;
2427 char scaled_packsize[FMT_SCALED_STRSIZE];
2428 char scaled_sent[FMT_SCALED_STRSIZE];
2429 int p_deltify = 0, p_written = 0, p_sent = 0;
2430 int print_colored = 0, print_found = 0, print_trees = 0;
2431 int print_searching = 0, print_total = 0;
2432 int print_deltify = 0, print_written = 0, print_sent = 0;
2434 if (a->verbosity < 0)
2435 return NULL;
2437 if (refname) {
2438 const char *status = success ? "accepted" : "rejected";
2440 if (success) {
2441 struct got_pathlist_entry *pe;
2442 TAILQ_FOREACH(pe, a->delete_branches, entry) {
2443 const char *branchname = pe->path;
2444 if (got_path_cmp(branchname, refname,
2445 strlen(branchname), strlen(refname)) == 0) {
2446 status = "deleted";
2447 a->sent_something = 1;
2448 break;
2453 if (a->printed_something)
2454 putchar('\n');
2455 printf("Server has %s %s", status, refname);
2456 if (errmsg)
2457 printf(": %s", errmsg);
2458 a->printed_something = 1;
2459 return NULL;
2462 if (a->last_ncolored != ncolored) {
2463 print_colored = 1;
2464 a->last_ncolored = ncolored;
2467 if (a->last_nfound != nfound) {
2468 print_colored = 1;
2469 print_found = 1;
2470 a->last_nfound = nfound;
2473 if (a->last_ntrees != ntrees) {
2474 print_colored = 1;
2475 print_found = 1;
2476 print_trees = 1;
2477 a->last_ntrees = ntrees;
2480 if ((print_colored || print_found || print_trees) &&
2481 !a->loading_done) {
2482 printf("\r");
2483 print_load_info(print_colored, print_found, print_trees,
2484 ncolored, nfound, ntrees);
2485 a->printed_something = 1;
2486 fflush(stdout);
2487 return NULL;
2488 } else if (!a->loading_done) {
2489 printf("\r");
2490 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
2491 printf("\n");
2492 a->loading_done = 1;
2495 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
2496 return got_error_from_errno("fmt_scaled");
2497 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
2498 return got_error_from_errno("fmt_scaled");
2500 if (a->last_ncommits != ncommits) {
2501 print_searching = 1;
2502 a->last_ncommits = ncommits;
2505 if (a->last_nobj_total != nobj_total) {
2506 print_searching = 1;
2507 print_total = 1;
2508 a->last_nobj_total = nobj_total;
2511 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
2512 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
2513 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
2514 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2515 return got_error(GOT_ERR_NO_SPACE);
2518 if (nobj_deltify > 0 || nobj_written > 0) {
2519 if (nobj_deltify > 0) {
2520 p_deltify = (nobj_deltify * 100) / nobj_total;
2521 if (p_deltify != a->last_p_deltify) {
2522 a->last_p_deltify = p_deltify;
2523 print_searching = 1;
2524 print_total = 1;
2525 print_deltify = 1;
2528 if (nobj_written > 0) {
2529 p_written = (nobj_written * 100) / nobj_total;
2530 if (p_written != a->last_p_written) {
2531 a->last_p_written = p_written;
2532 print_searching = 1;
2533 print_total = 1;
2534 print_deltify = 1;
2535 print_written = 1;
2540 if (bytes_sent > 0) {
2541 p_sent = (bytes_sent * 100) / packfile_size;
2542 if (p_sent != a->last_p_sent) {
2543 a->last_p_sent = p_sent;
2544 print_searching = 1;
2545 print_total = 1;
2546 print_deltify = 1;
2547 print_written = 1;
2548 print_sent = 1;
2550 a->sent_something = 1;
2553 if (print_searching || print_total || print_deltify || print_written ||
2554 print_sent)
2555 printf("\r");
2556 if (print_searching)
2557 printf("packing %d reference%s", ncommits,
2558 ncommits == 1 ? "" : "s");
2559 if (print_total)
2560 printf("; %d object%s", nobj_total,
2561 nobj_total == 1 ? "" : "s");
2562 if (print_deltify)
2563 printf("; deltify: %d%%", p_deltify);
2564 if (print_sent)
2565 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2566 scaled_packsize, p_sent);
2567 else if (print_written)
2568 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
2569 scaled_packsize, p_written);
2570 if (print_searching || print_total || print_deltify ||
2571 print_written || print_sent) {
2572 a->printed_something = 1;
2573 fflush(stdout);
2575 return NULL;
2578 struct got_fetch_progress_arg {
2579 char last_scaled_size[FMT_SCALED_STRSIZE];
2580 int last_p_indexed;
2581 int last_p_resolved;
2582 int verbosity;
2584 struct got_repository *repo;
2587 static const struct got_error *
2588 fetch_progress(void *arg, const char *message, off_t packfile_size,
2589 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
2591 struct got_fetch_progress_arg *a = arg;
2592 char scaled_size[FMT_SCALED_STRSIZE];
2593 int p_indexed, p_resolved;
2594 int print_size = 0, print_indexed = 0, print_resolved = 0;
2596 if (a->verbosity < 0)
2597 return NULL;
2599 if (message && message[0] != '\0') {
2600 printf("\rserver: %s", message);
2601 fflush(stdout);
2602 return NULL;
2605 if (packfile_size > 0 || nobj_indexed > 0) {
2606 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
2607 (a->last_scaled_size[0] == '\0' ||
2608 strcmp(scaled_size, a->last_scaled_size)) != 0) {
2609 print_size = 1;
2610 if (strlcpy(a->last_scaled_size, scaled_size,
2611 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
2612 return got_error(GOT_ERR_NO_SPACE);
2614 if (nobj_indexed > 0) {
2615 p_indexed = (nobj_indexed * 100) / nobj_total;
2616 if (p_indexed != a->last_p_indexed) {
2617 a->last_p_indexed = p_indexed;
2618 print_indexed = 1;
2619 print_size = 1;
2622 if (nobj_resolved > 0) {
2623 p_resolved = (nobj_resolved * 100) /
2624 (nobj_total - nobj_loose);
2625 if (p_resolved != a->last_p_resolved) {
2626 a->last_p_resolved = p_resolved;
2627 print_resolved = 1;
2628 print_indexed = 1;
2629 print_size = 1;
2634 if (print_size || print_indexed || print_resolved)
2635 printf("\r");
2636 if (print_size)
2637 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
2638 if (print_indexed)
2639 printf("; indexing %d%%", p_indexed);
2640 if (print_resolved)
2641 printf("; resolving deltas %d%%", p_resolved);
2642 if (print_size || print_indexed || print_resolved) {
2643 putchar('\n');
2644 fflush(stdout);
2647 return NULL;
2650 static const struct got_error *
2651 create_symref(const char *refname, struct got_reference *target_ref,
2652 int verbosity, struct got_repository *repo)
2654 const struct got_error *err;
2655 struct got_reference *head_symref;
2657 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
2658 if (err)
2659 return err;
2661 err = got_ref_write(head_symref, repo);
2662 if (err == NULL && verbosity > 0) {
2663 printf("Created reference %s: %s\n", GOT_REF_HEAD,
2664 got_ref_get_name(target_ref));
2666 got_ref_close(head_symref);
2667 return err;
2670 static const struct got_error *
2671 create_ref(const char *refname, struct got_object_id *id,
2672 int verbosity, struct got_repository *repo)
2674 const struct got_error *err = NULL;
2675 struct got_reference *ref;
2676 char *id_str;
2678 err = got_object_id_str(&id_str, id);
2679 if (err)
2680 return err;
2682 err = got_ref_alloc(&ref, refname, id);
2683 if (err)
2684 goto done;
2686 err = got_ref_write(ref, repo);
2687 got_ref_close(ref);
2689 if (err == NULL && verbosity >= 0)
2690 printf("Created reference %s: %s\n", refname, id_str);
2691 done:
2692 free(id_str);
2693 return err;
2696 static const struct got_error *
2697 update_ref(struct got_reference *ref, struct got_object_id *new_id,
2698 int verbosity, struct got_repository *repo)
2700 const struct got_error *err = NULL;
2701 char *new_id_str = NULL;
2702 struct got_object_id *old_id = NULL;
2704 err = got_object_id_str(&new_id_str, new_id);
2705 if (err)
2706 goto done;
2708 if (strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
2709 err = got_ref_resolve(&old_id, repo, ref);
2710 if (err)
2711 goto done;
2712 if (got_object_id_cmp(old_id, new_id) == 0)
2713 goto done;
2714 if (verbosity >= 0) {
2715 printf("Rejecting update of existing tag %s: %s\n",
2716 got_ref_get_name(ref), new_id_str);
2718 goto done;
2721 if (got_ref_is_symbolic(ref)) {
2722 if (verbosity >= 0) {
2723 printf("Replacing reference %s: %s\n",
2724 got_ref_get_name(ref),
2725 got_ref_get_symref_target(ref));
2727 err = got_ref_change_symref_to_ref(ref, new_id);
2728 if (err)
2729 goto done;
2730 err = got_ref_write(ref, repo);
2731 if (err)
2732 goto done;
2733 } else {
2734 err = got_ref_resolve(&old_id, repo, ref);
2735 if (err)
2736 goto done;
2737 if (got_object_id_cmp(old_id, new_id) == 0)
2738 goto done;
2740 err = got_ref_change_ref(ref, new_id);
2741 if (err)
2742 goto done;
2743 err = got_ref_write(ref, repo);
2744 if (err)
2745 goto done;
2748 if (verbosity >= 0)
2749 printf("Updated %s: %s\n", got_ref_get_name(ref),
2750 new_id_str);
2751 done:
2752 free(old_id);
2753 free(new_id_str);
2754 return err;
2757 static const struct got_error *
2758 fetch_updated_remote(const char *proto, const char *host, const char *port,
2759 const char *server_path, int verbosity,
2760 const struct got_remote_repo *remote, struct got_repository *repo,
2761 struct got_reference *head_ref, const char *head_refname)
2763 const struct got_error *err = NULL, *unlock_err = NULL;
2764 struct got_pathlist_entry *pe;
2765 struct got_pathlist_head learned_refs;
2766 struct got_pathlist_head symrefs;
2767 struct got_pathlist_head wanted_branches;
2768 struct got_pathlist_head wanted_refs;
2769 struct got_object_id *pack_hash;
2770 struct got_fetch_progress_arg fpa;
2771 int fetchfd = -1;
2772 pid_t fetchpid = -1;
2774 TAILQ_INIT(&learned_refs);
2775 TAILQ_INIT(&symrefs);
2776 TAILQ_INIT(&wanted_branches);
2777 TAILQ_INIT(&wanted_refs);
2779 err = got_pathlist_insert(NULL, &wanted_branches, head_refname,
2780 NULL);
2781 if (err)
2782 goto done;
2784 err = got_fetch_connect(&fetchpid, &fetchfd, proto, host,
2785 port, server_path, verbosity);
2786 if (err)
2787 goto done;
2789 fpa.last_scaled_size[0] = '\0';
2790 fpa.last_p_indexed = -1;
2791 fpa.last_p_resolved = -1;
2792 fpa.verbosity = verbosity;
2793 fpa.repo = repo;
2795 err = got_fetch_pack(&pack_hash, &learned_refs, &symrefs,
2796 remote->name, 1, 0, &wanted_branches, &wanted_refs, 0, verbosity,
2797 fetchfd, repo, head_refname, NULL, 0, fetch_progress, &fpa);
2798 if (err)
2799 goto done;
2801 /* Update references provided with the pack file. */
2802 TAILQ_FOREACH(pe, &learned_refs, entry) {
2803 const char *refname = pe->path;
2804 struct got_object_id *id = pe->data;
2805 struct got_reference *ref;
2807 err = got_ref_open(&ref, repo, refname, 0);
2808 if (err) {
2809 if (err->code != GOT_ERR_NOT_REF)
2810 goto done;
2811 err = create_ref(refname, id, verbosity, repo);
2812 if (err)
2813 goto done;
2814 } else {
2815 err = update_ref(ref, id, verbosity, repo);
2816 unlock_err = got_ref_unlock(ref);
2817 if (unlock_err && err == NULL)
2818 err = unlock_err;
2819 got_ref_close(ref);
2820 if (err)
2821 goto done;
2825 /* Set the HEAD reference if the server provided one. */
2826 TAILQ_FOREACH(pe, &symrefs, entry) {
2827 struct got_reference *target_ref;
2828 const char *refname = pe->path;
2829 const char *target = pe->data;
2830 char *remote_refname = NULL, *remote_target = NULL;
2832 if (strcmp(refname, GOT_REF_HEAD) != 0)
2833 continue;
2835 err = got_ref_open(&target_ref, repo, target, 0);
2836 if (err) {
2837 if (err->code == GOT_ERR_NOT_REF) {
2838 err = NULL;
2839 continue;
2841 goto done;
2844 err = create_symref(refname, target_ref, verbosity, repo);
2845 got_ref_close(target_ref);
2846 if (err)
2847 goto done;
2849 if (remote->mirror_references)
2850 continue;
2852 if (strncmp("refs/heads/", target, 11) != 0)
2853 continue;
2855 if (asprintf(&remote_refname,
2856 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2857 refname) == -1) {
2858 err = got_error_from_errno("asprintf");
2859 goto done;
2861 if (asprintf(&remote_target,
2862 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
2863 target + 11) == -1) {
2864 err = got_error_from_errno("asprintf");
2865 free(remote_refname);
2866 goto done;
2868 err = got_ref_open(&target_ref, repo, remote_target, 0);
2869 if (err) {
2870 free(remote_refname);
2871 free(remote_target);
2872 if (err->code == GOT_ERR_NOT_REF) {
2873 err = NULL;
2874 continue;
2876 goto done;
2878 err = create_symref(remote_refname, target_ref,
2879 verbosity - 1, repo);
2880 free(remote_refname);
2881 free(remote_target);
2882 got_ref_close(target_ref);
2883 if (err)
2884 goto done;
2887 done:
2888 got_pathlist_free(&learned_refs, GOT_PATHLIST_FREE_NONE);
2889 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_NONE);
2890 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2891 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2892 return err;
2896 const struct got_error *
2897 got_worktree_cvg_commit(struct got_object_id **new_commit_id,
2898 struct got_worktree *worktree, struct got_pathlist_head *paths,
2899 const char *author, const char *committer, int allow_bad_symlinks,
2900 int show_diff, int commit_conflicts,
2901 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2902 got_worktree_status_cb status_cb, void *status_arg,
2903 const char *proto, const char *host, const char *port,
2904 const char *server_path, int verbosity,
2905 const struct got_remote_repo *remote,
2906 got_cancel_cb check_cancelled,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
2910 struct got_fileindex *fileindex = NULL;
2911 char *fileindex_path = NULL;
2912 struct got_pathlist_head commitable_paths;
2913 struct collect_commitables_arg cc_arg;
2914 struct got_pathlist_entry *pe;
2915 struct got_reference *head_ref = NULL, *head_ref2 = NULL;
2916 struct got_reference *commit_ref = NULL;
2917 struct got_object_id *head_commit_id = NULL;
2918 struct got_object_id *head_commit_id2 = NULL;
2919 char *head_refname = NULL;
2920 char *commit_refname = NULL;
2921 char *diff_path = NULL;
2922 int have_staged_files = 0;
2923 int sendfd = -1;
2924 pid_t sendpid = -1;
2925 struct got_send_progress_arg spa;
2926 struct got_pathlist_head commit_reflist;
2927 struct got_pathlist_head tag_names;
2928 struct got_pathlist_head delete_branches;
2930 *new_commit_id = NULL;
2932 memset(&cc_arg, 0, sizeof(cc_arg));
2933 TAILQ_INIT(&commitable_paths);
2934 TAILQ_INIT(&commit_reflist);
2935 TAILQ_INIT(&tag_names);
2936 TAILQ_INIT(&delete_branches);
2938 err = lock_worktree(worktree, LOCK_EX);
2939 if (err)
2940 goto done;
2942 err = got_worktree_cvg_get_commit_ref_name(&commit_refname,
2943 worktree);
2944 if (err)
2945 goto done;
2947 head_refname = worktree->head_ref_name;
2948 err = got_ref_open(&head_ref, repo, head_refname, 0);
2949 if (err)
2950 goto done;
2951 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2952 if (err)
2953 goto done;
2955 err = got_ref_alloc(&commit_ref, commit_refname, head_commit_id);
2956 if (err)
2957 goto done;
2958 err = got_ref_write(commit_ref, repo);
2959 if (err)
2960 goto done;
2962 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2963 if (err)
2964 goto done;
2966 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
2967 &have_staged_files);
2968 if (err && err->code != GOT_ERR_CANCELLED)
2969 goto done;
2970 if (have_staged_files) {
2971 err = check_non_staged_files(fileindex, paths);
2972 if (err)
2973 goto done;
2976 cc_arg.commitable_paths = &commitable_paths;
2977 cc_arg.worktree = worktree;
2978 cc_arg.fileindex = fileindex;
2979 cc_arg.repo = repo;
2980 cc_arg.have_staged_files = have_staged_files;
2981 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
2982 cc_arg.diff_header_shown = 0;
2983 cc_arg.commit_conflicts = commit_conflicts;
2984 if (show_diff) {
2985 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
2986 GOT_TMPDIR_STR "/got", ".diff");
2987 if (err)
2988 goto done;
2989 cc_arg.f1 = got_opentemp();
2990 if (cc_arg.f1 == NULL) {
2991 err = got_error_from_errno("got_opentemp");
2992 goto done;
2994 cc_arg.f2 = got_opentemp();
2995 if (cc_arg.f2 == NULL) {
2996 err = got_error_from_errno("got_opentemp");
2997 goto done;
3001 TAILQ_FOREACH(pe, paths, entry) {
3002 err = worktree_status(worktree, pe->path, fileindex, repo,
3003 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
3004 if (err)
3005 goto done;
3008 if (show_diff) {
3009 if (fflush(cc_arg.diff_outfile) == EOF) {
3010 err = got_error_from_errno("fflush");
3011 goto done;
3015 if (TAILQ_EMPTY(&commitable_paths)) {
3016 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3017 goto done;
3020 TAILQ_FOREACH(pe, paths, entry) {
3021 err = check_path_is_commitable(pe->path, &commitable_paths);
3022 if (err)
3023 goto done;
3026 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3027 struct got_commitable *ct = pe->data;
3028 const char *ct_path = ct->in_repo_path;
3030 while (ct_path[0] == '/')
3031 ct_path++;
3032 err = check_out_of_date(ct_path, ct->status,
3033 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3034 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3035 if (err)
3036 goto done;
3039 err = commit_worktree(new_commit_id, &commitable_paths,
3040 head_commit_id, NULL, worktree, author, committer,
3041 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
3042 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3043 if (err)
3044 goto done;
3047 * Check if a concurrent commit to our branch has occurred.
3048 * Lock the reference here to prevent concurrent modification.
3050 err = got_ref_open(&head_ref2, repo, head_refname, 1);
3051 if (err)
3052 goto done;
3053 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3054 if (err)
3055 goto done;
3056 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3057 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3058 goto done;
3061 err = got_pathlist_append(&commit_reflist, commit_refname,
3062 head_refname);
3063 if (err)
3064 goto done;
3066 /* Update commit ref in repository. */
3067 err = got_ref_change_ref(commit_ref, *new_commit_id);
3068 if (err)
3069 goto done;
3070 err = got_ref_write(commit_ref, repo);
3071 if (err)
3072 goto done;
3074 if (verbosity >= 0) {
3075 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
3076 remote->name, proto, host,
3077 port ? ":" : "", port ? port : "",
3078 *server_path == '/' ? "" : "/", server_path);
3081 /* Attempt send to remote branch. */
3082 err = got_send_connect(&sendpid, &sendfd, proto, host, port,
3083 server_path, verbosity);
3084 if (err)
3085 goto done;
3087 memset(&spa, 0, sizeof(spa));
3088 spa.last_scaled_packsize[0] = '\0';
3089 spa.last_p_deltify = -1;
3090 spa.last_p_written = -1;
3091 spa.verbosity = verbosity;
3092 spa.delete_branches = &delete_branches;
3093 err = got_send_pack(remote->name, &commit_reflist, &tag_names,
3094 &delete_branches, verbosity, 0, sendfd, repo, send_progress, &spa,
3095 check_cancelled, NULL);
3096 if (spa.printed_something)
3097 putchar('\n');
3098 if (err != NULL && err->code == GOT_ERR_SEND_ANCESTRY) {
3100 * Fetch new changes since remote has diverged.
3101 * No trivial-rebase yet; require update to be run manually.
3103 err = fetch_updated_remote(proto, host, port, server_path,
3104 verbosity, remote, repo, head_ref, head_refname);
3105 if (err == NULL)
3106 goto done;
3107 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3108 goto done;
3109 /* XXX: Rebase commit over fetched remote branch. */
3111 if (err) {
3112 goto done;
3115 /* Update branch head in repository. */
3116 err = got_ref_change_ref(head_ref2, *new_commit_id);
3117 if (err)
3118 goto done;
3119 err = got_ref_write(head_ref2, repo);
3120 if (err)
3121 goto done;
3123 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3124 if (err)
3125 goto done;
3127 err = ref_base_commit(worktree, repo);
3128 if (err)
3129 goto done;
3131 /* XXX: fileindex must be updated for other fetched changes? */
3132 err = update_fileindex_after_commit(worktree, &commitable_paths,
3133 *new_commit_id, fileindex, have_staged_files);
3134 sync_err = sync_fileindex(fileindex, fileindex_path);
3135 if (sync_err && err == NULL)
3136 err = sync_err;
3137 done:
3138 if (head_ref2) {
3139 unlockerr = got_ref_unlock(head_ref2);
3140 if (unlockerr && err == NULL)
3141 err = unlockerr;
3142 got_ref_close(head_ref2);
3144 if (commit_ref)
3145 got_ref_close(commit_ref);
3146 if (fileindex)
3147 got_fileindex_free(fileindex);
3148 unlockerr = lock_worktree(worktree, LOCK_SH);
3149 if (unlockerr && err == NULL)
3150 err = unlockerr;
3151 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3152 struct got_commitable *ct = pe->data;
3154 free_commitable(ct);
3156 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
3157 if (diff_path && unlink(diff_path) == -1 && err == NULL)
3158 err = got_error_from_errno2("unlink", diff_path);
3159 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
3160 err == NULL)
3161 err = got_error_from_errno("fclose");
3162 free(head_commit_id);
3163 free(head_commit_id2);
3164 free(commit_refname);
3165 free(fileindex_path);
3166 free(diff_path);
3167 return err;
3170 const struct got_error *
3171 got_worktree_cvg_get_commit_ref_name(char **refname,
3172 struct got_worktree *worktree)
3174 return get_ref_name(refname, worktree, GOT_WORKTREE_COMMIT_REF_PREFIX);