2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 * Copyright (C) 2023 Josh Rickmar <jrick@zettaport.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "got_compat.h"
23 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_worktree_cvg.h"
54 #include "got_commit_graph.h"
55 #include "got_fetch.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_opentemp.h"
60 #include "got_gotconfig.h"
62 #include "got_patch.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 static volatile sig_atomic_t sigint_received;
71 static volatile sig_atomic_t sigpipe_received;
74 catch_sigint(int signo)
80 catch_sigpipe(int signo)
88 const struct got_error *(*cmd_main)(int, char *[]);
89 void (*cmd_usage)(void);
90 const char *cmd_alias;
93 __dead static void usage(int, int);
94 __dead static void usage_import(void);
95 __dead static void usage_clone(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_tag(void);
104 __dead static void usage_add(void);
105 __dead static void usage_remove(void);
106 __dead static void usage_patch(void);
107 __dead static void usage_revert(void);
108 __dead static void usage_commit(void);
109 __dead static void usage_cherrypick(void);
110 __dead static void usage_backout(void);
111 __dead static void usage_cat(void);
112 __dead static void usage_info(void);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_checkout(int, char *[]);
117 static const struct got_error* cmd_update(int, char *[]);
118 static const struct got_error* cmd_log(int, char *[]);
119 static const struct got_error* cmd_diff(int, char *[]);
120 static const struct got_error* cmd_blame(int, char *[]);
121 static const struct got_error* cmd_tree(int, char *[]);
122 static const struct got_error* cmd_status(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_patch(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
132 static const struct got_error* cmd_info(int, char *[]);
134 static const struct got_cmd got_commands[] = {
135 { "import", cmd_import, usage_import, "im" },
136 { "clone", cmd_clone, usage_clone, "cl" },
137 { "checkout", cmd_checkout, usage_checkout, "co" },
138 { "update", cmd_update, usage_update, "up" },
139 { "log", cmd_log, usage_log, "" },
140 { "diff", cmd_diff, usage_diff, "di" },
141 { "blame", cmd_blame, usage_blame, "bl" },
142 { "tree", cmd_tree, usage_tree, "tr" },
143 { "status", cmd_status, usage_status, "st" },
144 { "tag", cmd_tag, usage_tag, "" },
145 { "add", cmd_add, usage_add, "" },
146 { "remove", cmd_remove, usage_remove, "rm" },
147 { "patch", cmd_patch, usage_patch, "pa" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "cat", cmd_cat, usage_cat, "" },
153 { "info", cmd_info, usage_info, "" },
157 list_commands(FILE *fp)
161 fprintf(fp, "commands:");
162 for (i = 0; i < nitems(got_commands); i++) {
163 const struct got_cmd *cmd = &got_commands[i];
164 fprintf(fp, " %s", cmd->cmd_name);
170 option_conflict(char a, char b)
172 errx(1, "-%c and -%c options are mutually exclusive", a, b);
176 main(int argc, char *argv[])
178 const struct got_cmd *cmd;
181 int hflag = 0, Vflag = 0;
182 static const struct option longopts[] = {
183 { "version", no_argument, NULL, 'V' },
187 setlocale(LC_CTYPE, "");
189 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
209 got_version_print_str();
214 usage(hflag, hflag ? 0 : 1);
216 signal(SIGINT, catch_sigint);
217 signal(SIGPIPE, catch_sigpipe);
219 for (i = 0; i < nitems(got_commands); i++) {
220 const struct got_error *error;
222 cmd = &got_commands[i];
224 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
225 strcmp(cmd->cmd_alias, argv[0]) != 0)
231 error = cmd->cmd_main(argc, argv);
232 if (error && error->code != GOT_ERR_CANCELLED &&
233 error->code != GOT_ERR_PRIVSEP_EXIT &&
234 !(sigpipe_received &&
235 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
237 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
239 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
246 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
247 list_commands(stderr);
252 usage(int hflag, int status)
254 FILE *fp = (status == 0) ? stdout : stderr;
256 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
271 editor = getenv("VISUAL");
273 editor = getenv("EDITOR");
276 err = got_path_find_prog(abspath, editor);
281 if (*abspath == NULL) {
282 *abspath = strdup("/usr/bin/vi");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
322 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
323 "[-r repository-path] directory\n", getprogname());
328 spawn_editor(const char *editor, const char *file)
331 sig_t sighup, sigint, sigquit;
334 sighup = signal(SIGHUP, SIG_IGN);
335 sigint = signal(SIGINT, SIG_IGN);
336 sigquit = signal(SIGQUIT, SIG_IGN);
338 switch (pid = fork()) {
342 execl(editor, editor, file, (char *)NULL);
346 while (waitpid(pid, &st, 0) == -1)
351 (void)signal(SIGHUP, sighup);
352 (void)signal(SIGINT, sigint);
353 (void)signal(SIGQUIT, sigquit);
355 if (!WIFEXITED(st)) {
360 return WEXITSTATUS(st);
363 static const struct got_error *
364 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
366 const struct got_error *err = NULL;
373 if (fseeko(fp, 0L, SEEK_SET) == -1)
374 return got_error_from_errno("fseeko");
376 *logmsg = malloc(filesize + 1);
378 return got_error_from_errno("malloc");
381 while (getline(&line, &linesize, fp) != -1) {
382 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
383 continue; /* remove comments and leading empty lines */
384 *len = strlcat(*logmsg, line, filesize + 1);
385 if (*len >= filesize + 1) {
386 err = got_error(GOT_ERR_NO_SPACE);
391 err = got_ferror(fp, GOT_ERR_IO);
395 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
396 (*logmsg)[*len - 1] = '\0';
409 static const struct got_error *
410 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
411 const char *initial_content, size_t initial_content_len,
412 int require_modification)
414 const struct got_error *err = NULL;
421 if (stat(logmsg_path, &st) == -1)
422 return got_error_from_errno2("stat", logmsg_path);
424 if (spawn_editor(editor, logmsg_path) == -1)
425 return got_error_from_errno("failed spawning editor");
427 if (require_modification) {
428 struct timespec timeout;
432 nanosleep(&timeout, NULL);
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno2("stat", logmsg_path);
438 if (require_modification && st.st_size == st2.st_size &&
439 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
440 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 "no changes made to commit message, aborting");
443 fp = fopen(logmsg_path, "re");
445 err = got_error_from_errno("fopen");
449 /* strip comments and leading/trailing newlines */
450 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
453 if (logmsg_len == 0) {
454 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
455 "commit message cannot be empty, aborting");
459 if (fp && fclose(fp) == EOF && err == NULL)
460 err = got_error_from_errno("fclose");
468 static const struct got_error *
469 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 const char *path_dir, const char *branch_name)
472 char *initial_content = NULL;
473 const struct got_error *err = NULL;
474 int initial_content_len;
477 initial_content_len = asprintf(&initial_content,
478 "\n# %s to be imported to branch %s\n", path_dir,
480 if (initial_content_len == -1)
481 return got_error_from_errno("asprintf");
483 err = got_opentemp_named_fd(logmsg_path, &fd,
484 GOT_TMPDIR_STR "/got-importmsg", "");
488 if (write(fd, initial_content, initial_content_len) == -1) {
489 err = got_error_from_errno2("write", *logmsg_path);
492 if (close(fd) == -1) {
493 err = got_error_from_errno2("close", *logmsg_path);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
499 initial_content_len, 1);
501 if (fd != -1 && close(fd) == -1 && err == NULL)
502 err = got_error_from_errno2("close", *logmsg_path);
503 free(initial_content);
511 static const struct got_error *
512 import_progress(void *arg, const char *path)
514 printf("A %s\n", path);
518 static const struct got_error *
519 valid_author(const char *author)
521 const char *email = author;
524 * Git' expects the author (or committer) to be in the form
525 * "name <email>", which are mostly free form (see the
526 * "committer" description in git-fast-import(1)). We're only
527 * doing this to avoid git's object parser breaking on commits
531 while (*author && *author != '\n' && *author != '<' && *author != '>')
533 if (author != email && *author == '<' && *(author - 1) != ' ')
534 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
535 "between author name and email required", email);
536 if (*author++ != '<')
537 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
538 while (*author && *author != '\n' && *author != '<' && *author != '>')
540 if (strcmp(author, ">") != 0)
541 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
545 static const struct got_error *
546 get_author(char **author, struct got_repository *repo,
547 struct got_worktree *worktree)
549 const struct got_error *err = NULL;
550 const char *got_author = NULL, *name, *email;
551 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
556 worktree_conf = got_worktree_get_gotconfig(worktree);
557 repo_conf = got_repo_get_gotconfig(repo);
560 * Priority of potential author information sources, from most
561 * significant to least significant:
562 * 1) work tree's .got/got.conf file
563 * 2) repository's got.conf file
564 * 3) repository's git config file
565 * 4) environment variables
566 * 5) global git config files (in user's home directory or /etc)
570 got_author = got_gotconfig_get_author(worktree_conf);
571 if (got_author == NULL)
572 got_author = got_gotconfig_get_author(repo_conf);
573 if (got_author == NULL) {
574 name = got_repo_get_gitconfig_author_name(repo);
575 email = got_repo_get_gitconfig_author_email(repo);
577 if (asprintf(author, "%s <%s>", name, email) == -1)
578 return got_error_from_errno("asprintf");
582 got_author = getenv("GOT_AUTHOR");
583 if (got_author == NULL) {
584 name = got_repo_get_global_gitconfig_author_name(repo);
585 email = got_repo_get_global_gitconfig_author_email(
588 if (asprintf(author, "%s <%s>", name, email)
590 return got_error_from_errno("asprintf");
593 /* TODO: Look up user in password database? */
594 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
598 *author = strdup(got_author);
600 return got_error_from_errno("strdup");
602 err = valid_author(*author);
610 static const struct got_error *
611 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
612 struct got_worktree *worktree)
614 const char *got_allowed_signers = NULL;
615 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
617 *allowed_signers = NULL;
620 worktree_conf = got_worktree_get_gotconfig(worktree);
621 repo_conf = got_repo_get_gotconfig(repo);
624 * Priority of potential author information sources, from most
625 * significant to least significant:
626 * 1) work tree's .got/got.conf file
627 * 2) repository's got.conf file
631 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
633 if (got_allowed_signers == NULL)
634 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
637 if (got_allowed_signers) {
638 *allowed_signers = strdup(got_allowed_signers);
639 if (*allowed_signers == NULL)
640 return got_error_from_errno("strdup");
645 static const struct got_error *
646 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
647 struct got_worktree *worktree)
649 const char *got_revoked_signers = NULL;
650 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
652 *revoked_signers = NULL;
655 worktree_conf = got_worktree_get_gotconfig(worktree);
656 repo_conf = got_repo_get_gotconfig(repo);
659 * Priority of potential author information sources, from most
660 * significant to least significant:
661 * 1) work tree's .got/got.conf file
662 * 2) repository's got.conf file
666 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
668 if (got_revoked_signers == NULL)
669 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
672 if (got_revoked_signers) {
673 *revoked_signers = strdup(got_revoked_signers);
674 if (*revoked_signers == NULL)
675 return got_error_from_errno("strdup");
681 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
683 const char *got_signer_id = NULL;
684 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
687 worktree_conf = got_worktree_get_gotconfig(worktree);
688 repo_conf = got_repo_get_gotconfig(repo);
691 * Priority of potential author information sources, from most
692 * significant to least significant:
693 * 1) work tree's .got/got.conf file
694 * 2) repository's got.conf file
698 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
699 if (got_signer_id == NULL)
700 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
702 return got_signer_id;
705 static const struct got_error *
706 get_gitconfig_path(char **gitconfig_path)
708 const char *homedir = getenv("HOME");
710 *gitconfig_path = NULL;
712 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
713 return got_error_from_errno("asprintf");
719 static const struct got_error *
720 cmd_import(int argc, char *argv[])
722 const struct got_error *error = NULL;
723 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
724 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
725 const char *branch_name = NULL;
726 char *id_str = NULL, *logmsg_path = NULL;
727 char refname[PATH_MAX] = "refs/heads/";
728 struct got_repository *repo = NULL;
729 struct got_reference *branch_ref = NULL, *head_ref = NULL;
730 struct got_object_id *new_commit_id = NULL;
732 struct got_pathlist_head ignores;
733 struct got_pathlist_entry *pe;
734 int preserve_logmsg = 0;
735 int *pack_fds = NULL;
737 TAILQ_INIT(&ignores);
740 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
746 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
749 branch_name = optarg;
752 if (optarg[0] == '\0')
754 error = got_pathlist_insert(&pe, &ignores, optarg,
760 logmsg = strdup(optarg);
761 if (logmsg == NULL) {
762 error = got_error_from_errno("strdup");
767 repo_path = realpath(optarg, NULL);
768 if (repo_path == NULL) {
769 error = got_error_from_errno2("realpath",
786 if (repo_path == NULL) {
787 repo_path = getcwd(NULL, 0);
788 if (repo_path == NULL)
789 return got_error_from_errno("getcwd");
791 got_path_strip_trailing_slashes(repo_path);
792 error = get_gitconfig_path(&gitconfig_path);
795 error = got_repo_pack_fds_open(&pack_fds);
798 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
802 error = get_author(&author, repo, NULL);
807 * Don't let the user create a branch name with a leading '-'.
808 * While technically a valid reference name, this case is usually
809 * an unintended typo.
811 if (branch_name && branch_name[0] == '-')
812 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
814 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
815 if (error && error->code != GOT_ERR_NOT_REF)
819 n = strlcat(refname, branch_name, sizeof(refname));
820 else if (head_ref && got_ref_is_symbolic(head_ref))
821 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
824 n = strlcat(refname, "main", sizeof(refname));
825 if (n >= sizeof(refname)) {
826 error = got_error(GOT_ERR_NO_SPACE);
830 error = got_ref_open(&branch_ref, repo, refname, 0);
832 if (error->code != GOT_ERR_NOT_REF)
835 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
836 "import target branch already exists");
840 path_dir = realpath(argv[0], NULL);
841 if (path_dir == NULL) {
842 error = got_error_from_errno2("realpath", argv[0]);
845 got_path_strip_trailing_slashes(path_dir);
848 * unveil(2) traverses exec(2); if an editor is used we have
849 * to apply unveil after the log message has been written.
851 if (logmsg == NULL || *logmsg == '\0') {
852 error = get_editor(&editor);
856 error = collect_import_msg(&logmsg, &logmsg_path, editor,
859 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
866 if (unveil(path_dir, "r") != 0) {
867 error = got_error_from_errno2("unveil", path_dir);
873 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
880 error = got_repo_import(&new_commit_id, path_dir, logmsg,
881 author, &ignores, repo, import_progress, NULL);
888 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
895 error = got_ref_write(branch_ref, repo);
902 error = got_object_id_str(&id_str, new_commit_id);
909 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
911 if (error->code != GOT_ERR_NOT_REF) {
917 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
925 error = got_ref_write(head_ref, repo);
933 printf("Created branch %s with commit %s\n",
934 got_ref_get_name(branch_ref), id_str);
937 const struct got_error *pack_err =
938 got_repo_pack_fds_close(pack_fds);
943 const struct got_error *close_err = got_repo_close(repo);
947 if (preserve_logmsg) {
948 fprintf(stderr, "%s: log message preserved in %s\n",
949 getprogname(), logmsg_path);
950 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
951 error = got_error_from_errno2("unlink", logmsg_path);
959 free(gitconfig_path);
961 got_ref_close(branch_ref);
963 got_ref_close(head_ref);
970 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
971 "repository-URL [directory]\n", getprogname());
975 struct got_fetch_progress_arg {
976 char last_scaled_size[FMT_SCALED_STRSIZE];
981 struct got_repository *repo;
986 struct got_pathlist_head *symrefs;
987 struct got_pathlist_head *wanted_branches;
988 struct got_pathlist_head *wanted_refs;
992 const char *remote_repo_path;
994 int fetch_all_branches;
995 int mirror_references;
999 /* XXX forward declaration */
1000 static const struct got_error *
1001 create_config_files(const char *proto, const char *host, const char *port,
1002 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1003 int mirror_references, struct got_pathlist_head *symrefs,
1004 struct got_pathlist_head *wanted_branches,
1005 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1007 static const struct got_error *
1008 fetch_progress(void *arg, const char *message, off_t packfile_size,
1009 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1011 const struct got_error *err = NULL;
1012 struct got_fetch_progress_arg *a = arg;
1013 char scaled_size[FMT_SCALED_STRSIZE];
1014 int p_indexed, p_resolved;
1015 int print_size = 0, print_indexed = 0, print_resolved = 0;
1018 * In order to allow a failed clone to be resumed with 'got fetch'
1019 * we try to create configuration files as soon as possible.
1020 * Once the server has sent information about its default branch
1021 * we have all required information.
1023 if (a->create_configs && !a->configs_created &&
1024 !TAILQ_EMPTY(a->config_info.symrefs)) {
1025 err = create_config_files(a->config_info.proto,
1026 a->config_info.host, a->config_info.port,
1027 a->config_info.remote_repo_path,
1028 a->config_info.git_url,
1029 a->config_info.fetch_all_branches,
1030 a->config_info.mirror_references,
1031 a->config_info.symrefs,
1032 a->config_info.wanted_branches,
1033 a->config_info.wanted_refs, a->repo);
1036 a->configs_created = 1;
1039 if (a->verbosity < 0)
1042 if (message && message[0] != '\0') {
1043 printf("\rserver: %s", message);
1048 if (packfile_size > 0 || nobj_indexed > 0) {
1049 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1050 (a->last_scaled_size[0] == '\0' ||
1051 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1053 if (strlcpy(a->last_scaled_size, scaled_size,
1054 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1055 return got_error(GOT_ERR_NO_SPACE);
1057 if (nobj_indexed > 0) {
1058 p_indexed = (nobj_indexed * 100) / nobj_total;
1059 if (p_indexed != a->last_p_indexed) {
1060 a->last_p_indexed = p_indexed;
1065 if (nobj_resolved > 0) {
1066 p_resolved = (nobj_resolved * 100) /
1067 (nobj_total - nobj_loose);
1068 if (p_resolved != a->last_p_resolved) {
1069 a->last_p_resolved = p_resolved;
1077 if (print_size || print_indexed || print_resolved)
1080 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1082 printf("; indexing %d%%", p_indexed);
1084 printf("; resolving deltas %d%%", p_resolved);
1085 if (print_size || print_indexed || print_resolved)
1091 static const struct got_error *
1092 create_symref(const char *refname, struct got_reference *target_ref,
1093 int verbosity, struct got_repository *repo)
1095 const struct got_error *err;
1096 struct got_reference *head_symref;
1098 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1102 err = got_ref_write(head_symref, repo);
1103 if (err == NULL && verbosity > 0) {
1104 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1105 got_ref_get_name(target_ref));
1107 got_ref_close(head_symref);
1111 static const struct got_error *
1112 list_remote_refs(struct got_pathlist_head *symrefs,
1113 struct got_pathlist_head *refs)
1115 const struct got_error *err;
1116 struct got_pathlist_entry *pe;
1118 TAILQ_FOREACH(pe, symrefs, entry) {
1119 const char *refname = pe->path;
1120 const char *targetref = pe->data;
1122 printf("%s: %s\n", refname, targetref);
1125 TAILQ_FOREACH(pe, refs, entry) {
1126 const char *refname = pe->path;
1127 struct got_object_id *id = pe->data;
1130 err = got_object_id_str(&id_str, id);
1133 printf("%s: %s\n", refname, id_str);
1140 static const struct got_error *
1141 create_ref(const char *refname, struct got_object_id *id,
1142 int verbosity, struct got_repository *repo)
1144 const struct got_error *err = NULL;
1145 struct got_reference *ref;
1148 err = got_object_id_str(&id_str, id);
1152 err = got_ref_alloc(&ref, refname, id);
1156 err = got_ref_write(ref, repo);
1159 if (err == NULL && verbosity >= 0)
1160 printf("Created reference %s: %s\n", refname, id_str);
1167 match_wanted_ref(const char *refname, const char *wanted_ref)
1169 if (strncmp(refname, "refs/", 5) != 0)
1174 * Prevent fetching of references that won't make any
1175 * sense outside of the remote repository's context.
1177 if (strncmp(refname, "got/", 4) == 0)
1179 if (strncmp(refname, "remotes/", 8) == 0)
1182 if (strncmp(wanted_ref, "refs/", 5) == 0)
1185 /* Allow prefix match. */
1186 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1189 /* Allow exact match. */
1190 return (strcmp(refname, wanted_ref) == 0);
1194 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1196 struct got_pathlist_entry *pe;
1198 TAILQ_FOREACH(pe, wanted_refs, entry) {
1199 if (match_wanted_ref(refname, pe->path))
1206 static const struct got_error *
1207 create_wanted_ref(const char *refname, struct got_object_id *id,
1208 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1210 const struct got_error *err;
1211 char *remote_refname;
1213 if (strncmp("refs/", refname, 5) == 0)
1216 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1217 remote_repo_name, refname) == -1)
1218 return got_error_from_errno("asprintf");
1220 err = create_ref(remote_refname, id, verbosity, repo);
1221 free(remote_refname);
1225 static const struct got_error *
1226 create_gotconfig(const char *proto, const char *host, const char *port,
1227 const char *remote_repo_path, const char *default_branch,
1228 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1229 struct got_pathlist_head *wanted_refs, int mirror_references,
1230 struct got_repository *repo)
1232 const struct got_error *err = NULL;
1233 char *gotconfig_path = NULL;
1234 char *gotconfig = NULL;
1235 FILE *gotconfig_file = NULL;
1236 const char *branchname = NULL;
1237 char *branches = NULL, *refs = NULL;
1240 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1241 struct got_pathlist_entry *pe;
1242 TAILQ_FOREACH(pe, wanted_branches, entry) {
1244 branchname = pe->path;
1245 if (strncmp(branchname, "refs/heads/", 11) == 0)
1247 if (asprintf(&s, "%s\"%s\" ",
1248 branches ? branches : "", branchname) == -1) {
1249 err = got_error_from_errno("asprintf");
1255 } else if (!fetch_all_branches && default_branch) {
1256 branchname = default_branch;
1257 if (strncmp(branchname, "refs/heads/", 11) == 0)
1259 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1260 err = got_error_from_errno("asprintf");
1264 if (!TAILQ_EMPTY(wanted_refs)) {
1265 struct got_pathlist_entry *pe;
1266 TAILQ_FOREACH(pe, wanted_refs, entry) {
1268 const char *refname = pe->path;
1269 if (strncmp(refname, "refs/", 5) == 0)
1271 if (asprintf(&s, "%s\"%s\" ",
1272 refs ? refs : "", refname) == -1) {
1273 err = got_error_from_errno("asprintf");
1281 /* Create got.conf(5). */
1282 gotconfig_path = got_repo_get_path_gotconfig(repo);
1283 if (gotconfig_path == NULL) {
1284 err = got_error_from_errno("got_repo_get_path_gotconfig");
1287 gotconfig_file = fopen(gotconfig_path, "ae");
1288 if (gotconfig_file == NULL) {
1289 err = got_error_from_errno2("fopen", gotconfig_path);
1292 if (asprintf(&gotconfig,
1297 "\trepository \"%s\"\n"
1303 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1304 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1305 remote_repo_path, branches ? "\tbranch { " : "",
1306 branches ? branches : "", branches ? "}\n" : "",
1307 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1308 mirror_references ? "\tmirror_references yes\n" : "",
1309 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1310 err = got_error_from_errno("asprintf");
1313 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1314 if (n != strlen(gotconfig)) {
1315 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1320 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1321 err = got_error_from_errno2("fclose", gotconfig_path);
1322 free(gotconfig_path);
1327 static const struct got_error *
1328 create_gitconfig(const char *git_url, const char *default_branch,
1329 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1330 struct got_pathlist_head *wanted_refs, int mirror_references,
1331 struct got_repository *repo)
1333 const struct got_error *err = NULL;
1334 char *gitconfig_path = NULL;
1335 char *gitconfig = NULL;
1336 FILE *gitconfig_file = NULL;
1337 char *branches = NULL, *refs = NULL;
1338 const char *branchname;
1341 /* Create a config file Git can understand. */
1342 gitconfig_path = got_repo_get_path_gitconfig(repo);
1343 if (gitconfig_path == NULL) {
1344 err = got_error_from_errno("got_repo_get_path_gitconfig");
1347 gitconfig_file = fopen(gitconfig_path, "ae");
1348 if (gitconfig_file == NULL) {
1349 err = got_error_from_errno2("fopen", gitconfig_path);
1352 if (fetch_all_branches) {
1353 if (mirror_references) {
1354 if (asprintf(&branches,
1355 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1356 err = got_error_from_errno("asprintf");
1359 } else if (asprintf(&branches,
1360 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1361 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1362 err = got_error_from_errno("asprintf");
1365 } else if (!TAILQ_EMPTY(wanted_branches)) {
1366 struct got_pathlist_entry *pe;
1367 TAILQ_FOREACH(pe, wanted_branches, entry) {
1369 branchname = pe->path;
1370 if (strncmp(branchname, "refs/heads/", 11) == 0)
1372 if (mirror_references) {
1374 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1375 branches ? branches : "",
1376 branchname, branchname) == -1) {
1377 err = got_error_from_errno("asprintf");
1380 } else if (asprintf(&s,
1381 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1382 branches ? branches : "",
1383 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1384 branchname) == -1) {
1385 err = got_error_from_errno("asprintf");
1393 * If the server specified a default branch, use just that one.
1394 * Otherwise fall back to fetching all branches on next fetch.
1396 if (default_branch) {
1397 branchname = default_branch;
1398 if (strncmp(branchname, "refs/heads/", 11) == 0)
1401 branchname = "*"; /* fall back to all branches */
1402 if (mirror_references) {
1403 if (asprintf(&branches,
1404 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1405 branchname, branchname) == -1) {
1406 err = got_error_from_errno("asprintf");
1409 } else if (asprintf(&branches,
1410 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1411 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1412 branchname) == -1) {
1413 err = got_error_from_errno("asprintf");
1417 if (!TAILQ_EMPTY(wanted_refs)) {
1418 struct got_pathlist_entry *pe;
1419 TAILQ_FOREACH(pe, wanted_refs, entry) {
1421 const char *refname = pe->path;
1422 if (strncmp(refname, "refs/", 5) == 0)
1424 if (mirror_references) {
1426 "%s\tfetch = refs/%s:refs/%s\n",
1427 refs ? refs : "", refname, refname) == -1) {
1428 err = got_error_from_errno("asprintf");
1431 } else if (asprintf(&s,
1432 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1434 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1436 err = got_error_from_errno("asprintf");
1444 if (asprintf(&gitconfig,
1449 "\tfetch = refs/tags/*:refs/tags/*\n",
1450 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1451 refs ? refs : "") == -1) {
1452 err = got_error_from_errno("asprintf");
1455 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1456 if (n != strlen(gitconfig)) {
1457 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1461 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1462 err = got_error_from_errno2("fclose", gitconfig_path);
1463 free(gitconfig_path);
1468 static const struct got_error *
1469 create_config_files(const char *proto, const char *host, const char *port,
1470 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1471 int mirror_references, struct got_pathlist_head *symrefs,
1472 struct got_pathlist_head *wanted_branches,
1473 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1475 const struct got_error *err = NULL;
1476 const char *default_branch = NULL;
1477 struct got_pathlist_entry *pe;
1480 * If we asked for a set of wanted branches then use the first
1483 if (!TAILQ_EMPTY(wanted_branches)) {
1484 pe = TAILQ_FIRST(wanted_branches);
1485 default_branch = pe->path;
1487 /* First HEAD ref listed by server is the default branch. */
1488 TAILQ_FOREACH(pe, symrefs, entry) {
1489 const char *refname = pe->path;
1490 const char *target = pe->data;
1492 if (strcmp(refname, GOT_REF_HEAD) != 0)
1495 default_branch = target;
1500 /* Create got.conf(5). */
1501 err = create_gotconfig(proto, host, port, remote_repo_path,
1502 default_branch, fetch_all_branches, wanted_branches,
1503 wanted_refs, mirror_references, repo);
1507 /* Create a config file Git can understand. */
1508 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1509 wanted_branches, wanted_refs, mirror_references, repo);
1512 static const struct got_error *
1513 cmd_clone(int argc, char *argv[])
1515 const struct got_error *error = NULL;
1516 const char *uri, *dirname;
1517 char *proto, *host, *port, *repo_name, *server_path;
1518 char *default_destdir = NULL, *id_str = NULL;
1519 const char *repo_path;
1520 struct got_repository *repo = NULL;
1521 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1522 struct got_pathlist_entry *pe;
1523 struct got_object_id *pack_hash = NULL;
1524 int ch, fetchfd = -1, fetchstatus;
1525 pid_t fetchpid = -1;
1526 struct got_fetch_progress_arg fpa;
1527 char *git_url = NULL;
1528 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1529 int bflag = 0, list_refs_only = 0;
1530 int *pack_fds = NULL;
1533 TAILQ_INIT(&symrefs);
1534 TAILQ_INIT(&wanted_branches);
1535 TAILQ_INIT(&wanted_refs);
1537 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1540 fetch_all_branches = 1;
1543 error = got_pathlist_append(&wanted_branches,
1553 mirror_references = 1;
1559 error = got_pathlist_append(&wanted_refs,
1567 else if (verbosity < 3)
1578 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1579 option_conflict('a', 'b');
1580 if (list_refs_only) {
1581 if (!TAILQ_EMPTY(&wanted_branches))
1582 option_conflict('l', 'b');
1583 if (fetch_all_branches)
1584 option_conflict('l', 'a');
1585 if (mirror_references)
1586 option_conflict('l', 'm');
1587 if (!TAILQ_EMPTY(&wanted_refs))
1588 option_conflict('l', 'R');
1600 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1605 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1606 host, port ? ":" : "", port ? port : "",
1607 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1608 error = got_error_from_errno("asprintf");
1612 if (strcmp(proto, "git") == 0) {
1614 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1615 "sendfd dns inet unveil", NULL) == -1)
1618 } else if (strcmp(proto, "git+ssh") == 0 ||
1619 strcmp(proto, "ssh") == 0) {
1621 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1622 "sendfd unveil", NULL) == -1)
1625 } else if (strcmp(proto, "http") == 0 ||
1626 strcmp(proto, "git+http") == 0) {
1627 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1630 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1633 if (dirname == NULL) {
1634 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1635 error = got_error_from_errno("asprintf");
1638 repo_path = default_destdir;
1640 repo_path = dirname;
1642 if (!list_refs_only) {
1643 error = got_path_mkdir(repo_path);
1645 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1646 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1648 if (!got_path_dir_is_empty(repo_path)) {
1649 error = got_error_path(repo_path,
1650 GOT_ERR_DIR_NOT_EMPTY);
1655 error = got_dial_apply_unveil(proto);
1659 error = apply_unveil(repo_path, 0, NULL);
1664 printf("Connecting to %s\n", git_url);
1666 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1667 server_path, verbosity);
1671 if (!list_refs_only) {
1672 error = got_repo_init(repo_path, NULL);
1675 error = got_repo_pack_fds_open(&pack_fds);
1678 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1683 fpa.last_scaled_size[0] = '\0';
1684 fpa.last_p_indexed = -1;
1685 fpa.last_p_resolved = -1;
1686 fpa.verbosity = verbosity;
1687 fpa.create_configs = 1;
1688 fpa.configs_created = 0;
1690 fpa.config_info.symrefs = &symrefs;
1691 fpa.config_info.wanted_branches = &wanted_branches;
1692 fpa.config_info.wanted_refs = &wanted_refs;
1693 fpa.config_info.proto = proto;
1694 fpa.config_info.host = host;
1695 fpa.config_info.port = port;
1696 fpa.config_info.remote_repo_path = server_path;
1697 fpa.config_info.git_url = git_url;
1698 fpa.config_info.fetch_all_branches = fetch_all_branches;
1699 fpa.config_info.mirror_references = mirror_references;
1700 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1701 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1702 fetch_all_branches, &wanted_branches, &wanted_refs,
1703 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1704 fetch_progress, &fpa);
1708 if (list_refs_only) {
1709 error = list_remote_refs(&symrefs, &refs);
1713 if (pack_hash == NULL) {
1714 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1715 "server sent an empty pack file");
1718 error = got_object_id_str(&id_str, pack_hash);
1722 printf("\nFetched %s.pack\n", id_str);
1725 /* Set up references provided with the pack file. */
1726 TAILQ_FOREACH(pe, &refs, entry) {
1727 const char *refname = pe->path;
1728 struct got_object_id *id = pe->data;
1729 char *remote_refname;
1731 if (is_wanted_ref(&wanted_refs, refname) &&
1732 !mirror_references) {
1733 error = create_wanted_ref(refname, id,
1734 GOT_FETCH_DEFAULT_REMOTE_NAME,
1735 verbosity - 1, repo);
1741 error = create_ref(refname, id, verbosity - 1, repo);
1745 if (mirror_references)
1748 if (strncmp("refs/heads/", refname, 11) != 0)
1751 if (asprintf(&remote_refname,
1752 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 refname + 11) == -1) {
1754 error = got_error_from_errno("asprintf");
1757 error = create_ref(remote_refname, id, verbosity - 1, repo);
1758 free(remote_refname);
1763 /* Set the HEAD reference if the server provided one. */
1764 TAILQ_FOREACH(pe, &symrefs, entry) {
1765 struct got_reference *target_ref;
1766 const char *refname = pe->path;
1767 const char *target = pe->data;
1768 char *remote_refname = NULL, *remote_target = NULL;
1770 if (strcmp(refname, GOT_REF_HEAD) != 0)
1773 error = got_ref_open(&target_ref, repo, target, 0);
1775 if (error->code == GOT_ERR_NOT_REF) {
1782 error = create_symref(refname, target_ref, verbosity, repo);
1783 got_ref_close(target_ref);
1787 if (mirror_references)
1790 if (strncmp("refs/heads/", target, 11) != 0)
1793 if (asprintf(&remote_refname,
1794 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1796 error = got_error_from_errno("asprintf");
1799 if (asprintf(&remote_target,
1800 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1801 target + 11) == -1) {
1802 error = got_error_from_errno("asprintf");
1803 free(remote_refname);
1806 error = got_ref_open(&target_ref, repo, remote_target, 0);
1808 free(remote_refname);
1809 free(remote_target);
1810 if (error->code == GOT_ERR_NOT_REF) {
1816 error = create_symref(remote_refname, target_ref,
1817 verbosity - 1, repo);
1818 free(remote_refname);
1819 free(remote_target);
1820 got_ref_close(target_ref);
1826 * We failed to set the HEAD reference. If we asked for
1827 * a set of wanted branches use the first of one of those
1828 * which could be fetched instead.
1830 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1831 const char *target = pe->path;
1832 struct got_reference *target_ref;
1834 error = got_ref_open(&target_ref, repo, target, 0);
1836 if (error->code == GOT_ERR_NOT_REF) {
1843 error = create_symref(GOT_REF_HEAD, target_ref,
1845 got_ref_close(target_ref);
1851 if (!fpa.configs_created && pe != NULL) {
1852 error = create_config_files(fpa.config_info.proto,
1853 fpa.config_info.host, fpa.config_info.port,
1854 fpa.config_info.remote_repo_path,
1855 fpa.config_info.git_url,
1856 fpa.config_info.fetch_all_branches,
1857 fpa.config_info.mirror_references,
1858 fpa.config_info.symrefs,
1859 fpa.config_info.wanted_branches,
1860 fpa.config_info.wanted_refs, fpa.repo);
1867 printf("Created %s repository '%s'\n",
1868 mirror_references ? "mirrored" : "cloned", repo_path);
1871 const struct got_error *pack_err =
1872 got_repo_pack_fds_close(pack_fds);
1877 if (kill(fetchpid, SIGTERM) == -1)
1878 error = got_error_from_errno("kill");
1879 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1880 error = got_error_from_errno("waitpid");
1882 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1883 error = got_error_from_errno("close");
1885 const struct got_error *close_err = got_repo_close(repo);
1889 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1890 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1891 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1892 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1899 free(default_destdir);
1904 static const struct got_error *
1905 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1906 int replace_tags, int verbosity, struct got_repository *repo)
1908 const struct got_error *err = NULL;
1909 char *new_id_str = NULL;
1910 struct got_object_id *old_id = NULL;
1912 err = got_object_id_str(&new_id_str, new_id);
1916 if (!replace_tags &&
1917 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1918 err = got_ref_resolve(&old_id, repo, ref);
1921 if (got_object_id_cmp(old_id, new_id) == 0)
1923 if (verbosity >= 0) {
1924 printf("Rejecting update of existing tag %s: %s\n",
1925 got_ref_get_name(ref), new_id_str);
1930 if (got_ref_is_symbolic(ref)) {
1931 if (verbosity >= 0) {
1932 printf("Replacing reference %s: %s\n",
1933 got_ref_get_name(ref),
1934 got_ref_get_symref_target(ref));
1936 err = got_ref_change_symref_to_ref(ref, new_id);
1939 err = got_ref_write(ref, repo);
1943 err = got_ref_resolve(&old_id, repo, ref);
1946 if (got_object_id_cmp(old_id, new_id) == 0)
1949 err = got_ref_change_ref(ref, new_id);
1952 err = got_ref_write(ref, repo);
1958 printf("Updated %s: %s\n", got_ref_get_name(ref),
1966 static const struct got_error *
1967 update_wanted_ref(const char *refname, struct got_object_id *id,
1968 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1970 const struct got_error *err, *unlock_err;
1971 char *remote_refname;
1972 struct got_reference *ref;
1974 if (strncmp("refs/", refname, 5) == 0)
1977 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1978 remote_repo_name, refname) == -1)
1979 return got_error_from_errno("asprintf");
1981 err = got_ref_open(&ref, repo, remote_refname, 1);
1983 if (err->code != GOT_ERR_NOT_REF)
1985 err = create_ref(remote_refname, id, verbosity, repo);
1987 err = update_ref(ref, id, 0, verbosity, repo);
1988 unlock_err = got_ref_unlock(ref);
1989 if (unlock_err && err == NULL)
1994 free(remote_refname);
1999 usage_checkout(void)
2001 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2002 "[-p path-prefix] repository-path [work-tree-path]\n",
2008 show_worktree_base_ref_warning(void)
2010 fprintf(stderr, "%s: warning: could not create a reference "
2011 "to the work tree's base commit; the commit could be "
2012 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2013 "repository writable and running 'got update' will prevent this\n",
2017 struct got_checkout_progress_arg {
2018 const char *worktree_path;
2019 int had_base_commit_ref_error;
2023 static const struct got_error *
2024 checkout_progress(void *arg, unsigned char status, const char *path)
2026 struct got_checkout_progress_arg *a = arg;
2028 /* Base commit bump happens silently. */
2029 if (status == GOT_STATUS_BUMP_BASE)
2032 if (status == GOT_STATUS_BASE_REF_ERR) {
2033 a->had_base_commit_ref_error = 1;
2037 while (path[0] == '/')
2040 if (a->verbosity >= 0)
2041 printf("%c %s/%s\n", status, a->worktree_path, path);
2046 static const struct got_error *
2047 check_cancelled(void *arg)
2049 if (sigint_received || sigpipe_received)
2050 return got_error(GOT_ERR_CANCELLED);
2054 static const struct got_error *
2055 check_linear_ancestry(struct got_object_id *commit_id,
2056 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2057 struct got_repository *repo)
2059 const struct got_error *err = NULL;
2060 struct got_object_id *yca_id;
2062 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2063 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2068 return got_error(GOT_ERR_ANCESTRY);
2071 * Require a straight line of history between the target commit
2072 * and the work tree's base commit.
2074 * Non-linear situations such as this require a rebase:
2076 * (commit) D F (base_commit)
2084 * 'got update' only handles linear cases:
2085 * Update forwards in time: A (base/yca) - B - C - D (commit)
2086 * Update backwards in time: D (base) - C - B - A (commit/yca)
2088 if (allow_forwards_in_time_only) {
2089 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2090 return got_error(GOT_ERR_ANCESTRY);
2091 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2092 got_object_id_cmp(base_commit_id, yca_id) != 0)
2093 return got_error(GOT_ERR_ANCESTRY);
2099 static const struct got_error *
2100 check_same_branch(struct got_object_id *commit_id,
2101 struct got_reference *head_ref, struct got_repository *repo)
2103 const struct got_error *err = NULL;
2104 struct got_commit_graph *graph = NULL;
2105 struct got_object_id *head_commit_id = NULL;
2107 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2111 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2114 err = got_commit_graph_open(&graph, "/", 1);
2118 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2119 check_cancelled, NULL);
2124 struct got_object_id id;
2126 err = got_commit_graph_iter_next(&id, graph, repo,
2127 check_cancelled, NULL);
2129 if (err->code == GOT_ERR_ITER_COMPLETED)
2130 err = got_error(GOT_ERR_ANCESTRY);
2134 if (got_object_id_cmp(&id, commit_id) == 0)
2139 got_commit_graph_close(graph);
2140 free(head_commit_id);
2144 static const struct got_error *
2145 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2147 static char msg[512];
2148 const char *branch_name;
2150 if (got_ref_is_symbolic(ref))
2151 branch_name = got_ref_get_symref_target(ref);
2153 branch_name = got_ref_get_name(ref);
2155 if (strncmp("refs/heads/", branch_name, 11) == 0)
2158 snprintf(msg, sizeof(msg),
2159 "target commit is not contained in branch '%s'; "
2160 "the branch to use must be specified with -b; "
2161 "if necessary a new branch can be created for "
2162 "this commit with 'got branch -c %s BRANCH_NAME'",
2163 branch_name, commit_id_str);
2165 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2168 static const struct got_error *
2169 cmd_checkout(int argc, char *argv[])
2171 const struct got_error *error = NULL;
2172 struct got_repository *repo = NULL;
2173 struct got_reference *head_ref = NULL, *ref = NULL;
2174 struct got_worktree *worktree = NULL;
2175 char *repo_path = NULL;
2176 char *worktree_path = NULL;
2177 const char *path_prefix = "";
2178 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2179 char *commit_id_str = NULL;
2180 struct got_object_id *commit_id = NULL;
2182 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2183 struct got_pathlist_head paths;
2184 struct got_checkout_progress_arg cpa;
2185 int *pack_fds = NULL;
2190 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2191 "unveil", NULL) == -1)
2195 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2198 branch_name = optarg;
2201 commit_id_str = strdup(optarg);
2202 if (commit_id_str == NULL)
2203 return got_error_from_errno("strdup");
2209 path_prefix = optarg;
2224 char *base, *dotgit;
2226 repo_path = realpath(argv[0], NULL);
2227 if (repo_path == NULL)
2228 return got_error_from_errno2("realpath", argv[0]);
2229 cwd = getcwd(NULL, 0);
2231 error = got_error_from_errno("getcwd");
2238 error = got_path_basename(&base, path);
2241 dotgit = strstr(base, ".git");
2244 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2245 error = got_error_from_errno("asprintf");
2250 } else if (argc == 2) {
2251 repo_path = realpath(argv[0], NULL);
2252 if (repo_path == NULL) {
2253 error = got_error_from_errno2("realpath", argv[0]);
2256 worktree_path = realpath(argv[1], NULL);
2257 if (worktree_path == NULL) {
2258 if (errno != ENOENT) {
2259 error = got_error_from_errno2("realpath",
2263 worktree_path = strdup(argv[1]);
2264 if (worktree_path == NULL) {
2265 error = got_error_from_errno("strdup");
2272 got_path_strip_trailing_slashes(repo_path);
2273 got_path_strip_trailing_slashes(worktree_path);
2275 error = got_repo_pack_fds_open(&pack_fds);
2279 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2283 /* Pre-create work tree path for unveil(2) */
2284 error = got_path_mkdir(worktree_path);
2286 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2287 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2289 if (!allow_nonempty &&
2290 !got_path_dir_is_empty(worktree_path)) {
2291 error = got_error_path(worktree_path,
2292 GOT_ERR_DIR_NOT_EMPTY);
2297 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2301 error = got_ref_open(&head_ref, repo, branch_name, 0);
2305 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2306 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2309 error = got_worktree_open(&worktree, worktree_path);
2313 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2317 if (!same_path_prefix) {
2318 error = got_error(GOT_ERR_PATH_PREFIX);
2322 if (commit_id_str) {
2323 struct got_reflist_head refs;
2325 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2329 error = got_repo_match_object_id(&commit_id, NULL,
2330 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2331 got_ref_list_free(&refs);
2334 error = check_linear_ancestry(commit_id,
2335 got_worktree_get_base_commit_id(worktree), 0, repo);
2336 if (error != NULL) {
2337 if (error->code == GOT_ERR_ANCESTRY) {
2338 error = checkout_ancestry_error(
2339 head_ref, commit_id_str);
2343 error = check_same_branch(commit_id, head_ref, repo);
2345 if (error->code == GOT_ERR_ANCESTRY) {
2346 error = checkout_ancestry_error(
2347 head_ref, commit_id_str);
2351 error = got_worktree_set_base_commit_id(worktree, repo,
2355 /* Expand potentially abbreviated commit ID string. */
2356 free(commit_id_str);
2357 error = got_object_id_str(&commit_id_str, commit_id);
2361 commit_id = got_object_id_dup(
2362 got_worktree_get_base_commit_id(worktree));
2363 if (commit_id == NULL) {
2364 error = got_error_from_errno("got_object_id_dup");
2367 error = got_object_id_str(&commit_id_str, commit_id);
2372 error = got_pathlist_append(&paths, "", NULL);
2375 cpa.worktree_path = worktree_path;
2376 cpa.had_base_commit_ref_error = 0;
2377 cpa.verbosity = verbosity;
2378 error = got_worktree_checkout_files(worktree, &paths, repo,
2379 checkout_progress, &cpa, check_cancelled, NULL);
2383 if (got_ref_is_symbolic(head_ref)) {
2384 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
2387 refname = got_ref_get_name(ref);
2389 refname = got_ref_get_name(head_ref);
2390 printf("Checked out %s: %s\n", refname, commit_id_str);
2391 printf("Now shut up and hack\n");
2392 if (cpa.had_base_commit_ref_error)
2393 show_worktree_base_ref_warning();
2396 const struct got_error *pack_err =
2397 got_repo_pack_fds_close(pack_fds);
2402 got_ref_close(head_ref);
2406 const struct got_error *close_err = got_repo_close(repo);
2410 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2411 free(commit_id_str);
2414 free(worktree_path);
2419 struct got_update_progress_arg {
2431 print_update_progress_stats(struct got_update_progress_arg *upa)
2433 if (!upa->did_something)
2436 if (upa->conflicts > 0)
2437 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2438 if (upa->obstructed > 0)
2439 printf("File paths obstructed by a non-regular file: %d\n",
2441 if (upa->not_updated > 0)
2442 printf("Files not updated because of existing merge "
2443 "conflicts: %d\n", upa->not_updated);
2447 * The meaning of some status codes differs between merge-style operations and
2448 * update operations. For example, the ! status code means "file was missing"
2449 * if changes were merged into the work tree, and "missing file was restored"
2450 * if the work tree was updated. This function should be used by any operation
2451 * which merges changes into the work tree without updating the work tree.
2454 print_merge_progress_stats(struct got_update_progress_arg *upa)
2456 if (!upa->did_something)
2459 if (upa->conflicts > 0)
2460 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2461 if (upa->obstructed > 0)
2462 printf("File paths obstructed by a non-regular file: %d\n",
2464 if (upa->missing > 0)
2465 printf("Files which had incoming changes but could not be "
2466 "found in the work tree: %d\n", upa->missing);
2467 if (upa->not_deleted > 0)
2468 printf("Files not deleted due to differences in deleted "
2469 "content: %d\n", upa->not_deleted);
2470 if (upa->unversioned > 0)
2471 printf("Files not merged because an unversioned file was "
2472 "found in the work tree: %d\n", upa->unversioned);
2478 fprintf(stderr, "usage: %s update [-qtvX] [-c commit] [-r remote] "
2479 "[path ...]\n", getprogname());
2483 static const struct got_error *
2484 update_progress(void *arg, unsigned char status, const char *path)
2486 struct got_update_progress_arg *upa = arg;
2488 if (status == GOT_STATUS_EXISTS ||
2489 status == GOT_STATUS_BASE_REF_ERR)
2492 upa->did_something = 1;
2494 /* Base commit bump happens silently. */
2495 if (status == GOT_STATUS_BUMP_BASE)
2498 if (status == GOT_STATUS_CONFLICT)
2500 if (status == GOT_STATUS_OBSTRUCTED)
2502 if (status == GOT_STATUS_CANNOT_UPDATE)
2504 if (status == GOT_STATUS_MISSING)
2506 if (status == GOT_STATUS_CANNOT_DELETE)
2508 if (status == GOT_STATUS_UNVERSIONED)
2511 while (path[0] == '/')
2513 if (upa->verbosity >= 0)
2514 printf("%c %s\n", status, path);
2519 static const struct got_error *
2520 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2522 const struct got_error *err;
2525 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2529 return got_error(GOT_ERR_REBASING);
2531 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2535 return got_error(GOT_ERR_HISTEDIT_BUSY);
2540 static const struct got_error *
2541 check_merge_in_progress(struct got_worktree *worktree,
2542 struct got_repository *repo)
2544 const struct got_error *err;
2547 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
2551 return got_error(GOT_ERR_MERGE_BUSY);
2556 static const struct got_error *
2557 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2558 char *argv[], struct got_worktree *worktree)
2560 const struct got_error *err = NULL;
2562 struct got_pathlist_entry *new;
2568 return got_error_from_errno("strdup");
2569 return got_pathlist_append(paths, path, NULL);
2572 for (i = 0; i < argc; i++) {
2573 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2576 err = got_pathlist_insert(&new, paths, path, NULL);
2577 if (err || new == NULL /* duplicate */) {
2587 static const struct got_error *
2588 wrap_not_worktree_error(const struct got_error *orig_err,
2589 const char *cmdname, const char *path)
2591 const struct got_error *err;
2592 struct got_repository *repo;
2593 static char msg[512];
2594 int *pack_fds = NULL;
2596 err = got_repo_pack_fds_open(&pack_fds);
2600 err = got_repo_open(&repo, path, NULL, pack_fds);
2604 snprintf(msg, sizeof(msg),
2605 "'got %s' needs a work tree in addition to a git repository\n"
2606 "Work trees can be checked out from this Git repository with "
2608 "The got(1) manual page contains more information.", cmdname);
2609 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2611 const struct got_error *close_err = got_repo_close(repo);
2616 const struct got_error *pack_err =
2617 got_repo_pack_fds_close(pack_fds);
2624 static const struct got_error *
2625 cmd_update(int argc, char *argv[])
2627 const struct got_error *error = NULL, *unlock_err;
2628 char *worktree_path = NULL;
2629 const char *repo_path = NULL;
2630 const char *remote_name = NULL;
2631 char *proto = NULL, *host = NULL, *port = NULL;
2632 char *repo_name = NULL, *server_path = NULL;
2633 const struct got_remote_repo *remotes, *remote = NULL;
2635 char *id_str = NULL;
2636 struct got_repository *repo = NULL;
2637 struct got_worktree *worktree = NULL;
2638 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2639 struct got_pathlist_head paths, refs, symrefs;
2640 struct got_pathlist_head wanted_branches, wanted_refs;
2641 struct got_pathlist_entry *pe;
2642 struct got_reflist_head remote_refs;
2643 struct got_reflist_entry *re;
2644 struct got_object_id *pack_hash = NULL;
2645 int i, ch, fetchfd = -1, fetchstatus;
2646 pid_t fetchpid = -1;
2647 struct got_fetch_progress_arg fpa;
2648 struct got_update_progress_arg upa;
2650 int delete_remote = 0;
2651 int replace_tags = 0;
2652 int *pack_fds = NULL;
2653 const char *remote_head = NULL, *worktree_branch = NULL;
2654 struct got_object_id *commit_id = NULL;
2655 char *commit_id_str = NULL;
2656 const char *refname;
2657 struct got_reference *head_ref = NULL;
2661 TAILQ_INIT(&symrefs);
2662 TAILQ_INIT(&remote_refs);
2663 TAILQ_INIT(&wanted_branches);
2664 TAILQ_INIT(&wanted_refs);
2666 while ((ch = getopt(argc, argv, "c:qr:vX")) != -1) {
2669 commit_id_str = strdup(optarg);
2670 if (commit_id_str == NULL)
2671 return got_error_from_errno("strdup");
2680 remote_name = optarg;
2685 else if (verbosity < 3)
2699 if (delete_remote) {
2701 option_conflict('X', 't');
2702 if (remote_name == NULL)
2703 errx(1, "-X option requires a remote name");
2705 if (remote_name == NULL)
2706 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2708 worktree_path = getcwd(NULL, 0);
2709 if (worktree_path == NULL) {
2710 error = got_error_from_errno("getcwd");
2713 error = got_worktree_open(&worktree, worktree_path);
2715 if (error->code == GOT_ERR_NOT_WORKTREE)
2716 error = wrap_not_worktree_error(error, "update",
2720 repo_path = got_worktree_get_repo_path(worktree);
2722 error = got_repo_pack_fds_open(&pack_fds);
2726 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2730 error = check_rebase_or_histedit_in_progress(worktree);
2733 error = check_merge_in_progress(worktree, repo);
2737 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2741 worktree_conf = got_worktree_get_gotconfig(worktree);
2742 if (worktree_conf) {
2743 got_gotconfig_get_remotes(&nremotes, &remotes,
2745 for (i = 0; i < nremotes; i++) {
2746 if (strcmp(remotes[i].name, remote_name) == 0) {
2747 remote = &remotes[i];
2753 if (remote == NULL) {
2754 repo_conf = got_repo_get_gotconfig(repo);
2756 got_gotconfig_get_remotes(&nremotes, &remotes,
2758 for (i = 0; i < nremotes; i++) {
2759 if (strcmp(remotes[i].name, remote_name) == 0) {
2760 remote = &remotes[i];
2766 if (remote == NULL) {
2767 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2768 for (i = 0; i < nremotes; i++) {
2769 if (strcmp(remotes[i].name, remote_name) == 0) {
2770 remote = &remotes[i];
2775 if (remote == NULL) {
2776 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2780 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2781 &repo_name, remote->fetch_url);
2785 if (strcmp(proto, "git") == 0) {
2787 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2788 "sendfd dns inet unveil", NULL) == -1)
2791 } else if (strcmp(proto, "git+ssh") == 0 ||
2792 strcmp(proto, "ssh") == 0) {
2794 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2795 "sendfd unveil", NULL) == -1)
2798 } else if (strcmp(proto, "http") == 0 ||
2799 strcmp(proto, "git+http") == 0) {
2800 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2803 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2807 error = got_dial_apply_unveil(proto);
2811 error = apply_unveil(got_repo_get_path(repo), 0,
2812 got_worktree_get_root_path(worktree));
2816 if (verbosity >= 0) {
2817 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2818 remote->name, proto, host,
2819 port ? ":" : "", port ? port : "",
2820 *server_path == '/' ? "" : "/", server_path);
2823 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2824 server_path, verbosity);
2829 * If set, get this remote's HEAD ref target so
2830 * if it has changed on the server we can fetch it.
2832 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2833 got_ref_cmp_by_name, repo);
2837 TAILQ_FOREACH(re, &remote_refs, entry) {
2838 const char *remote_refname, *remote_target;
2839 size_t remote_name_len;
2841 if (!got_ref_is_symbolic(re->ref))
2844 remote_name_len = strlen(remote->name);
2845 remote_refname = got_ref_get_name(re->ref);
2847 /* we only want refs/remotes/$remote->name/HEAD */
2848 if (strncmp(remote_refname + 13, remote->name,
2849 remote_name_len) != 0)
2852 if (strcmp(remote_refname + remote_name_len + 14,
2857 * Take the name itself because we already
2858 * only match with refs/heads/ in fetch_pack().
2860 remote_target = got_ref_get_symref_target(re->ref);
2861 remote_head = remote_target + remote_name_len + 14;
2865 refname = got_worktree_get_head_ref_name(worktree);
2866 if (strncmp(refname, "refs/heads/", 11) == 0)
2867 worktree_branch = refname;
2869 fpa.last_scaled_size[0] = '\0';
2870 fpa.last_p_indexed = -1;
2871 fpa.last_p_resolved = -1;
2872 fpa.verbosity = verbosity;
2874 fpa.create_configs = 0;
2875 fpa.configs_created = 0;
2876 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2878 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2879 remote->mirror_references, 0, &wanted_branches, &wanted_refs,
2880 0, verbosity, fetchfd, repo, worktree_branch, remote_head,
2881 0, fetch_progress, &fpa);
2885 if (pack_hash != NULL && verbosity >= 0) {
2886 error = got_object_id_str(&id_str, pack_hash);
2889 printf("\nFetched %s.pack\n", id_str);
2894 /* Update references provided with the pack file. */
2895 TAILQ_FOREACH(pe, &refs, entry) {
2896 const char *refname = pe->path;
2897 struct got_object_id *id = pe->data;
2898 struct got_reference *ref;
2900 if (is_wanted_ref(&wanted_refs, refname)) {
2901 error = update_wanted_ref(refname, id,
2902 remote->name, verbosity, repo);
2908 error = got_ref_open(&ref, repo, refname, 1);
2910 if (error->code != GOT_ERR_NOT_REF)
2912 error = create_ref(refname, id, verbosity,
2917 error = update_ref(ref, id, replace_tags,
2919 unlock_err = got_ref_unlock(ref);
2920 if (unlock_err && error == NULL)
2928 /* Update worktree */
2929 error = got_ref_open(&head_ref, repo,
2930 got_worktree_get_head_ref_name(worktree), 0);
2933 if (commit_id_str == NULL) {
2934 error = got_ref_resolve(&commit_id, repo, head_ref);
2937 error = got_object_id_str(&commit_id_str, commit_id);
2941 struct got_reflist_head refs;
2943 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2947 error = got_repo_match_object_id(&commit_id, NULL,
2948 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2949 got_ref_list_free(&refs);
2950 free(commit_id_str);
2951 commit_id_str = NULL;
2954 error = got_object_id_str(&commit_id_str, commit_id);
2960 error = check_linear_ancestry(commit_id,
2961 got_worktree_get_base_commit_id(worktree), 0, repo);
2962 if (error != NULL) {
2963 if (error->code == GOT_ERR_ANCESTRY)
2964 error = got_error(GOT_ERR_BRANCH_MOVED);
2967 error = check_same_branch(commit_id, head_ref, repo);
2971 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2973 error = got_worktree_set_base_commit_id(worktree, repo,
2979 memset(&upa, 0, sizeof(upa));
2980 upa.verbosity = verbosity;
2981 error = got_worktree_checkout_files(worktree, &paths, repo,
2982 update_progress, &upa, check_cancelled, NULL);
2986 if (upa.did_something) {
2987 printf("Updated to %s: %s\n",
2988 got_worktree_get_head_ref_name(worktree), commit_id_str);
2990 printf("Already up-to-date\n");
2992 print_update_progress_stats(&upa);
2995 if (kill(fetchpid, SIGTERM) == -1)
2996 error = got_error_from_errno("kill");
2997 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2998 error = got_error_from_errno("waitpid");
3000 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
3001 error = got_error_from_errno("close");
3003 const struct got_error *close_err = got_repo_close(repo);
3008 got_worktree_close(worktree);
3010 const struct got_error *pack_err =
3011 got_repo_pack_fds_close(pack_fds);
3015 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3016 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
3017 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
3018 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
3019 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
3020 got_ref_list_free(&remote_refs);
3022 free(worktree_path);
3030 free(commit_id_str);
3034 static const struct got_error *
3035 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3036 const char *path, int diff_context, int ignore_whitespace,
3037 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3038 struct got_repository *repo, FILE *outfile)
3040 const struct got_error *err = NULL;
3041 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3042 FILE *f1 = NULL, *f2 = NULL;
3043 int fd1 = -1, fd2 = -1;
3045 fd1 = got_opentempfd();
3047 return got_error_from_errno("got_opentempfd");
3048 fd2 = got_opentempfd();
3050 err = got_error_from_errno("got_opentempfd");
3055 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3061 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3065 f1 = got_opentemp();
3067 err = got_error_from_errno("got_opentemp");
3070 f2 = got_opentemp();
3072 err = got_error_from_errno("got_opentemp");
3076 while (path[0] == '/')
3078 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3079 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3080 force_text_diff, dsa, outfile);
3082 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3083 err = got_error_from_errno("close");
3085 got_object_blob_close(blob1);
3086 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3087 err = got_error_from_errno("close");
3089 got_object_blob_close(blob2);
3090 if (f1 && fclose(f1) == EOF && err == NULL)
3091 err = got_error_from_errno("fclose");
3092 if (f2 && fclose(f2) == EOF && err == NULL)
3093 err = got_error_from_errno("fclose");
3097 static const struct got_error *
3098 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3099 const char *path, int diff_context, int ignore_whitespace,
3100 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3101 struct got_repository *repo, FILE *outfile)
3103 const struct got_error *err = NULL;
3104 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3105 struct got_diff_blob_output_unidiff_arg arg;
3106 FILE *f1 = NULL, *f2 = NULL;
3107 int fd1 = -1, fd2 = -1;
3110 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3113 fd1 = got_opentempfd();
3115 err = got_error_from_errno("got_opentempfd");
3120 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3124 f1 = got_opentemp();
3126 err = got_error_from_errno("got_opentemp");
3130 f2 = got_opentemp();
3132 err = got_error_from_errno("got_opentemp");
3135 fd2 = got_opentempfd();
3137 err = got_error_from_errno("got_opentempfd");
3140 arg.diff_context = diff_context;
3141 arg.ignore_whitespace = ignore_whitespace;
3142 arg.force_text_diff = force_text_diff;
3144 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3145 arg.outfile = outfile;
3148 while (path[0] == '/')
3150 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3151 got_diff_blob_output_unidiff, &arg, 1);
3154 got_object_tree_close(tree1);
3156 got_object_tree_close(tree2);
3157 if (f1 && fclose(f1) == EOF && err == NULL)
3158 err = got_error_from_errno("fclose");
3159 if (f2 && fclose(f2) == EOF && err == NULL)
3160 err = got_error_from_errno("fclose");
3161 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3162 err = got_error_from_errno("close");
3163 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3164 err = got_error_from_errno("close");
3168 static const struct got_error *
3169 get_changed_paths(struct got_pathlist_head *paths,
3170 struct got_commit_object *commit, struct got_repository *repo,
3171 struct got_diffstat_cb_arg *dsa)
3173 const struct got_error *err = NULL;
3174 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3175 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3176 struct got_object_qid *qid;
3177 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3178 FILE *f1 = NULL, *f2 = NULL;
3179 int fd1 = -1, fd2 = -1;
3182 cb = got_diff_tree_compute_diffstat;
3184 f1 = got_opentemp();
3186 err = got_error_from_errno("got_opentemp");
3189 f2 = got_opentemp();
3191 err = got_error_from_errno("got_opentemp");
3194 fd1 = got_opentempfd();
3196 err = got_error_from_errno("got_opentempfd");
3199 fd2 = got_opentempfd();
3201 err = got_error_from_errno("got_opentempfd");
3206 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3208 struct got_commit_object *pcommit;
3209 err = got_object_open_as_commit(&pcommit, repo,
3214 tree_id1 = got_object_id_dup(
3215 got_object_commit_get_tree_id(pcommit));
3216 if (tree_id1 == NULL) {
3217 got_object_commit_close(pcommit);
3218 return got_error_from_errno("got_object_id_dup");
3220 got_object_commit_close(pcommit);
3225 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3230 tree_id2 = got_object_commit_get_tree_id(commit);
3231 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3235 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3236 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3239 got_object_tree_close(tree1);
3241 got_object_tree_close(tree2);
3242 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 err = got_error_from_errno("close");
3246 if (f1 && fclose(f1) == EOF && err == NULL)
3247 err = got_error_from_errno("fclose");
3248 if (f2 && fclose(f2) == EOF && err == NULL)
3249 err = got_error_from_errno("fclose");
3254 static const struct got_error *
3255 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3256 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3257 struct got_repository *repo, FILE *outfile)
3259 const struct got_error *err = NULL;
3260 struct got_commit_object *pcommit = NULL;
3261 char *id_str1 = NULL, *id_str2 = NULL;
3262 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3263 struct got_object_qid *qid;
3265 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3267 err = got_object_open_as_commit(&pcommit, repo,
3271 err = got_object_id_str(&id_str1, &qid->id);
3276 err = got_object_id_str(&id_str2, id);
3280 if (path && path[0] != '\0') {
3282 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3286 err = got_object_id_by_path(&obj_id1, repo,
3289 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3295 err = got_object_get_type(&obj_type, repo, obj_id2);
3301 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3302 fprintf(outfile, "commit - %s\n",
3303 id_str1 ? id_str1 : "/dev/null");
3304 fprintf(outfile, "commit + %s\n", id_str2);
3306 case GOT_OBJ_TYPE_BLOB:
3307 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3308 0, 0, dsa, repo, outfile);
3310 case GOT_OBJ_TYPE_TREE:
3311 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3312 0, 0, dsa, repo, outfile);
3315 err = got_error(GOT_ERR_OBJ_TYPE);
3321 obj_id2 = got_object_commit_get_tree_id(commit);
3323 obj_id1 = got_object_commit_get_tree_id(pcommit);
3325 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3326 fprintf(outfile, "commit - %s\n",
3327 id_str1 ? id_str1 : "/dev/null");
3328 fprintf(outfile, "commit + %s\n", id_str2);
3329 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3330 dsa, repo, outfile);
3336 got_object_commit_close(pcommit);
3341 get_datestr(time_t *time, char *datebuf)
3343 struct tm mytm, *tm;
3346 tm = gmtime_r(time, &mytm);
3349 s = asctime_r(tm, datebuf);
3352 p = strchr(s, '\n');
3358 static const struct got_error *
3359 match_commit(int *have_match, struct got_object_id *id,
3360 struct got_commit_object *commit, regex_t *regex)
3362 const struct got_error *err = NULL;
3363 regmatch_t regmatch;
3364 char *id_str = NULL, *logmsg = NULL;
3368 err = got_object_id_str(&id_str, id);
3372 err = got_object_commit_get_logmsg(&logmsg, commit);
3376 if (regexec(regex, got_object_commit_get_author(commit), 1,
3377 ®match, 0) == 0 ||
3378 regexec(regex, got_object_commit_get_committer(commit), 1,
3379 ®match, 0) == 0 ||
3380 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3381 regexec(regex, logmsg, 1, ®match, 0) == 0)
3390 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3393 regmatch_t regmatch;
3394 struct got_pathlist_entry *pe;
3398 TAILQ_FOREACH(pe, changed_paths, entry) {
3399 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3406 static const struct got_error *
3407 match_patch(int *have_match, struct got_commit_object *commit,
3408 struct got_object_id *id, const char *path, int diff_context,
3409 struct got_repository *repo, regex_t *regex, FILE *f)
3411 const struct got_error *err = NULL;
3413 size_t linesize = 0;
3414 regmatch_t regmatch;
3418 err = got_opentemp_truncate(f);
3422 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3426 if (fseeko(f, 0L, SEEK_SET) == -1) {
3427 err = got_error_from_errno("fseeko");
3431 while (getline(&line, &linesize, f) != -1) {
3432 if (regexec(regex, line, 1, ®match, 0) == 0) {
3442 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3444 static const struct got_error*
3445 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3446 struct got_object_id *id, struct got_repository *repo,
3449 static const struct got_error *err = NULL;
3450 struct got_reflist_entry *re;
3456 TAILQ_FOREACH(re, refs, entry) {
3457 struct got_tag_object *tag = NULL;
3458 struct got_object_id *ref_id;
3461 name = got_ref_get_name(re->ref);
3462 if (strcmp(name, GOT_REF_HEAD) == 0)
3464 if (strncmp(name, "refs/", 5) == 0)
3466 if (strncmp(name, "got/", 4) == 0)
3468 if (strncmp(name, "heads/", 6) == 0)
3470 if (strncmp(name, "remotes/", 8) == 0) {
3474 s = strstr(name, "/" GOT_REF_HEAD);
3475 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
3478 err = got_ref_resolve(&ref_id, repo, re->ref);
3481 if (strncmp(name, "tags/", 5) == 0) {
3482 err = got_object_open_as_tag(&tag, repo, ref_id);
3484 if (err->code != GOT_ERR_OBJ_TYPE) {
3488 /* Ref points at something other than a tag. */
3493 cmp = got_object_id_cmp(tag ?
3494 got_object_tag_get_object_id(tag) : ref_id, id);
3497 got_object_tag_close(tag);
3501 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3502 s ? ", " : "", name) == -1) {
3503 err = got_error_from_errno("asprintf");
3514 static const struct got_error *
3515 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3516 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3518 const struct got_error *err = NULL;
3519 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3520 char *comma, *s, *nl;
3521 struct got_reflist_head *refs;
3522 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3524 time_t committer_time;
3526 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3528 err = build_refs_str(&ref_str, refs, id, repo, 1);
3532 /* Display the first matching ref only. */
3533 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3537 if (ref_str == NULL) {
3538 err = got_object_id_str(&id_str, id);
3543 committer_time = got_object_commit_get_committer_time(commit);
3544 if (gmtime_r(&committer_time, &tm) == NULL) {
3545 err = got_error_from_errno("gmtime_r");
3548 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
3549 err = got_error(GOT_ERR_NO_SPACE);
3553 err = got_object_commit_get_logmsg(&logmsg0, commit);
3558 while (isspace((unsigned char)s[0]))
3561 nl = strchr(s, '\n');
3567 printf("%s%-7s %s\n", datebuf, ref_str, s);
3569 printf("%s%.7s %s\n", datebuf, id_str, s);
3571 if (fflush(stdout) != 0 && err == NULL)
3572 err = got_error_from_errno("fflush");
3580 static const struct got_error *
3581 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
3583 struct got_pathlist_entry *pe;
3586 printf("%s\n", header);
3588 TAILQ_FOREACH(pe, dsa->paths, entry) {
3589 struct got_diff_changed_path *cp = pe->data;
3590 int pad = dsa->max_path_len - pe->path_len + 1;
3592 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
3593 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
3595 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
3596 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
3597 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
3599 if (fflush(stdout) != 0)
3600 return got_error_from_errno("fflush");
3605 static const struct got_error *
3611 if (fseeko(f, 0L, SEEK_SET) == -1)
3612 return got_error_from_errno("fseek");
3615 r = fread(buf, 1, sizeof(buf), f);
3618 return got_error_from_errno("fread");
3622 if (fwrite(buf, 1, r, stdout) != r)
3623 return got_ferror(stdout, GOT_ERR_IO);
3629 static const struct got_error *
3630 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3631 struct got_repository *repo, const char *path,
3632 struct got_pathlist_head *changed_paths,
3633 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
3634 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
3637 const struct got_error *err = NULL;
3639 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3641 time_t committer_time;
3642 const char *author, *committer;
3643 char *refs_str = NULL;
3645 err = got_object_id_str(&id_str, id);
3649 if (custom_refs_str == NULL) {
3650 struct got_reflist_head *refs;
3651 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3653 err = build_refs_str(&refs_str, refs, id, repo, 0);
3659 printf(GOT_COMMIT_SEP_STR);
3660 if (custom_refs_str)
3661 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
3664 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
3665 refs_str ? " (" : "", refs_str ? refs_str : "",
3666 refs_str ? ")" : "");
3671 printf("from: %s\n", got_object_commit_get_author(commit));
3672 author = got_object_commit_get_author(commit);
3673 committer = got_object_commit_get_committer(commit);
3674 if (strcmp(author, committer) != 0)
3675 printf("via: %s\n", committer);
3676 committer_time = got_object_commit_get_committer_time(commit);
3677 datestr = get_datestr(&committer_time, datebuf);
3679 printf("date: %s UTC\n", datestr);
3680 if (got_object_commit_get_nparents(commit) > 1) {
3681 const struct got_object_id_queue *parent_ids;
3682 struct got_object_qid *qid;
3684 parent_ids = got_object_commit_get_parent_ids(commit);
3685 STAILQ_FOREACH(qid, parent_ids, entry) {
3686 err = got_object_id_str(&id_str, &qid->id);
3689 printf("parent %d: %s\n", n++, id_str);
3695 err = got_object_commit_get_logmsg(&logmsg0, commit);
3701 line = strsep(&logmsg, "\n");
3703 printf(" %s\n", line);
3707 if (changed_paths && diffstat == NULL) {
3708 struct got_pathlist_entry *pe;
3710 TAILQ_FOREACH(pe, changed_paths, entry) {
3711 struct got_diff_changed_path *cp = pe->data;
3713 printf(" %c %s\n", cp->status, pe->path);
3721 err = got_error_from_errno("got_opentemp");
3726 err = print_patch(commit, id, path, diff_context, diffstat,
3727 repo, diffstat == NULL ? stdout : f);
3732 err = print_diffstat(diffstat, NULL);
3744 if (fflush(stdout) != 0 && err == NULL)
3745 err = got_error_from_errno("fflush");
3747 if (f && fclose(f) == EOF && err == NULL)
3748 err = got_error_from_errno("fclose");
3754 static const struct got_error *
3755 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3756 struct got_repository *repo, const char *path, int show_changed_paths,
3757 int show_diffstat, int show_patch, const char *search_pattern,
3758 int diff_context, int limit, int log_branches, int reverse_display_order,
3759 struct got_reflist_object_id_map *refs_idmap, int one_line,
3762 const struct got_error *err;
3763 struct got_commit_graph *graph;
3766 struct got_object_id_queue reversed_commits;
3767 struct got_object_qid *qid;
3768 struct got_commit_object *commit;
3769 struct got_pathlist_head changed_paths;
3771 STAILQ_INIT(&reversed_commits);
3772 TAILQ_INIT(&changed_paths);
3774 if (search_pattern && regcomp(®ex, search_pattern,
3775 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3776 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3778 err = got_commit_graph_open(&graph, path, !log_branches);
3781 err = got_commit_graph_iter_start(graph, root_id, repo,
3782 check_cancelled, NULL);
3786 struct got_object_id id;
3787 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3788 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3790 if (sigint_received || sigpipe_received)
3793 err = got_commit_graph_iter_next(&id, graph, repo,
3794 check_cancelled, NULL);
3796 if (err->code == GOT_ERR_ITER_COMPLETED)
3801 err = got_object_open_as_commit(&commit, repo, &id);
3805 if ((show_changed_paths || (show_diffstat && !show_patch))
3806 && !reverse_display_order) {
3807 err = get_changed_paths(&changed_paths, commit, repo,
3808 show_diffstat ? &dsa : NULL);
3813 if (search_pattern) {
3814 err = match_commit(&have_match, &id, commit, ®ex);
3816 got_object_commit_close(commit);
3819 if (have_match == 0 && show_changed_paths)
3820 match_changed_paths(&have_match,
3821 &changed_paths, ®ex);
3822 if (have_match == 0 && show_patch) {
3823 err = match_patch(&have_match, commit, &id,
3824 path, diff_context, repo, ®ex, tmpfile);
3828 if (have_match == 0) {
3829 got_object_commit_close(commit);
3830 got_pathlist_free(&changed_paths,
3831 GOT_PATHLIST_FREE_ALL);
3836 if (reverse_display_order) {
3837 err = got_object_qid_alloc(&qid, &id);
3840 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3841 got_object_commit_close(commit);
3844 err = print_commit_oneline(commit, &id,
3847 err = print_commit(commit, &id, repo, path,
3848 (show_changed_paths || show_diffstat) ?
3849 &changed_paths : NULL,
3850 show_diffstat ? &dsa : NULL, show_patch,
3851 diff_context, refs_idmap, NULL, NULL);
3852 got_object_commit_close(commit);
3856 if ((limit && --limit == 0) ||
3857 (end_id && got_object_id_cmp(&id, end_id) == 0))
3860 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3862 if (reverse_display_order) {
3863 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3864 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3865 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3867 err = got_object_open_as_commit(&commit, repo,
3871 if (show_changed_paths ||
3872 (show_diffstat && !show_patch)) {
3873 err = get_changed_paths(&changed_paths, commit,
3874 repo, show_diffstat ? &dsa : NULL);
3879 err = print_commit_oneline(commit, &qid->id,
3882 err = print_commit(commit, &qid->id, repo, path,
3883 (show_changed_paths || show_diffstat) ?
3884 &changed_paths : NULL,
3885 show_diffstat ? &dsa : NULL, show_patch,
3886 diff_context, refs_idmap, NULL, NULL);
3887 got_object_commit_close(commit);
3890 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3894 while (!STAILQ_EMPTY(&reversed_commits)) {
3895 qid = STAILQ_FIRST(&reversed_commits);
3896 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3897 got_object_qid_free(qid);
3899 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3902 got_commit_graph_close(graph);
3909 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
3910 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
3911 "[path]\n", getprogname());
3916 get_default_log_limit(void)
3918 const char *got_default_log_limit;
3922 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3923 if (got_default_log_limit == NULL)
3925 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3931 static const struct got_error *
3932 cmd_log(int argc, char *argv[])
3934 const struct got_error *error;
3935 struct got_repository *repo = NULL;
3936 struct got_worktree *worktree = NULL;
3937 struct got_object_id *start_id = NULL, *end_id = NULL;
3938 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3939 const char *start_commit = NULL, *end_commit = NULL;
3940 const char *search_pattern = NULL;
3941 int diff_context = -1, ch;
3942 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3943 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
3945 struct got_reflist_head refs;
3946 struct got_reflist_object_id_map *refs_idmap = NULL;
3947 FILE *tmpfile = NULL;
3948 int *pack_fds = NULL;
3953 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3959 limit = get_default_log_limit();
3961 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
3967 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3970 errx(1, "number of context lines is %s: %s",
3974 start_commit = optarg;
3980 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3982 errx(1, "number of commits is %s: %s",
3986 show_changed_paths = 1;
3992 reverse_display_order = 1;
3995 repo_path = realpath(optarg, NULL);
3996 if (repo_path == NULL)
3997 return got_error_from_errno2("realpath",
3999 got_path_strip_trailing_slashes(repo_path);
4002 search_pattern = optarg;
4008 end_commit = optarg;
4019 if (diff_context == -1)
4021 else if (!show_patch)
4022 errx(1, "-C requires -p");
4024 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4025 errx(1, "cannot use -s with -d, -p or -P");
4027 cwd = getcwd(NULL, 0);
4029 error = got_error_from_errno("getcwd");
4033 error = got_repo_pack_fds_open(&pack_fds);
4037 if (repo_path == NULL) {
4038 error = got_worktree_open(&worktree, cwd);
4039 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4046 error = got_worktree_resolve_path(&path, worktree,
4051 path = strdup(argv[0]);
4053 error = got_error_from_errno("strdup");
4057 } else if (argc != 0)
4060 if (repo_path == NULL) {
4061 repo_path = worktree ?
4062 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4064 if (repo_path == NULL) {
4065 error = got_error_from_errno("strdup");
4069 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4073 error = apply_unveil(got_repo_get_path(repo), 1,
4074 worktree ? got_worktree_get_root_path(worktree) : NULL);
4078 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4082 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4086 if (start_commit == NULL) {
4087 struct got_reference *head_ref;
4088 struct got_commit_object *commit = NULL;
4089 error = got_ref_open(&head_ref, repo,
4090 worktree ? got_worktree_get_head_ref_name(worktree)
4094 error = got_ref_resolve(&start_id, repo, head_ref);
4095 got_ref_close(head_ref);
4098 error = got_object_open_as_commit(&commit, repo,
4102 got_object_commit_close(commit);
4104 error = got_repo_match_object_id(&start_id, NULL,
4105 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4109 if (end_commit != NULL) {
4110 error = got_repo_match_object_id(&end_id, NULL,
4111 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4118 * If a path was specified on the command line it was resolved
4119 * to a path in the work tree above. Prepend the work tree's
4120 * path prefix to obtain the corresponding in-repository path.
4124 prefix = got_worktree_get_path_prefix(worktree);
4125 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4126 (path[0] != '\0') ? "/" : "", path) == -1) {
4127 error = got_error_from_errno("asprintf");
4132 error = got_repo_map_path(&in_repo_path, repo,
4138 path = in_repo_path;
4142 /* Release work tree lock. */
4143 got_worktree_close(worktree);
4147 if (search_pattern && show_patch) {
4148 tmpfile = got_opentemp();
4149 if (tmpfile == NULL) {
4150 error = got_error_from_errno("got_opentemp");
4155 error = print_commits(start_id, end_id, repo, path ? path : "",
4156 show_changed_paths, show_diffstat, show_patch, search_pattern,
4157 diff_context, limit, log_branches, reverse_display_order,
4158 refs_idmap, one_line, tmpfile);
4166 got_worktree_close(worktree);
4168 const struct got_error *close_err = got_repo_close(repo);
4173 const struct got_error *pack_err =
4174 got_repo_pack_fds_close(pack_fds);
4179 got_reflist_object_id_map_free(refs_idmap);
4180 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4181 error = got_error_from_errno("fclose");
4182 got_ref_list_free(&refs);
4189 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4190 "[-r repository-path] [object1 object2 | path ...]\n",
4195 struct print_diff_arg {
4196 struct got_repository *repo;
4197 struct got_worktree *worktree;
4198 struct got_diffstat_cb_arg *diffstat;
4203 enum got_diff_algorithm diff_algo;
4204 int ignore_whitespace;
4205 int force_text_diff;
4212 * Create a file which contains the target path of a symlink so we can feed
4213 * it as content to the diff engine.
4215 static const struct got_error *
4216 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4217 const char *abspath)
4219 const struct got_error *err = NULL;
4220 char target_path[PATH_MAX];
4221 ssize_t target_len, outlen;
4226 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4227 if (target_len == -1)
4228 return got_error_from_errno2("readlinkat", abspath);
4230 target_len = readlink(abspath, target_path, PATH_MAX);
4231 if (target_len == -1)
4232 return got_error_from_errno2("readlink", abspath);
4235 *fd = got_opentempfd();
4237 return got_error_from_errno("got_opentempfd");
4239 outlen = write(*fd, target_path, target_len);
4241 err = got_error_from_errno("got_opentempfd");
4245 if (lseek(*fd, 0, SEEK_SET) == -1) {
4246 err = got_error_from_errno2("lseek", abspath);
4257 static const struct got_error *
4258 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4259 const char *path, struct got_object_id *blob_id,
4260 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4261 int dirfd, const char *de_name)
4263 struct print_diff_arg *a = arg;
4264 const struct got_error *err = NULL;
4265 struct got_blob_object *blob1 = NULL;
4266 int fd = -1, fd1 = -1, fd2 = -1;
4268 char *abspath = NULL, *label1 = NULL;
4273 memset(&sb, 0, sizeof(sb));
4275 if (a->diff_staged) {
4276 if (staged_status != GOT_STATUS_MODIFY &&
4277 staged_status != GOT_STATUS_ADD &&
4278 staged_status != GOT_STATUS_DELETE)
4281 if (staged_status == GOT_STATUS_DELETE)
4283 if (status == GOT_STATUS_NONEXISTENT)
4284 return got_error_set_errno(ENOENT, path);
4285 if (status != GOT_STATUS_MODIFY &&
4286 status != GOT_STATUS_ADD &&
4287 status != GOT_STATUS_DELETE &&
4288 status != GOT_STATUS_CONFLICT)
4292 err = got_opentemp_truncate(a->f1);
4294 return got_error_from_errno("got_opentemp_truncate");
4295 err = got_opentemp_truncate(a->f2);
4297 return got_error_from_errno("got_opentemp_truncate");
4299 if (!a->header_shown) {
4300 if (fprintf(a->outfile, "diff %s%s\n",
4301 a->diff_staged ? "-s " : "",
4302 got_worktree_get_root_path(a->worktree)) < 0) {
4303 err = got_error_from_errno("fprintf");
4306 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4307 err = got_error_from_errno("fprintf");
4310 if (fprintf(a->outfile, "path + %s%s\n",
4311 got_worktree_get_root_path(a->worktree),
4312 a->diff_staged ? " (staged changes)" : "") < 0) {
4313 err = got_error_from_errno("fprintf");
4316 a->header_shown = 1;
4319 if (a->diff_staged) {
4320 const char *label1 = NULL, *label2 = NULL;
4321 switch (staged_status) {
4322 case GOT_STATUS_MODIFY:
4326 case GOT_STATUS_ADD:
4329 case GOT_STATUS_DELETE:
4333 return got_error(GOT_ERR_FILE_STATUS);
4335 fd1 = got_opentempfd();
4337 err = got_error_from_errno("got_opentempfd");
4340 fd2 = got_opentempfd();
4342 err = got_error_from_errno("got_opentempfd");
4345 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4346 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4347 a->diff_algo, a->diff_context, a->ignore_whitespace,
4348 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4352 fd1 = got_opentempfd();
4354 err = got_error_from_errno("got_opentempfd");
4358 if (staged_status == GOT_STATUS_ADD ||
4359 staged_status == GOT_STATUS_MODIFY) {
4361 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4365 err = got_object_id_str(&id_str, staged_blob_id);
4368 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4369 err = got_error_from_errno("asprintf");
4374 } else if (status != GOT_STATUS_ADD) {
4375 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4381 if (status != GOT_STATUS_DELETE) {
4382 if (asprintf(&abspath, "%s/%s",
4383 got_worktree_get_root_path(a->worktree), path) == -1) {
4384 err = got_error_from_errno("asprintf");
4389 fd = openat(dirfd, de_name,
4390 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4392 if (!got_err_open_nofollow_on_symlink()) {
4393 err = got_error_from_errno2("openat",
4397 err = get_symlink_target_file(&fd, dirfd,
4403 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4405 if (!got_err_open_nofollow_on_symlink()) {
4406 err = got_error_from_errno2("open",
4410 err = get_symlink_target_file(&fd, dirfd,
4416 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4417 err = got_error_from_errno2("fstatat", abspath);
4420 f2 = fdopen(fd, "r");
4422 err = got_error_from_errno2("fdopen", abspath);
4430 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4436 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4437 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4438 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
4440 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4441 err = got_error_from_errno("close");
4442 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4443 err = got_error_from_errno("close");
4445 got_object_blob_close(blob1);
4446 if (fd != -1 && close(fd) == -1 && err == NULL)
4447 err = got_error_from_errno("close");
4448 if (f2 && fclose(f2) == EOF && err == NULL)
4449 err = got_error_from_errno("fclose");
4454 static const struct got_error *
4455 cmd_diff(int argc, char *argv[])
4457 const struct got_error *error;
4458 struct got_repository *repo = NULL;
4459 struct got_worktree *worktree = NULL;
4460 char *cwd = NULL, *repo_path = NULL;
4461 const char *commit_args[2] = { NULL, NULL };
4462 int ncommit_args = 0;
4463 struct got_object_id *ids[2] = { NULL, NULL };
4464 char *labels[2] = { NULL, NULL };
4465 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4466 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4467 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
4469 struct got_reflist_head refs;
4470 struct got_pathlist_head diffstat_paths, paths;
4471 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4472 int fd1 = -1, fd2 = -1;
4473 int *pack_fds = NULL;
4474 struct got_diffstat_cb_arg dsa;
4476 memset(&dsa, 0, sizeof(dsa));
4480 TAILQ_INIT(&diffstat_paths);
4483 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4488 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
4491 force_text_diff = 1;
4494 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4497 errx(1, "number of context lines is %s: %s",
4501 if (ncommit_args >= 2)
4502 errx(1, "too many -c options used");
4503 commit_args[ncommit_args++] = optarg;
4512 repo_path = realpath(optarg, NULL);
4513 if (repo_path == NULL)
4514 return got_error_from_errno2("realpath",
4516 got_path_strip_trailing_slashes(repo_path);
4523 ignore_whitespace = 1;
4534 cwd = getcwd(NULL, 0);
4536 error = got_error_from_errno("getcwd");
4540 error = got_repo_pack_fds_open(&pack_fds);
4544 if (repo_path == NULL) {
4545 error = got_worktree_open(&worktree, cwd);
4546 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4552 strdup(got_worktree_get_repo_path(worktree));
4553 if (repo_path == NULL) {
4554 error = got_error_from_errno("strdup");
4558 repo_path = strdup(cwd);
4559 if (repo_path == NULL) {
4560 error = got_error_from_errno("strdup");
4566 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4571 if (show_diffstat) {
4572 dsa.paths = &diffstat_paths;
4573 dsa.force_text = force_text_diff;
4574 dsa.ignore_ws = ignore_whitespace;
4575 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4578 if (rflag || worktree == NULL || ncommit_args > 0) {
4580 error = got_error_msg(GOT_ERR_NOT_IMPL,
4581 "-P option can only be used when diffing "
4586 error = got_error_msg(GOT_ERR_NOT_IMPL,
4587 "-s option can only be used when diffing "
4593 error = apply_unveil(got_repo_get_path(repo), 1,
4594 worktree ? got_worktree_get_root_path(worktree) : NULL);
4598 if ((!force_path && argc == 2) || ncommit_args > 0) {
4599 int obj_type = (ncommit_args > 0 ?
4600 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4601 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4605 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4607 if (ncommit_args > 0)
4608 arg = commit_args[i];
4611 error = got_repo_match_object_id(&ids[i], &labels[i],
4612 arg, obj_type, &refs, repo);
4614 if (error->code != GOT_ERR_NOT_REF &&
4615 error->code != GOT_ERR_NO_OBJ)
4617 if (ncommit_args > 0)
4625 f1 = got_opentemp();
4627 error = got_error_from_errno("got_opentemp");
4631 f2 = got_opentemp();
4633 error = got_error_from_errno("got_opentemp");
4637 outfile = got_opentemp();
4638 if (outfile == NULL) {
4639 error = got_error_from_errno("got_opentemp");
4643 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4644 struct print_diff_arg arg;
4647 if (worktree == NULL) {
4648 if (argc == 2 && ids[0] == NULL) {
4649 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4651 } else if (argc == 2 && ids[1] == NULL) {
4652 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4654 } else if (argc > 0) {
4655 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4656 "%s", "specified paths cannot be resolved");
4659 error = got_error(GOT_ERR_NOT_WORKTREE);
4664 error = get_worktree_paths_from_argv(&paths, argc, argv,
4669 error = got_object_id_str(&id_str,
4670 got_worktree_get_base_commit_id(worktree));
4674 arg.worktree = worktree;
4675 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4676 arg.diff_context = diff_context;
4677 arg.id_str = id_str;
4678 arg.header_shown = 0;
4679 arg.diff_staged = diff_staged;
4680 arg.ignore_whitespace = ignore_whitespace;
4681 arg.force_text_diff = force_text_diff;
4682 arg.diffstat = show_diffstat ? &dsa : NULL;
4685 arg.outfile = outfile;
4687 error = got_worktree_status(worktree, &paths, repo, 0,
4688 print_diff, &arg, check_cancelled, NULL);
4693 if (show_diffstat && dsa.nfiles > 0) {
4696 if (asprintf(&header, "diffstat %s%s",
4697 diff_staged ? "-s " : "",
4698 got_worktree_get_root_path(worktree)) == -1) {
4699 error = got_error_from_errno("asprintf");
4703 error = print_diffstat(&dsa, header);
4709 error = printfile(outfile);
4713 if (ncommit_args == 1) {
4714 struct got_commit_object *commit;
4715 error = got_object_open_as_commit(&commit, repo, ids[0]);
4719 labels[1] = labels[0];
4721 if (got_object_commit_get_nparents(commit) > 0) {
4722 const struct got_object_id_queue *pids;
4723 struct got_object_qid *pid;
4724 pids = got_object_commit_get_parent_ids(commit);
4725 pid = STAILQ_FIRST(pids);
4726 ids[0] = got_object_id_dup(&pid->id);
4727 if (ids[0] == NULL) {
4728 error = got_error_from_errno(
4729 "got_object_id_dup");
4730 got_object_commit_close(commit);
4733 error = got_object_id_str(&labels[0], ids[0]);
4735 got_object_commit_close(commit);
4740 labels[0] = strdup("/dev/null");
4741 if (labels[0] == NULL) {
4742 error = got_error_from_errno("strdup");
4743 got_object_commit_close(commit);
4748 got_object_commit_close(commit);
4751 if (ncommit_args == 0 && argc > 2) {
4752 error = got_error_msg(GOT_ERR_BAD_PATH,
4753 "path arguments cannot be used when diffing two objects");
4758 error = got_object_get_type(&type1, repo, ids[0]);
4763 error = got_object_get_type(&type2, repo, ids[1]);
4766 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4767 error = got_error(GOT_ERR_OBJ_TYPE);
4770 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
4771 error = got_error_msg(GOT_ERR_OBJ_TYPE,
4772 "path arguments cannot be used when diffing blobs");
4776 for (i = 0; ncommit_args > 0 && i < argc; i++) {
4778 struct got_pathlist_entry *new;
4782 error = got_worktree_resolve_path(&p, worktree,
4786 prefix = got_worktree_get_path_prefix(worktree);
4787 while (prefix[0] == '/')
4789 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4790 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4792 error = got_error_from_errno("asprintf");
4798 char *mapped_path, *s;
4799 error = got_repo_map_path(&mapped_path, repo, argv[i]);
4805 in_repo_path = strdup(s);
4806 if (in_repo_path == NULL) {
4807 error = got_error_from_errno("asprintf");
4814 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4815 if (error || new == NULL /* duplicate */)
4822 /* Release work tree lock. */
4823 got_worktree_close(worktree);
4827 fd1 = got_opentempfd();
4829 error = got_error_from_errno("got_opentempfd");
4833 fd2 = got_opentempfd();
4835 error = got_error_from_errno("got_opentempfd");
4839 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4840 case GOT_OBJ_TYPE_BLOB:
4841 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4842 fd1, fd2, ids[0], ids[1], NULL, NULL,
4843 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4844 ignore_whitespace, force_text_diff,
4845 show_diffstat ? &dsa : NULL, repo, outfile);
4847 case GOT_OBJ_TYPE_TREE:
4848 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
4849 ids[0], ids[1], &paths, "", "",
4850 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4851 ignore_whitespace, force_text_diff,
4852 show_diffstat ? &dsa : NULL, repo, outfile);
4854 case GOT_OBJ_TYPE_COMMIT:
4855 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
4856 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
4857 fd1, fd2, ids[0], ids[1], &paths,
4858 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4859 ignore_whitespace, force_text_diff,
4860 show_diffstat ? &dsa : NULL, repo, outfile);
4863 error = got_error(GOT_ERR_OBJ_TYPE);
4868 if (show_diffstat && dsa.nfiles > 0) {
4869 char *header = NULL;
4871 if (asprintf(&header, "diffstat %s %s",
4872 labels[0], labels[1]) == -1) {
4873 error = got_error_from_errno("asprintf");
4877 error = print_diffstat(&dsa, header);
4883 error = printfile(outfile);
4891 got_worktree_close(worktree);
4893 const struct got_error *close_err = got_repo_close(repo);
4898 const struct got_error *pack_err =
4899 got_repo_pack_fds_close(pack_fds);
4903 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
4904 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
4905 got_ref_list_free(&refs);
4906 if (outfile && fclose(outfile) == EOF && error == NULL)
4907 error = got_error_from_errno("fclose");
4908 if (f1 && fclose(f1) == EOF && error == NULL)
4909 error = got_error_from_errno("fclose");
4910 if (f2 && fclose(f2) == EOF && error == NULL)
4911 error = got_error_from_errno("fclose");
4912 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4913 error = got_error_from_errno("close");
4914 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4915 error = got_error_from_errno("close");
4923 "usage: %s blame [-c commit] [-r repository-path] path\n",
4932 char datebuf[11]; /* YYYY-MM-DD + NUL */
4935 struct blame_cb_args {
4936 struct blame_line *lines;
4940 off_t *line_offsets;
4942 struct got_repository *repo;
4945 static const struct got_error *
4946 blame_cb(void *arg, int nlines, int lineno,
4947 struct got_commit_object *commit, struct got_object_id *id)
4949 const struct got_error *err = NULL;
4950 struct blame_cb_args *a = arg;
4951 struct blame_line *bline;
4953 size_t linesize = 0;
4956 time_t committer_time;
4958 if (nlines != a->nlines ||
4959 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4960 return got_error(GOT_ERR_RANGE);
4962 if (sigint_received)
4963 return got_error(GOT_ERR_ITER_COMPLETED);
4966 return NULL; /* no change in this commit */
4968 /* Annotate this line. */
4969 bline = &a->lines[lineno - 1];
4970 if (bline->annotated)
4972 err = got_object_id_str(&bline->id_str, id);
4976 bline->committer = strdup(got_object_commit_get_committer(commit));
4977 if (bline->committer == NULL) {
4978 err = got_error_from_errno("strdup");
4982 committer_time = got_object_commit_get_committer_time(commit);
4983 if (gmtime_r(&committer_time, &tm) == NULL)
4984 return got_error_from_errno("gmtime_r");
4985 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4987 err = got_error(GOT_ERR_NO_SPACE);
4990 bline->annotated = 1;
4992 /* Print lines annotated so far. */
4993 bline = &a->lines[a->lineno_cur - 1];
4994 if (!bline->annotated)
4997 offset = a->line_offsets[a->lineno_cur - 1];
4998 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4999 err = got_error_from_errno("fseeko");
5003 while (a->lineno_cur <= a->nlines && bline->annotated) {
5004 char *smallerthan, *at, *nl, *committer;
5007 if (getline(&line, &linesize, a->f) == -1) {
5009 err = got_error_from_errno("getline");
5013 committer = bline->committer;
5014 smallerthan = strchr(committer, '<');
5015 if (smallerthan && smallerthan[1] != '\0')
5016 committer = smallerthan + 1;
5017 at = strchr(committer, '@');
5020 len = strlen(committer);
5022 committer[8] = '\0';
5024 nl = strchr(line, '\n');
5027 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5028 bline->id_str, bline->datebuf, committer, line);
5031 bline = &a->lines[a->lineno_cur - 1];
5038 static const struct got_error *
5039 cmd_blame(int argc, char *argv[])
5041 const struct got_error *error;
5042 struct got_repository *repo = NULL;
5043 struct got_worktree *worktree = NULL;
5044 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5045 char *link_target = NULL;
5046 struct got_object_id *obj_id = NULL;
5047 struct got_object_id *commit_id = NULL;
5048 struct got_commit_object *commit = NULL;
5049 struct got_blob_object *blob = NULL;
5050 char *commit_id_str = NULL;
5051 struct blame_cb_args bca;
5052 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5054 int *pack_fds = NULL;
5055 FILE *f1 = NULL, *f2 = NULL;
5057 fd1 = got_opentempfd();
5059 return got_error_from_errno("got_opentempfd");
5061 memset(&bca, 0, sizeof(bca));
5064 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5069 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5072 commit_id_str = optarg;
5075 repo_path = realpath(optarg, NULL);
5076 if (repo_path == NULL)
5077 return got_error_from_errno2("realpath",
5079 got_path_strip_trailing_slashes(repo_path);
5095 cwd = getcwd(NULL, 0);
5097 error = got_error_from_errno("getcwd");
5101 error = got_repo_pack_fds_open(&pack_fds);
5105 if (repo_path == NULL) {
5106 error = got_worktree_open(&worktree, cwd);
5107 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5113 strdup(got_worktree_get_repo_path(worktree));
5114 if (repo_path == NULL) {
5115 error = got_error_from_errno("strdup");
5120 repo_path = strdup(cwd);
5121 if (repo_path == NULL) {
5122 error = got_error_from_errno("strdup");
5128 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5133 const char *prefix = got_worktree_get_path_prefix(worktree);
5136 error = got_worktree_resolve_path(&p, worktree, path);
5139 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5140 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5142 error = got_error_from_errno("asprintf");
5147 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5149 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5152 error = got_repo_map_path(&in_repo_path, repo, path);
5157 if (commit_id_str == NULL) {
5158 struct got_reference *head_ref;
5159 error = got_ref_open(&head_ref, repo, worktree ?
5160 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5163 error = got_ref_resolve(&commit_id, repo, head_ref);
5164 got_ref_close(head_ref);
5168 struct got_reflist_head refs;
5170 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5174 error = got_repo_match_object_id(&commit_id, NULL,
5175 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5176 got_ref_list_free(&refs);
5182 /* Release work tree lock. */
5183 got_worktree_close(worktree);
5187 error = got_object_open_as_commit(&commit, repo, commit_id);
5191 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5196 error = got_object_id_by_path(&obj_id, repo, commit,
5197 link_target ? link_target : in_repo_path);
5201 error = got_object_get_type(&obj_type, repo, obj_id);
5205 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5206 error = got_error_path(link_target ? link_target : in_repo_path,
5211 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5214 bca.f = got_opentemp();
5215 if (bca.f == NULL) {
5216 error = got_error_from_errno("got_opentemp");
5219 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5220 &bca.line_offsets, bca.f, blob);
5221 if (error || bca.nlines == 0)
5224 /* Don't include \n at EOF in the blame line count. */
5225 if (bca.line_offsets[bca.nlines - 1] == filesize)
5228 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5229 if (bca.lines == NULL) {
5230 error = got_error_from_errno("calloc");
5234 bca.nlines_prec = 0;
5242 fd2 = got_opentempfd();
5244 error = got_error_from_errno("got_opentempfd");
5247 fd3 = got_opentempfd();
5249 error = got_error_from_errno("got_opentempfd");
5252 f1 = got_opentemp();
5254 error = got_error_from_errno("got_opentemp");
5257 f2 = got_opentemp();
5259 error = got_error_from_errno("got_opentemp");
5262 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5263 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5264 check_cancelled, NULL, fd2, fd3, f1, f2);
5273 got_object_commit_close(commit);
5275 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5276 error = got_error_from_errno("close");
5277 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5278 error = got_error_from_errno("close");
5279 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5280 error = got_error_from_errno("close");
5281 if (f1 && fclose(f1) == EOF && error == NULL)
5282 error = got_error_from_errno("fclose");
5283 if (f2 && fclose(f2) == EOF && error == NULL)
5284 error = got_error_from_errno("fclose");
5287 got_object_blob_close(blob);
5289 got_worktree_close(worktree);
5291 const struct got_error *close_err = got_repo_close(repo);
5296 const struct got_error *pack_err =
5297 got_repo_pack_fds_close(pack_fds);
5302 for (i = 0; i < bca.nlines; i++) {
5303 struct blame_line *bline = &bca.lines[i];
5304 free(bline->id_str);
5305 free(bline->committer);
5309 free(bca.line_offsets);
5310 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5311 error = got_error_from_errno("fclose");
5318 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5319 "[path]\n", getprogname());
5323 static const struct got_error *
5324 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5325 const char *root_path, struct got_repository *repo)
5327 const struct got_error *err = NULL;
5328 int is_root_path = (strcmp(path, root_path) == 0);
5329 const char *modestr = "";
5330 mode_t mode = got_tree_entry_get_mode(te);
5331 char *link_target = NULL;
5333 path += strlen(root_path);
5334 while (path[0] == '/')
5337 if (got_object_tree_entry_is_submodule(te))
5339 else if (S_ISLNK(mode)) {
5342 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5345 for (i = 0; link_target[i] != '\0'; i++) {
5346 if (!isprint((unsigned char)link_target[i]))
5347 link_target[i] = '?';
5352 else if (S_ISDIR(mode))
5354 else if (mode & S_IXUSR)
5357 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5358 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5359 link_target ? " -> ": "", link_target ? link_target : "");
5365 static const struct got_error *
5366 print_tree(const char *path, struct got_commit_object *commit,
5367 int show_ids, int recurse, const char *root_path,
5368 struct got_repository *repo)
5370 const struct got_error *err = NULL;
5371 struct got_object_id *tree_id = NULL;
5372 struct got_tree_object *tree = NULL;
5375 err = got_object_id_by_path(&tree_id, repo, commit, path);
5379 err = got_object_open_as_tree(&tree, repo, tree_id);
5382 nentries = got_object_tree_get_nentries(tree);
5383 for (i = 0; i < nentries; i++) {
5384 struct got_tree_entry *te;
5387 if (sigint_received || sigpipe_received)
5390 te = got_object_tree_get_entry(tree, i);
5393 err = got_object_id_str(&id_str,
5394 got_tree_entry_get_id(te));
5397 if (asprintf(&id, "%s ", id_str) == -1) {
5398 err = got_error_from_errno("asprintf");
5404 err = print_entry(te, id, path, root_path, repo);
5409 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5411 if (asprintf(&child_path, "%s%s%s", path,
5412 path[0] == '/' && path[1] == '\0' ? "" : "/",
5413 got_tree_entry_get_name(te)) == -1) {
5414 err = got_error_from_errno("asprintf");
5417 err = print_tree(child_path, commit, show_ids, 1,
5426 got_object_tree_close(tree);
5431 static const struct got_error *
5432 cmd_tree(int argc, char *argv[])
5434 const struct got_error *error;
5435 struct got_repository *repo = NULL;
5436 struct got_worktree *worktree = NULL;
5437 const char *path, *refname = NULL;
5438 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5439 struct got_object_id *commit_id = NULL;
5440 struct got_commit_object *commit = NULL;
5441 char *commit_id_str = NULL;
5442 int show_ids = 0, recurse = 0;
5444 int *pack_fds = NULL;
5447 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5452 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
5455 commit_id_str = optarg;
5464 repo_path = realpath(optarg, NULL);
5465 if (repo_path == NULL)
5466 return got_error_from_errno2("realpath",
5468 got_path_strip_trailing_slashes(repo_path);
5486 cwd = getcwd(NULL, 0);
5488 error = got_error_from_errno("getcwd");
5492 error = got_repo_pack_fds_open(&pack_fds);
5496 if (repo_path == NULL) {
5497 error = got_worktree_open(&worktree, cwd);
5498 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5504 strdup(got_worktree_get_repo_path(worktree));
5505 if (repo_path == NULL)
5506 error = got_error_from_errno("strdup");
5510 repo_path = strdup(cwd);
5511 if (repo_path == NULL) {
5512 error = got_error_from_errno("strdup");
5518 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5523 const char *prefix = got_worktree_get_path_prefix(worktree);
5526 if (path == NULL || got_path_is_root_dir(path))
5528 error = got_worktree_resolve_path(&p, worktree, path);
5531 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5532 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5534 error = got_error_from_errno("asprintf");
5539 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5543 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5548 error = got_repo_map_path(&in_repo_path, repo, path);
5553 if (commit_id_str == NULL) {
5554 struct got_reference *head_ref;
5556 refname = got_worktree_get_head_ref_name(worktree);
5558 refname = GOT_REF_HEAD;
5559 error = got_ref_open(&head_ref, repo, refname, 0);
5562 error = got_ref_resolve(&commit_id, repo, head_ref);
5563 got_ref_close(head_ref);
5567 struct got_reflist_head refs;
5569 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5573 error = got_repo_match_object_id(&commit_id, NULL,
5574 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5575 got_ref_list_free(&refs);
5581 /* Release work tree lock. */
5582 got_worktree_close(worktree);
5586 error = got_object_open_as_commit(&commit, repo, commit_id);
5590 error = print_tree(in_repo_path, commit, show_ids, recurse,
5591 in_repo_path, repo);
5598 got_object_commit_close(commit);
5600 got_worktree_close(worktree);
5602 const struct got_error *close_err = got_repo_close(repo);
5607 const struct got_error *pack_err =
5608 got_repo_pack_fds_close(pack_fds);
5618 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5619 "[-s status-codes] [path ...]\n", getprogname());
5623 struct got_status_arg {
5628 static const struct got_error *
5629 print_status(void *arg, unsigned char status, unsigned char staged_status,
5630 const char *path, struct got_object_id *blob_id,
5631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5632 int dirfd, const char *de_name)
5634 struct got_status_arg *st = arg;
5636 if (status == staged_status && (status == GOT_STATUS_DELETE))
5637 status = GOT_STATUS_NO_CHANGE;
5638 if (st != NULL && st->status_codes) {
5639 size_t ncodes = strlen(st->status_codes);
5642 for (i = 0; i < ncodes ; i++) {
5644 if (status == st->status_codes[i] ||
5645 staged_status == st->status_codes[i]) {
5650 if (status == st->status_codes[i] ||
5651 staged_status == st->status_codes[i])
5656 if (st->suppress && j == 0)
5663 printf("%c%c %s\n", status, staged_status, path);
5667 static const struct got_error *
5668 cmd_status(int argc, char *argv[])
5670 const struct got_error *error = NULL;
5671 struct got_repository *repo = NULL;
5672 struct got_worktree *worktree = NULL;
5673 struct got_status_arg st;
5675 struct got_pathlist_head paths;
5676 int ch, i, no_ignores = 0;
5677 int *pack_fds = NULL;
5681 memset(&st, 0, sizeof(st));
5682 st.status_codes = NULL;
5686 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5691 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
5697 if (st.status_codes != NULL && st.suppress == 0)
5698 option_conflict('S', 's');
5702 for (i = 0; optarg[i] != '\0'; i++) {
5703 switch (optarg[i]) {
5704 case GOT_STATUS_MODIFY:
5705 case GOT_STATUS_ADD:
5706 case GOT_STATUS_DELETE:
5707 case GOT_STATUS_CONFLICT:
5708 case GOT_STATUS_MISSING:
5709 case GOT_STATUS_OBSTRUCTED:
5710 case GOT_STATUS_UNVERSIONED:
5711 case GOT_STATUS_MODE_CHANGE:
5712 case GOT_STATUS_NONEXISTENT:
5715 errx(1, "invalid status code '%c'",
5719 if (ch == 's' && st.suppress)
5720 option_conflict('s', 'S');
5721 st.status_codes = optarg;
5732 cwd = getcwd(NULL, 0);
5734 error = got_error_from_errno("getcwd");
5738 error = got_repo_pack_fds_open(&pack_fds);
5742 error = got_worktree_open(&worktree, cwd);
5744 if (error->code == GOT_ERR_NOT_WORKTREE)
5745 error = wrap_not_worktree_error(error, "status", cwd);
5749 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5754 error = apply_unveil(got_repo_get_path(repo), 1,
5755 got_worktree_get_root_path(worktree));
5759 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5763 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5764 print_status, &st, check_cancelled, NULL);
5767 const struct got_error *pack_err =
5768 got_repo_pack_fds_close(pack_fds);
5773 const struct got_error *close_err = got_repo_close(repo);
5778 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5786 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
5787 "[-r repository-path] [-s signer-id] name\n", getprogname());
5792 static const struct got_error *
5793 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5795 const struct got_error *err = NULL;
5796 struct got_reflist_entry *re, *se, *new;
5797 struct got_object_id *re_id, *se_id;
5798 struct got_tag_object *re_tag, *se_tag;
5799 time_t re_time, se_time;
5801 STAILQ_FOREACH(re, tags, entry) {
5802 se = STAILQ_FIRST(sorted);
5804 err = got_reflist_entry_dup(&new, re);
5807 STAILQ_INSERT_HEAD(sorted, new, entry);
5810 err = got_ref_resolve(&re_id, repo, re->ref);
5813 err = got_object_open_as_tag(&re_tag, repo, re_id);
5817 re_time = got_object_tag_get_tagger_time(re_tag);
5818 got_object_tag_close(re_tag);
5822 err = got_ref_resolve(&se_id, repo, re->ref);
5825 err = got_object_open_as_tag(&se_tag, repo, se_id);
5829 se_time = got_object_tag_get_tagger_time(se_tag);
5830 got_object_tag_close(se_tag);
5832 if (se_time > re_time) {
5833 err = got_reflist_entry_dup(&new, re);
5836 STAILQ_INSERT_AFTER(sorted, se, new, entry);
5839 se = STAILQ_NEXT(se, entry);
5848 static const struct got_error *
5849 get_tag_refname(char **refname, const char *tag_name)
5851 const struct got_error *err;
5853 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5854 *refname = strdup(tag_name);
5855 if (*refname == NULL)
5856 return got_error_from_errno("strdup");
5857 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
5858 err = got_error_from_errno("asprintf");
5866 static const struct got_error *
5867 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
5868 const char *allowed_signers, const char *revoked_signers, int verbosity)
5870 static const struct got_error *err = NULL;
5871 struct got_reflist_head refs;
5872 struct got_reflist_entry *re;
5873 char *wanted_refname = NULL;
5878 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5883 struct got_reference *ref;
5884 err = get_tag_refname(&wanted_refname, tag_name);
5887 /* Wanted tag reference should exist. */
5888 err = got_ref_open(&ref, repo, wanted_refname, 0);
5894 TAILQ_FOREACH(re, &refs, entry) {
5895 const char *refname;
5896 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5898 const char *tagger, *ssh_sig = NULL;
5899 char *sig_msg = NULL;
5901 struct got_object_id *id;
5902 struct got_tag_object *tag;
5903 struct got_commit_object *commit = NULL;
5905 refname = got_ref_get_name(re->ref);
5906 if (strncmp(refname, "refs/tags/", 10) != 0 ||
5907 (wanted_refname && strcmp(refname, wanted_refname) != 0))
5910 refstr = got_ref_to_str(re->ref);
5911 if (refstr == NULL) {
5912 err = got_error_from_errno("got_ref_to_str");
5916 err = got_ref_resolve(&id, repo, re->ref);
5919 err = got_object_open_as_tag(&tag, repo, id);
5921 if (err->code != GOT_ERR_OBJ_TYPE) {
5925 /* "lightweight" tag */
5926 err = got_object_open_as_commit(&commit, repo, id);
5931 tagger = got_object_commit_get_committer(commit);
5933 got_object_commit_get_committer_time(commit);
5934 err = got_object_id_str(&id_str, id);
5940 tagger = got_object_tag_get_tagger(tag);
5941 tagger_time = got_object_tag_get_tagger_time(tag);
5942 err = got_object_id_str(&id_str,
5943 got_object_tag_get_object_id(tag));
5948 if (tag && verify_tags) {
5949 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
5950 got_object_tag_get_message(tag));
5951 if (ssh_sig && allowed_signers == NULL) {
5952 err = got_error_msg(
5953 GOT_ERR_VERIFY_TAG_SIGNATURE,
5954 "SSH signature verification requires "
5955 "setting allowed_signers in "
5961 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5963 printf("from: %s\n", tagger);
5964 datestr = get_datestr(&tagger_time, datebuf);
5966 printf("date: %s UTC\n", datestr);
5968 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5970 switch (got_object_tag_get_object_type(tag)) {
5971 case GOT_OBJ_TYPE_BLOB:
5972 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5975 case GOT_OBJ_TYPE_TREE:
5976 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5979 case GOT_OBJ_TYPE_COMMIT:
5980 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5983 case GOT_OBJ_TYPE_TAG:
5984 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5994 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
5995 allowed_signers, revoked_signers, verbosity);
5996 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
6000 printf("signature: %s", sig_msg);
6006 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6009 got_object_commit_close(commit);
6011 tagmsg0 = strdup(got_object_tag_get_message(tag));
6012 got_object_tag_close(tag);
6013 if (tagmsg0 == NULL) {
6014 err = got_error_from_errno("strdup");
6021 line = strsep(&tagmsg, "\n");
6023 printf(" %s\n", line);
6028 got_ref_list_free(&refs);
6029 free(wanted_refname);
6031 if (err == NULL && bad_sigs)
6032 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
6036 static const struct got_error *
6037 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6038 const char *tag_name, const char *repo_path)
6040 const struct got_error *err = NULL;
6041 char *template = NULL, *initial_content = NULL;
6042 char *editor = NULL;
6043 int initial_content_len;
6046 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6047 err = got_error_from_errno("asprintf");
6051 initial_content_len = asprintf(&initial_content,
6052 "\n# tagging commit %s as %s\n",
6053 commit_id_str, tag_name);
6054 if (initial_content_len == -1) {
6055 err = got_error_from_errno("asprintf");
6059 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
6063 if (write(fd, initial_content, initial_content_len) == -1) {
6064 err = got_error_from_errno2("write", *tagmsg_path);
6067 if (close(fd) == -1) {
6068 err = got_error_from_errno2("close", *tagmsg_path);
6073 err = get_editor(&editor);
6076 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6077 initial_content_len, 1);
6079 free(initial_content);
6083 if (fd != -1 && close(fd) == -1 && err == NULL)
6084 err = got_error_from_errno2("close", *tagmsg_path);
6093 static const struct got_error *
6094 add_tag(struct got_repository *repo, const char *tagger,
6095 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
6096 const char *signer_id, int verbosity)
6098 const struct got_error *err = NULL;
6099 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6100 char *label = NULL, *commit_id_str = NULL;
6101 struct got_reference *ref = NULL;
6102 char *refname = NULL, *tagmsg = NULL;
6103 char *tagmsg_path = NULL, *tag_id_str = NULL;
6104 int preserve_tagmsg = 0;
6105 struct got_reflist_head refs;
6110 * Don't let the user create a tag name with a leading '-'.
6111 * While technically a valid reference name, this case is usually
6112 * an unintended typo.
6114 if (tag_name[0] == '-')
6115 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6117 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6121 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6122 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6126 err = got_object_id_str(&commit_id_str, commit_id);
6130 err = get_tag_refname(&refname, tag_name);
6133 if (strncmp("refs/tags/", tag_name, 10) == 0)
6136 err = got_ref_open(&ref, repo, refname, 0);
6138 err = got_error(GOT_ERR_TAG_EXISTS);
6140 } else if (err->code != GOT_ERR_NOT_REF)
6143 if (tagmsg_arg == NULL) {
6144 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6145 tag_name, got_repo_get_path(repo));
6147 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6148 tagmsg_path != NULL)
6149 preserve_tagmsg = 1;
6152 /* Editor is done; we can now apply unveil(2) */
6153 err = got_sigs_apply_unveil();
6156 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
6161 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6162 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
6166 preserve_tagmsg = 1;
6170 err = got_ref_alloc(&ref, refname, tag_id);
6173 preserve_tagmsg = 1;
6177 err = got_ref_write(ref, repo);
6180 preserve_tagmsg = 1;
6184 err = got_object_id_str(&tag_id_str, tag_id);
6187 preserve_tagmsg = 1;
6190 printf("Created tag %s\n", tag_id_str);
6192 if (preserve_tagmsg) {
6193 fprintf(stderr, "%s: tag message preserved in %s\n",
6194 getprogname(), tagmsg_path);
6195 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6196 err = got_error_from_errno2("unlink", tagmsg_path);
6201 free(commit_id_str);
6205 got_ref_list_free(&refs);
6209 static const struct got_error *
6210 cmd_tag(int argc, char *argv[])
6212 const struct got_error *error = NULL;
6213 struct got_repository *repo = NULL;
6214 struct got_worktree *worktree = NULL;
6215 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6216 char *gitconfig_path = NULL, *tagger = NULL;
6217 char *allowed_signers = NULL, *revoked_signers = NULL;
6218 const char *signer_id = NULL;
6219 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
6220 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
6221 int *pack_fds = NULL;
6224 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6225 "sendfd unveil", NULL) == -1)
6229 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
6232 commit_id_arg = optarg;
6241 repo_path = realpath(optarg, NULL);
6242 if (repo_path == NULL) {
6243 error = got_error_from_errno2("realpath",
6247 got_path_strip_trailing_slashes(repo_path);
6258 else if (verbosity < 3)
6270 if (do_list || verify_tags) {
6271 if (commit_id_arg != NULL)
6273 "-c option can only be used when creating a tag");
6276 option_conflict('l', 'm');
6278 option_conflict('V', 'm');
6282 option_conflict('l', 's');
6284 option_conflict('V', 's');
6288 } else if (argc != 1)
6294 cwd = getcwd(NULL, 0);
6296 error = got_error_from_errno("getcwd");
6300 error = got_repo_pack_fds_open(&pack_fds);
6304 if (repo_path == NULL) {
6305 error = got_worktree_open(&worktree, cwd);
6306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6312 strdup(got_worktree_get_repo_path(worktree));
6313 if (repo_path == NULL)
6314 error = got_error_from_errno("strdup");
6318 repo_path = strdup(cwd);
6319 if (repo_path == NULL) {
6320 error = got_error_from_errno("strdup");
6326 if (do_list || verify_tags) {
6327 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6330 error = get_allowed_signers(&allowed_signers, repo, worktree);
6333 error = get_revoked_signers(&revoked_signers, repo, worktree);
6337 /* Release work tree lock. */
6338 got_worktree_close(worktree);
6343 * Remove "cpath" promise unless needed for signature tmpfile
6347 got_sigs_apply_unveil();
6350 if (pledge("stdio rpath wpath flock proc exec sendfd "
6351 "unveil", NULL) == -1)
6355 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6358 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
6359 revoked_signers, verbosity);
6361 error = get_gitconfig_path(&gitconfig_path);
6364 error = got_repo_open(&repo, repo_path, gitconfig_path,
6369 error = get_author(&tagger, repo, worktree);
6372 if (signer_id == NULL)
6373 signer_id = get_signer_id(repo, worktree);
6377 error = got_sigs_apply_unveil();
6381 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6386 if (commit_id_arg == NULL) {
6387 struct got_reference *head_ref;
6388 struct got_object_id *commit_id;
6389 error = got_ref_open(&head_ref, repo,
6390 worktree ? got_worktree_get_head_ref_name(worktree)
6394 error = got_ref_resolve(&commit_id, repo, head_ref);
6395 got_ref_close(head_ref);
6398 error = got_object_id_str(&commit_id_str, commit_id);
6405 /* Release work tree lock. */
6406 got_worktree_close(worktree);
6410 error = add_tag(repo, tagger, tag_name,
6411 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
6412 signer_id, verbosity);
6416 const struct got_error *close_err = got_repo_close(repo);
6421 got_worktree_close(worktree);
6423 const struct got_error *pack_err =
6424 got_repo_pack_fds_close(pack_fds);
6430 free(gitconfig_path);
6431 free(commit_id_str);
6433 free(allowed_signers);
6434 free(revoked_signers);
6441 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
6445 static const struct got_error *
6446 add_progress(void *arg, unsigned char status, const char *path)
6448 while (path[0] == '/')
6450 printf("%c %s\n", status, path);
6454 static const struct got_error *
6455 cmd_add(int argc, char *argv[])
6457 const struct got_error *error = NULL;
6458 struct got_repository *repo = NULL;
6459 struct got_worktree *worktree = NULL;
6461 struct got_pathlist_head paths;
6462 struct got_pathlist_entry *pe;
6463 int ch, can_recurse = 0, no_ignores = 0;
6464 int *pack_fds = NULL;
6469 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6474 while ((ch = getopt(argc, argv, "IR")) != -1) {
6494 cwd = getcwd(NULL, 0);
6496 error = got_error_from_errno("getcwd");
6500 error = got_repo_pack_fds_open(&pack_fds);
6504 error = got_worktree_open(&worktree, cwd);
6506 if (error->code == GOT_ERR_NOT_WORKTREE)
6507 error = wrap_not_worktree_error(error, "add", cwd);
6511 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6516 error = apply_unveil(got_repo_get_path(repo), 1,
6517 got_worktree_get_root_path(worktree));
6521 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6528 TAILQ_FOREACH(pe, &paths, entry) {
6529 if (asprintf(&ondisk_path, "%s/%s",
6530 got_worktree_get_root_path(worktree),
6532 error = got_error_from_errno("asprintf");
6535 if (lstat(ondisk_path, &sb) == -1) {
6536 if (errno == ENOENT) {
6540 error = got_error_from_errno2("lstat",
6546 if (S_ISDIR(sb.st_mode)) {
6547 error = got_error_msg(GOT_ERR_BAD_PATH,
6548 "adding directories requires -R option");
6554 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6555 NULL, repo, no_ignores);
6558 const struct got_error *close_err = got_repo_close(repo);
6563 got_worktree_close(worktree);
6565 const struct got_error *pack_err =
6566 got_repo_pack_fds_close(pack_fds);
6570 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6578 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
6583 static const struct got_error *
6584 print_remove_status(void *arg, unsigned char status,
6585 unsigned char staged_status, const char *path)
6587 while (path[0] == '/')
6589 if (status == GOT_STATUS_NONEXISTENT)
6591 if (status == staged_status && (status == GOT_STATUS_DELETE))
6592 status = GOT_STATUS_NO_CHANGE;
6593 printf("%c%c %s\n", status, staged_status, path);
6597 static const struct got_error *
6598 cmd_remove(int argc, char *argv[])
6600 const struct got_error *error = NULL;
6601 struct got_worktree *worktree = NULL;
6602 struct got_repository *repo = NULL;
6603 const char *status_codes = NULL;
6605 struct got_pathlist_head paths;
6606 struct got_pathlist_entry *pe;
6607 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6608 int ignore_missing_paths = 0;
6609 int *pack_fds = NULL;
6614 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6619 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6622 delete_local_mods = 1;
6623 ignore_missing_paths = 1;
6632 for (i = 0; optarg[i] != '\0'; i++) {
6633 switch (optarg[i]) {
6634 case GOT_STATUS_MODIFY:
6635 delete_local_mods = 1;
6637 case GOT_STATUS_MISSING:
6638 ignore_missing_paths = 1;
6641 errx(1, "invalid status code '%c'",
6645 status_codes = optarg;
6659 cwd = getcwd(NULL, 0);
6661 error = got_error_from_errno("getcwd");
6665 error = got_repo_pack_fds_open(&pack_fds);
6669 error = got_worktree_open(&worktree, cwd);
6671 if (error->code == GOT_ERR_NOT_WORKTREE)
6672 error = wrap_not_worktree_error(error, "remove", cwd);
6676 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6681 error = apply_unveil(got_repo_get_path(repo), 1,
6682 got_worktree_get_root_path(worktree));
6686 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6693 TAILQ_FOREACH(pe, &paths, entry) {
6694 if (asprintf(&ondisk_path, "%s/%s",
6695 got_worktree_get_root_path(worktree),
6697 error = got_error_from_errno("asprintf");
6700 if (lstat(ondisk_path, &sb) == -1) {
6701 if (errno == ENOENT) {
6705 error = got_error_from_errno2("lstat",
6711 if (S_ISDIR(sb.st_mode)) {
6712 error = got_error_msg(GOT_ERR_BAD_PATH,
6713 "removing directories requires -R option");
6719 error = got_worktree_schedule_delete(worktree, &paths,
6720 delete_local_mods, status_codes, print_remove_status, NULL,
6721 repo, keep_on_disk, ignore_missing_paths);
6724 const struct got_error *close_err = got_repo_close(repo);
6729 got_worktree_close(worktree);
6731 const struct got_error *pack_err =
6732 got_repo_pack_fds_close(pack_fds);
6736 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6744 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
6745 "[patchfile]\n", getprogname());
6749 static const struct got_error *
6750 patch_from_stdin(int *patchfd)
6752 const struct got_error *err = NULL;
6755 sig_t sighup, sigint, sigquit;
6757 *patchfd = got_opentempfd();
6759 return got_error_from_errno("got_opentempfd");
6761 sighup = signal(SIGHUP, SIG_DFL);
6762 sigint = signal(SIGINT, SIG_DFL);
6763 sigquit = signal(SIGQUIT, SIG_DFL);
6766 r = read(0, buf, sizeof(buf));
6768 err = got_error_from_errno("read");
6773 if (write(*patchfd, buf, r) == -1) {
6774 err = got_error_from_errno("write");
6779 signal(SIGHUP, sighup);
6780 signal(SIGINT, sigint);
6781 signal(SIGQUIT, sigquit);
6783 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
6784 err = got_error_from_errno("lseek");
6794 struct got_patch_progress_arg {
6800 static const struct got_error *
6801 patch_progress(void *arg, const char *old, const char *new,
6802 unsigned char status, const struct got_error *error, int old_from,
6803 int old_lines, int new_from, int new_lines, int offset,
6804 int ws_mangled, const struct got_error *hunk_err)
6806 const char *path = new == NULL ? old : new;
6807 struct got_patch_progress_arg *a = arg;
6809 while (*path == '/')
6812 if (status != GOT_STATUS_NO_CHANGE &&
6813 status != 0 /* per-hunk progress */) {
6814 printf("%c %s\n", status, path);
6815 a->did_something = 1;
6818 if (hunk_err == NULL) {
6819 if (status == GOT_STATUS_CANNOT_UPDATE)
6821 else if (status == GOT_STATUS_CONFLICT)
6826 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6828 if (offset != 0 || hunk_err != NULL || ws_mangled) {
6829 printf("@@ -%d,%d +%d,%d @@ ", old_from,
6830 old_lines, new_from, new_lines);
6831 if (hunk_err != NULL)
6832 printf("%s\n", hunk_err->msg);
6833 else if (offset != 0)
6834 printf("applied with offset %d\n", offset);
6836 printf("hunk contains mangled whitespace\n");
6843 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
6845 if (!ppa->did_something)
6848 if (ppa->conflicts > 0)
6849 printf("Files with merge conflicts: %d\n", ppa->conflicts);
6851 if (ppa->rejects > 0) {
6852 printf("Files where patch failed to apply: %d\n",
6857 static const struct got_error *
6858 cmd_patch(int argc, char *argv[])
6860 const struct got_error *error = NULL, *close_error = NULL;
6861 struct got_worktree *worktree = NULL;
6862 struct got_repository *repo = NULL;
6863 struct got_reflist_head refs;
6864 struct got_object_id *commit_id = NULL;
6865 const char *commit_id_str = NULL;
6869 int ch, nop = 0, strip = -1, reverse = 0;
6871 int *pack_fds = NULL;
6872 struct got_patch_progress_arg ppa;
6877 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
6878 "unveil", NULL) == -1)
6882 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
6885 commit_id_str = optarg;
6891 strip = strtonum(optarg, 0, INT_MAX, &errstr);
6893 errx(1, "pathname strip count is %s: %s",
6909 error = patch_from_stdin(&patchfd);
6912 } else if (argc == 1) {
6913 patchfd = open(argv[0], O_RDONLY);
6914 if (patchfd == -1) {
6915 error = got_error_from_errno2("open", argv[0]);
6918 if (fstat(patchfd, &sb) == -1) {
6919 error = got_error_from_errno2("fstat", argv[0]);
6922 if (!S_ISREG(sb.st_mode)) {
6923 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
6929 if ((cwd = getcwd(NULL, 0)) == NULL) {
6930 error = got_error_from_errno("getcwd");
6934 error = got_repo_pack_fds_open(&pack_fds);
6938 error = got_worktree_open(&worktree, cwd);
6942 const char *repo_path = got_worktree_get_repo_path(worktree);
6943 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6947 error = apply_unveil(got_repo_get_path(repo), 0,
6948 got_worktree_get_root_path(worktree));
6952 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6956 if (commit_id_str != NULL) {
6957 error = got_repo_match_object_id(&commit_id, NULL,
6958 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6963 memset(&ppa, 0, sizeof(ppa));
6964 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
6965 commit_id, patch_progress, &ppa, check_cancelled, NULL);
6966 print_patch_progress_stats(&ppa);
6968 got_ref_list_free(&refs);
6971 close_error = got_repo_close(repo);
6973 error = close_error;
6975 if (worktree != NULL) {
6976 close_error = got_worktree_close(worktree);
6978 error = close_error;
6981 const struct got_error *pack_err =
6982 got_repo_pack_fds_close(pack_fds);
6993 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
6998 static const struct got_error *
6999 revert_progress(void *arg, unsigned char status, const char *path)
7001 if (status == GOT_STATUS_UNVERSIONED)
7004 while (path[0] == '/')
7006 printf("%c %s\n", status, path);
7010 struct choose_patch_arg {
7011 FILE *patch_script_file;
7015 static const struct got_error *
7016 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7017 int nchanges, const char *action)
7019 const struct got_error *err;
7021 size_t linesize = 0;
7025 case GOT_STATUS_ADD:
7026 printf("A %s\n%s this addition? [y/n] ", path, action);
7028 case GOT_STATUS_DELETE:
7029 printf("D %s\n%s this deletion? [y/n] ", path, action);
7031 case GOT_STATUS_MODIFY:
7032 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7033 return got_error_from_errno("fseek");
7034 printf(GOT_COMMIT_SEP_STR);
7035 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7037 if (linelen == -1 && ferror(patch_file)) {
7038 err = got_error_from_errno("getline");
7043 printf(GOT_COMMIT_SEP_STR);
7044 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7045 path, n, nchanges, action);
7048 return got_error_path(path, GOT_ERR_FILE_STATUS);
7055 static const struct got_error *
7056 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7057 FILE *patch_file, int n, int nchanges)
7059 const struct got_error *err = NULL;
7061 size_t linesize = 0;
7064 struct choose_patch_arg *a = arg;
7066 *choice = GOT_PATCH_CHOICE_NONE;
7068 if (a->patch_script_file) {
7070 err = show_change(status, path, patch_file, n, nchanges,
7074 linelen = getline(&line, &linesize, a->patch_script_file);
7075 if (linelen == -1) {
7076 if (ferror(a->patch_script_file))
7077 return got_error_from_errno("getline");
7080 nl = strchr(line, '\n');
7083 if (strcmp(line, "y") == 0) {
7084 *choice = GOT_PATCH_CHOICE_YES;
7086 } else if (strcmp(line, "n") == 0) {
7087 *choice = GOT_PATCH_CHOICE_NO;
7089 } else if (strcmp(line, "q") == 0 &&
7090 status == GOT_STATUS_MODIFY) {
7091 *choice = GOT_PATCH_CHOICE_QUIT;
7094 printf("invalid response '%s'\n", line);
7099 while (resp != 'y' && resp != 'n' && resp != 'q') {
7100 err = show_change(status, path, patch_file, n, nchanges,
7107 if (status == GOT_STATUS_MODIFY) {
7108 if (resp != 'y' && resp != 'n' && resp != 'q') {
7109 printf("invalid response '%c'\n", resp);
7112 } else if (resp != 'y' && resp != 'n') {
7113 printf("invalid response '%c'\n", resp);
7119 *choice = GOT_PATCH_CHOICE_YES;
7120 else if (resp == 'n')
7121 *choice = GOT_PATCH_CHOICE_NO;
7122 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7123 *choice = GOT_PATCH_CHOICE_QUIT;
7128 struct wt_commitable_path_arg {
7129 struct got_pathlist_head *commit_paths;
7134 * Shortcut work tree status callback to determine if the set of paths scanned
7135 * has at least one versioned path that is being modified and, if not NULL, is
7136 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
7137 * soon as a path is passed with a status that satisfies this criteria.
7139 static const struct got_error *
7140 worktree_has_commitable_path(void *arg, unsigned char status,
7141 unsigned char staged_status, const char *path,
7142 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7143 struct got_object_id *commit_id, int dirfd, const char *de_name)
7145 struct wt_commitable_path_arg *a = arg;
7147 if (status == staged_status && (status == GOT_STATUS_DELETE))
7148 status = GOT_STATUS_NO_CHANGE;
7150 if (!(status == GOT_STATUS_NO_CHANGE ||
7151 status == GOT_STATUS_UNVERSIONED) ||
7152 staged_status != GOT_STATUS_NO_CHANGE) {
7153 if (a->commit_paths != NULL) {
7154 struct got_pathlist_entry *pe;
7156 TAILQ_FOREACH(pe, a->commit_paths, entry) {
7157 if (strncmp(path, pe->path,
7158 pe->path_len) == 0) {
7159 *a->has_changes = 1;
7164 *a->has_changes = 1;
7166 if (*a->has_changes)
7167 return got_error(GOT_ERR_FILE_MODIFIED);
7174 * Check that the changeset of the commit identified by id is
7175 * comprised of at least one modified path that is being committed.
7177 static const struct got_error *
7178 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
7179 struct got_object_id *id, struct got_worktree *worktree,
7180 struct got_repository *repo)
7182 const struct got_error *err;
7183 struct got_pathlist_head paths;
7184 struct got_commit_object *commit = NULL, *pcommit = NULL;
7185 struct got_tree_object *tree = NULL, *ptree = NULL;
7186 struct got_object_qid *pid;
7190 err = got_object_open_as_commit(&commit, repo, id);
7194 err = got_object_open_as_tree(&tree, repo,
7195 got_object_commit_get_tree_id(commit));
7199 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7201 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
7205 err = got_object_open_as_tree(&ptree, repo,
7206 got_object_commit_get_tree_id(pcommit));
7211 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
7212 got_diff_tree_collect_changed_paths, &paths, 0);
7216 err = got_worktree_status(worktree, &paths, repo, 0,
7217 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
7218 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
7220 * At least one changed path in the referenced commit is
7221 * modified in the work tree, that's all we need to know!
7227 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
7229 got_object_commit_close(commit);
7231 got_object_commit_close(pcommit);
7233 got_object_tree_close(tree);
7235 got_object_tree_close(ptree);
7240 * Remove any "logmsg" reference comprised entirely of paths that have
7241 * been reverted in this work tree. If any path in the logmsg ref changeset
7242 * remains in a changed state in the worktree, do not remove the reference.
7244 static const struct got_error *
7245 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
7247 const struct got_error *err;
7248 struct got_reflist_head refs;
7249 struct got_reflist_entry *re;
7250 struct got_commit_object *commit = NULL;
7251 struct got_object_id *commit_id = NULL;
7252 struct wt_commitable_path_arg wcpa;
7253 char *uuidstr = NULL;
7257 err = got_worktree_get_uuid(&uuidstr, worktree);
7261 err = got_ref_list(&refs, repo, "refs/got/worktree",
7262 got_ref_cmp_by_name, repo);
7266 TAILQ_FOREACH(re, &refs, entry) {
7267 const char *refname;
7268 int has_changes = 0;
7270 refname = got_ref_get_name(re->ref);
7272 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7273 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
7274 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7275 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7276 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
7277 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7281 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7282 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7286 err = got_repo_match_object_id(&commit_id, NULL, refname,
7287 GOT_OBJ_TYPE_COMMIT, NULL, repo);
7291 err = got_object_open_as_commit(&commit, repo, commit_id);
7295 wcpa.commit_paths = NULL;
7296 wcpa.has_changes = &has_changes;
7298 err = commit_path_changed_in_worktree(&wcpa, commit_id,
7304 err = got_ref_delete(re->ref, repo);
7309 got_object_commit_close(commit);
7318 got_ref_list_free(&refs);
7320 got_object_commit_close(commit);
7324 static const struct got_error *
7325 cmd_revert(int argc, char *argv[])
7327 const struct got_error *error = NULL;
7328 struct got_worktree *worktree = NULL;
7329 struct got_repository *repo = NULL;
7330 char *cwd = NULL, *path = NULL;
7331 struct got_pathlist_head paths;
7332 struct got_pathlist_entry *pe;
7333 int ch, can_recurse = 0, pflag = 0;
7334 FILE *patch_script_file = NULL;
7335 const char *patch_script_path = NULL;
7336 struct choose_patch_arg cpa;
7337 int *pack_fds = NULL;
7342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7343 "unveil", NULL) == -1)
7347 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
7350 patch_script_path = optarg;
7369 if (patch_script_path && !pflag)
7370 errx(1, "-F option can only be used together with -p option");
7372 cwd = getcwd(NULL, 0);
7374 error = got_error_from_errno("getcwd");
7378 error = got_repo_pack_fds_open(&pack_fds);
7382 error = got_worktree_open(&worktree, cwd);
7384 if (error->code == GOT_ERR_NOT_WORKTREE)
7385 error = wrap_not_worktree_error(error, "revert", cwd);
7389 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7394 if (patch_script_path) {
7395 patch_script_file = fopen(patch_script_path, "re");
7396 if (patch_script_file == NULL) {
7397 error = got_error_from_errno2("fopen",
7404 * XXX "c" perm needed on repo dir to delete merge references.
7406 error = apply_unveil(got_repo_get_path(repo), 0,
7407 got_worktree_get_root_path(worktree));
7411 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7418 TAILQ_FOREACH(pe, &paths, entry) {
7419 if (asprintf(&ondisk_path, "%s/%s",
7420 got_worktree_get_root_path(worktree),
7422 error = got_error_from_errno("asprintf");
7425 if (lstat(ondisk_path, &sb) == -1) {
7426 if (errno == ENOENT) {
7430 error = got_error_from_errno2("lstat",
7436 if (S_ISDIR(sb.st_mode)) {
7437 error = got_error_msg(GOT_ERR_BAD_PATH,
7438 "reverting directories requires -R option");
7444 cpa.patch_script_file = patch_script_file;
7445 cpa.action = "revert";
7446 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7447 pflag ? choose_patch : NULL, &cpa, repo);
7449 error = rm_logmsg_ref(worktree, repo);
7451 if (patch_script_file && fclose(patch_script_file) == EOF &&
7453 error = got_error_from_errno2("fclose", patch_script_path);
7455 const struct got_error *close_err = got_repo_close(repo);
7460 got_worktree_close(worktree);
7462 const struct got_error *pack_err =
7463 got_repo_pack_fds_close(pack_fds);
7467 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7476 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
7477 "[-m message] [path ...]\n", getprogname());
7481 struct collect_commit_logmsg_arg {
7482 const char *cmdline_log;
7483 const char *prepared_log;
7484 const char *merged_log;
7485 int non_interactive;
7487 const char *worktree_path;
7488 const char *repo_path;
7489 const char *dial_proto;
7493 static const struct got_error *
7494 read_prepared_logmsg(char **logmsg, const char *path)
7496 const struct got_error *err = NULL;
7502 memset(&sb, 0, sizeof(sb));
7504 f = fopen(path, "re");
7506 return got_error_from_errno2("fopen", path);
7508 if (fstat(fileno(f), &sb) == -1) {
7509 err = got_error_from_errno2("fstat", path);
7512 if (sb.st_size == 0) {
7513 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7517 *logmsg = malloc(sb.st_size + 1);
7518 if (*logmsg == NULL) {
7519 err = got_error_from_errno("malloc");
7523 r = fread(*logmsg, 1, sb.st_size, f);
7524 if (r != sb.st_size) {
7526 err = got_error_from_errno2("fread", path);
7528 err = got_error(GOT_ERR_IO);
7531 (*logmsg)[sb.st_size] = '\0';
7533 if (fclose(f) == EOF && err == NULL)
7534 err = got_error_from_errno2("fclose", path);
7542 static const struct got_error *
7543 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
7544 const char *diff_path, char **logmsg, void *arg)
7546 char *initial_content = NULL;
7547 struct got_pathlist_entry *pe;
7548 const struct got_error *err = NULL;
7549 char *template = NULL;
7550 char *prepared_msg = NULL, *merged_msg = NULL;
7551 struct collect_commit_logmsg_arg *a = arg;
7552 int initial_content_len;
7556 /* if a message was specified on the command line, just use it */
7557 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
7558 len = strlen(a->cmdline_log) + 1;
7559 *logmsg = malloc(len + 1);
7560 if (*logmsg == NULL)
7561 return got_error_from_errno("malloc");
7562 strlcpy(*logmsg, a->cmdline_log, len);
7564 } else if (a->prepared_log != NULL && a->non_interactive)
7565 return read_prepared_logmsg(logmsg, a->prepared_log);
7567 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7568 return got_error_from_errno("asprintf");
7570 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
7574 if (a->prepared_log) {
7575 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
7578 } else if (a->merged_log) {
7579 err = read_prepared_logmsg(&merged_msg, a->merged_log);
7584 initial_content_len = asprintf(&initial_content,
7585 "%s%s\n# changes to be committed:\n",
7586 prepared_msg ? prepared_msg : "",
7587 merged_msg ? merged_msg : "");
7588 if (initial_content_len == -1) {
7589 err = got_error_from_errno("asprintf");
7593 if (write(fd, initial_content, initial_content_len) == -1) {
7594 err = got_error_from_errno2("write", a->logmsg_path);
7598 TAILQ_FOREACH(pe, commitable_paths, entry) {
7599 struct got_commitable *ct = pe->data;
7600 dprintf(fd, "# %c %s\n",
7601 got_commitable_get_status(ct),
7602 got_commitable_get_path(ct));
7606 dprintf(fd, "# detailed changes can be viewed in %s\n",
7610 if (close(fd) == -1) {
7611 err = got_error_from_errno2("close", a->logmsg_path);
7616 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7617 initial_content_len, a->prepared_log ? 0 : 1);
7619 free(initial_content);
7624 if (fd != -1 && close(fd) == -1 && err == NULL)
7625 err = got_error_from_errno2("close", a->logmsg_path);
7627 /* Editor is done; we can now apply unveil(2) */
7629 err = got_dial_apply_unveil(a->dial_proto);
7631 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7639 static const struct got_error *
7640 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
7641 const char *type, int has_content)
7643 const struct got_error *err = NULL;
7644 char *logmsg = NULL;
7646 err = got_object_commit_get_logmsg(&logmsg, commit);
7650 if (fprintf(f, "%s# log message of %s commit %s:%s",
7651 has_content ? "\n" : "", type, idstr, logmsg) < 0)
7652 err = got_ferror(f, GOT_ERR_IO);
7659 * Lookup "logmsg" references of backed-out and cherrypicked commits
7660 * belonging to the current work tree. If found, and the worktree has
7661 * at least one modified file that was changed in the referenced commit,
7662 * add its log message to a new temporary file at *logmsg_path.
7663 * Add all refs found to matched_refs to be scheduled for removal on
7664 * successful commit.
7666 static const struct got_error *
7667 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
7668 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
7669 struct got_repository *repo)
7671 const struct got_error *err;
7672 struct got_commit_object *commit = NULL;
7673 struct got_object_id *id = NULL;
7674 struct got_reflist_head refs;
7675 struct got_reflist_entry *re, *re_match;
7677 char *uuidstr = NULL;
7678 int added_logmsg = 0;
7682 *logmsg_path = NULL;
7684 err = got_worktree_get_uuid(&uuidstr, worktree);
7688 err = got_ref_list(&refs, repo, "refs/got/worktree",
7689 got_ref_cmp_by_name, repo);
7693 TAILQ_FOREACH(re, &refs, entry) {
7694 const char *refname, *type;
7695 struct wt_commitable_path_arg wcpa;
7698 refname = got_ref_get_name(re->ref);
7700 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7701 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
7702 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7703 type = "cherrypicked";
7704 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7705 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
7706 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7707 type = "backed-out";
7711 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7712 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7716 err = got_repo_match_object_id(&id, NULL, refname,
7717 GOT_OBJ_TYPE_COMMIT, NULL, repo);
7721 err = got_object_open_as_commit(&commit, repo, id);
7725 wcpa.commit_paths = paths;
7726 wcpa.has_changes = &add_logmsg;
7728 err = commit_path_changed_in_worktree(&wcpa, id,
7735 err = got_opentemp_named(logmsg_path, &f,
7736 "got-commit-logmsg", "");
7740 err = cat_logmsg(f, commit, refname, type,
7747 err = got_reflist_entry_dup(&re_match, re);
7750 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
7753 got_object_commit_close(commit);
7762 got_ref_list_free(&refs);
7764 got_object_commit_close(commit);
7765 if (f && fclose(f) == EOF && err == NULL)
7766 err = got_error_from_errno("fclose");
7767 if (!added_logmsg) {
7768 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
7769 err = got_error_from_errno2("unlink", *logmsg_path);
7770 *logmsg_path = NULL;
7775 static const struct got_error *
7776 cmd_commit(int argc, char *argv[])
7778 const struct got_error *error = NULL;
7779 struct got_worktree *worktree = NULL;
7780 struct got_repository *repo = NULL;
7781 char *cwd = NULL, *id_str = NULL;
7782 struct got_object_id *id = NULL;
7783 const char *logmsg = NULL;
7784 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
7785 struct collect_commit_logmsg_arg cl_arg;
7786 const char *author = NULL;
7787 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
7788 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7789 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7790 int show_diff = 1, commit_conflicts = 0;
7791 struct got_pathlist_head paths;
7792 struct got_reflist_head refs;
7793 struct got_reflist_entry *re;
7794 int *pack_fds = NULL;
7795 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7796 const struct got_remote_repo *remotes, *remote = NULL;
7798 char *proto = NULL, *host = NULL, *port = NULL;
7799 char *repo_name = NULL, *server_path = NULL;
7800 const char *remote_name;
7806 cl_arg.logmsg_path = NULL;
7809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7810 "unveil", NULL) == -1)
7814 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
7818 error = valid_author(author);
7823 commit_conflicts = 1;
7827 option_conflict('F', 'm');
7828 prepared_logmsg = realpath(optarg, NULL);
7829 if (prepared_logmsg == NULL)
7830 return got_error_from_errno2("realpath",
7834 if (prepared_logmsg)
7835 option_conflict('m', 'F');
7839 non_interactive = 1;
7845 allow_bad_symlinks = 1;
7856 cwd = getcwd(NULL, 0);
7858 error = got_error_from_errno("getcwd");
7862 error = got_repo_pack_fds_open(&pack_fds);
7866 error = got_worktree_open(&worktree, cwd);
7868 if (error->code == GOT_ERR_NOT_WORKTREE)
7869 error = wrap_not_worktree_error(error, "commit", cwd);
7873 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7876 if (rebase_in_progress) {
7877 error = got_error(GOT_ERR_REBASING);
7881 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7886 error = get_gitconfig_path(&gitconfig_path);
7889 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7890 gitconfig_path, pack_fds);
7894 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7897 if (merge_in_progress) {
7898 error = got_error(GOT_ERR_MERGE_BUSY);
7902 error = get_author(&committer, repo, worktree);
7909 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7910 worktree_conf = got_worktree_get_gotconfig(worktree);
7911 if (worktree_conf) {
7912 got_gotconfig_get_remotes(&nremotes, &remotes,
7914 for (i = 0; i < nremotes; i++) {
7915 if (strcmp(remotes[i].name, remote_name) == 0) {
7916 remote = &remotes[i];
7921 if (remote == NULL) {
7922 repo_conf = got_repo_get_gotconfig(repo);
7924 got_gotconfig_get_remotes(&nremotes, &remotes,
7926 for (i = 0; i < nremotes; i++) {
7927 if (strcmp(remotes[i].name, remote_name) == 0) {
7928 remote = &remotes[i];
7934 if (remote == NULL) {
7935 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7936 for (i = 0; i < nremotes; i++) {
7937 if (strcmp(remotes[i].name, remote_name) == 0) {
7938 remote = &remotes[i];
7943 if (remote == NULL) {
7944 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7948 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7949 &repo_name, remote->fetch_url);
7953 if (strcmp(proto, "git") == 0) {
7955 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7956 "sendfd dns inet unveil", NULL) == -1)
7959 } else if (strcmp(proto, "git+ssh") == 0 ||
7960 strcmp(proto, "ssh") == 0) {
7962 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7963 "sendfd unveil", NULL) == -1)
7966 } else if (strcmp(proto, "http") == 0 ||
7967 strcmp(proto, "git+http") == 0) {
7968 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7971 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7976 /*if (verbosity >= 0) {
7977 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
7978 remote->name, proto, host,
7979 port ? ":" : "", port ? port : "",
7980 *server_path == '/' ? "" : "/", server_path);
7984 * unveil(2) traverses exec(2); if an editor is used we have
7985 * to apply unveil after the log message has been written during
7988 if (logmsg == NULL || strlen(logmsg) == 0)
7989 error = get_editor(&editor);
7991 error = got_dial_apply_unveil(proto);
7994 error = apply_unveil(got_repo_get_path(repo), 0,
7995 got_worktree_get_root_path(worktree));
8000 if (prepared_logmsg == NULL) {
8001 error = lookup_logmsg_ref(&merged_logmsg,
8002 argc > 0 ? &paths : NULL, &refs, worktree, repo);
8007 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8011 cl_arg.editor = editor;
8012 cl_arg.cmdline_log = logmsg;
8013 cl_arg.prepared_log = prepared_logmsg;
8014 cl_arg.merged_log = merged_logmsg;
8015 cl_arg.non_interactive = non_interactive;
8016 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8017 cl_arg.repo_path = got_repo_get_path(repo);
8018 cl_arg.dial_proto = proto;
8019 error = got_worktree_cvg_commit(&id, worktree, &paths, author,
8020 committer, allow_bad_symlinks, show_diff, commit_conflicts,
8021 collect_commit_logmsg, &cl_arg, print_status, NULL, proto, host,
8022 port, server_path, verbosity, remote, check_cancelled, repo);
8024 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8025 cl_arg.logmsg_path != NULL)
8026 preserve_logmsg = 1;
8030 error = got_object_id_str(&id_str, id);
8033 printf("Created commit %s\n", id_str);
8035 TAILQ_FOREACH(re, &refs, entry) {
8036 error = got_ref_delete(re->ref, repo);
8042 if (preserve_logmsg) {
8043 fprintf(stderr, "%s: log message preserved in %s\n",
8044 getprogname(), cl_arg.logmsg_path);
8045 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8047 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8048 free(cl_arg.logmsg_path);
8049 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
8050 error = got_error_from_errno2("unlink", merged_logmsg);
8051 free(merged_logmsg);
8053 const struct got_error *close_err = got_repo_close(repo);
8058 got_worktree_close(worktree);
8060 const struct got_error *pack_err =
8061 got_repo_pack_fds_close(pack_fds);
8065 got_ref_list_free(&refs);
8066 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8069 free(gitconfig_path);
8072 free(prepared_logmsg);
8077 * Print and if delete is set delete all ref_prefix references.
8078 * If wanted_ref is not NULL, only print or delete this reference.
8080 static const struct got_error *
8081 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
8082 const char *wanted_ref, int delete, struct got_worktree *worktree,
8083 struct got_repository *repo)
8085 const struct got_error *err;
8086 struct got_pathlist_head paths;
8087 struct got_reflist_head refs;
8088 struct got_reflist_entry *re;
8089 struct got_reflist_object_id_map *refs_idmap = NULL;
8090 struct got_commit_object *commit = NULL;
8091 struct got_object_id *id = NULL;
8092 const char *header_prefix;
8093 char *uuidstr = NULL;
8099 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
8103 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8107 if (worktree != NULL) {
8108 err = got_worktree_get_uuid(&uuidstr, worktree);
8114 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
8118 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
8119 header_prefix = "backout";
8121 header_prefix = "cherrypick";
8123 TAILQ_FOREACH(re, &refs, entry) {
8124 const char *refname, *wt;
8126 refname = got_ref_get_name(re->ref);
8128 err = check_cancelled(NULL);
8132 if (strncmp(refname, ref_prefix, prefix_len) == 0)
8133 refname += prefix_len + 1; /* skip '-' delimiter */
8139 if (worktree == NULL || strncmp(refname, uuidstr,
8140 GOT_WORKTREE_UUID_STRLEN) == 0)
8141 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8145 err = got_repo_match_object_id(&id, NULL, refname,
8146 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8150 err = got_object_open_as_commit(&commit, repo, id);
8155 found = strncmp(wanted_ref, refname,
8156 strlen(wanted_ref)) == 0;
8157 if (wanted_ref && !found) {
8158 struct got_reflist_head *ci_refs;
8160 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
8164 char *refs_str = NULL;
8165 char const *r = NULL;
8167 err = build_refs_str(&refs_str, ci_refs, id,
8174 if (strncmp(r, wanted_ref,
8175 strlen(wanted_ref)) == 0) {
8187 if (wanted_ref == NULL || found) {
8189 err = got_ref_delete(re->ref, repo);
8192 printf("Deleted: ");
8193 err = print_commit_oneline(commit, id, repo,
8197 * Print paths modified by commit to help
8198 * associate commits with worktree changes.
8200 err = get_changed_paths(&paths, commit,
8205 err = print_commit(commit, id, repo, NULL,
8206 &paths, NULL, 0, 0, refs_idmap, NULL,
8208 got_pathlist_free(&paths,
8209 GOT_PATHLIST_FREE_ALL);
8211 if (worktree == NULL)
8212 printf("work tree: %.*s\n\n",
8213 GOT_WORKTREE_UUID_STRLEN, wt);
8219 got_object_commit_close(commit);
8225 if (wanted_ref != NULL && !found)
8226 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
8231 got_ref_list_free(&refs);
8232 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8234 got_reflist_object_id_map_free(refs_idmap);
8236 got_object_commit_close(commit);
8241 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
8242 * identified by id for log messages to prepopulate the editor on commit.
8244 static const struct got_error *
8245 logmsg_ref(struct got_object_id *id, const char *prefix,
8246 struct got_worktree *worktree, struct got_repository *repo)
8248 const struct got_error *err = NULL;
8249 char *idstr, *ref = NULL, *refname = NULL;
8250 int histedit_in_progress;
8251 int rebase_in_progress, merge_in_progress;
8254 * Silenty refuse to create merge reference if any histedit, merge,
8255 * or rebase operation is in progress.
8257 err = got_worktree_histedit_in_progress(&histedit_in_progress,
8261 if (histedit_in_progress)
8264 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8267 if (rebase_in_progress)
8270 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
8274 if (merge_in_progress)
8277 err = got_object_id_str(&idstr, id);
8281 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
8285 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
8286 err = got_error_from_errno("asprintf");
8290 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
8300 usage_cherrypick(void)
8302 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
8307 static const struct got_error *
8308 cmd_cherrypick(int argc, char *argv[])
8310 const struct got_error *error = NULL;
8311 struct got_worktree *worktree = NULL;
8312 struct got_repository *repo = NULL;
8313 char *cwd = NULL, *commit_id_str = NULL;
8314 struct got_object_id *commit_id = NULL;
8315 struct got_commit_object *commit = NULL;
8316 struct got_object_qid *pid;
8317 int ch, list_refs = 0, remove_refs = 0;
8318 struct got_update_progress_arg upa;
8319 int *pack_fds = NULL;
8322 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8323 "unveil", NULL) == -1)
8327 while ((ch = getopt(argc, argv, "lX")) != -1) {
8344 if (list_refs || remove_refs) {
8345 if (argc != 0 && argc != 1)
8347 } else if (argc != 1)
8349 if (list_refs && remove_refs)
8350 option_conflict('l', 'X');
8352 cwd = getcwd(NULL, 0);
8354 error = got_error_from_errno("getcwd");
8358 error = got_repo_pack_fds_open(&pack_fds);
8362 error = got_worktree_open(&worktree, cwd);
8364 if (list_refs || remove_refs) {
8365 if (error->code != GOT_ERR_NOT_WORKTREE)
8368 if (error->code == GOT_ERR_NOT_WORKTREE)
8369 error = wrap_not_worktree_error(error,
8375 error = got_repo_open(&repo,
8376 worktree ? got_worktree_get_repo_path(worktree) : cwd,
8381 error = apply_unveil(got_repo_get_path(repo), 0,
8382 worktree ? got_worktree_get_root_path(worktree) : NULL);
8386 if (list_refs || remove_refs) {
8387 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8388 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
8389 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8393 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8394 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8397 error = got_object_id_str(&commit_id_str, commit_id);
8401 error = got_object_open_as_commit(&commit, repo, commit_id);
8404 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8405 memset(&upa, 0, sizeof(upa));
8406 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
8407 commit_id, repo, update_progress, &upa, check_cancelled,
8412 if (upa.did_something) {
8413 error = logmsg_ref(commit_id,
8414 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
8417 printf("Merged commit %s\n", commit_id_str);
8419 print_merge_progress_stats(&upa);
8423 got_object_commit_close(commit);
8424 free(commit_id_str);
8426 got_worktree_close(worktree);
8428 const struct got_error *close_err = got_repo_close(repo);
8433 const struct got_error *pack_err =
8434 got_repo_pack_fds_close(pack_fds);
8445 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
8449 static const struct got_error *
8450 cmd_backout(int argc, char *argv[])
8452 const struct got_error *error = NULL;
8453 struct got_worktree *worktree = NULL;
8454 struct got_repository *repo = NULL;
8455 char *cwd = NULL, *commit_id_str = NULL;
8456 struct got_object_id *commit_id = NULL;
8457 struct got_commit_object *commit = NULL;
8458 struct got_object_qid *pid;
8459 int ch, list_refs = 0, remove_refs = 0;
8460 struct got_update_progress_arg upa;
8461 int *pack_fds = NULL;
8464 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8465 "unveil", NULL) == -1)
8469 while ((ch = getopt(argc, argv, "lX")) != -1) {
8486 if (list_refs || remove_refs) {
8487 if (argc != 0 && argc != 1)
8489 } else if (argc != 1)
8491 if (list_refs && remove_refs)
8492 option_conflict('l', 'X');
8494 cwd = getcwd(NULL, 0);
8496 error = got_error_from_errno("getcwd");
8500 error = got_repo_pack_fds_open(&pack_fds);
8504 error = got_worktree_open(&worktree, cwd);
8506 if (list_refs || remove_refs) {
8507 if (error->code != GOT_ERR_NOT_WORKTREE)
8510 if (error->code == GOT_ERR_NOT_WORKTREE)
8511 error = wrap_not_worktree_error(error,
8517 error = got_repo_open(&repo,
8518 worktree ? got_worktree_get_repo_path(worktree) : cwd,
8523 error = apply_unveil(got_repo_get_path(repo), 0,
8524 worktree ? got_worktree_get_root_path(worktree) : NULL);
8528 if (list_refs || remove_refs) {
8529 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
8530 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
8531 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8535 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8536 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8539 error = got_object_id_str(&commit_id_str, commit_id);
8543 error = got_object_open_as_commit(&commit, repo, commit_id);
8546 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8548 error = got_error(GOT_ERR_ROOT_COMMIT);
8552 memset(&upa, 0, sizeof(upa));
8553 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
8554 repo, update_progress, &upa, check_cancelled, NULL);
8558 if (upa.did_something) {
8559 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8563 printf("Backed out commit %s\n", commit_id_str);
8565 print_merge_progress_stats(&upa);
8569 got_object_commit_close(commit);
8570 free(commit_id_str);
8572 got_worktree_close(worktree);
8574 const struct got_error *close_err = got_repo_close(repo);
8579 const struct got_error *pack_err =
8580 got_repo_pack_fds_close(pack_fds);
8590 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
8591 "arg ...\n", getprogname());
8595 static const struct got_error *
8596 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8598 const struct got_error *err;
8599 struct got_blob_object *blob;
8602 fd = got_opentempfd();
8604 return got_error_from_errno("got_opentempfd");
8606 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
8610 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8612 if (fd != -1 && close(fd) == -1 && err == NULL)
8613 err = got_error_from_errno("close");
8615 got_object_blob_close(blob);
8619 static const struct got_error *
8620 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8622 const struct got_error *err;
8623 struct got_tree_object *tree;
8626 err = got_object_open_as_tree(&tree, repo, id);
8630 nentries = got_object_tree_get_nentries(tree);
8631 for (i = 0; i < nentries; i++) {
8632 struct got_tree_entry *te;
8634 if (sigint_received || sigpipe_received)
8636 te = got_object_tree_get_entry(tree, i);
8637 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8640 fprintf(outfile, "%s %.7o %s\n", id_str,
8641 got_tree_entry_get_mode(te),
8642 got_tree_entry_get_name(te));
8646 got_object_tree_close(tree);
8650 static const struct got_error *
8651 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8653 const struct got_error *err;
8654 struct got_commit_object *commit;
8655 const struct got_object_id_queue *parent_ids;
8656 struct got_object_qid *pid;
8657 char *id_str = NULL;
8658 const char *logmsg = NULL;
8661 err = got_object_open_as_commit(&commit, repo, id);
8665 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8669 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8670 parent_ids = got_object_commit_get_parent_ids(commit);
8671 fprintf(outfile, "numparents %d\n",
8672 got_object_commit_get_nparents(commit));
8673 STAILQ_FOREACH(pid, parent_ids, entry) {
8675 err = got_object_id_str(&pid_str, &pid->id);
8678 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8681 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8682 got_object_commit_get_author_gmtoff(commit));
8683 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
8684 got_object_commit_get_author(commit),
8685 (long long)got_object_commit_get_author_time(commit),
8688 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8689 got_object_commit_get_committer_gmtoff(commit));
8690 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
8691 got_object_commit_get_committer(commit),
8692 (long long)got_object_commit_get_committer_time(commit),
8695 logmsg = got_object_commit_get_logmsg_raw(commit);
8696 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8697 fprintf(outfile, "%s", logmsg);
8700 got_object_commit_close(commit);
8704 static const struct got_error *
8705 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8707 const struct got_error *err;
8708 struct got_tag_object *tag;
8709 char *id_str = NULL;
8710 const char *tagmsg = NULL;
8713 err = got_object_open_as_tag(&tag, repo, id);
8717 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8721 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8723 switch (got_object_tag_get_object_type(tag)) {
8724 case GOT_OBJ_TYPE_BLOB:
8725 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8726 GOT_OBJ_LABEL_BLOB);
8728 case GOT_OBJ_TYPE_TREE:
8729 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8730 GOT_OBJ_LABEL_TREE);
8732 case GOT_OBJ_TYPE_COMMIT:
8733 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8734 GOT_OBJ_LABEL_COMMIT);
8736 case GOT_OBJ_TYPE_TAG:
8737 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8744 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8745 got_object_tag_get_name(tag));
8747 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8748 got_object_tag_get_tagger_gmtoff(tag));
8749 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
8750 got_object_tag_get_tagger(tag),
8751 (long long)got_object_tag_get_tagger_time(tag),
8754 tagmsg = got_object_tag_get_message(tag);
8755 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8756 fprintf(outfile, "%s", tagmsg);
8759 got_object_tag_close(tag);
8763 static const struct got_error *
8764 cmd_cat(int argc, char *argv[])
8766 const struct got_error *error;
8767 struct got_repository *repo = NULL;
8768 struct got_worktree *worktree = NULL;
8769 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8770 const char *commit_id_str = NULL;
8771 struct got_object_id *id = NULL, *commit_id = NULL;
8772 struct got_commit_object *commit = NULL;
8773 int ch, obj_type, i, force_path = 0;
8774 struct got_reflist_head refs;
8775 int *pack_fds = NULL;
8780 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8785 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
8788 commit_id_str = optarg;
8794 repo_path = realpath(optarg, NULL);
8795 if (repo_path == NULL)
8796 return got_error_from_errno2("realpath",
8798 got_path_strip_trailing_slashes(repo_path);
8809 cwd = getcwd(NULL, 0);
8811 error = got_error_from_errno("getcwd");
8815 error = got_repo_pack_fds_open(&pack_fds);
8819 if (repo_path == NULL) {
8820 error = got_worktree_open(&worktree, cwd);
8821 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8825 got_worktree_get_repo_path(worktree));
8826 if (repo_path == NULL) {
8827 error = got_error_from_errno("strdup");
8831 /* Release work tree lock. */
8832 got_worktree_close(worktree);
8837 if (repo_path == NULL) {
8838 repo_path = strdup(cwd);
8839 if (repo_path == NULL)
8840 return got_error_from_errno("strdup");
8843 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8848 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8852 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8856 if (commit_id_str == NULL)
8857 commit_id_str = GOT_REF_HEAD;
8858 error = got_repo_match_object_id(&commit_id, NULL,
8859 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8863 error = got_object_open_as_commit(&commit, repo, commit_id);
8867 for (i = 0; i < argc; i++) {
8869 error = got_object_id_by_path(&id, repo, commit,
8874 error = got_repo_match_object_id(&id, &label, argv[i],
8875 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
8878 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8879 error->code != GOT_ERR_NOT_REF)
8881 error = got_object_id_by_path(&id, repo,
8888 error = got_object_get_type(&obj_type, repo, id);
8893 case GOT_OBJ_TYPE_BLOB:
8894 error = cat_blob(id, repo, stdout);
8896 case GOT_OBJ_TYPE_TREE:
8897 error = cat_tree(id, repo, stdout);
8899 case GOT_OBJ_TYPE_COMMIT:
8900 error = cat_commit(id, repo, stdout);
8902 case GOT_OBJ_TYPE_TAG:
8903 error = cat_tag(id, repo, stdout);
8906 error = got_error(GOT_ERR_OBJ_TYPE);
8921 got_object_commit_close(commit);
8923 got_worktree_close(worktree);
8925 const struct got_error *close_err = got_repo_close(repo);
8930 const struct got_error *pack_err =
8931 got_repo_pack_fds_close(pack_fds);
8936 got_ref_list_free(&refs);
8943 fprintf(stderr, "usage: %s info [path ...]\n",
8948 static const struct got_error *
8949 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
8950 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8951 struct got_object_id *commit_id)
8953 const struct got_error *err = NULL;
8954 char *id_str = NULL;
8956 struct tm mytm, *tm;
8957 struct got_pathlist_head *paths = arg;
8958 struct got_pathlist_entry *pe;
8961 * Clear error indication from any of the path arguments which
8962 * would cause this file index entry to be displayed.
8964 TAILQ_FOREACH(pe, paths, entry) {
8965 if (got_path_cmp(path, pe->path, strlen(path),
8966 pe->path_len) == 0 ||
8967 got_path_is_child(path, pe->path, pe->path_len))
8968 pe->data = NULL; /* no error */
8971 printf(GOT_COMMIT_SEP_STR);
8973 printf("symlink: %s\n", path);
8974 else if (S_ISREG(mode)) {
8975 printf("file: %s\n", path);
8976 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
8977 } else if (S_ISDIR(mode))
8978 printf("directory: %s\n", path);
8980 printf("something: %s\n", path);
8982 tm = localtime_r(&mtime, &mytm);
8985 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
8986 return got_error(GOT_ERR_NO_SPACE);
8987 printf("timestamp: %s\n", datebuf);
8990 err = got_object_id_str(&id_str, blob_id);
8993 printf("based on blob: %s\n", id_str);
8997 if (staged_blob_id) {
8998 err = got_object_id_str(&id_str, staged_blob_id);
9001 printf("based on staged blob: %s\n", id_str);
9006 err = got_object_id_str(&id_str, commit_id);
9009 printf("based on commit: %s\n", id_str);
9016 static const struct got_error *
9017 cmd_info(int argc, char *argv[])
9019 const struct got_error *error = NULL;
9020 struct got_worktree *worktree = NULL;
9021 char *cwd = NULL, *id_str = NULL;
9022 struct got_pathlist_head paths;
9023 char *uuidstr = NULL;
9024 int ch, show_files = 0;
9029 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9034 while ((ch = getopt(argc, argv, "")) != -1) {
9045 cwd = getcwd(NULL, 0);
9047 error = got_error_from_errno("getcwd");
9051 error = got_worktree_open(&worktree, cwd);
9053 if (error->code == GOT_ERR_NOT_WORKTREE)
9054 error = wrap_not_worktree_error(error, "info", cwd);
9059 /* Remove "wpath cpath proc exec sendfd" promises. */
9060 if (pledge("stdio rpath flock unveil", NULL) == -1)
9063 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9068 error = get_worktree_paths_from_argv(&paths, argc, argv,
9075 error = got_object_id_str(&id_str,
9076 got_worktree_get_base_commit_id(worktree));
9080 error = got_worktree_get_uuid(&uuidstr, worktree);
9084 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9085 printf("work tree base commit: %s\n", id_str);
9086 printf("work tree path prefix: %s\n",
9087 got_worktree_get_path_prefix(worktree));
9088 printf("work tree branch reference: %s\n",
9089 got_worktree_get_head_ref_name(worktree));
9090 printf("work tree UUID: %s\n", uuidstr);
9091 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9094 struct got_pathlist_entry *pe;
9095 TAILQ_FOREACH(pe, &paths, entry) {
9096 if (pe->path_len == 0)
9099 * Assume this path will fail. This will be corrected
9100 * in print_path_info() in case the path does suceeed.
9102 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
9104 error = got_worktree_path_info(worktree, &paths,
9105 print_path_info, &paths, check_cancelled, NULL);
9108 TAILQ_FOREACH(pe, &paths, entry) {
9109 if (pe->data != NULL) {
9110 const struct got_error *perr;
9113 error = got_error_fmt(perr->code, "%s",
9121 got_worktree_close(worktree);
9122 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);