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>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
42 #include "got_compat.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"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(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_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
394 const struct got_error *err = NULL;
401 if (fseeko(fp, 0L, SEEK_SET) == -1)
402 return got_error_from_errno("fseeko");
404 *logmsg = malloc(filesize + 1);
406 return got_error_from_errno("malloc");
409 while (getline(&line, &linesize, fp) != -1) {
410 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
411 continue; /* remove comments and leading empty lines */
412 *len = strlcat(*logmsg, line, filesize + 1);
413 if (*len >= filesize + 1) {
414 err = got_error(GOT_ERR_NO_SPACE);
419 err = got_ferror(fp, GOT_ERR_IO);
423 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
424 (*logmsg)[*len - 1] = '\0';
437 static const struct got_error *
438 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
439 const char *initial_content, size_t initial_content_len,
440 int require_modification)
442 const struct got_error *err = NULL;
449 if (stat(logmsg_path, &st) == -1)
450 return got_error_from_errno2("stat", logmsg_path);
452 if (spawn_editor(editor, logmsg_path) == -1)
453 return got_error_from_errno("failed spawning editor");
455 if (require_modification) {
456 struct timespec timeout;
460 nanosleep(&timeout, NULL);
463 if (stat(logmsg_path, &st2) == -1)
464 return got_error_from_errno("stat");
466 if (require_modification && st.st_size == st2.st_size &&
467 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
468 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "no changes made to commit message, aborting");
471 fp = fopen(logmsg_path, "re");
473 err = got_error_from_errno("fopen");
477 /* strip comments and leading/trailing newlines */
478 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
481 if (logmsg_len == 0) {
482 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
483 "commit message cannot be empty, aborting");
487 if (fp && fclose(fp) == EOF && err == NULL)
488 err = got_error_from_errno("fclose");
496 static const struct got_error *
497 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
498 const char *path_dir, const char *branch_name)
500 char *initial_content = NULL;
501 const struct got_error *err = NULL;
502 int initial_content_len;
505 initial_content_len = asprintf(&initial_content,
506 "\n# %s to be imported to branch %s\n", path_dir,
508 if (initial_content_len == -1)
509 return got_error_from_errno("asprintf");
511 err = got_opentemp_named_fd(logmsg_path, &fd,
512 GOT_TMPDIR_STR "/got-importmsg", "");
516 if (write(fd, initial_content, initial_content_len) == -1) {
517 err = got_error_from_errno2("write", *logmsg_path);
521 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
522 initial_content_len, 1);
524 if (fd != -1 && close(fd) == -1 && err == NULL)
525 err = got_error_from_errno2("close", *logmsg_path);
526 free(initial_content);
534 static const struct got_error *
535 import_progress(void *arg, const char *path)
537 printf("A %s\n", path);
541 static const struct got_error *
542 valid_author(const char *author)
544 const char *email = author;
547 * Git' expects the author (or committer) to be in the form
548 * "name <email>", which are mostly free form (see the
549 * "committer" description in git-fast-import(1)). We're only
550 * doing this to avoid git's object parser breaking on commits
554 while (*author && *author != '\n' && *author != '<' && *author != '>')
556 if (author != email && *author == '<' && *(author - 1) != ' ')
557 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
558 "between author name and email required", email);
559 if (*author++ != '<')
560 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
561 while (*author && *author != '\n' && *author != '<' && *author != '>')
563 if (strcmp(author, ">") != 0)
564 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 static const struct got_error *
569 get_author(char **author, struct got_repository *repo,
570 struct got_worktree *worktree)
572 const struct got_error *err = NULL;
573 const char *got_author = NULL, *name, *email;
574 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
579 worktree_conf = got_worktree_get_gotconfig(worktree);
580 repo_conf = got_repo_get_gotconfig(repo);
583 * Priority of potential author information sources, from most
584 * significant to least significant:
585 * 1) work tree's .got/got.conf file
586 * 2) repository's got.conf file
587 * 3) repository's git config file
588 * 4) environment variables
589 * 5) global git config files (in user's home directory or /etc)
593 got_author = got_gotconfig_get_author(worktree_conf);
594 if (got_author == NULL)
595 got_author = got_gotconfig_get_author(repo_conf);
596 if (got_author == NULL) {
597 name = got_repo_get_gitconfig_author_name(repo);
598 email = got_repo_get_gitconfig_author_email(repo);
600 if (asprintf(author, "%s <%s>", name, email) == -1)
601 return got_error_from_errno("asprintf");
605 got_author = getenv("GOT_AUTHOR");
606 if (got_author == NULL) {
607 name = got_repo_get_global_gitconfig_author_name(repo);
608 email = got_repo_get_global_gitconfig_author_email(
611 if (asprintf(author, "%s <%s>", name, email)
613 return got_error_from_errno("asprintf");
616 /* TODO: Look up user in password database? */
617 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
621 *author = strdup(got_author);
623 return got_error_from_errno("strdup");
625 err = valid_author(*author);
633 static const struct got_error *
634 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
635 struct got_worktree *worktree)
637 const char *got_allowed_signers = NULL;
638 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
640 *allowed_signers = NULL;
643 worktree_conf = got_worktree_get_gotconfig(worktree);
644 repo_conf = got_repo_get_gotconfig(repo);
647 * Priority of potential author information sources, from most
648 * significant to least significant:
649 * 1) work tree's .got/got.conf file
650 * 2) repository's got.conf file
654 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
656 if (got_allowed_signers == NULL)
657 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
660 if (got_allowed_signers) {
661 *allowed_signers = strdup(got_allowed_signers);
662 if (*allowed_signers == NULL)
663 return got_error_from_errno("strdup");
668 static const struct got_error *
669 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
670 struct got_worktree *worktree)
672 const char *got_revoked_signers = NULL;
673 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
675 *revoked_signers = NULL;
678 worktree_conf = got_worktree_get_gotconfig(worktree);
679 repo_conf = got_repo_get_gotconfig(repo);
682 * Priority of potential author information sources, from most
683 * significant to least significant:
684 * 1) work tree's .got/got.conf file
685 * 2) repository's got.conf file
689 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
691 if (got_revoked_signers == NULL)
692 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
695 if (got_revoked_signers) {
696 *revoked_signers = strdup(got_revoked_signers);
697 if (*revoked_signers == NULL)
698 return got_error_from_errno("strdup");
704 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
706 const char *got_signer_id = NULL;
707 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
710 worktree_conf = got_worktree_get_gotconfig(worktree);
711 repo_conf = got_repo_get_gotconfig(repo);
714 * Priority of potential author information sources, from most
715 * significant to least significant:
716 * 1) work tree's .got/got.conf file
717 * 2) repository's got.conf file
721 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
722 if (got_signer_id == NULL)
723 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
725 return got_signer_id;
728 static const struct got_error *
729 get_gitconfig_path(char **gitconfig_path)
731 const char *homedir = getenv("HOME");
733 *gitconfig_path = NULL;
735 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
736 return got_error_from_errno("asprintf");
742 static const struct got_error *
743 cmd_import(int argc, char *argv[])
745 const struct got_error *error = NULL;
746 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
747 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
748 const char *branch_name = NULL;
749 char *id_str = NULL, *logmsg_path = NULL;
750 char refname[PATH_MAX] = "refs/heads/";
751 struct got_repository *repo = NULL;
752 struct got_reference *branch_ref = NULL, *head_ref = NULL;
753 struct got_object_id *new_commit_id = NULL;
755 struct got_pathlist_head ignores;
756 struct got_pathlist_entry *pe;
757 int preserve_logmsg = 0;
758 int *pack_fds = NULL;
760 TAILQ_INIT(&ignores);
763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
769 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
772 branch_name = optarg;
775 if (optarg[0] == '\0')
777 error = got_pathlist_insert(&pe, &ignores, optarg,
783 logmsg = strdup(optarg);
784 if (logmsg == NULL) {
785 error = got_error_from_errno("strdup");
790 repo_path = realpath(optarg, NULL);
791 if (repo_path == NULL) {
792 error = got_error_from_errno2("realpath",
809 if (repo_path == NULL) {
810 repo_path = getcwd(NULL, 0);
811 if (repo_path == NULL)
812 return got_error_from_errno("getcwd");
814 got_path_strip_trailing_slashes(repo_path);
815 error = get_gitconfig_path(&gitconfig_path);
818 error = got_repo_pack_fds_open(&pack_fds);
821 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
825 error = get_author(&author, repo, NULL);
830 * Don't let the user create a branch name with a leading '-'.
831 * While technically a valid reference name, this case is usually
832 * an unintended typo.
834 if (branch_name && branch_name[0] == '-')
835 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
837 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
838 if (error && error->code != GOT_ERR_NOT_REF)
842 n = strlcat(refname, branch_name, sizeof(refname));
843 else if (head_ref && got_ref_is_symbolic(head_ref))
844 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
847 n = strlcat(refname, "main", sizeof(refname));
848 if (n >= sizeof(refname)) {
849 error = got_error(GOT_ERR_NO_SPACE);
853 error = got_ref_open(&branch_ref, repo, refname, 0);
855 if (error->code != GOT_ERR_NOT_REF)
858 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
859 "import target branch already exists");
863 path_dir = realpath(argv[0], NULL);
864 if (path_dir == NULL) {
865 error = got_error_from_errno2("realpath", argv[0]);
868 got_path_strip_trailing_slashes(path_dir);
871 * unveil(2) traverses exec(2); if an editor is used we have
872 * to apply unveil after the log message has been written.
874 if (logmsg == NULL || strlen(logmsg) == 0) {
875 error = get_editor(&editor);
879 error = collect_import_msg(&logmsg, &logmsg_path, editor,
882 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
889 if (unveil(path_dir, "r") != 0) {
890 error = got_error_from_errno2("unveil", path_dir);
896 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
903 error = got_repo_import(&new_commit_id, path_dir, logmsg,
904 author, &ignores, repo, import_progress, NULL);
911 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
918 error = got_ref_write(branch_ref, repo);
925 error = got_object_id_str(&id_str, new_commit_id);
932 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
934 if (error->code != GOT_ERR_NOT_REF) {
940 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
948 error = got_ref_write(head_ref, repo);
956 printf("Created branch %s with commit %s\n",
957 got_ref_get_name(branch_ref), id_str);
960 const struct got_error *pack_err =
961 got_repo_pack_fds_close(pack_fds);
965 if (preserve_logmsg) {
966 fprintf(stderr, "%s: log message preserved in %s\n",
967 getprogname(), logmsg_path);
968 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
969 error = got_error_from_errno2("unlink", logmsg_path);
977 free(gitconfig_path);
979 got_ref_close(branch_ref);
981 got_ref_close(head_ref);
988 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
989 "repository-URL [directory]\n", getprogname());
993 struct got_fetch_progress_arg {
994 char last_scaled_size[FMT_SCALED_STRSIZE];
999 struct got_repository *repo;
1002 int configs_created;
1004 struct got_pathlist_head *symrefs;
1005 struct got_pathlist_head *wanted_branches;
1006 struct got_pathlist_head *wanted_refs;
1010 const char *remote_repo_path;
1011 const char *git_url;
1012 int fetch_all_branches;
1013 int mirror_references;
1017 /* XXX forward declaration */
1018 static const struct got_error *
1019 create_config_files(const char *proto, const char *host, const char *port,
1020 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1021 int mirror_references, struct got_pathlist_head *symrefs,
1022 struct got_pathlist_head *wanted_branches,
1023 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1025 static const struct got_error *
1026 fetch_progress(void *arg, const char *message, off_t packfile_size,
1027 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1029 const struct got_error *err = NULL;
1030 struct got_fetch_progress_arg *a = arg;
1031 char scaled_size[FMT_SCALED_STRSIZE];
1032 int p_indexed, p_resolved;
1033 int print_size = 0, print_indexed = 0, print_resolved = 0;
1036 * In order to allow a failed clone to be resumed with 'got fetch'
1037 * we try to create configuration files as soon as possible.
1038 * Once the server has sent information about its default branch
1039 * we have all required information.
1041 if (a->create_configs && !a->configs_created &&
1042 !TAILQ_EMPTY(a->config_info.symrefs)) {
1043 err = create_config_files(a->config_info.proto,
1044 a->config_info.host, a->config_info.port,
1045 a->config_info.remote_repo_path,
1046 a->config_info.git_url,
1047 a->config_info.fetch_all_branches,
1048 a->config_info.mirror_references,
1049 a->config_info.symrefs,
1050 a->config_info.wanted_branches,
1051 a->config_info.wanted_refs, a->repo);
1054 a->configs_created = 1;
1057 if (a->verbosity < 0)
1060 if (message && message[0] != '\0') {
1061 printf("\rserver: %s", message);
1066 if (packfile_size > 0 || nobj_indexed > 0) {
1067 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1068 (a->last_scaled_size[0] == '\0' ||
1069 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1071 if (strlcpy(a->last_scaled_size, scaled_size,
1072 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1073 return got_error(GOT_ERR_NO_SPACE);
1075 if (nobj_indexed > 0) {
1076 p_indexed = (nobj_indexed * 100) / nobj_total;
1077 if (p_indexed != a->last_p_indexed) {
1078 a->last_p_indexed = p_indexed;
1083 if (nobj_resolved > 0) {
1084 p_resolved = (nobj_resolved * 100) /
1085 (nobj_total - nobj_loose);
1086 if (p_resolved != a->last_p_resolved) {
1087 a->last_p_resolved = p_resolved;
1095 if (print_size || print_indexed || print_resolved)
1098 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1100 printf("; indexing %d%%", p_indexed);
1102 printf("; resolving deltas %d%%", p_resolved);
1103 if (print_size || print_indexed || print_resolved)
1109 static const struct got_error *
1110 create_symref(const char *refname, struct got_reference *target_ref,
1111 int verbosity, struct got_repository *repo)
1113 const struct got_error *err;
1114 struct got_reference *head_symref;
1116 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1120 err = got_ref_write(head_symref, repo);
1121 if (err == NULL && verbosity > 0) {
1122 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1123 got_ref_get_name(target_ref));
1125 got_ref_close(head_symref);
1129 static const struct got_error *
1130 list_remote_refs(struct got_pathlist_head *symrefs,
1131 struct got_pathlist_head *refs)
1133 const struct got_error *err;
1134 struct got_pathlist_entry *pe;
1136 TAILQ_FOREACH(pe, symrefs, entry) {
1137 const char *refname = pe->path;
1138 const char *targetref = pe->data;
1140 printf("%s: %s\n", refname, targetref);
1143 TAILQ_FOREACH(pe, refs, entry) {
1144 const char *refname = pe->path;
1145 struct got_object_id *id = pe->data;
1148 err = got_object_id_str(&id_str, id);
1151 printf("%s: %s\n", refname, id_str);
1158 static const struct got_error *
1159 create_ref(const char *refname, struct got_object_id *id,
1160 int verbosity, struct got_repository *repo)
1162 const struct got_error *err = NULL;
1163 struct got_reference *ref;
1166 err = got_object_id_str(&id_str, id);
1170 err = got_ref_alloc(&ref, refname, id);
1174 err = got_ref_write(ref, repo);
1177 if (err == NULL && verbosity >= 0)
1178 printf("Created reference %s: %s\n", refname, id_str);
1185 match_wanted_ref(const char *refname, const char *wanted_ref)
1187 if (strncmp(refname, "refs/", 5) != 0)
1192 * Prevent fetching of references that won't make any
1193 * sense outside of the remote repository's context.
1195 if (strncmp(refname, "got/", 4) == 0)
1197 if (strncmp(refname, "remotes/", 8) == 0)
1200 if (strncmp(wanted_ref, "refs/", 5) == 0)
1203 /* Allow prefix match. */
1204 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1207 /* Allow exact match. */
1208 return (strcmp(refname, wanted_ref) == 0);
1212 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1214 struct got_pathlist_entry *pe;
1216 TAILQ_FOREACH(pe, wanted_refs, entry) {
1217 if (match_wanted_ref(refname, pe->path))
1224 static const struct got_error *
1225 create_wanted_ref(const char *refname, struct got_object_id *id,
1226 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1228 const struct got_error *err;
1229 char *remote_refname;
1231 if (strncmp("refs/", refname, 5) == 0)
1234 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1235 remote_repo_name, refname) == -1)
1236 return got_error_from_errno("asprintf");
1238 err = create_ref(remote_refname, id, verbosity, repo);
1239 free(remote_refname);
1243 static const struct got_error *
1244 create_gotconfig(const char *proto, const char *host, const char *port,
1245 const char *remote_repo_path, const char *default_branch,
1246 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1247 struct got_pathlist_head *wanted_refs, int mirror_references,
1248 struct got_repository *repo)
1250 const struct got_error *err = NULL;
1251 char *gotconfig_path = NULL;
1252 char *gotconfig = NULL;
1253 FILE *gotconfig_file = NULL;
1254 const char *branchname = NULL;
1255 char *branches = NULL, *refs = NULL;
1258 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1259 struct got_pathlist_entry *pe;
1260 TAILQ_FOREACH(pe, wanted_branches, entry) {
1262 branchname = pe->path;
1263 if (strncmp(branchname, "refs/heads/", 11) == 0)
1265 if (asprintf(&s, "%s\"%s\" ",
1266 branches ? branches : "", branchname) == -1) {
1267 err = got_error_from_errno("asprintf");
1273 } else if (!fetch_all_branches && default_branch) {
1274 branchname = default_branch;
1275 if (strncmp(branchname, "refs/heads/", 11) == 0)
1277 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1278 err = got_error_from_errno("asprintf");
1282 if (!TAILQ_EMPTY(wanted_refs)) {
1283 struct got_pathlist_entry *pe;
1284 TAILQ_FOREACH(pe, wanted_refs, entry) {
1286 const char *refname = pe->path;
1287 if (strncmp(refname, "refs/", 5) == 0)
1289 if (asprintf(&s, "%s\"%s\" ",
1290 refs ? refs : "", refname) == -1) {
1291 err = got_error_from_errno("asprintf");
1299 /* Create got.conf(5). */
1300 gotconfig_path = got_repo_get_path_gotconfig(repo);
1301 if (gotconfig_path == NULL) {
1302 err = got_error_from_errno("got_repo_get_path_gotconfig");
1305 gotconfig_file = fopen(gotconfig_path, "ae");
1306 if (gotconfig_file == NULL) {
1307 err = got_error_from_errno2("fopen", gotconfig_path);
1310 if (asprintf(&gotconfig,
1315 "\trepository \"%s\"\n"
1321 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1322 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1323 remote_repo_path, branches ? "\tbranch { " : "",
1324 branches ? branches : "", branches ? "}\n" : "",
1325 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1326 mirror_references ? "\tmirror_references yes\n" : "",
1327 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1328 err = got_error_from_errno("asprintf");
1331 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1332 if (n != strlen(gotconfig)) {
1333 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1338 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1339 err = got_error_from_errno2("fclose", gotconfig_path);
1340 free(gotconfig_path);
1345 static const struct got_error *
1346 create_gitconfig(const char *git_url, const char *default_branch,
1347 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1348 struct got_pathlist_head *wanted_refs, int mirror_references,
1349 struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 char *gitconfig_path = NULL;
1353 char *gitconfig = NULL;
1354 FILE *gitconfig_file = NULL;
1355 char *branches = NULL, *refs = NULL;
1356 const char *branchname;
1359 /* Create a config file Git can understand. */
1360 gitconfig_path = got_repo_get_path_gitconfig(repo);
1361 if (gitconfig_path == NULL) {
1362 err = got_error_from_errno("got_repo_get_path_gitconfig");
1365 gitconfig_file = fopen(gitconfig_path, "ae");
1366 if (gitconfig_file == NULL) {
1367 err = got_error_from_errno2("fopen", gitconfig_path);
1370 if (fetch_all_branches) {
1371 if (mirror_references) {
1372 if (asprintf(&branches,
1373 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1374 err = got_error_from_errno("asprintf");
1377 } else if (asprintf(&branches,
1378 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1379 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1380 err = got_error_from_errno("asprintf");
1383 } else if (!TAILQ_EMPTY(wanted_branches)) {
1384 struct got_pathlist_entry *pe;
1385 TAILQ_FOREACH(pe, wanted_branches, entry) {
1387 branchname = pe->path;
1388 if (strncmp(branchname, "refs/heads/", 11) == 0)
1390 if (mirror_references) {
1392 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1393 branches ? branches : "",
1394 branchname, branchname) == -1) {
1395 err = got_error_from_errno("asprintf");
1398 } else if (asprintf(&s,
1399 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1400 branches ? branches : "",
1401 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1402 branchname) == -1) {
1403 err = got_error_from_errno("asprintf");
1411 * If the server specified a default branch, use just that one.
1412 * Otherwise fall back to fetching all branches on next fetch.
1414 if (default_branch) {
1415 branchname = default_branch;
1416 if (strncmp(branchname, "refs/heads/", 11) == 0)
1419 branchname = "*"; /* fall back to all branches */
1420 if (mirror_references) {
1421 if (asprintf(&branches,
1422 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1423 branchname, branchname) == -1) {
1424 err = got_error_from_errno("asprintf");
1427 } else if (asprintf(&branches,
1428 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1429 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1430 branchname) == -1) {
1431 err = got_error_from_errno("asprintf");
1435 if (!TAILQ_EMPTY(wanted_refs)) {
1436 struct got_pathlist_entry *pe;
1437 TAILQ_FOREACH(pe, wanted_refs, entry) {
1439 const char *refname = pe->path;
1440 if (strncmp(refname, "refs/", 5) == 0)
1442 if (mirror_references) {
1444 "%s\tfetch = refs/%s:refs/%s\n",
1445 refs ? refs : "", refname, refname) == -1) {
1446 err = got_error_from_errno("asprintf");
1449 } else if (asprintf(&s,
1450 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1452 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1454 err = got_error_from_errno("asprintf");
1462 if (asprintf(&gitconfig,
1467 "\tfetch = refs/tags/*:refs/tags/*\n",
1468 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1469 refs ? refs : "") == -1) {
1470 err = got_error_from_errno("asprintf");
1473 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1474 if (n != strlen(gitconfig)) {
1475 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1479 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1480 err = got_error_from_errno2("fclose", gitconfig_path);
1481 free(gitconfig_path);
1486 static const struct got_error *
1487 create_config_files(const char *proto, const char *host, const char *port,
1488 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1489 int mirror_references, struct got_pathlist_head *symrefs,
1490 struct got_pathlist_head *wanted_branches,
1491 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 const char *default_branch = NULL;
1495 struct got_pathlist_entry *pe;
1498 * If we asked for a set of wanted branches then use the first
1501 if (!TAILQ_EMPTY(wanted_branches)) {
1502 pe = TAILQ_FIRST(wanted_branches);
1503 default_branch = pe->path;
1505 /* First HEAD ref listed by server is the default branch. */
1506 TAILQ_FOREACH(pe, symrefs, entry) {
1507 const char *refname = pe->path;
1508 const char *target = pe->data;
1510 if (strcmp(refname, GOT_REF_HEAD) != 0)
1513 default_branch = target;
1518 /* Create got.conf(5). */
1519 err = create_gotconfig(proto, host, port, remote_repo_path,
1520 default_branch, fetch_all_branches, wanted_branches,
1521 wanted_refs, mirror_references, repo);
1525 /* Create a config file Git can understand. */
1526 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1527 wanted_branches, wanted_refs, mirror_references, repo);
1530 static const struct got_error *
1531 cmd_clone(int argc, char *argv[])
1533 const struct got_error *error = NULL;
1534 const char *uri, *dirname;
1535 char *proto, *host, *port, *repo_name, *server_path;
1536 char *default_destdir = NULL, *id_str = NULL;
1537 const char *repo_path;
1538 struct got_repository *repo = NULL;
1539 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1540 struct got_pathlist_entry *pe;
1541 struct got_object_id *pack_hash = NULL;
1542 int ch, fetchfd = -1, fetchstatus;
1543 pid_t fetchpid = -1;
1544 struct got_fetch_progress_arg fpa;
1545 char *git_url = NULL;
1546 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1547 int list_refs_only = 0;
1548 int *pack_fds = NULL;
1551 TAILQ_INIT(&symrefs);
1552 TAILQ_INIT(&wanted_branches);
1553 TAILQ_INIT(&wanted_refs);
1555 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1558 fetch_all_branches = 1;
1561 error = got_pathlist_append(&wanted_branches,
1570 mirror_references = 1;
1576 error = got_pathlist_append(&wanted_refs,
1584 else if (verbosity < 3)
1595 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1596 option_conflict('a', 'b');
1597 if (list_refs_only) {
1598 if (!TAILQ_EMPTY(&wanted_branches))
1599 option_conflict('l', 'b');
1600 if (fetch_all_branches)
1601 option_conflict('l', 'a');
1602 if (mirror_references)
1603 option_conflict('l', 'm');
1604 if (!TAILQ_EMPTY(&wanted_refs))
1605 option_conflict('l', 'R');
1617 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1622 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1623 host, port ? ":" : "", port ? port : "",
1624 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1625 error = got_error_from_errno("asprintf");
1629 if (strcmp(proto, "git") == 0) {
1631 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1632 "sendfd dns inet unveil", NULL) == -1)
1635 } else if (strcmp(proto, "git+ssh") == 0 ||
1636 strcmp(proto, "ssh") == 0) {
1638 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1639 "sendfd unveil", NULL) == -1)
1642 } else if (strcmp(proto, "http") == 0 ||
1643 strcmp(proto, "git+http") == 0) {
1644 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1647 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1650 if (dirname == NULL) {
1651 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1652 error = got_error_from_errno("asprintf");
1655 repo_path = default_destdir;
1657 repo_path = dirname;
1659 if (!list_refs_only) {
1660 error = got_path_mkdir(repo_path);
1662 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1663 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1665 if (!got_path_dir_is_empty(repo_path)) {
1666 error = got_error_path(repo_path,
1667 GOT_ERR_DIR_NOT_EMPTY);
1672 error = got_dial_apply_unveil(proto);
1676 error = apply_unveil(repo_path, 0, NULL);
1681 printf("Connecting to %s\n", git_url);
1683 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1684 server_path, verbosity);
1688 if (!list_refs_only) {
1689 error = got_repo_init(repo_path, NULL);
1692 error = got_repo_pack_fds_open(&pack_fds);
1695 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1700 fpa.last_scaled_size[0] = '\0';
1701 fpa.last_p_indexed = -1;
1702 fpa.last_p_resolved = -1;
1703 fpa.verbosity = verbosity;
1704 fpa.create_configs = 1;
1705 fpa.configs_created = 0;
1707 fpa.config_info.symrefs = &symrefs;
1708 fpa.config_info.wanted_branches = &wanted_branches;
1709 fpa.config_info.wanted_refs = &wanted_refs;
1710 fpa.config_info.proto = proto;
1711 fpa.config_info.host = host;
1712 fpa.config_info.port = port;
1713 fpa.config_info.remote_repo_path = server_path;
1714 fpa.config_info.git_url = git_url;
1715 fpa.config_info.fetch_all_branches = fetch_all_branches;
1716 fpa.config_info.mirror_references = mirror_references;
1717 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1718 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1719 fetch_all_branches, &wanted_branches, &wanted_refs,
1720 list_refs_only, verbosity, fetchfd, repo, NULL, 0,
1721 fetch_progress, &fpa);
1725 if (list_refs_only) {
1726 error = list_remote_refs(&symrefs, &refs);
1730 if (pack_hash == NULL) {
1731 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1732 "server sent an empty pack file");
1735 error = got_object_id_str(&id_str, pack_hash);
1739 printf("\nFetched %s.pack\n", id_str);
1742 /* Set up references provided with the pack file. */
1743 TAILQ_FOREACH(pe, &refs, entry) {
1744 const char *refname = pe->path;
1745 struct got_object_id *id = pe->data;
1746 char *remote_refname;
1748 if (is_wanted_ref(&wanted_refs, refname) &&
1749 !mirror_references) {
1750 error = create_wanted_ref(refname, id,
1751 GOT_FETCH_DEFAULT_REMOTE_NAME,
1752 verbosity - 1, repo);
1758 error = create_ref(refname, id, verbosity - 1, repo);
1762 if (mirror_references)
1765 if (strncmp("refs/heads/", refname, 11) != 0)
1768 if (asprintf(&remote_refname,
1769 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1770 refname + 11) == -1) {
1771 error = got_error_from_errno("asprintf");
1774 error = create_ref(remote_refname, id, verbosity - 1, repo);
1775 free(remote_refname);
1780 /* Set the HEAD reference if the server provided one. */
1781 TAILQ_FOREACH(pe, &symrefs, entry) {
1782 struct got_reference *target_ref;
1783 const char *refname = pe->path;
1784 const char *target = pe->data;
1785 char *remote_refname = NULL, *remote_target = NULL;
1787 if (strcmp(refname, GOT_REF_HEAD) != 0)
1790 error = got_ref_open(&target_ref, repo, target, 0);
1792 if (error->code == GOT_ERR_NOT_REF) {
1799 error = create_symref(refname, target_ref, verbosity, repo);
1800 got_ref_close(target_ref);
1804 if (mirror_references)
1807 if (strncmp("refs/heads/", target, 11) != 0)
1810 if (asprintf(&remote_refname,
1811 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1813 error = got_error_from_errno("asprintf");
1816 if (asprintf(&remote_target,
1817 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1818 target + 11) == -1) {
1819 error = got_error_from_errno("asprintf");
1820 free(remote_refname);
1823 error = got_ref_open(&target_ref, repo, remote_target, 0);
1825 free(remote_refname);
1826 free(remote_target);
1827 if (error->code == GOT_ERR_NOT_REF) {
1833 error = create_symref(remote_refname, target_ref,
1834 verbosity - 1, repo);
1835 free(remote_refname);
1836 free(remote_target);
1837 got_ref_close(target_ref);
1843 * We failed to set the HEAD reference. If we asked for
1844 * a set of wanted branches use the first of one of those
1845 * which could be fetched instead.
1847 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1848 const char *target = pe->path;
1849 struct got_reference *target_ref;
1851 error = got_ref_open(&target_ref, repo, target, 0);
1853 if (error->code == GOT_ERR_NOT_REF) {
1860 error = create_symref(GOT_REF_HEAD, target_ref,
1862 got_ref_close(target_ref);
1868 if (!fpa.configs_created && pe != NULL) {
1869 error = create_config_files(fpa.config_info.proto,
1870 fpa.config_info.host, fpa.config_info.port,
1871 fpa.config_info.remote_repo_path,
1872 fpa.config_info.git_url,
1873 fpa.config_info.fetch_all_branches,
1874 fpa.config_info.mirror_references,
1875 fpa.config_info.symrefs,
1876 fpa.config_info.wanted_branches,
1877 fpa.config_info.wanted_refs, fpa.repo);
1884 printf("Created %s repository '%s'\n",
1885 mirror_references ? "mirrored" : "cloned", repo_path);
1888 const struct got_error *pack_err =
1889 got_repo_pack_fds_close(pack_fds);
1894 if (kill(fetchpid, SIGTERM) == -1)
1895 error = got_error_from_errno("kill");
1896 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1897 error = got_error_from_errno("waitpid");
1899 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1900 error = got_error_from_errno("close");
1902 const struct got_error *close_err = got_repo_close(repo);
1906 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1907 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1908 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1909 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1916 free(default_destdir);
1921 static const struct got_error *
1922 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1923 int replace_tags, int verbosity, struct got_repository *repo)
1925 const struct got_error *err = NULL;
1926 char *new_id_str = NULL;
1927 struct got_object_id *old_id = NULL;
1929 err = got_object_id_str(&new_id_str, new_id);
1933 if (!replace_tags &&
1934 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1935 err = got_ref_resolve(&old_id, repo, ref);
1938 if (got_object_id_cmp(old_id, new_id) == 0)
1940 if (verbosity >= 0) {
1941 printf("Rejecting update of existing tag %s: %s\n",
1942 got_ref_get_name(ref), new_id_str);
1947 if (got_ref_is_symbolic(ref)) {
1948 if (verbosity >= 0) {
1949 printf("Replacing reference %s: %s\n",
1950 got_ref_get_name(ref),
1951 got_ref_get_symref_target(ref));
1953 err = got_ref_change_symref_to_ref(ref, new_id);
1956 err = got_ref_write(ref, repo);
1960 err = got_ref_resolve(&old_id, repo, ref);
1963 if (got_object_id_cmp(old_id, new_id) == 0)
1966 err = got_ref_change_ref(ref, new_id);
1969 err = got_ref_write(ref, repo);
1975 printf("Updated %s: %s\n", got_ref_get_name(ref),
1983 static const struct got_error *
1984 update_symref(const char *refname, struct got_reference *target_ref,
1985 int verbosity, struct got_repository *repo)
1987 const struct got_error *err = NULL, *unlock_err;
1988 struct got_reference *symref;
1989 int symref_is_locked = 0;
1991 err = got_ref_open(&symref, repo, refname, 1);
1993 if (err->code != GOT_ERR_NOT_REF)
1995 err = got_ref_alloc_symref(&symref, refname, target_ref);
1999 err = got_ref_write(symref, repo);
2004 printf("Created reference %s: %s\n",
2005 got_ref_get_name(symref),
2006 got_ref_get_symref_target(symref));
2008 symref_is_locked = 1;
2010 if (strcmp(got_ref_get_symref_target(symref),
2011 got_ref_get_name(target_ref)) == 0)
2014 err = got_ref_change_symref(symref,
2015 got_ref_get_name(target_ref));
2019 err = got_ref_write(symref, repo);
2024 printf("Updated %s: %s\n", got_ref_get_name(symref),
2025 got_ref_get_symref_target(symref));
2029 if (symref_is_locked) {
2030 unlock_err = got_ref_unlock(symref);
2031 if (unlock_err && err == NULL)
2034 got_ref_close(symref);
2041 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2042 "[-R reference] [-r repository-path] [remote-repository]\n",
2047 static const struct got_error *
2048 delete_missing_ref(struct got_reference *ref,
2049 int verbosity, struct got_repository *repo)
2051 const struct got_error *err = NULL;
2052 struct got_object_id *id = NULL;
2053 char *id_str = NULL;
2055 if (got_ref_is_symbolic(ref)) {
2056 err = got_ref_delete(ref, repo);
2059 if (verbosity >= 0) {
2060 printf("Deleted %s: %s\n",
2061 got_ref_get_name(ref),
2062 got_ref_get_symref_target(ref));
2065 err = got_ref_resolve(&id, repo, ref);
2068 err = got_object_id_str(&id_str, id);
2072 err = got_ref_delete(ref, repo);
2075 if (verbosity >= 0) {
2076 printf("Deleted %s: %s\n",
2077 got_ref_get_name(ref), id_str);
2086 static const struct got_error *
2087 delete_missing_refs(struct got_pathlist_head *their_refs,
2088 struct got_pathlist_head *their_symrefs,
2089 const struct got_remote_repo *remote,
2090 int verbosity, struct got_repository *repo)
2092 const struct got_error *err = NULL, *unlock_err;
2093 struct got_reflist_head my_refs;
2094 struct got_reflist_entry *re;
2095 struct got_pathlist_entry *pe;
2096 char *remote_namespace = NULL;
2097 char *local_refname = NULL;
2099 TAILQ_INIT(&my_refs);
2101 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2103 return got_error_from_errno("asprintf");
2105 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2109 TAILQ_FOREACH(re, &my_refs, entry) {
2110 const char *refname = got_ref_get_name(re->ref);
2111 const char *their_refname;
2113 if (remote->mirror_references) {
2114 their_refname = refname;
2116 if (strncmp(refname, remote_namespace,
2117 strlen(remote_namespace)) == 0) {
2118 if (strcmp(refname + strlen(remote_namespace),
2121 if (asprintf(&local_refname, "refs/heads/%s",
2122 refname + strlen(remote_namespace)) == -1) {
2123 err = got_error_from_errno("asprintf");
2126 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2129 their_refname = local_refname;
2132 TAILQ_FOREACH(pe, their_refs, entry) {
2133 if (strcmp(their_refname, pe->path) == 0)
2139 TAILQ_FOREACH(pe, their_symrefs, entry) {
2140 if (strcmp(their_refname, pe->path) == 0)
2146 err = delete_missing_ref(re->ref, verbosity, repo);
2150 if (local_refname) {
2151 struct got_reference *ref;
2152 err = got_ref_open(&ref, repo, local_refname, 1);
2154 if (err->code != GOT_ERR_NOT_REF)
2156 free(local_refname);
2157 local_refname = NULL;
2160 err = delete_missing_ref(ref, verbosity, repo);
2163 unlock_err = got_ref_unlock(ref);
2165 if (unlock_err && err == NULL) {
2170 free(local_refname);
2171 local_refname = NULL;
2175 got_ref_list_free(&my_refs);
2176 free(remote_namespace);
2177 free(local_refname);
2181 static const struct got_error *
2182 update_wanted_ref(const char *refname, struct got_object_id *id,
2183 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2185 const struct got_error *err, *unlock_err;
2186 char *remote_refname;
2187 struct got_reference *ref;
2189 if (strncmp("refs/", refname, 5) == 0)
2192 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2193 remote_repo_name, refname) == -1)
2194 return got_error_from_errno("asprintf");
2196 err = got_ref_open(&ref, repo, remote_refname, 1);
2198 if (err->code != GOT_ERR_NOT_REF)
2200 err = create_ref(remote_refname, id, verbosity, repo);
2202 err = update_ref(ref, id, 0, verbosity, repo);
2203 unlock_err = got_ref_unlock(ref);
2204 if (unlock_err && err == NULL)
2209 free(remote_refname);
2213 static const struct got_error *
2214 delete_ref(struct got_repository *repo, struct got_reference *ref)
2216 const struct got_error *err = NULL;
2217 struct got_object_id *id = NULL;
2218 char *id_str = NULL;
2221 if (got_ref_is_symbolic(ref)) {
2222 target = got_ref_get_symref_target(ref);
2224 err = got_ref_resolve(&id, repo, ref);
2227 err = got_object_id_str(&id_str, id);
2233 err = got_ref_delete(ref, repo);
2237 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2244 static const struct got_error *
2245 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2247 const struct got_error *err = NULL;
2248 struct got_reflist_head refs;
2249 struct got_reflist_entry *re;
2254 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2255 err = got_error_from_errno("asprintf");
2258 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2262 TAILQ_FOREACH(re, &refs, entry)
2263 delete_ref(repo, re->ref);
2265 got_ref_list_free(&refs);
2269 static const struct got_error *
2270 cmd_fetch(int argc, char *argv[])
2272 const struct got_error *error = NULL, *unlock_err;
2273 char *cwd = NULL, *repo_path = NULL;
2274 const char *remote_name;
2275 char *proto = NULL, *host = NULL, *port = NULL;
2276 char *repo_name = NULL, *server_path = NULL;
2277 const struct got_remote_repo *remotes, *remote = NULL;
2279 char *id_str = NULL;
2280 struct got_repository *repo = NULL;
2281 struct got_worktree *worktree = NULL;
2282 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2283 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2284 struct got_pathlist_entry *pe;
2285 struct got_object_id *pack_hash = NULL;
2286 int i, ch, fetchfd = -1, fetchstatus;
2287 pid_t fetchpid = -1;
2288 struct got_fetch_progress_arg fpa;
2289 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2290 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2291 int *pack_fds = NULL, have_bflag = 0;
2292 const char *worktree_branch = NULL;
2295 TAILQ_INIT(&symrefs);
2296 TAILQ_INIT(&wanted_branches);
2297 TAILQ_INIT(&wanted_refs);
2299 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2302 fetch_all_branches = 1;
2305 error = got_pathlist_append(&wanted_branches,
2321 error = got_pathlist_append(&wanted_refs,
2327 repo_path = realpath(optarg, NULL);
2328 if (repo_path == NULL)
2329 return got_error_from_errno2("realpath",
2331 got_path_strip_trailing_slashes(repo_path);
2339 else if (verbosity < 3)
2353 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2354 option_conflict('a', 'b');
2355 if (list_refs_only) {
2356 if (!TAILQ_EMPTY(&wanted_branches))
2357 option_conflict('l', 'b');
2358 if (fetch_all_branches)
2359 option_conflict('l', 'a');
2361 option_conflict('l', 'd');
2363 option_conflict('l', 'X');
2365 if (delete_remote) {
2366 if (fetch_all_branches)
2367 option_conflict('X', 'a');
2368 if (!TAILQ_EMPTY(&wanted_branches))
2369 option_conflict('X', 'b');
2371 option_conflict('X', 'd');
2373 option_conflict('X', 't');
2374 if (!TAILQ_EMPTY(&wanted_refs))
2375 option_conflict('X', 'R');
2380 errx(1, "-X option requires a remote name");
2381 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2382 } else if (argc == 1)
2383 remote_name = argv[0];
2387 cwd = getcwd(NULL, 0);
2389 error = got_error_from_errno("getcwd");
2393 error = got_repo_pack_fds_open(&pack_fds);
2397 if (repo_path == NULL) {
2398 error = got_worktree_open(&worktree, cwd);
2399 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2405 strdup(got_worktree_get_repo_path(worktree));
2406 if (repo_path == NULL)
2407 error = got_error_from_errno("strdup");
2411 repo_path = strdup(cwd);
2412 if (repo_path == NULL) {
2413 error = got_error_from_errno("strdup");
2419 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2423 if (delete_remote) {
2424 error = delete_refs_for_remote(repo, remote_name);
2425 goto done; /* nothing else to do */
2429 worktree_conf = got_worktree_get_gotconfig(worktree);
2430 if (worktree_conf) {
2431 got_gotconfig_get_remotes(&nremotes, &remotes,
2433 for (i = 0; i < nremotes; i++) {
2434 if (strcmp(remotes[i].name, remote_name) == 0) {
2435 remote = &remotes[i];
2441 if (remote == NULL) {
2442 repo_conf = got_repo_get_gotconfig(repo);
2444 got_gotconfig_get_remotes(&nremotes, &remotes,
2446 for (i = 0; i < nremotes; i++) {
2447 if (strcmp(remotes[i].name, remote_name) == 0) {
2448 remote = &remotes[i];
2454 if (remote == NULL) {
2455 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2456 for (i = 0; i < nremotes; i++) {
2457 if (strcmp(remotes[i].name, remote_name) == 0) {
2458 remote = &remotes[i];
2463 if (remote == NULL) {
2464 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2468 if (TAILQ_EMPTY(&wanted_branches)) {
2469 if (!fetch_all_branches)
2470 fetch_all_branches = remote->fetch_all_branches;
2471 for (i = 0; i < remote->nfetch_branches; i++) {
2472 error = got_pathlist_append(&wanted_branches,
2473 remote->fetch_branches[i], NULL);
2478 if (TAILQ_EMPTY(&wanted_refs)) {
2479 for (i = 0; i < remote->nfetch_refs; i++) {
2480 error = got_pathlist_append(&wanted_refs,
2481 remote->fetch_refs[i], NULL);
2487 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2488 &repo_name, remote->fetch_url);
2492 if (strcmp(proto, "git") == 0) {
2494 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2495 "sendfd dns inet unveil", NULL) == -1)
2498 } else if (strcmp(proto, "git+ssh") == 0 ||
2499 strcmp(proto, "ssh") == 0) {
2501 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2502 "sendfd unveil", NULL) == -1)
2505 } else if (strcmp(proto, "http") == 0 ||
2506 strcmp(proto, "git+http") == 0) {
2507 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2510 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2514 error = got_dial_apply_unveil(proto);
2518 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2522 if (verbosity >= 0) {
2523 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2524 remote->name, proto, host,
2525 port ? ":" : "", port ? port : "",
2526 *server_path == '/' ? "" : "/", server_path);
2529 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2530 server_path, verbosity);
2534 if (worktree && !have_bflag) {
2535 const char *refname;
2537 refname = got_worktree_get_head_ref_name(worktree);
2538 if (strncmp(refname, "refs/heads/", 11) == 0)
2539 worktree_branch = refname;
2542 fpa.last_scaled_size[0] = '\0';
2543 fpa.last_p_indexed = -1;
2544 fpa.last_p_resolved = -1;
2545 fpa.verbosity = verbosity;
2547 fpa.create_configs = 0;
2548 fpa.configs_created = 0;
2549 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2551 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2552 remote->mirror_references, fetch_all_branches, &wanted_branches,
2553 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2554 worktree_branch, have_bflag, fetch_progress, &fpa);
2558 if (list_refs_only) {
2559 error = list_remote_refs(&symrefs, &refs);
2563 if (pack_hash == NULL) {
2565 printf("Already up-to-date\n");
2566 } else if (verbosity >= 0) {
2567 error = got_object_id_str(&id_str, pack_hash);
2570 printf("\nFetched %s.pack\n", id_str);
2575 /* Update references provided with the pack file. */
2576 TAILQ_FOREACH(pe, &refs, entry) {
2577 const char *refname = pe->path;
2578 struct got_object_id *id = pe->data;
2579 struct got_reference *ref;
2580 char *remote_refname;
2582 if (is_wanted_ref(&wanted_refs, refname) &&
2583 !remote->mirror_references) {
2584 error = update_wanted_ref(refname, id,
2585 remote->name, verbosity, repo);
2591 if (remote->mirror_references ||
2592 strncmp("refs/tags/", refname, 10) == 0) {
2593 error = got_ref_open(&ref, repo, refname, 1);
2595 if (error->code != GOT_ERR_NOT_REF)
2597 error = create_ref(refname, id, verbosity,
2602 error = update_ref(ref, id, replace_tags,
2604 unlock_err = got_ref_unlock(ref);
2605 if (unlock_err && error == NULL)
2611 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2612 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2613 remote_name, refname + 11) == -1) {
2614 error = got_error_from_errno("asprintf");
2618 error = got_ref_open(&ref, repo, remote_refname, 1);
2620 if (error->code != GOT_ERR_NOT_REF)
2622 error = create_ref(remote_refname, id,
2627 error = update_ref(ref, id, replace_tags,
2629 unlock_err = got_ref_unlock(ref);
2630 if (unlock_err && error == NULL)
2637 /* Also create a local branch if none exists yet. */
2638 error = got_ref_open(&ref, repo, refname, 1);
2640 if (error->code != GOT_ERR_NOT_REF)
2642 error = create_ref(refname, id, verbosity,
2647 unlock_err = got_ref_unlock(ref);
2648 if (unlock_err && error == NULL)
2655 error = delete_missing_refs(&refs, &symrefs, remote,
2661 if (!remote->mirror_references) {
2662 /* Update remote HEAD reference if the server provided one. */
2663 TAILQ_FOREACH(pe, &symrefs, entry) {
2664 struct got_reference *target_ref;
2665 const char *refname = pe->path;
2666 const char *target = pe->data;
2667 char *remote_refname = NULL, *remote_target = NULL;
2669 if (strcmp(refname, GOT_REF_HEAD) != 0)
2672 if (strncmp("refs/heads/", target, 11) != 0)
2675 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2676 remote->name, refname) == -1) {
2677 error = got_error_from_errno("asprintf");
2680 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2681 remote->name, target + 11) == -1) {
2682 error = got_error_from_errno("asprintf");
2683 free(remote_refname);
2687 error = got_ref_open(&target_ref, repo, remote_target,
2690 free(remote_refname);
2691 free(remote_target);
2692 if (error->code == GOT_ERR_NOT_REF) {
2698 error = update_symref(remote_refname, target_ref,
2700 free(remote_refname);
2701 free(remote_target);
2702 got_ref_close(target_ref);
2709 if (kill(fetchpid, SIGTERM) == -1)
2710 error = got_error_from_errno("kill");
2711 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2712 error = got_error_from_errno("waitpid");
2714 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2715 error = got_error_from_errno("close");
2717 const struct got_error *close_err = got_repo_close(repo);
2722 got_worktree_close(worktree);
2724 const struct got_error *pack_err =
2725 got_repo_pack_fds_close(pack_fds);
2729 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2730 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2731 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2732 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2747 usage_checkout(void)
2749 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2750 "[-p path-prefix] repository-path [work-tree-path]\n",
2756 show_worktree_base_ref_warning(void)
2758 fprintf(stderr, "%s: warning: could not create a reference "
2759 "to the work tree's base commit; the commit could be "
2760 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2761 "repository writable and running 'got update' will prevent this\n",
2765 struct got_checkout_progress_arg {
2766 const char *worktree_path;
2767 int had_base_commit_ref_error;
2771 static const struct got_error *
2772 checkout_progress(void *arg, unsigned char status, const char *path)
2774 struct got_checkout_progress_arg *a = arg;
2776 /* Base commit bump happens silently. */
2777 if (status == GOT_STATUS_BUMP_BASE)
2780 if (status == GOT_STATUS_BASE_REF_ERR) {
2781 a->had_base_commit_ref_error = 1;
2785 while (path[0] == '/')
2788 if (a->verbosity >= 0)
2789 printf("%c %s/%s\n", status, a->worktree_path, path);
2794 static const struct got_error *
2795 check_cancelled(void *arg)
2797 if (sigint_received || sigpipe_received)
2798 return got_error(GOT_ERR_CANCELLED);
2802 static const struct got_error *
2803 check_linear_ancestry(struct got_object_id *commit_id,
2804 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2805 struct got_repository *repo)
2807 const struct got_error *err = NULL;
2808 struct got_object_id *yca_id;
2810 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2811 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2816 return got_error(GOT_ERR_ANCESTRY);
2819 * Require a straight line of history between the target commit
2820 * and the work tree's base commit.
2822 * Non-linear situations such as this require a rebase:
2824 * (commit) D F (base_commit)
2832 * 'got update' only handles linear cases:
2833 * Update forwards in time: A (base/yca) - B - C - D (commit)
2834 * Update backwards in time: D (base) - C - B - A (commit/yca)
2836 if (allow_forwards_in_time_only) {
2837 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2838 return got_error(GOT_ERR_ANCESTRY);
2839 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2840 got_object_id_cmp(base_commit_id, yca_id) != 0)
2841 return got_error(GOT_ERR_ANCESTRY);
2847 static const struct got_error *
2848 check_same_branch(struct got_object_id *commit_id,
2849 struct got_reference *head_ref, struct got_object_id *yca_id,
2850 struct got_repository *repo)
2852 const struct got_error *err = NULL;
2853 struct got_commit_graph *graph = NULL;
2854 struct got_object_id *head_commit_id = NULL;
2855 int is_same_branch = 0;
2857 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2861 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2865 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2870 err = got_commit_graph_open(&graph, "/", 1);
2874 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2875 check_cancelled, NULL);
2880 struct got_object_id id;
2882 err = got_commit_graph_iter_next(&id, graph, repo,
2883 check_cancelled, NULL);
2885 if (err->code == GOT_ERR_ITER_COMPLETED)
2890 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2892 if (got_object_id_cmp(&id, commit_id) == 0) {
2899 got_commit_graph_close(graph);
2900 free(head_commit_id);
2901 if (!err && !is_same_branch)
2902 err = got_error(GOT_ERR_ANCESTRY);
2906 static const struct got_error *
2907 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2909 static char msg[512];
2910 const char *branch_name;
2912 if (got_ref_is_symbolic(ref))
2913 branch_name = got_ref_get_symref_target(ref);
2915 branch_name = got_ref_get_name(ref);
2917 if (strncmp("refs/heads/", branch_name, 11) == 0)
2920 snprintf(msg, sizeof(msg),
2921 "target commit is not contained in branch '%s'; "
2922 "the branch to use must be specified with -b; "
2923 "if necessary a new branch can be created for "
2924 "this commit with 'got branch -c %s BRANCH_NAME'",
2925 branch_name, commit_id_str);
2927 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2930 static const struct got_error *
2931 cmd_checkout(int argc, char *argv[])
2933 const struct got_error *error = NULL;
2934 struct got_repository *repo = NULL;
2935 struct got_reference *head_ref = NULL, *ref = NULL;
2936 struct got_worktree *worktree = NULL;
2937 char *repo_path = NULL;
2938 char *worktree_path = NULL;
2939 const char *path_prefix = "";
2940 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2941 char *commit_id_str = NULL;
2942 struct got_object_id *commit_id = NULL;
2944 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2945 struct got_pathlist_head paths;
2946 struct got_checkout_progress_arg cpa;
2947 int *pack_fds = NULL;
2952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2953 "unveil", NULL) == -1)
2957 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2960 branch_name = optarg;
2963 commit_id_str = strdup(optarg);
2964 if (commit_id_str == NULL)
2965 return got_error_from_errno("strdup");
2971 path_prefix = optarg;
2986 char *base, *dotgit;
2988 repo_path = realpath(argv[0], NULL);
2989 if (repo_path == NULL)
2990 return got_error_from_errno2("realpath", argv[0]);
2991 cwd = getcwd(NULL, 0);
2993 error = got_error_from_errno("getcwd");
3000 error = got_path_basename(&base, path);
3003 dotgit = strstr(base, ".git");
3006 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3007 error = got_error_from_errno("asprintf");
3012 } else if (argc == 2) {
3013 repo_path = realpath(argv[0], NULL);
3014 if (repo_path == NULL) {
3015 error = got_error_from_errno2("realpath", argv[0]);
3018 worktree_path = realpath(argv[1], NULL);
3019 if (worktree_path == NULL) {
3020 if (errno != ENOENT) {
3021 error = got_error_from_errno2("realpath",
3025 worktree_path = strdup(argv[1]);
3026 if (worktree_path == NULL) {
3027 error = got_error_from_errno("strdup");
3034 got_path_strip_trailing_slashes(repo_path);
3035 got_path_strip_trailing_slashes(worktree_path);
3037 error = got_repo_pack_fds_open(&pack_fds);
3041 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3045 /* Pre-create work tree path for unveil(2) */
3046 error = got_path_mkdir(worktree_path);
3048 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3049 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3051 if (!allow_nonempty &&
3052 !got_path_dir_is_empty(worktree_path)) {
3053 error = got_error_path(worktree_path,
3054 GOT_ERR_DIR_NOT_EMPTY);
3059 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3063 error = got_ref_open(&head_ref, repo, branch_name, 0);
3067 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3068 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3071 error = got_worktree_open(&worktree, worktree_path);
3075 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3079 if (!same_path_prefix) {
3080 error = got_error(GOT_ERR_PATH_PREFIX);
3084 if (commit_id_str) {
3085 struct got_reflist_head refs;
3087 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3091 error = got_repo_match_object_id(&commit_id, NULL,
3092 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3093 got_ref_list_free(&refs);
3096 error = check_linear_ancestry(commit_id,
3097 got_worktree_get_base_commit_id(worktree), 0, repo);
3098 if (error != NULL) {
3099 if (error->code == GOT_ERR_ANCESTRY) {
3100 error = checkout_ancestry_error(
3101 head_ref, commit_id_str);
3105 error = check_same_branch(commit_id, head_ref, NULL, repo);
3107 if (error->code == GOT_ERR_ANCESTRY) {
3108 error = checkout_ancestry_error(
3109 head_ref, commit_id_str);
3113 error = got_worktree_set_base_commit_id(worktree, repo,
3117 /* Expand potentially abbreviated commit ID string. */
3118 free(commit_id_str);
3119 error = got_object_id_str(&commit_id_str, commit_id);
3123 commit_id = got_object_id_dup(
3124 got_worktree_get_base_commit_id(worktree));
3125 if (commit_id == NULL) {
3126 error = got_error_from_errno("got_object_id_dup");
3129 error = got_object_id_str(&commit_id_str, commit_id);
3134 error = got_pathlist_append(&paths, "", NULL);
3137 cpa.worktree_path = worktree_path;
3138 cpa.had_base_commit_ref_error = 0;
3139 cpa.verbosity = verbosity;
3140 error = got_worktree_checkout_files(worktree, &paths, repo,
3141 checkout_progress, &cpa, check_cancelled, NULL);
3145 if (got_ref_is_symbolic(head_ref)) {
3146 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3149 refname = got_ref_get_name(ref);
3151 refname = got_ref_get_name(head_ref);
3152 printf("Checked out %s: %s\n", refname, commit_id_str);
3153 printf("Now shut up and hack\n");
3154 if (cpa.had_base_commit_ref_error)
3155 show_worktree_base_ref_warning();
3158 const struct got_error *pack_err =
3159 got_repo_pack_fds_close(pack_fds);
3164 got_ref_close(head_ref);
3167 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3168 free(commit_id_str);
3171 free(worktree_path);
3176 struct got_update_progress_arg {
3188 print_update_progress_stats(struct got_update_progress_arg *upa)
3190 if (!upa->did_something)
3193 if (upa->conflicts > 0)
3194 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3195 if (upa->obstructed > 0)
3196 printf("File paths obstructed by a non-regular file: %d\n",
3198 if (upa->not_updated > 0)
3199 printf("Files not updated because of existing merge "
3200 "conflicts: %d\n", upa->not_updated);
3204 * The meaning of some status codes differs between merge-style operations and
3205 * update operations. For example, the ! status code means "file was missing"
3206 * if changes were merged into the work tree, and "missing file was restored"
3207 * if the work tree was updated. This function should be used by any operation
3208 * which merges changes into the work tree without updating the work tree.
3211 print_merge_progress_stats(struct got_update_progress_arg *upa)
3213 if (!upa->did_something)
3216 if (upa->conflicts > 0)
3217 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3218 if (upa->obstructed > 0)
3219 printf("File paths obstructed by a non-regular file: %d\n",
3221 if (upa->missing > 0)
3222 printf("Files which had incoming changes but could not be "
3223 "found in the work tree: %d\n", upa->missing);
3224 if (upa->not_deleted > 0)
3225 printf("Files not deleted due to differences in deleted "
3226 "content: %d\n", upa->not_deleted);
3227 if (upa->unversioned > 0)
3228 printf("Files not merged because an unversioned file was "
3229 "found in the work tree: %d\n", upa->unversioned);
3235 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3236 "[path ...]\n", getprogname());
3240 static const struct got_error *
3241 update_progress(void *arg, unsigned char status, const char *path)
3243 struct got_update_progress_arg *upa = arg;
3245 if (status == GOT_STATUS_EXISTS ||
3246 status == GOT_STATUS_BASE_REF_ERR)
3249 upa->did_something = 1;
3251 /* Base commit bump happens silently. */
3252 if (status == GOT_STATUS_BUMP_BASE)
3255 if (status == GOT_STATUS_CONFLICT)
3257 if (status == GOT_STATUS_OBSTRUCTED)
3259 if (status == GOT_STATUS_CANNOT_UPDATE)
3261 if (status == GOT_STATUS_MISSING)
3263 if (status == GOT_STATUS_CANNOT_DELETE)
3265 if (status == GOT_STATUS_UNVERSIONED)
3268 while (path[0] == '/')
3270 if (upa->verbosity >= 0)
3271 printf("%c %s\n", status, path);
3276 static const struct got_error *
3277 switch_head_ref(struct got_reference *head_ref,
3278 struct got_object_id *commit_id, struct got_worktree *worktree,
3279 struct got_repository *repo)
3281 const struct got_error *err = NULL;
3283 int ref_has_moved = 0;
3285 /* Trivial case: switching between two different references. */
3286 if (strcmp(got_ref_get_name(head_ref),
3287 got_worktree_get_head_ref_name(worktree)) != 0) {
3288 printf("Switching work tree from %s to %s\n",
3289 got_worktree_get_head_ref_name(worktree),
3290 got_ref_get_name(head_ref));
3291 return got_worktree_set_head_ref(worktree, head_ref);
3294 err = check_linear_ancestry(commit_id,
3295 got_worktree_get_base_commit_id(worktree), 0, repo);
3297 if (err->code != GOT_ERR_ANCESTRY)
3304 /* Switching to a rebased branch with the same reference name. */
3305 err = got_object_id_str(&base_id_str,
3306 got_worktree_get_base_commit_id(worktree));
3309 printf("Reference %s now points at a different branch\n",
3310 got_worktree_get_head_ref_name(worktree));
3311 printf("Switching work tree from %s to %s\n", base_id_str,
3312 got_worktree_get_head_ref_name(worktree));
3316 static const struct got_error *
3317 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3319 const struct got_error *err;
3322 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3326 return got_error(GOT_ERR_REBASING);
3328 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3332 return got_error(GOT_ERR_HISTEDIT_BUSY);
3337 static const struct got_error *
3338 check_merge_in_progress(struct got_worktree *worktree,
3339 struct got_repository *repo)
3341 const struct got_error *err;
3344 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3348 return got_error(GOT_ERR_MERGE_BUSY);
3353 static const struct got_error *
3354 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3355 char *argv[], struct got_worktree *worktree)
3357 const struct got_error *err = NULL;
3359 struct got_pathlist_entry *new;
3365 return got_error_from_errno("strdup");
3366 return got_pathlist_append(paths, path, NULL);
3369 for (i = 0; i < argc; i++) {
3370 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3373 err = got_pathlist_insert(&new, paths, path, NULL);
3374 if (err || new == NULL /* duplicate */) {
3384 static const struct got_error *
3385 wrap_not_worktree_error(const struct got_error *orig_err,
3386 const char *cmdname, const char *path)
3388 const struct got_error *err;
3389 struct got_repository *repo;
3390 static char msg[512];
3391 int *pack_fds = NULL;
3393 err = got_repo_pack_fds_open(&pack_fds);
3397 err = got_repo_open(&repo, path, NULL, pack_fds);
3401 snprintf(msg, sizeof(msg),
3402 "'got %s' needs a work tree in addition to a git repository\n"
3403 "Work trees can be checked out from this Git repository with "
3405 "The got(1) manual page contains more information.", cmdname);
3406 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3407 got_repo_close(repo);
3409 const struct got_error *pack_err =
3410 got_repo_pack_fds_close(pack_fds);
3417 static const struct got_error *
3418 cmd_update(int argc, char *argv[])
3420 const struct got_error *error = NULL;
3421 struct got_repository *repo = NULL;
3422 struct got_worktree *worktree = NULL;
3423 char *worktree_path = NULL;
3424 struct got_object_id *commit_id = NULL;
3425 char *commit_id_str = NULL;
3426 const char *branch_name = NULL;
3427 struct got_reference *head_ref = NULL;
3428 struct got_pathlist_head paths;
3429 struct got_pathlist_entry *pe;
3430 int ch, verbosity = 0;
3431 struct got_update_progress_arg upa;
3432 int *pack_fds = NULL;
3437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3438 "unveil", NULL) == -1)
3442 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3445 branch_name = optarg;
3448 commit_id_str = strdup(optarg);
3449 if (commit_id_str == NULL)
3450 return got_error_from_errno("strdup");
3464 worktree_path = getcwd(NULL, 0);
3465 if (worktree_path == NULL) {
3466 error = got_error_from_errno("getcwd");
3470 error = got_repo_pack_fds_open(&pack_fds);
3474 error = got_worktree_open(&worktree, worktree_path);
3476 if (error->code == GOT_ERR_NOT_WORKTREE)
3477 error = wrap_not_worktree_error(error, "update",
3482 error = check_rebase_or_histedit_in_progress(worktree);
3486 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3491 error = apply_unveil(got_repo_get_path(repo), 0,
3492 got_worktree_get_root_path(worktree));
3496 error = check_merge_in_progress(worktree, repo);
3500 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3504 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3505 got_worktree_get_head_ref_name(worktree), 0);
3508 if (commit_id_str == NULL) {
3509 error = got_ref_resolve(&commit_id, repo, head_ref);
3512 error = got_object_id_str(&commit_id_str, commit_id);
3516 struct got_reflist_head refs;
3518 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3522 error = got_repo_match_object_id(&commit_id, NULL,
3523 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3524 got_ref_list_free(&refs);
3525 free(commit_id_str);
3526 commit_id_str = NULL;
3529 error = got_object_id_str(&commit_id_str, commit_id);
3535 struct got_object_id *head_commit_id;
3536 TAILQ_FOREACH(pe, &paths, entry) {
3537 if (pe->path_len == 0)
3539 error = got_error_msg(GOT_ERR_BAD_PATH,
3540 "switching between branches requires that "
3541 "the entire work tree gets updated");
3544 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3547 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3549 free(head_commit_id);
3552 error = check_same_branch(commit_id, head_ref, NULL, repo);
3555 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3559 error = check_linear_ancestry(commit_id,
3560 got_worktree_get_base_commit_id(worktree), 0, repo);
3561 if (error != NULL) {
3562 if (error->code == GOT_ERR_ANCESTRY)
3563 error = got_error(GOT_ERR_BRANCH_MOVED);
3566 error = check_same_branch(commit_id, head_ref, NULL, repo);
3571 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3573 error = got_worktree_set_base_commit_id(worktree, repo,
3579 memset(&upa, 0, sizeof(upa));
3580 upa.verbosity = verbosity;
3581 error = got_worktree_checkout_files(worktree, &paths, repo,
3582 update_progress, &upa, check_cancelled, NULL);
3586 if (upa.did_something) {
3587 printf("Updated to %s: %s\n",
3588 got_worktree_get_head_ref_name(worktree), commit_id_str);
3590 printf("Already up-to-date\n");
3592 print_update_progress_stats(&upa);
3595 const struct got_error *pack_err =
3596 got_repo_pack_fds_close(pack_fds);
3600 free(worktree_path);
3601 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3603 free(commit_id_str);
3607 static const struct got_error *
3608 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3609 const char *path, int diff_context, int ignore_whitespace,
3610 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3611 struct got_repository *repo, FILE *outfile)
3613 const struct got_error *err = NULL;
3614 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3615 FILE *f1 = NULL, *f2 = NULL;
3616 int fd1 = -1, fd2 = -1;
3618 fd1 = got_opentempfd();
3620 return got_error_from_errno("got_opentempfd");
3621 fd2 = got_opentempfd();
3623 err = got_error_from_errno("got_opentempfd");
3628 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3634 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3638 f1 = got_opentemp();
3640 err = got_error_from_errno("got_opentemp");
3643 f2 = got_opentemp();
3645 err = got_error_from_errno("got_opentemp");
3649 while (path[0] == '/')
3651 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3652 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3653 force_text_diff, dsa, outfile);
3655 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3656 err = got_error_from_errno("close");
3658 got_object_blob_close(blob1);
3659 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3660 err = got_error_from_errno("close");
3661 got_object_blob_close(blob2);
3662 if (f1 && fclose(f1) == EOF && err == NULL)
3663 err = got_error_from_errno("fclose");
3664 if (f2 && fclose(f2) == EOF && err == NULL)
3665 err = got_error_from_errno("fclose");
3669 static const struct got_error *
3670 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3671 const char *path, int diff_context, int ignore_whitespace,
3672 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3673 struct got_repository *repo, FILE *outfile)
3675 const struct got_error *err = NULL;
3676 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3677 struct got_diff_blob_output_unidiff_arg arg;
3678 FILE *f1 = NULL, *f2 = NULL;
3679 int fd1 = -1, fd2 = -1;
3682 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3685 fd1 = got_opentempfd();
3687 err = got_error_from_errno("got_opentempfd");
3692 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3696 f1 = got_opentemp();
3698 err = got_error_from_errno("got_opentemp");
3702 f2 = got_opentemp();
3704 err = got_error_from_errno("got_opentemp");
3707 fd2 = got_opentempfd();
3709 err = got_error_from_errno("got_opentempfd");
3712 arg.diff_context = diff_context;
3713 arg.ignore_whitespace = ignore_whitespace;
3714 arg.force_text_diff = force_text_diff;
3716 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3717 arg.outfile = outfile;
3720 while (path[0] == '/')
3722 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3723 got_diff_blob_output_unidiff, &arg, 1);
3726 got_object_tree_close(tree1);
3728 got_object_tree_close(tree2);
3729 if (f1 && fclose(f1) == EOF && err == NULL)
3730 err = got_error_from_errno("fclose");
3731 if (f2 && fclose(f2) == EOF && err == NULL)
3732 err = got_error_from_errno("fclose");
3733 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3734 err = got_error_from_errno("close");
3735 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3736 err = got_error_from_errno("close");
3740 static const struct got_error *
3741 get_changed_paths(struct got_pathlist_head *paths,
3742 struct got_commit_object *commit, struct got_repository *repo,
3743 struct got_diffstat_cb_arg *dsa)
3745 const struct got_error *err = NULL;
3746 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3747 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3748 struct got_object_qid *qid;
3749 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3750 FILE *f1 = NULL, *f2 = NULL;
3751 int fd1 = -1, fd2 = -1;
3754 cb = got_diff_tree_compute_diffstat;
3756 f1 = got_opentemp();
3758 err = got_error_from_errno("got_opentemp");
3761 f2 = got_opentemp();
3763 err = got_error_from_errno("got_opentemp");
3766 fd1 = got_opentempfd();
3768 err = got_error_from_errno("got_opentempfd");
3771 fd2 = got_opentempfd();
3773 err = got_error_from_errno("got_opentempfd");
3778 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3780 struct got_commit_object *pcommit;
3781 err = got_object_open_as_commit(&pcommit, repo,
3786 tree_id1 = got_object_id_dup(
3787 got_object_commit_get_tree_id(pcommit));
3788 if (tree_id1 == NULL) {
3789 got_object_commit_close(pcommit);
3790 return got_error_from_errno("got_object_id_dup");
3792 got_object_commit_close(pcommit);
3797 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3802 tree_id2 = got_object_commit_get_tree_id(commit);
3803 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3807 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3808 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3811 got_object_tree_close(tree1);
3813 got_object_tree_close(tree2);
3814 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3815 err = got_error_from_errno("close");
3816 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3817 err = got_error_from_errno("close");
3818 if (f1 && fclose(f1) == EOF && err == NULL)
3819 err = got_error_from_errno("fclose");
3820 if (f2 && fclose(f2) == EOF && err == NULL)
3821 err = got_error_from_errno("fclose");
3826 static const struct got_error *
3827 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3828 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3829 struct got_repository *repo, FILE *outfile)
3831 const struct got_error *err = NULL;
3832 struct got_commit_object *pcommit = NULL;
3833 char *id_str1 = NULL, *id_str2 = NULL;
3834 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3835 struct got_object_qid *qid;
3837 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3839 err = got_object_open_as_commit(&pcommit, repo,
3843 err = got_object_id_str(&id_str1, &qid->id);
3848 err = got_object_id_str(&id_str2, id);
3852 if (path && path[0] != '\0') {
3854 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3858 err = got_object_id_by_path(&obj_id1, repo,
3861 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3867 err = got_object_get_type(&obj_type, repo, obj_id2);
3873 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3874 fprintf(outfile, "commit - %s\n",
3875 id_str1 ? id_str1 : "/dev/null");
3876 fprintf(outfile, "commit + %s\n", id_str2);
3878 case GOT_OBJ_TYPE_BLOB:
3879 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3880 0, 0, dsa, repo, outfile);
3882 case GOT_OBJ_TYPE_TREE:
3883 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3884 0, 0, dsa, repo, outfile);
3887 err = got_error(GOT_ERR_OBJ_TYPE);
3893 obj_id2 = got_object_commit_get_tree_id(commit);
3895 obj_id1 = got_object_commit_get_tree_id(pcommit);
3897 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3898 fprintf(outfile, "commit - %s\n",
3899 id_str1 ? id_str1 : "/dev/null");
3900 fprintf(outfile, "commit + %s\n", id_str2);
3901 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3902 dsa, repo, outfile);
3908 got_object_commit_close(pcommit);
3913 get_datestr(time_t *time, char *datebuf)
3915 struct tm mytm, *tm;
3918 tm = gmtime_r(time, &mytm);
3921 s = asctime_r(tm, datebuf);
3924 p = strchr(s, '\n');
3930 static const struct got_error *
3931 match_commit(int *have_match, struct got_object_id *id,
3932 struct got_commit_object *commit, regex_t *regex)
3934 const struct got_error *err = NULL;
3935 regmatch_t regmatch;
3936 char *id_str = NULL, *logmsg = NULL;
3940 err = got_object_id_str(&id_str, id);
3944 err = got_object_commit_get_logmsg(&logmsg, commit);
3948 if (regexec(regex, got_object_commit_get_author(commit), 1,
3949 ®match, 0) == 0 ||
3950 regexec(regex, got_object_commit_get_committer(commit), 1,
3951 ®match, 0) == 0 ||
3952 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3953 regexec(regex, logmsg, 1, ®match, 0) == 0)
3962 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3965 regmatch_t regmatch;
3966 struct got_pathlist_entry *pe;
3970 TAILQ_FOREACH(pe, changed_paths, entry) {
3971 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3978 static const struct got_error *
3979 match_patch(int *have_match, struct got_commit_object *commit,
3980 struct got_object_id *id, const char *path, int diff_context,
3981 struct got_repository *repo, regex_t *regex, FILE *f)
3983 const struct got_error *err = NULL;
3985 size_t linesize = 0;
3986 regmatch_t regmatch;
3990 err = got_opentemp_truncate(f);
3994 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3998 if (fseeko(f, 0L, SEEK_SET) == -1) {
3999 err = got_error_from_errno("fseeko");
4003 while (getline(&line, &linesize, f) != -1) {
4004 if (regexec(regex, line, 1, ®match, 0) == 0) {
4014 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4016 static const struct got_error*
4017 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4018 struct got_object_id *id, struct got_repository *repo,
4021 static const struct got_error *err = NULL;
4022 struct got_reflist_entry *re;
4028 TAILQ_FOREACH(re, refs, entry) {
4029 struct got_tag_object *tag = NULL;
4030 struct got_object_id *ref_id;
4033 name = got_ref_get_name(re->ref);
4034 if (strcmp(name, GOT_REF_HEAD) == 0)
4036 if (strncmp(name, "refs/", 5) == 0)
4038 if (strncmp(name, "got/", 4) == 0)
4040 if (strncmp(name, "heads/", 6) == 0)
4042 if (strncmp(name, "remotes/", 8) == 0) {
4046 s = strstr(name, "/" GOT_REF_HEAD);
4047 if (s != NULL && s[strlen(s)] == '\0')
4050 err = got_ref_resolve(&ref_id, repo, re->ref);
4053 if (strncmp(name, "tags/", 5) == 0) {
4054 err = got_object_open_as_tag(&tag, repo, ref_id);
4056 if (err->code != GOT_ERR_OBJ_TYPE) {
4060 /* Ref points at something other than a tag. */
4065 cmp = got_object_id_cmp(tag ?
4066 got_object_tag_get_object_id(tag) : ref_id, id);
4069 got_object_tag_close(tag);
4073 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4074 s ? ", " : "", name) == -1) {
4075 err = got_error_from_errno("asprintf");
4086 static const struct got_error *
4087 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4088 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4090 const struct got_error *err = NULL;
4091 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4092 char *comma, *s, *nl;
4093 struct got_reflist_head *refs;
4094 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4096 time_t committer_time;
4098 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4100 err = build_refs_str(&ref_str, refs, id, repo, 1);
4104 /* Display the first matching ref only. */
4105 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4109 if (ref_str == NULL) {
4110 err = got_object_id_str(&id_str, id);
4115 committer_time = got_object_commit_get_committer_time(commit);
4116 if (gmtime_r(&committer_time, &tm) == NULL) {
4117 err = got_error_from_errno("gmtime_r");
4120 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4121 err = got_error(GOT_ERR_NO_SPACE);
4125 err = got_object_commit_get_logmsg(&logmsg0, commit);
4130 while (isspace((unsigned char)s[0]))
4133 nl = strchr(s, '\n');
4139 printf("%s%-7s %s\n", datebuf, ref_str, s);
4141 printf("%s%.7s %s\n", datebuf, id_str, s);
4143 if (fflush(stdout) != 0 && err == NULL)
4144 err = got_error_from_errno("fflush");
4152 static const struct got_error *
4153 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4155 struct got_pathlist_entry *pe;
4158 printf("%s\n", header);
4160 TAILQ_FOREACH(pe, dsa->paths, entry) {
4161 struct got_diff_changed_path *cp = pe->data;
4162 int pad = dsa->max_path_len - pe->path_len + 1;
4164 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4165 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4167 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4168 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4169 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4171 if (fflush(stdout) != 0)
4172 return got_error_from_errno("fflush");
4177 static const struct got_error *
4183 if (fseeko(f, 0L, SEEK_SET) == -1)
4184 return got_error_from_errno("fseek");
4187 r = fread(buf, 1, sizeof(buf), f);
4190 return got_error_from_errno("fread");
4194 if (fwrite(buf, 1, r, stdout) != r)
4195 return got_ferror(stdout, GOT_ERR_IO);
4201 static const struct got_error *
4202 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4203 struct got_repository *repo, const char *path,
4204 struct got_pathlist_head *changed_paths,
4205 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4206 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4209 const struct got_error *err = NULL;
4211 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4213 time_t committer_time;
4214 const char *author, *committer;
4215 char *refs_str = NULL;
4217 err = got_object_id_str(&id_str, id);
4221 if (custom_refs_str == NULL) {
4222 struct got_reflist_head *refs;
4223 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4225 err = build_refs_str(&refs_str, refs, id, repo, 0);
4231 printf(GOT_COMMIT_SEP_STR);
4232 if (custom_refs_str)
4233 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4236 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4237 refs_str ? " (" : "", refs_str ? refs_str : "",
4238 refs_str ? ")" : "");
4243 printf("from: %s\n", got_object_commit_get_author(commit));
4244 author = got_object_commit_get_author(commit);
4245 committer = got_object_commit_get_committer(commit);
4246 if (strcmp(author, committer) != 0)
4247 printf("via: %s\n", committer);
4248 committer_time = got_object_commit_get_committer_time(commit);
4249 datestr = get_datestr(&committer_time, datebuf);
4251 printf("date: %s UTC\n", datestr);
4252 if (got_object_commit_get_nparents(commit) > 1) {
4253 const struct got_object_id_queue *parent_ids;
4254 struct got_object_qid *qid;
4256 parent_ids = got_object_commit_get_parent_ids(commit);
4257 STAILQ_FOREACH(qid, parent_ids, entry) {
4258 err = got_object_id_str(&id_str, &qid->id);
4261 printf("parent %d: %s\n", n++, id_str);
4267 err = got_object_commit_get_logmsg(&logmsg0, commit);
4273 line = strsep(&logmsg, "\n");
4275 printf(" %s\n", line);
4279 if (changed_paths && diffstat == NULL) {
4280 struct got_pathlist_entry *pe;
4282 TAILQ_FOREACH(pe, changed_paths, entry) {
4283 struct got_diff_changed_path *cp = pe->data;
4285 printf(" %c %s\n", cp->status, pe->path);
4293 err = got_error_from_errno("got_opentemp");
4298 err = print_patch(commit, id, path, diff_context, diffstat,
4299 repo, diffstat == NULL ? stdout : f);
4304 err = print_diffstat(diffstat, NULL);
4316 if (fflush(stdout) != 0 && err == NULL)
4317 err = got_error_from_errno("fflush");
4319 if (f && fclose(f) == EOF && err == NULL)
4320 err = got_error_from_errno("fclose");
4326 static const struct got_error *
4327 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4328 struct got_repository *repo, const char *path, int show_changed_paths,
4329 int show_diffstat, int show_patch, const char *search_pattern,
4330 int diff_context, int limit, int log_branches, int reverse_display_order,
4331 struct got_reflist_object_id_map *refs_idmap, int one_line,
4334 const struct got_error *err;
4335 struct got_commit_graph *graph;
4338 struct got_object_id_queue reversed_commits;
4339 struct got_object_qid *qid;
4340 struct got_commit_object *commit;
4341 struct got_pathlist_head changed_paths;
4343 STAILQ_INIT(&reversed_commits);
4344 TAILQ_INIT(&changed_paths);
4346 if (search_pattern && regcomp(®ex, search_pattern,
4347 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4348 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4350 err = got_commit_graph_open(&graph, path, !log_branches);
4353 err = got_commit_graph_iter_start(graph, root_id, repo,
4354 check_cancelled, NULL);
4358 struct got_object_id id;
4359 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4360 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4362 if (sigint_received || sigpipe_received)
4365 err = got_commit_graph_iter_next(&id, graph, repo,
4366 check_cancelled, NULL);
4368 if (err->code == GOT_ERR_ITER_COMPLETED)
4373 err = got_object_open_as_commit(&commit, repo, &id);
4377 if ((show_changed_paths || (show_diffstat && !show_patch))
4378 && !reverse_display_order) {
4379 err = get_changed_paths(&changed_paths, commit, repo,
4380 show_diffstat ? &dsa : NULL);
4385 if (search_pattern) {
4386 err = match_commit(&have_match, &id, commit, ®ex);
4388 got_object_commit_close(commit);
4391 if (have_match == 0 && show_changed_paths)
4392 match_changed_paths(&have_match,
4393 &changed_paths, ®ex);
4394 if (have_match == 0 && show_patch) {
4395 err = match_patch(&have_match, commit, &id,
4396 path, diff_context, repo, ®ex, tmpfile);
4400 if (have_match == 0) {
4401 got_object_commit_close(commit);
4402 got_pathlist_free(&changed_paths,
4403 GOT_PATHLIST_FREE_ALL);
4408 if (reverse_display_order) {
4409 err = got_object_qid_alloc(&qid, &id);
4412 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4413 got_object_commit_close(commit);
4416 err = print_commit_oneline(commit, &id,
4419 err = print_commit(commit, &id, repo, path,
4420 (show_changed_paths || show_diffstat) ?
4421 &changed_paths : NULL,
4422 show_diffstat ? &dsa : NULL, show_patch,
4423 diff_context, refs_idmap, NULL, NULL);
4424 got_object_commit_close(commit);
4428 if ((limit && --limit == 0) ||
4429 (end_id && got_object_id_cmp(&id, end_id) == 0))
4432 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4434 if (reverse_display_order) {
4435 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4436 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4437 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4439 err = got_object_open_as_commit(&commit, repo,
4443 if (show_changed_paths ||
4444 (show_diffstat && !show_patch)) {
4445 err = get_changed_paths(&changed_paths, commit,
4446 repo, show_diffstat ? &dsa : NULL);
4451 err = print_commit_oneline(commit, &qid->id,
4454 err = print_commit(commit, &qid->id, repo, path,
4455 (show_changed_paths || show_diffstat) ?
4456 &changed_paths : NULL,
4457 show_diffstat ? &dsa : NULL, show_patch,
4458 diff_context, refs_idmap, NULL, NULL);
4459 got_object_commit_close(commit);
4462 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4466 while (!STAILQ_EMPTY(&reversed_commits)) {
4467 qid = STAILQ_FIRST(&reversed_commits);
4468 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4469 got_object_qid_free(qid);
4471 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4474 got_commit_graph_close(graph);
4481 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4482 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4483 "[path]\n", getprogname());
4488 get_default_log_limit(void)
4490 const char *got_default_log_limit;
4494 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4495 if (got_default_log_limit == NULL)
4497 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4503 static const struct got_error *
4504 cmd_log(int argc, char *argv[])
4506 const struct got_error *error;
4507 struct got_repository *repo = NULL;
4508 struct got_worktree *worktree = NULL;
4509 struct got_object_id *start_id = NULL, *end_id = NULL;
4510 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4511 const char *start_commit = NULL, *end_commit = NULL;
4512 const char *search_pattern = NULL;
4513 int diff_context = -1, ch;
4514 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4515 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4517 struct got_reflist_head refs;
4518 struct got_reflist_object_id_map *refs_idmap = NULL;
4519 FILE *tmpfile = NULL;
4520 int *pack_fds = NULL;
4525 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4531 limit = get_default_log_limit();
4533 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4539 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4542 errx(1, "number of context lines is %s: %s",
4546 start_commit = optarg;
4552 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4554 errx(1, "number of commits is %s: %s",
4558 show_changed_paths = 1;
4564 reverse_display_order = 1;
4567 repo_path = realpath(optarg, NULL);
4568 if (repo_path == NULL)
4569 return got_error_from_errno2("realpath",
4571 got_path_strip_trailing_slashes(repo_path);
4574 search_pattern = optarg;
4580 end_commit = optarg;
4591 if (diff_context == -1)
4593 else if (!show_patch)
4594 errx(1, "-C requires -p");
4596 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4597 errx(1, "cannot use -s with -d, -p or -P");
4599 cwd = getcwd(NULL, 0);
4601 error = got_error_from_errno("getcwd");
4605 error = got_repo_pack_fds_open(&pack_fds);
4609 if (repo_path == NULL) {
4610 error = got_worktree_open(&worktree, cwd);
4611 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4618 error = got_worktree_resolve_path(&path, worktree,
4623 path = strdup(argv[0]);
4625 error = got_error_from_errno("strdup");
4629 } else if (argc != 0)
4632 if (repo_path == NULL) {
4633 repo_path = worktree ?
4634 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4636 if (repo_path == NULL) {
4637 error = got_error_from_errno("strdup");
4641 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4645 error = apply_unveil(got_repo_get_path(repo), 1,
4646 worktree ? got_worktree_get_root_path(worktree) : NULL);
4650 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4654 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4658 if (start_commit == NULL) {
4659 struct got_reference *head_ref;
4660 struct got_commit_object *commit = NULL;
4661 error = got_ref_open(&head_ref, repo,
4662 worktree ? got_worktree_get_head_ref_name(worktree)
4666 error = got_ref_resolve(&start_id, repo, head_ref);
4667 got_ref_close(head_ref);
4670 error = got_object_open_as_commit(&commit, repo,
4674 got_object_commit_close(commit);
4676 error = got_repo_match_object_id(&start_id, NULL,
4677 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4681 if (end_commit != NULL) {
4682 error = got_repo_match_object_id(&end_id, NULL,
4683 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4690 * If a path was specified on the command line it was resolved
4691 * to a path in the work tree above. Prepend the work tree's
4692 * path prefix to obtain the corresponding in-repository path.
4696 prefix = got_worktree_get_path_prefix(worktree);
4697 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4698 (path[0] != '\0') ? "/" : "", path) == -1) {
4699 error = got_error_from_errno("asprintf");
4704 error = got_repo_map_path(&in_repo_path, repo,
4710 path = in_repo_path;
4714 /* Release work tree lock. */
4715 got_worktree_close(worktree);
4719 if (search_pattern && show_patch) {
4720 tmpfile = got_opentemp();
4721 if (tmpfile == NULL) {
4722 error = got_error_from_errno("got_opentemp");
4727 error = print_commits(start_id, end_id, repo, path ? path : "",
4728 show_changed_paths, show_diffstat, show_patch, search_pattern,
4729 diff_context, limit, log_branches, reverse_display_order,
4730 refs_idmap, one_line, tmpfile);
4736 got_worktree_close(worktree);
4738 const struct got_error *close_err = got_repo_close(repo);
4743 const struct got_error *pack_err =
4744 got_repo_pack_fds_close(pack_fds);
4749 got_reflist_object_id_map_free(refs_idmap);
4750 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4751 error = got_error_from_errno("fclose");
4752 got_ref_list_free(&refs);
4759 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4760 "[-r repository-path] [object1 object2 | path ...]\n",
4765 struct print_diff_arg {
4766 struct got_repository *repo;
4767 struct got_worktree *worktree;
4768 struct got_diffstat_cb_arg *diffstat;
4773 enum got_diff_algorithm diff_algo;
4774 int ignore_whitespace;
4775 int force_text_diff;
4782 * Create a file which contains the target path of a symlink so we can feed
4783 * it as content to the diff engine.
4785 static const struct got_error *
4786 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4787 const char *abspath)
4789 const struct got_error *err = NULL;
4790 char target_path[PATH_MAX];
4791 ssize_t target_len, outlen;
4796 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4797 if (target_len == -1)
4798 return got_error_from_errno2("readlinkat", abspath);
4800 target_len = readlink(abspath, target_path, PATH_MAX);
4801 if (target_len == -1)
4802 return got_error_from_errno2("readlink", abspath);
4805 *fd = got_opentempfd();
4807 return got_error_from_errno("got_opentempfd");
4809 outlen = write(*fd, target_path, target_len);
4811 err = got_error_from_errno("got_opentempfd");
4815 if (lseek(*fd, 0, SEEK_SET) == -1) {
4816 err = got_error_from_errno2("lseek", abspath);
4827 static const struct got_error *
4828 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4829 const char *path, struct got_object_id *blob_id,
4830 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4831 int dirfd, const char *de_name)
4833 struct print_diff_arg *a = arg;
4834 const struct got_error *err = NULL;
4835 struct got_blob_object *blob1 = NULL;
4836 int fd = -1, fd1 = -1, fd2 = -1;
4838 char *abspath = NULL, *label1 = NULL;
4843 memset(&sb, 0, sizeof(sb));
4845 if (a->diff_staged) {
4846 if (staged_status != GOT_STATUS_MODIFY &&
4847 staged_status != GOT_STATUS_ADD &&
4848 staged_status != GOT_STATUS_DELETE)
4851 if (staged_status == GOT_STATUS_DELETE)
4853 if (status == GOT_STATUS_NONEXISTENT)
4854 return got_error_set_errno(ENOENT, path);
4855 if (status != GOT_STATUS_MODIFY &&
4856 status != GOT_STATUS_ADD &&
4857 status != GOT_STATUS_DELETE &&
4858 status != GOT_STATUS_CONFLICT)
4862 err = got_opentemp_truncate(a->f1);
4864 return got_error_from_errno("got_opentemp_truncate");
4865 err = got_opentemp_truncate(a->f2);
4867 return got_error_from_errno("got_opentemp_truncate");
4869 if (!a->header_shown) {
4870 if (fprintf(a->outfile, "diff %s%s\n",
4871 a->diff_staged ? "-s " : "",
4872 got_worktree_get_root_path(a->worktree)) < 0) {
4873 err = got_error_from_errno("fprintf");
4876 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4877 err = got_error_from_errno("fprintf");
4880 if (fprintf(a->outfile, "path + %s%s\n",
4881 got_worktree_get_root_path(a->worktree),
4882 a->diff_staged ? " (staged changes)" : "") < 0) {
4883 err = got_error_from_errno("fprintf");
4886 a->header_shown = 1;
4889 if (a->diff_staged) {
4890 const char *label1 = NULL, *label2 = NULL;
4891 switch (staged_status) {
4892 case GOT_STATUS_MODIFY:
4896 case GOT_STATUS_ADD:
4899 case GOT_STATUS_DELETE:
4903 return got_error(GOT_ERR_FILE_STATUS);
4905 fd1 = got_opentempfd();
4907 err = got_error_from_errno("got_opentempfd");
4910 fd2 = got_opentempfd();
4912 err = got_error_from_errno("got_opentempfd");
4915 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4916 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4917 a->diff_algo, a->diff_context, a->ignore_whitespace,
4918 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4922 fd1 = got_opentempfd();
4924 err = got_error_from_errno("got_opentempfd");
4928 if (staged_status == GOT_STATUS_ADD ||
4929 staged_status == GOT_STATUS_MODIFY) {
4931 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4935 err = got_object_id_str(&id_str, staged_blob_id);
4938 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4939 err = got_error_from_errno("asprintf");
4944 } else if (status != GOT_STATUS_ADD) {
4945 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4951 if (status != GOT_STATUS_DELETE) {
4952 if (asprintf(&abspath, "%s/%s",
4953 got_worktree_get_root_path(a->worktree), path) == -1) {
4954 err = got_error_from_errno("asprintf");
4959 fd = openat(dirfd, de_name,
4960 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4962 if (!got_err_open_nofollow_on_symlink()) {
4963 err = got_error_from_errno2("openat",
4967 err = get_symlink_target_file(&fd, dirfd,
4973 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4975 if (!got_err_open_nofollow_on_symlink()) {
4976 err = got_error_from_errno2("open",
4980 err = get_symlink_target_file(&fd, dirfd,
4986 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4987 err = got_error_from_errno2("fstatat", abspath);
4990 f2 = fdopen(fd, "r");
4992 err = got_error_from_errno2("fdopen", abspath);
5000 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5006 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5007 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5008 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5010 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5011 err = got_error_from_errno("close");
5012 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5013 err = got_error_from_errno("close");
5015 got_object_blob_close(blob1);
5016 if (fd != -1 && close(fd) == -1 && err == NULL)
5017 err = got_error_from_errno("close");
5018 if (f2 && fclose(f2) == EOF && err == NULL)
5019 err = got_error_from_errno("fclose");
5024 static const struct got_error *
5025 cmd_diff(int argc, char *argv[])
5027 const struct got_error *error;
5028 struct got_repository *repo = NULL;
5029 struct got_worktree *worktree = NULL;
5030 char *cwd = NULL, *repo_path = NULL;
5031 const char *commit_args[2] = { NULL, NULL };
5032 int ncommit_args = 0;
5033 struct got_object_id *ids[2] = { NULL, NULL };
5034 char *labels[2] = { NULL, NULL };
5035 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5036 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5037 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5039 struct got_reflist_head refs;
5040 struct got_pathlist_head diffstat_paths, paths;
5041 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5042 int fd1 = -1, fd2 = -1;
5043 int *pack_fds = NULL;
5044 struct got_diffstat_cb_arg dsa;
5046 memset(&dsa, 0, sizeof(dsa));
5050 TAILQ_INIT(&diffstat_paths);
5053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5058 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5061 force_text_diff = 1;
5064 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5067 errx(1, "number of context lines is %s: %s",
5071 if (ncommit_args >= 2)
5072 errx(1, "too many -c options used");
5073 commit_args[ncommit_args++] = optarg;
5082 repo_path = realpath(optarg, NULL);
5083 if (repo_path == NULL)
5084 return got_error_from_errno2("realpath",
5086 got_path_strip_trailing_slashes(repo_path);
5093 ignore_whitespace = 1;
5104 cwd = getcwd(NULL, 0);
5106 error = got_error_from_errno("getcwd");
5110 error = got_repo_pack_fds_open(&pack_fds);
5114 if (repo_path == NULL) {
5115 error = got_worktree_open(&worktree, cwd);
5116 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5122 strdup(got_worktree_get_repo_path(worktree));
5123 if (repo_path == NULL) {
5124 error = got_error_from_errno("strdup");
5128 repo_path = strdup(cwd);
5129 if (repo_path == NULL) {
5130 error = got_error_from_errno("strdup");
5136 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5141 if (show_diffstat) {
5142 dsa.paths = &diffstat_paths;
5143 dsa.force_text = force_text_diff;
5144 dsa.ignore_ws = ignore_whitespace;
5145 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5148 if (rflag || worktree == NULL || ncommit_args > 0) {
5150 error = got_error_msg(GOT_ERR_NOT_IMPL,
5151 "-P option can only be used when diffing "
5156 error = got_error_msg(GOT_ERR_NOT_IMPL,
5157 "-s option can only be used when diffing "
5163 error = apply_unveil(got_repo_get_path(repo), 1,
5164 worktree ? got_worktree_get_root_path(worktree) : NULL);
5168 if ((!force_path && argc == 2) || ncommit_args > 0) {
5169 int obj_type = (ncommit_args > 0 ?
5170 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5171 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5175 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5177 if (ncommit_args > 0)
5178 arg = commit_args[i];
5181 error = got_repo_match_object_id(&ids[i], &labels[i],
5182 arg, obj_type, &refs, repo);
5184 if (error->code != GOT_ERR_NOT_REF &&
5185 error->code != GOT_ERR_NO_OBJ)
5187 if (ncommit_args > 0)
5195 f1 = got_opentemp();
5197 error = got_error_from_errno("got_opentemp");
5201 f2 = got_opentemp();
5203 error = got_error_from_errno("got_opentemp");
5207 outfile = got_opentemp();
5208 if (outfile == NULL) {
5209 error = got_error_from_errno("got_opentemp");
5213 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5214 struct print_diff_arg arg;
5217 if (worktree == NULL) {
5218 if (argc == 2 && ids[0] == NULL) {
5219 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5221 } else if (argc == 2 && ids[1] == NULL) {
5222 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5224 } else if (argc > 0) {
5225 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5226 "%s", "specified paths cannot be resolved");
5229 error = got_error(GOT_ERR_NOT_WORKTREE);
5234 error = get_worktree_paths_from_argv(&paths, argc, argv,
5239 error = got_object_id_str(&id_str,
5240 got_worktree_get_base_commit_id(worktree));
5244 arg.worktree = worktree;
5245 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5246 arg.diff_context = diff_context;
5247 arg.id_str = id_str;
5248 arg.header_shown = 0;
5249 arg.diff_staged = diff_staged;
5250 arg.ignore_whitespace = ignore_whitespace;
5251 arg.force_text_diff = force_text_diff;
5252 arg.diffstat = show_diffstat ? &dsa : NULL;
5255 arg.outfile = outfile;
5257 error = got_worktree_status(worktree, &paths, repo, 0,
5258 print_diff, &arg, check_cancelled, NULL);
5263 if (show_diffstat && dsa.nfiles > 0) {
5266 if (asprintf(&header, "diffstat %s%s",
5267 diff_staged ? "-s " : "",
5268 got_worktree_get_root_path(worktree)) == -1) {
5269 error = got_error_from_errno("asprintf");
5273 error = print_diffstat(&dsa, header);
5279 error = printfile(outfile);
5283 if (ncommit_args == 1) {
5284 struct got_commit_object *commit;
5285 error = got_object_open_as_commit(&commit, repo, ids[0]);
5289 labels[1] = labels[0];
5291 if (got_object_commit_get_nparents(commit) > 0) {
5292 const struct got_object_id_queue *pids;
5293 struct got_object_qid *pid;
5294 pids = got_object_commit_get_parent_ids(commit);
5295 pid = STAILQ_FIRST(pids);
5296 ids[0] = got_object_id_dup(&pid->id);
5297 if (ids[0] == NULL) {
5298 error = got_error_from_errno(
5299 "got_object_id_dup");
5300 got_object_commit_close(commit);
5303 error = got_object_id_str(&labels[0], ids[0]);
5305 got_object_commit_close(commit);
5310 labels[0] = strdup("/dev/null");
5311 if (labels[0] == NULL) {
5312 error = got_error_from_errno("strdup");
5313 got_object_commit_close(commit);
5318 got_object_commit_close(commit);
5321 if (ncommit_args == 0 && argc > 2) {
5322 error = got_error_msg(GOT_ERR_BAD_PATH,
5323 "path arguments cannot be used when diffing two objects");
5328 error = got_object_get_type(&type1, repo, ids[0]);
5333 error = got_object_get_type(&type2, repo, ids[1]);
5336 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5337 error = got_error(GOT_ERR_OBJ_TYPE);
5340 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5341 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5342 "path arguments cannot be used when diffing blobs");
5346 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5348 struct got_pathlist_entry *new;
5352 error = got_worktree_resolve_path(&p, worktree,
5356 prefix = got_worktree_get_path_prefix(worktree);
5357 while (prefix[0] == '/')
5359 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5360 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5362 error = got_error_from_errno("asprintf");
5368 char *mapped_path, *s;
5369 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5375 in_repo_path = strdup(s);
5376 if (in_repo_path == NULL) {
5377 error = got_error_from_errno("asprintf");
5384 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5385 if (error || new == NULL /* duplicate */)
5392 /* Release work tree lock. */
5393 got_worktree_close(worktree);
5397 fd1 = got_opentempfd();
5399 error = got_error_from_errno("got_opentempfd");
5403 fd2 = got_opentempfd();
5405 error = got_error_from_errno("got_opentempfd");
5409 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5410 case GOT_OBJ_TYPE_BLOB:
5411 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5412 fd1, fd2, ids[0], ids[1], NULL, NULL,
5413 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5414 ignore_whitespace, force_text_diff,
5415 show_diffstat ? &dsa : NULL, repo, outfile);
5417 case GOT_OBJ_TYPE_TREE:
5418 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5419 ids[0], ids[1], &paths, "", "",
5420 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5421 ignore_whitespace, force_text_diff,
5422 show_diffstat ? &dsa : NULL, repo, outfile);
5424 case GOT_OBJ_TYPE_COMMIT:
5425 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5426 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5427 fd1, fd2, ids[0], ids[1], &paths,
5428 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5429 ignore_whitespace, force_text_diff,
5430 show_diffstat ? &dsa : NULL, repo, outfile);
5433 error = got_error(GOT_ERR_OBJ_TYPE);
5438 if (show_diffstat && dsa.nfiles > 0) {
5439 char *header = NULL;
5441 if (asprintf(&header, "diffstat %s %s",
5442 labels[0], labels[1]) == -1) {
5443 error = got_error_from_errno("asprintf");
5447 error = print_diffstat(&dsa, header);
5453 error = printfile(outfile);
5461 got_worktree_close(worktree);
5463 const struct got_error *close_err = got_repo_close(repo);
5468 const struct got_error *pack_err =
5469 got_repo_pack_fds_close(pack_fds);
5473 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5474 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5475 got_ref_list_free(&refs);
5476 if (outfile && fclose(outfile) == EOF && error == NULL)
5477 error = got_error_from_errno("fclose");
5478 if (f1 && fclose(f1) == EOF && error == NULL)
5479 error = got_error_from_errno("fclose");
5480 if (f2 && fclose(f2) == EOF && error == NULL)
5481 error = got_error_from_errno("fclose");
5482 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5483 error = got_error_from_errno("close");
5484 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5485 error = got_error_from_errno("close");
5493 "usage: %s blame [-c commit] [-r repository-path] path\n",
5502 char datebuf[11]; /* YYYY-MM-DD + NUL */
5505 struct blame_cb_args {
5506 struct blame_line *lines;
5510 off_t *line_offsets;
5512 struct got_repository *repo;
5515 static const struct got_error *
5516 blame_cb(void *arg, int nlines, int lineno,
5517 struct got_commit_object *commit, struct got_object_id *id)
5519 const struct got_error *err = NULL;
5520 struct blame_cb_args *a = arg;
5521 struct blame_line *bline;
5523 size_t linesize = 0;
5526 time_t committer_time;
5528 if (nlines != a->nlines ||
5529 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5530 return got_error(GOT_ERR_RANGE);
5532 if (sigint_received)
5533 return got_error(GOT_ERR_ITER_COMPLETED);
5536 return NULL; /* no change in this commit */
5538 /* Annotate this line. */
5539 bline = &a->lines[lineno - 1];
5540 if (bline->annotated)
5542 err = got_object_id_str(&bline->id_str, id);
5546 bline->committer = strdup(got_object_commit_get_committer(commit));
5547 if (bline->committer == NULL) {
5548 err = got_error_from_errno("strdup");
5552 committer_time = got_object_commit_get_committer_time(commit);
5553 if (gmtime_r(&committer_time, &tm) == NULL)
5554 return got_error_from_errno("gmtime_r");
5555 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5557 err = got_error(GOT_ERR_NO_SPACE);
5560 bline->annotated = 1;
5562 /* Print lines annotated so far. */
5563 bline = &a->lines[a->lineno_cur - 1];
5564 if (!bline->annotated)
5567 offset = a->line_offsets[a->lineno_cur - 1];
5568 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5569 err = got_error_from_errno("fseeko");
5573 while (a->lineno_cur <= a->nlines && bline->annotated) {
5574 char *smallerthan, *at, *nl, *committer;
5577 if (getline(&line, &linesize, a->f) == -1) {
5579 err = got_error_from_errno("getline");
5583 committer = bline->committer;
5584 smallerthan = strchr(committer, '<');
5585 if (smallerthan && smallerthan[1] != '\0')
5586 committer = smallerthan + 1;
5587 at = strchr(committer, '@');
5590 len = strlen(committer);
5592 committer[8] = '\0';
5594 nl = strchr(line, '\n');
5597 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5598 bline->id_str, bline->datebuf, committer, line);
5601 bline = &a->lines[a->lineno_cur - 1];
5608 static const struct got_error *
5609 cmd_blame(int argc, char *argv[])
5611 const struct got_error *error;
5612 struct got_repository *repo = NULL;
5613 struct got_worktree *worktree = NULL;
5614 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5615 char *link_target = NULL;
5616 struct got_object_id *obj_id = NULL;
5617 struct got_object_id *commit_id = NULL;
5618 struct got_commit_object *commit = NULL;
5619 struct got_blob_object *blob = NULL;
5620 char *commit_id_str = NULL;
5621 struct blame_cb_args bca;
5622 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5624 int *pack_fds = NULL;
5625 FILE *f1 = NULL, *f2 = NULL;
5627 fd1 = got_opentempfd();
5629 return got_error_from_errno("got_opentempfd");
5631 memset(&bca, 0, sizeof(bca));
5634 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5639 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5642 commit_id_str = optarg;
5645 repo_path = realpath(optarg, NULL);
5646 if (repo_path == NULL)
5647 return got_error_from_errno2("realpath",
5649 got_path_strip_trailing_slashes(repo_path);
5665 cwd = getcwd(NULL, 0);
5667 error = got_error_from_errno("getcwd");
5671 error = got_repo_pack_fds_open(&pack_fds);
5675 if (repo_path == NULL) {
5676 error = got_worktree_open(&worktree, cwd);
5677 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 strdup(got_worktree_get_repo_path(worktree));
5684 if (repo_path == NULL) {
5685 error = got_error_from_errno("strdup");
5690 repo_path = strdup(cwd);
5691 if (repo_path == NULL) {
5692 error = got_error_from_errno("strdup");
5698 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5703 const char *prefix = got_worktree_get_path_prefix(worktree);
5706 error = got_worktree_resolve_path(&p, worktree, path);
5709 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5710 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5712 error = got_error_from_errno("asprintf");
5717 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5719 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5722 error = got_repo_map_path(&in_repo_path, repo, path);
5727 if (commit_id_str == NULL) {
5728 struct got_reference *head_ref;
5729 error = got_ref_open(&head_ref, repo, worktree ?
5730 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5733 error = got_ref_resolve(&commit_id, repo, head_ref);
5734 got_ref_close(head_ref);
5738 struct got_reflist_head refs;
5740 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5744 error = got_repo_match_object_id(&commit_id, NULL,
5745 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5746 got_ref_list_free(&refs);
5752 /* Release work tree lock. */
5753 got_worktree_close(worktree);
5757 error = got_object_open_as_commit(&commit, repo, commit_id);
5761 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5766 error = got_object_id_by_path(&obj_id, repo, commit,
5767 link_target ? link_target : in_repo_path);
5771 error = got_object_get_type(&obj_type, repo, obj_id);
5775 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5776 error = got_error_path(link_target ? link_target : in_repo_path,
5781 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5784 bca.f = got_opentemp();
5785 if (bca.f == NULL) {
5786 error = got_error_from_errno("got_opentemp");
5789 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5790 &bca.line_offsets, bca.f, blob);
5791 if (error || bca.nlines == 0)
5794 /* Don't include \n at EOF in the blame line count. */
5795 if (bca.line_offsets[bca.nlines - 1] == filesize)
5798 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5799 if (bca.lines == NULL) {
5800 error = got_error_from_errno("calloc");
5804 bca.nlines_prec = 0;
5812 fd2 = got_opentempfd();
5814 error = got_error_from_errno("got_opentempfd");
5817 fd3 = got_opentempfd();
5819 error = got_error_from_errno("got_opentempfd");
5822 f1 = got_opentemp();
5824 error = got_error_from_errno("got_opentemp");
5827 f2 = got_opentemp();
5829 error = got_error_from_errno("got_opentemp");
5832 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5833 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5834 check_cancelled, NULL, fd2, fd3, f1, f2);
5843 got_object_commit_close(commit);
5845 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5846 error = got_error_from_errno("close");
5847 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5848 error = got_error_from_errno("close");
5849 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5850 error = got_error_from_errno("close");
5851 if (f1 && fclose(f1) == EOF && error == NULL)
5852 error = got_error_from_errno("fclose");
5853 if (f2 && fclose(f2) == EOF && error == NULL)
5854 error = got_error_from_errno("fclose");
5857 got_object_blob_close(blob);
5859 got_worktree_close(worktree);
5861 const struct got_error *close_err = got_repo_close(repo);
5866 const struct got_error *pack_err =
5867 got_repo_pack_fds_close(pack_fds);
5872 for (i = 0; i < bca.nlines; i++) {
5873 struct blame_line *bline = &bca.lines[i];
5874 free(bline->id_str);
5875 free(bline->committer);
5879 free(bca.line_offsets);
5880 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5881 error = got_error_from_errno("fclose");
5888 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5889 "[path]\n", getprogname());
5893 static const struct got_error *
5894 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5895 const char *root_path, struct got_repository *repo)
5897 const struct got_error *err = NULL;
5898 int is_root_path = (strcmp(path, root_path) == 0);
5899 const char *modestr = "";
5900 mode_t mode = got_tree_entry_get_mode(te);
5901 char *link_target = NULL;
5903 path += strlen(root_path);
5904 while (path[0] == '/')
5907 if (got_object_tree_entry_is_submodule(te))
5909 else if (S_ISLNK(mode)) {
5912 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5915 for (i = 0; i < strlen(link_target); i++) {
5916 if (!isprint((unsigned char)link_target[i]))
5917 link_target[i] = '?';
5922 else if (S_ISDIR(mode))
5924 else if (mode & S_IXUSR)
5927 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5928 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5929 link_target ? " -> ": "", link_target ? link_target : "");
5935 static const struct got_error *
5936 print_tree(const char *path, struct got_commit_object *commit,
5937 int show_ids, int recurse, const char *root_path,
5938 struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 struct got_object_id *tree_id = NULL;
5942 struct got_tree_object *tree = NULL;
5945 err = got_object_id_by_path(&tree_id, repo, commit, path);
5949 err = got_object_open_as_tree(&tree, repo, tree_id);
5952 nentries = got_object_tree_get_nentries(tree);
5953 for (i = 0; i < nentries; i++) {
5954 struct got_tree_entry *te;
5957 if (sigint_received || sigpipe_received)
5960 te = got_object_tree_get_entry(tree, i);
5963 err = got_object_id_str(&id_str,
5964 got_tree_entry_get_id(te));
5967 if (asprintf(&id, "%s ", id_str) == -1) {
5968 err = got_error_from_errno("asprintf");
5974 err = print_entry(te, id, path, root_path, repo);
5979 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5981 if (asprintf(&child_path, "%s%s%s", path,
5982 path[0] == '/' && path[1] == '\0' ? "" : "/",
5983 got_tree_entry_get_name(te)) == -1) {
5984 err = got_error_from_errno("asprintf");
5987 err = print_tree(child_path, commit, show_ids, 1,
5996 got_object_tree_close(tree);
6001 static const struct got_error *
6002 cmd_tree(int argc, char *argv[])
6004 const struct got_error *error;
6005 struct got_repository *repo = NULL;
6006 struct got_worktree *worktree = NULL;
6007 const char *path, *refname = NULL;
6008 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6009 struct got_object_id *commit_id = NULL;
6010 struct got_commit_object *commit = NULL;
6011 char *commit_id_str = NULL;
6012 int show_ids = 0, recurse = 0;
6014 int *pack_fds = NULL;
6017 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6022 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6025 commit_id_str = optarg;
6034 repo_path = realpath(optarg, NULL);
6035 if (repo_path == NULL)
6036 return got_error_from_errno2("realpath",
6038 got_path_strip_trailing_slashes(repo_path);
6056 cwd = getcwd(NULL, 0);
6058 error = got_error_from_errno("getcwd");
6062 error = got_repo_pack_fds_open(&pack_fds);
6066 if (repo_path == NULL) {
6067 error = got_worktree_open(&worktree, cwd);
6068 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6074 strdup(got_worktree_get_repo_path(worktree));
6075 if (repo_path == NULL)
6076 error = got_error_from_errno("strdup");
6080 repo_path = strdup(cwd);
6081 if (repo_path == NULL) {
6082 error = got_error_from_errno("strdup");
6088 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6093 const char *prefix = got_worktree_get_path_prefix(worktree);
6098 error = got_worktree_resolve_path(&p, worktree, path);
6101 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6102 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6104 error = got_error_from_errno("asprintf");
6109 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6113 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6118 error = got_repo_map_path(&in_repo_path, repo, path);
6123 if (commit_id_str == NULL) {
6124 struct got_reference *head_ref;
6126 refname = got_worktree_get_head_ref_name(worktree);
6128 refname = GOT_REF_HEAD;
6129 error = got_ref_open(&head_ref, repo, refname, 0);
6132 error = got_ref_resolve(&commit_id, repo, head_ref);
6133 got_ref_close(head_ref);
6137 struct got_reflist_head refs;
6139 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6143 error = got_repo_match_object_id(&commit_id, NULL,
6144 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6145 got_ref_list_free(&refs);
6151 /* Release work tree lock. */
6152 got_worktree_close(worktree);
6156 error = got_object_open_as_commit(&commit, repo, commit_id);
6160 error = print_tree(in_repo_path, commit, show_ids, recurse,
6161 in_repo_path, repo);
6168 got_object_commit_close(commit);
6170 got_worktree_close(worktree);
6172 const struct got_error *close_err = got_repo_close(repo);
6177 const struct got_error *pack_err =
6178 got_repo_pack_fds_close(pack_fds);
6188 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6189 "[-s status-codes] [path ...]\n", getprogname());
6193 struct got_status_arg {
6198 static const struct got_error *
6199 print_status(void *arg, unsigned char status, unsigned char staged_status,
6200 const char *path, struct got_object_id *blob_id,
6201 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6202 int dirfd, const char *de_name)
6204 struct got_status_arg *st = arg;
6206 if (status == staged_status && (status == GOT_STATUS_DELETE))
6207 status = GOT_STATUS_NO_CHANGE;
6208 if (st != NULL && st->status_codes) {
6209 size_t ncodes = strlen(st->status_codes);
6212 for (i = 0; i < ncodes ; i++) {
6214 if (status == st->status_codes[i] ||
6215 staged_status == st->status_codes[i]) {
6220 if (status == st->status_codes[i] ||
6221 staged_status == st->status_codes[i])
6226 if (st->suppress && j == 0)
6233 printf("%c%c %s\n", status, staged_status, path);
6237 static const struct got_error *
6238 cmd_status(int argc, char *argv[])
6240 const struct got_error *error = NULL;
6241 struct got_repository *repo = NULL;
6242 struct got_worktree *worktree = NULL;
6243 struct got_status_arg st;
6245 struct got_pathlist_head paths;
6246 int ch, i, no_ignores = 0;
6247 int *pack_fds = NULL;
6251 memset(&st, 0, sizeof(st));
6252 st.status_codes = NULL;
6256 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6261 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6267 if (st.status_codes != NULL && st.suppress == 0)
6268 option_conflict('S', 's');
6272 for (i = 0; i < strlen(optarg); i++) {
6273 switch (optarg[i]) {
6274 case GOT_STATUS_MODIFY:
6275 case GOT_STATUS_ADD:
6276 case GOT_STATUS_DELETE:
6277 case GOT_STATUS_CONFLICT:
6278 case GOT_STATUS_MISSING:
6279 case GOT_STATUS_OBSTRUCTED:
6280 case GOT_STATUS_UNVERSIONED:
6281 case GOT_STATUS_MODE_CHANGE:
6282 case GOT_STATUS_NONEXISTENT:
6285 errx(1, "invalid status code '%c'",
6289 if (ch == 's' && st.suppress)
6290 option_conflict('s', 'S');
6291 st.status_codes = optarg;
6302 cwd = getcwd(NULL, 0);
6304 error = got_error_from_errno("getcwd");
6308 error = got_repo_pack_fds_open(&pack_fds);
6312 error = got_worktree_open(&worktree, cwd);
6314 if (error->code == GOT_ERR_NOT_WORKTREE)
6315 error = wrap_not_worktree_error(error, "status", cwd);
6319 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6324 error = apply_unveil(got_repo_get_path(repo), 1,
6325 got_worktree_get_root_path(worktree));
6329 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6333 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6334 print_status, &st, check_cancelled, NULL);
6337 const struct got_error *pack_err =
6338 got_repo_pack_fds_close(pack_fds);
6343 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6351 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6352 "[-s reference] [name]\n", getprogname());
6356 static const struct got_error *
6357 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6359 static const struct got_error *err = NULL;
6360 struct got_reflist_head refs;
6361 struct got_reflist_entry *re;
6364 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6365 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6370 TAILQ_FOREACH(re, &refs, entry) {
6372 refstr = got_ref_to_str(re->ref);
6373 if (refstr == NULL) {
6374 err = got_error_from_errno("got_ref_to_str");
6377 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6381 got_ref_list_free(&refs);
6385 static const struct got_error *
6386 delete_ref_by_name(struct got_repository *repo, const char *refname)
6388 const struct got_error *err;
6389 struct got_reference *ref;
6391 err = got_ref_open(&ref, repo, refname, 0);
6395 err = delete_ref(repo, ref);
6400 static const struct got_error *
6401 add_ref(struct got_repository *repo, const char *refname, const char *target)
6403 const struct got_error *err = NULL;
6404 struct got_object_id *id = NULL;
6405 struct got_reference *ref = NULL;
6406 struct got_reflist_head refs;
6409 * Don't let the user create a reference name with a leading '-'.
6410 * While technically a valid reference name, this case is usually
6411 * an unintended typo.
6413 if (refname[0] == '-')
6414 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6417 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6420 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6422 got_ref_list_free(&refs);
6426 err = got_ref_alloc(&ref, refname, id);
6430 err = got_ref_write(ref, repo);
6438 static const struct got_error *
6439 add_symref(struct got_repository *repo, const char *refname, const char *target)
6441 const struct got_error *err = NULL;
6442 struct got_reference *ref = NULL;
6443 struct got_reference *target_ref = NULL;
6446 * Don't let the user create a reference name with a leading '-'.
6447 * While technically a valid reference name, this case is usually
6448 * an unintended typo.
6450 if (refname[0] == '-')
6451 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6453 err = got_ref_open(&target_ref, repo, target, 0);
6457 err = got_ref_alloc_symref(&ref, refname, target_ref);
6461 err = got_ref_write(ref, repo);
6464 got_ref_close(target_ref);
6470 static const struct got_error *
6471 cmd_ref(int argc, char *argv[])
6473 const struct got_error *error = NULL;
6474 struct got_repository *repo = NULL;
6475 struct got_worktree *worktree = NULL;
6476 char *cwd = NULL, *repo_path = NULL;
6477 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6478 const char *obj_arg = NULL, *symref_target= NULL;
6479 char *refname = NULL;
6480 int *pack_fds = NULL;
6483 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6484 "sendfd unveil", NULL) == -1)
6488 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6500 repo_path = realpath(optarg, NULL);
6501 if (repo_path == NULL)
6502 return got_error_from_errno2("realpath",
6504 got_path_strip_trailing_slashes(repo_path);
6507 symref_target = optarg;
6518 if (obj_arg && do_list)
6519 option_conflict('c', 'l');
6520 if (obj_arg && do_delete)
6521 option_conflict('c', 'd');
6522 if (obj_arg && symref_target)
6523 option_conflict('c', 's');
6524 if (symref_target && do_delete)
6525 option_conflict('s', 'd');
6526 if (symref_target && do_list)
6527 option_conflict('s', 'l');
6528 if (do_delete && do_list)
6529 option_conflict('d', 'l');
6530 if (sort_by_time && !do_list)
6531 errx(1, "-t option requires -l option");
6537 if (argc != 0 && argc != 1)
6540 refname = strdup(argv[0]);
6541 if (refname == NULL) {
6542 error = got_error_from_errno("strdup");
6549 refname = strdup(argv[0]);
6550 if (refname == NULL) {
6551 error = got_error_from_errno("strdup");
6557 got_path_strip_trailing_slashes(refname);
6559 cwd = getcwd(NULL, 0);
6561 error = got_error_from_errno("getcwd");
6565 error = got_repo_pack_fds_open(&pack_fds);
6569 if (repo_path == NULL) {
6570 error = got_worktree_open(&worktree, cwd);
6571 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6577 strdup(got_worktree_get_repo_path(worktree));
6578 if (repo_path == NULL)
6579 error = got_error_from_errno("strdup");
6583 repo_path = strdup(cwd);
6584 if (repo_path == NULL) {
6585 error = got_error_from_errno("strdup");
6591 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6597 /* Remove "cpath" promise. */
6598 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6604 error = apply_unveil(got_repo_get_path(repo), do_list,
6605 worktree ? got_worktree_get_root_path(worktree) : NULL);
6610 error = list_refs(repo, refname, sort_by_time);
6612 error = delete_ref_by_name(repo, refname);
6613 else if (symref_target)
6614 error = add_symref(repo, refname, symref_target);
6616 if (obj_arg == NULL)
6618 error = add_ref(repo, refname, obj_arg);
6623 const struct got_error *close_err = got_repo_close(repo);
6628 got_worktree_close(worktree);
6630 const struct got_error *pack_err =
6631 got_repo_pack_fds_close(pack_fds);
6643 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6644 "[-r repository-path] [name]\n", getprogname());
6648 static const struct got_error *
6649 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6650 struct got_reference *ref)
6652 const struct got_error *err = NULL;
6653 const char *refname, *marker = " ";
6656 refname = got_ref_get_name(ref);
6657 if (worktree && strcmp(refname,
6658 got_worktree_get_head_ref_name(worktree)) == 0) {
6659 struct got_object_id *id = NULL;
6661 err = got_ref_resolve(&id, repo, ref);
6664 if (got_object_id_cmp(id,
6665 got_worktree_get_base_commit_id(worktree)) == 0)
6672 if (strncmp(refname, "refs/heads/", 11) == 0)
6674 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6676 if (strncmp(refname, "refs/remotes/", 13) == 0)
6679 refstr = got_ref_to_str(ref);
6681 return got_error_from_errno("got_ref_to_str");
6683 printf("%s%s: %s\n", marker, refname, refstr);
6688 static const struct got_error *
6689 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6691 const char *refname;
6693 if (worktree == NULL)
6694 return got_error(GOT_ERR_NOT_WORKTREE);
6696 refname = got_worktree_get_head_ref_name(worktree);
6698 if (strncmp(refname, "refs/heads/", 11) == 0)
6700 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6703 printf("%s\n", refname);
6708 static const struct got_error *
6709 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6712 static const struct got_error *err = NULL;
6713 struct got_reflist_head refs;
6714 struct got_reflist_entry *re;
6715 struct got_reference *temp_ref = NULL;
6716 int rebase_in_progress, histedit_in_progress;
6721 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6726 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6731 if (rebase_in_progress || histedit_in_progress) {
6732 err = got_ref_open(&temp_ref, repo,
6733 got_worktree_get_head_ref_name(worktree), 0);
6736 list_branch(repo, worktree, temp_ref);
6737 got_ref_close(temp_ref);
6741 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6742 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6747 TAILQ_FOREACH(re, &refs, entry)
6748 list_branch(repo, worktree, re->ref);
6750 got_ref_list_free(&refs);
6752 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6753 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6758 TAILQ_FOREACH(re, &refs, entry)
6759 list_branch(repo, worktree, re->ref);
6761 got_ref_list_free(&refs);
6766 static const struct got_error *
6767 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6768 const char *branch_name)
6770 const struct got_error *err = NULL;
6771 struct got_reference *ref = NULL;
6772 char *refname, *remote_refname = NULL;
6774 if (strncmp(branch_name, "refs/", 5) == 0)
6776 if (strncmp(branch_name, "heads/", 6) == 0)
6778 else if (strncmp(branch_name, "remotes/", 8) == 0)
6781 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6782 return got_error_from_errno("asprintf");
6784 if (asprintf(&remote_refname, "refs/remotes/%s",
6785 branch_name) == -1) {
6786 err = got_error_from_errno("asprintf");
6790 err = got_ref_open(&ref, repo, refname, 0);
6792 const struct got_error *err2;
6793 if (err->code != GOT_ERR_NOT_REF)
6796 * Keep 'err' intact such that if neither branch exists
6797 * we report "refs/heads" rather than "refs/remotes" in
6798 * our error message.
6800 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6807 strcmp(got_worktree_get_head_ref_name(worktree),
6808 got_ref_get_name(ref)) == 0) {
6809 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6810 "will not delete this work tree's current branch");
6814 err = delete_ref(repo, ref);
6819 free(remote_refname);
6823 static const struct got_error *
6824 add_branch(struct got_repository *repo, const char *branch_name,
6825 struct got_object_id *base_commit_id)
6827 const struct got_error *err = NULL;
6828 struct got_reference *ref = NULL;
6829 char *refname = NULL;
6832 * Don't let the user create a branch name with a leading '-'.
6833 * While technically a valid reference name, this case is usually
6834 * an unintended typo.
6836 if (branch_name[0] == '-')
6837 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6839 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6842 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6843 err = got_error_from_errno("asprintf");
6847 err = got_ref_open(&ref, repo, refname, 0);
6849 err = got_error(GOT_ERR_BRANCH_EXISTS);
6851 } else if (err->code != GOT_ERR_NOT_REF)
6854 err = got_ref_alloc(&ref, refname, base_commit_id);
6858 err = got_ref_write(ref, repo);
6866 static const struct got_error *
6867 cmd_branch(int argc, char *argv[])
6869 const struct got_error *error = NULL;
6870 struct got_repository *repo = NULL;
6871 struct got_worktree *worktree = NULL;
6872 char *cwd = NULL, *repo_path = NULL;
6873 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6874 const char *delref = NULL, *commit_id_arg = NULL;
6875 struct got_reference *ref = NULL;
6876 struct got_pathlist_head paths;
6877 struct got_object_id *commit_id = NULL;
6878 char *commit_id_str = NULL;
6879 int *pack_fds = NULL;
6884 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6885 "sendfd unveil", NULL) == -1)
6889 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6892 commit_id_arg = optarg;
6904 repo_path = realpath(optarg, NULL);
6905 if (repo_path == NULL)
6906 return got_error_from_errno2("realpath",
6908 got_path_strip_trailing_slashes(repo_path);
6919 if (do_list && delref)
6920 option_conflict('l', 'd');
6921 if (sort_by_time && !do_list)
6922 errx(1, "-t option requires -l option");
6927 if (!do_list && !delref && argc == 0)
6930 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6931 errx(1, "-c option can only be used when creating a branch");
6933 if (do_list || delref) {
6936 } else if (!do_show && argc != 1)
6939 cwd = getcwd(NULL, 0);
6941 error = got_error_from_errno("getcwd");
6945 error = got_repo_pack_fds_open(&pack_fds);
6949 if (repo_path == NULL) {
6950 error = got_worktree_open(&worktree, cwd);
6951 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6957 strdup(got_worktree_get_repo_path(worktree));
6958 if (repo_path == NULL)
6959 error = got_error_from_errno("strdup");
6963 repo_path = strdup(cwd);
6964 if (repo_path == NULL) {
6965 error = got_error_from_errno("strdup");
6971 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6976 if (do_list || do_show) {
6977 /* Remove "cpath" promise. */
6978 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6984 error = apply_unveil(got_repo_get_path(repo), do_list,
6985 worktree ? got_worktree_get_root_path(worktree) : NULL);
6990 error = show_current_branch(repo, worktree);
6992 error = list_branches(repo, worktree, sort_by_time);
6994 error = delete_branch(repo, worktree, delref);
6996 struct got_reflist_head refs;
6998 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7002 if (commit_id_arg == NULL)
7003 commit_id_arg = worktree ?
7004 got_worktree_get_head_ref_name(worktree) :
7006 error = got_repo_match_object_id(&commit_id, NULL,
7007 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7008 got_ref_list_free(&refs);
7011 error = add_branch(repo, argv[0], commit_id);
7014 if (worktree && do_update) {
7015 struct got_update_progress_arg upa;
7016 char *branch_refname = NULL;
7018 error = got_object_id_str(&commit_id_str, commit_id);
7021 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7025 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7027 error = got_error_from_errno("asprintf");
7030 error = got_ref_open(&ref, repo, branch_refname, 0);
7031 free(branch_refname);
7034 error = switch_head_ref(ref, commit_id, worktree,
7038 error = got_worktree_set_base_commit_id(worktree, repo,
7042 memset(&upa, 0, sizeof(upa));
7043 error = got_worktree_checkout_files(worktree, &paths,
7044 repo, update_progress, &upa, check_cancelled,
7048 if (upa.did_something) {
7049 printf("Updated to %s: %s\n",
7050 got_worktree_get_head_ref_name(worktree),
7053 print_update_progress_stats(&upa);
7060 const struct got_error *close_err = got_repo_close(repo);
7065 got_worktree_close(worktree);
7067 const struct got_error *pack_err =
7068 got_repo_pack_fds_close(pack_fds);
7075 free(commit_id_str);
7076 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7084 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7085 "[-r repository-path] [-s signer-id] name\n", getprogname());
7090 static const struct got_error *
7091 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7093 const struct got_error *err = NULL;
7094 struct got_reflist_entry *re, *se, *new;
7095 struct got_object_id *re_id, *se_id;
7096 struct got_tag_object *re_tag, *se_tag;
7097 time_t re_time, se_time;
7099 STAILQ_FOREACH(re, tags, entry) {
7100 se = STAILQ_FIRST(sorted);
7102 err = got_reflist_entry_dup(&new, re);
7105 STAILQ_INSERT_HEAD(sorted, new, entry);
7108 err = got_ref_resolve(&re_id, repo, re->ref);
7111 err = got_object_open_as_tag(&re_tag, repo, re_id);
7115 re_time = got_object_tag_get_tagger_time(re_tag);
7116 got_object_tag_close(re_tag);
7120 err = got_ref_resolve(&se_id, repo, re->ref);
7123 err = got_object_open_as_tag(&se_tag, repo, se_id);
7127 se_time = got_object_tag_get_tagger_time(se_tag);
7128 got_object_tag_close(se_tag);
7130 if (se_time > re_time) {
7131 err = got_reflist_entry_dup(&new, re);
7134 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7137 se = STAILQ_NEXT(se, entry);
7146 static const struct got_error *
7147 get_tag_refname(char **refname, const char *tag_name)
7149 const struct got_error *err;
7151 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7152 *refname = strdup(tag_name);
7153 if (*refname == NULL)
7154 return got_error_from_errno("strdup");
7155 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7156 err = got_error_from_errno("asprintf");
7164 static const struct got_error *
7165 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7166 const char *allowed_signers, const char *revoked_signers, int verbosity)
7168 static const struct got_error *err = NULL;
7169 struct got_reflist_head refs;
7170 struct got_reflist_entry *re;
7171 char *wanted_refname = NULL;
7176 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7181 struct got_reference *ref;
7182 err = get_tag_refname(&wanted_refname, tag_name);
7185 /* Wanted tag reference should exist. */
7186 err = got_ref_open(&ref, repo, wanted_refname, 0);
7192 TAILQ_FOREACH(re, &refs, entry) {
7193 const char *refname;
7194 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7196 const char *tagger, *ssh_sig = NULL;
7197 char *sig_msg = NULL;
7199 struct got_object_id *id;
7200 struct got_tag_object *tag;
7201 struct got_commit_object *commit = NULL;
7203 refname = got_ref_get_name(re->ref);
7204 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7205 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7208 refstr = got_ref_to_str(re->ref);
7209 if (refstr == NULL) {
7210 err = got_error_from_errno("got_ref_to_str");
7214 err = got_ref_resolve(&id, repo, re->ref);
7217 err = got_object_open_as_tag(&tag, repo, id);
7219 if (err->code != GOT_ERR_OBJ_TYPE) {
7223 /* "lightweight" tag */
7224 err = got_object_open_as_commit(&commit, repo, id);
7229 tagger = got_object_commit_get_committer(commit);
7231 got_object_commit_get_committer_time(commit);
7232 err = got_object_id_str(&id_str, id);
7238 tagger = got_object_tag_get_tagger(tag);
7239 tagger_time = got_object_tag_get_tagger_time(tag);
7240 err = got_object_id_str(&id_str,
7241 got_object_tag_get_object_id(tag));
7246 if (tag && verify_tags) {
7247 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7248 got_object_tag_get_message(tag));
7249 if (ssh_sig && allowed_signers == NULL) {
7250 err = got_error_msg(
7251 GOT_ERR_VERIFY_TAG_SIGNATURE,
7252 "SSH signature verification requires "
7253 "setting allowed_signers in "
7259 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7261 printf("from: %s\n", tagger);
7262 datestr = get_datestr(&tagger_time, datebuf);
7264 printf("date: %s UTC\n", datestr);
7266 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7268 switch (got_object_tag_get_object_type(tag)) {
7269 case GOT_OBJ_TYPE_BLOB:
7270 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7273 case GOT_OBJ_TYPE_TREE:
7274 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7277 case GOT_OBJ_TYPE_COMMIT:
7278 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7281 case GOT_OBJ_TYPE_TAG:
7282 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7292 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7293 allowed_signers, revoked_signers, verbosity);
7294 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7298 printf("signature: %s", sig_msg);
7304 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7307 got_object_commit_close(commit);
7309 tagmsg0 = strdup(got_object_tag_get_message(tag));
7310 got_object_tag_close(tag);
7311 if (tagmsg0 == NULL) {
7312 err = got_error_from_errno("strdup");
7319 line = strsep(&tagmsg, "\n");
7321 printf(" %s\n", line);
7326 got_ref_list_free(&refs);
7327 free(wanted_refname);
7329 if (err == NULL && bad_sigs)
7330 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7334 static const struct got_error *
7335 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7336 const char *tag_name, const char *repo_path)
7338 const struct got_error *err = NULL;
7339 char *template = NULL, *initial_content = NULL;
7340 char *editor = NULL;
7341 int initial_content_len;
7344 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7345 err = got_error_from_errno("asprintf");
7349 initial_content_len = asprintf(&initial_content,
7350 "\n# tagging commit %s as %s\n",
7351 commit_id_str, tag_name);
7352 if (initial_content_len == -1) {
7353 err = got_error_from_errno("asprintf");
7357 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7361 if (write(fd, initial_content, initial_content_len) == -1) {
7362 err = got_error_from_errno2("write", *tagmsg_path);
7366 err = get_editor(&editor);
7369 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7370 initial_content_len, 1);
7372 free(initial_content);
7376 if (fd != -1 && close(fd) == -1 && err == NULL)
7377 err = got_error_from_errno2("close", *tagmsg_path);
7386 static const struct got_error *
7387 add_tag(struct got_repository *repo, const char *tagger,
7388 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7389 const char *signer_id, int verbosity)
7391 const struct got_error *err = NULL;
7392 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7393 char *label = NULL, *commit_id_str = NULL;
7394 struct got_reference *ref = NULL;
7395 char *refname = NULL, *tagmsg = NULL;
7396 char *tagmsg_path = NULL, *tag_id_str = NULL;
7397 int preserve_tagmsg = 0;
7398 struct got_reflist_head refs;
7403 * Don't let the user create a tag name with a leading '-'.
7404 * While technically a valid reference name, this case is usually
7405 * an unintended typo.
7407 if (tag_name[0] == '-')
7408 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7410 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7414 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7415 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7419 err = got_object_id_str(&commit_id_str, commit_id);
7423 err = get_tag_refname(&refname, tag_name);
7426 if (strncmp("refs/tags/", tag_name, 10) == 0)
7429 err = got_ref_open(&ref, repo, refname, 0);
7431 err = got_error(GOT_ERR_TAG_EXISTS);
7433 } else if (err->code != GOT_ERR_NOT_REF)
7436 if (tagmsg_arg == NULL) {
7437 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7438 tag_name, got_repo_get_path(repo));
7440 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7441 tagmsg_path != NULL)
7442 preserve_tagmsg = 1;
7445 /* Editor is done; we can now apply unveil(2) */
7446 err = got_sigs_apply_unveil();
7449 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7454 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7455 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7459 preserve_tagmsg = 1;
7463 err = got_ref_alloc(&ref, refname, tag_id);
7466 preserve_tagmsg = 1;
7470 err = got_ref_write(ref, repo);
7473 preserve_tagmsg = 1;
7477 err = got_object_id_str(&tag_id_str, tag_id);
7480 preserve_tagmsg = 1;
7483 printf("Created tag %s\n", tag_id_str);
7485 if (preserve_tagmsg) {
7486 fprintf(stderr, "%s: tag message preserved in %s\n",
7487 getprogname(), tagmsg_path);
7488 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink", tagmsg_path);
7494 free(commit_id_str);
7498 got_ref_list_free(&refs);
7502 static const struct got_error *
7503 cmd_tag(int argc, char *argv[])
7505 const struct got_error *error = NULL;
7506 struct got_repository *repo = NULL;
7507 struct got_worktree *worktree = NULL;
7508 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7509 char *gitconfig_path = NULL, *tagger = NULL;
7510 char *allowed_signers = NULL, *revoked_signers = NULL;
7511 const char *signer_id = NULL;
7512 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7513 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7514 int *pack_fds = NULL;
7517 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7518 "sendfd unveil", NULL) == -1)
7522 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7525 commit_id_arg = optarg;
7534 repo_path = realpath(optarg, NULL);
7535 if (repo_path == NULL) {
7536 error = got_error_from_errno2("realpath",
7540 got_path_strip_trailing_slashes(repo_path);
7551 else if (verbosity < 3)
7563 if (do_list || verify_tags) {
7564 if (commit_id_arg != NULL)
7566 "-c option can only be used when creating a tag");
7569 option_conflict('l', 'm');
7571 option_conflict('V', 'm');
7575 option_conflict('l', 's');
7577 option_conflict('V', 's');
7581 } else if (argc != 1)
7587 cwd = getcwd(NULL, 0);
7589 error = got_error_from_errno("getcwd");
7593 error = got_repo_pack_fds_open(&pack_fds);
7597 if (repo_path == NULL) {
7598 error = got_worktree_open(&worktree, cwd);
7599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7605 strdup(got_worktree_get_repo_path(worktree));
7606 if (repo_path == NULL)
7607 error = got_error_from_errno("strdup");
7611 repo_path = strdup(cwd);
7612 if (repo_path == NULL) {
7613 error = got_error_from_errno("strdup");
7619 if (do_list || verify_tags) {
7620 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7623 error = get_allowed_signers(&allowed_signers, repo, worktree);
7626 error = get_revoked_signers(&revoked_signers, repo, worktree);
7630 /* Release work tree lock. */
7631 got_worktree_close(worktree);
7636 * Remove "cpath" promise unless needed for signature tmpfile
7640 got_sigs_apply_unveil();
7643 if (pledge("stdio rpath wpath flock proc exec sendfd "
7644 "unveil", NULL) == -1)
7648 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7651 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7652 revoked_signers, verbosity);
7654 error = get_gitconfig_path(&gitconfig_path);
7657 error = got_repo_open(&repo, repo_path, gitconfig_path,
7662 error = get_author(&tagger, repo, worktree);
7665 if (signer_id == NULL)
7666 signer_id = get_signer_id(repo, worktree);
7670 error = got_sigs_apply_unveil();
7674 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7679 if (commit_id_arg == NULL) {
7680 struct got_reference *head_ref;
7681 struct got_object_id *commit_id;
7682 error = got_ref_open(&head_ref, repo,
7683 worktree ? got_worktree_get_head_ref_name(worktree)
7687 error = got_ref_resolve(&commit_id, repo, head_ref);
7688 got_ref_close(head_ref);
7691 error = got_object_id_str(&commit_id_str, commit_id);
7698 /* Release work tree lock. */
7699 got_worktree_close(worktree);
7703 error = add_tag(repo, tagger, tag_name,
7704 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7705 signer_id, verbosity);
7709 const struct got_error *close_err = got_repo_close(repo);
7714 got_worktree_close(worktree);
7716 const struct got_error *pack_err =
7717 got_repo_pack_fds_close(pack_fds);
7723 free(gitconfig_path);
7724 free(commit_id_str);
7726 free(allowed_signers);
7727 free(revoked_signers);
7734 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7738 static const struct got_error *
7739 add_progress(void *arg, unsigned char status, const char *path)
7741 while (path[0] == '/')
7743 printf("%c %s\n", status, path);
7747 static const struct got_error *
7748 cmd_add(int argc, char *argv[])
7750 const struct got_error *error = NULL;
7751 struct got_repository *repo = NULL;
7752 struct got_worktree *worktree = NULL;
7754 struct got_pathlist_head paths;
7755 struct got_pathlist_entry *pe;
7756 int ch, can_recurse = 0, no_ignores = 0;
7757 int *pack_fds = NULL;
7762 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7767 while ((ch = getopt(argc, argv, "IR")) != -1) {
7787 cwd = getcwd(NULL, 0);
7789 error = got_error_from_errno("getcwd");
7793 error = got_repo_pack_fds_open(&pack_fds);
7797 error = got_worktree_open(&worktree, cwd);
7799 if (error->code == GOT_ERR_NOT_WORKTREE)
7800 error = wrap_not_worktree_error(error, "add", cwd);
7804 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7809 error = apply_unveil(got_repo_get_path(repo), 1,
7810 got_worktree_get_root_path(worktree));
7814 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7821 TAILQ_FOREACH(pe, &paths, entry) {
7822 if (asprintf(&ondisk_path, "%s/%s",
7823 got_worktree_get_root_path(worktree),
7825 error = got_error_from_errno("asprintf");
7828 if (lstat(ondisk_path, &sb) == -1) {
7829 if (errno == ENOENT) {
7833 error = got_error_from_errno2("lstat",
7839 if (S_ISDIR(sb.st_mode)) {
7840 error = got_error_msg(GOT_ERR_BAD_PATH,
7841 "adding directories requires -R option");
7847 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7848 NULL, repo, no_ignores);
7851 const struct got_error *close_err = got_repo_close(repo);
7856 got_worktree_close(worktree);
7858 const struct got_error *pack_err =
7859 got_repo_pack_fds_close(pack_fds);
7863 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7871 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7876 static const struct got_error *
7877 print_remove_status(void *arg, unsigned char status,
7878 unsigned char staged_status, const char *path)
7880 while (path[0] == '/')
7882 if (status == GOT_STATUS_NONEXISTENT)
7884 if (status == staged_status && (status == GOT_STATUS_DELETE))
7885 status = GOT_STATUS_NO_CHANGE;
7886 printf("%c%c %s\n", status, staged_status, path);
7890 static const struct got_error *
7891 cmd_remove(int argc, char *argv[])
7893 const struct got_error *error = NULL;
7894 struct got_worktree *worktree = NULL;
7895 struct got_repository *repo = NULL;
7896 const char *status_codes = NULL;
7898 struct got_pathlist_head paths;
7899 struct got_pathlist_entry *pe;
7900 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7901 int ignore_missing_paths = 0;
7902 int *pack_fds = NULL;
7907 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7912 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7915 delete_local_mods = 1;
7916 ignore_missing_paths = 1;
7925 for (i = 0; i < strlen(optarg); i++) {
7926 switch (optarg[i]) {
7927 case GOT_STATUS_MODIFY:
7928 delete_local_mods = 1;
7930 case GOT_STATUS_MISSING:
7931 ignore_missing_paths = 1;
7934 errx(1, "invalid status code '%c'",
7938 status_codes = optarg;
7952 cwd = getcwd(NULL, 0);
7954 error = got_error_from_errno("getcwd");
7958 error = got_repo_pack_fds_open(&pack_fds);
7962 error = got_worktree_open(&worktree, cwd);
7964 if (error->code == GOT_ERR_NOT_WORKTREE)
7965 error = wrap_not_worktree_error(error, "remove", cwd);
7969 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7974 error = apply_unveil(got_repo_get_path(repo), 1,
7975 got_worktree_get_root_path(worktree));
7979 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7986 TAILQ_FOREACH(pe, &paths, entry) {
7987 if (asprintf(&ondisk_path, "%s/%s",
7988 got_worktree_get_root_path(worktree),
7990 error = got_error_from_errno("asprintf");
7993 if (lstat(ondisk_path, &sb) == -1) {
7994 if (errno == ENOENT) {
7998 error = got_error_from_errno2("lstat",
8004 if (S_ISDIR(sb.st_mode)) {
8005 error = got_error_msg(GOT_ERR_BAD_PATH,
8006 "removing directories requires -R option");
8012 error = got_worktree_schedule_delete(worktree, &paths,
8013 delete_local_mods, status_codes, print_remove_status, NULL,
8014 repo, keep_on_disk, ignore_missing_paths);
8017 const struct got_error *close_err = got_repo_close(repo);
8022 got_worktree_close(worktree);
8024 const struct got_error *pack_err =
8025 got_repo_pack_fds_close(pack_fds);
8029 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8037 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8038 "[patchfile]\n", getprogname());
8042 static const struct got_error *
8043 patch_from_stdin(int *patchfd)
8045 const struct got_error *err = NULL;
8048 sig_t sighup, sigint, sigquit;
8050 *patchfd = got_opentempfd();
8052 return got_error_from_errno("got_opentempfd");
8054 sighup = signal(SIGHUP, SIG_DFL);
8055 sigint = signal(SIGINT, SIG_DFL);
8056 sigquit = signal(SIGQUIT, SIG_DFL);
8059 r = read(0, buf, sizeof(buf));
8061 err = got_error_from_errno("read");
8066 if (write(*patchfd, buf, r) == -1) {
8067 err = got_error_from_errno("write");
8072 signal(SIGHUP, sighup);
8073 signal(SIGINT, sigint);
8074 signal(SIGQUIT, sigquit);
8076 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8077 err = got_error_from_errno("lseek");
8087 static const struct got_error *
8088 patch_progress(void *arg, const char *old, const char *new,
8089 unsigned char status, const struct got_error *error, int old_from,
8090 int old_lines, int new_from, int new_lines, int offset,
8091 int ws_mangled, const struct got_error *hunk_err)
8093 const char *path = new == NULL ? old : new;
8095 while (*path == '/')
8099 printf("%c %s\n", status, path);
8102 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8104 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8105 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8106 old_lines, new_from, new_lines);
8107 if (hunk_err != NULL)
8108 printf("%s\n", hunk_err->msg);
8109 else if (offset != 0)
8110 printf("applied with offset %d\n", offset);
8112 printf("hunk contains mangled whitespace\n");
8118 static const struct got_error *
8119 cmd_patch(int argc, char *argv[])
8121 const struct got_error *error = NULL, *close_error = NULL;
8122 struct got_worktree *worktree = NULL;
8123 struct got_repository *repo = NULL;
8124 struct got_reflist_head refs;
8125 struct got_object_id *commit_id = NULL;
8126 const char *commit_id_str = NULL;
8130 int ch, nop = 0, strip = -1, reverse = 0;
8132 int *pack_fds = NULL;
8137 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8138 "unveil", NULL) == -1)
8142 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8145 commit_id_str = optarg;
8151 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8153 errx(1, "pathname strip count is %s: %s",
8169 error = patch_from_stdin(&patchfd);
8172 } else if (argc == 1) {
8173 patchfd = open(argv[0], O_RDONLY);
8174 if (patchfd == -1) {
8175 error = got_error_from_errno2("open", argv[0]);
8178 if (fstat(patchfd, &sb) == -1) {
8179 error = got_error_from_errno2("fstat", argv[0]);
8182 if (!S_ISREG(sb.st_mode)) {
8183 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8189 if ((cwd = getcwd(NULL, 0)) == NULL) {
8190 error = got_error_from_errno("getcwd");
8194 error = got_repo_pack_fds_open(&pack_fds);
8198 error = got_worktree_open(&worktree, cwd);
8202 const char *repo_path = got_worktree_get_repo_path(worktree);
8203 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8207 error = apply_unveil(got_repo_get_path(repo), 0,
8208 got_worktree_get_root_path(worktree));
8212 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8216 if (commit_id_str != NULL) {
8217 error = got_repo_match_object_id(&commit_id, NULL,
8218 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8223 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8224 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8227 got_ref_list_free(&refs);
8230 close_error = got_repo_close(repo);
8232 error = close_error;
8234 if (worktree != NULL) {
8235 close_error = got_worktree_close(worktree);
8237 error = close_error;
8240 const struct got_error *pack_err =
8241 got_repo_pack_fds_close(pack_fds);
8252 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8257 static const struct got_error *
8258 revert_progress(void *arg, unsigned char status, const char *path)
8260 if (status == GOT_STATUS_UNVERSIONED)
8263 while (path[0] == '/')
8265 printf("%c %s\n", status, path);
8269 struct choose_patch_arg {
8270 FILE *patch_script_file;
8274 static const struct got_error *
8275 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8276 int nchanges, const char *action)
8278 const struct got_error *err;
8280 size_t linesize = 0;
8284 case GOT_STATUS_ADD:
8285 printf("A %s\n%s this addition? [y/n] ", path, action);
8287 case GOT_STATUS_DELETE:
8288 printf("D %s\n%s this deletion? [y/n] ", path, action);
8290 case GOT_STATUS_MODIFY:
8291 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8292 return got_error_from_errno("fseek");
8293 printf(GOT_COMMIT_SEP_STR);
8294 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8296 if (linelen == -1 && ferror(patch_file)) {
8297 err = got_error_from_errno("getline");
8302 printf(GOT_COMMIT_SEP_STR);
8303 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8304 path, n, nchanges, action);
8307 return got_error_path(path, GOT_ERR_FILE_STATUS);
8314 static const struct got_error *
8315 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8316 FILE *patch_file, int n, int nchanges)
8318 const struct got_error *err = NULL;
8320 size_t linesize = 0;
8323 struct choose_patch_arg *a = arg;
8325 *choice = GOT_PATCH_CHOICE_NONE;
8327 if (a->patch_script_file) {
8329 err = show_change(status, path, patch_file, n, nchanges,
8333 linelen = getline(&line, &linesize, a->patch_script_file);
8334 if (linelen == -1) {
8335 if (ferror(a->patch_script_file))
8336 return got_error_from_errno("getline");
8339 nl = strchr(line, '\n');
8342 if (strcmp(line, "y") == 0) {
8343 *choice = GOT_PATCH_CHOICE_YES;
8345 } else if (strcmp(line, "n") == 0) {
8346 *choice = GOT_PATCH_CHOICE_NO;
8348 } else if (strcmp(line, "q") == 0 &&
8349 status == GOT_STATUS_MODIFY) {
8350 *choice = GOT_PATCH_CHOICE_QUIT;
8353 printf("invalid response '%s'\n", line);
8358 while (resp != 'y' && resp != 'n' && resp != 'q') {
8359 err = show_change(status, path, patch_file, n, nchanges,
8366 if (status == GOT_STATUS_MODIFY) {
8367 if (resp != 'y' && resp != 'n' && resp != 'q') {
8368 printf("invalid response '%c'\n", resp);
8371 } else if (resp != 'y' && resp != 'n') {
8372 printf("invalid response '%c'\n", resp);
8378 *choice = GOT_PATCH_CHOICE_YES;
8379 else if (resp == 'n')
8380 *choice = GOT_PATCH_CHOICE_NO;
8381 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8382 *choice = GOT_PATCH_CHOICE_QUIT;
8387 struct wt_commitable_path_arg {
8388 struct got_pathlist_head *commit_paths;
8393 * Shortcut work tree status callback to determine if the set of paths scanned
8394 * has at least one versioned path that is being modified and, if not NULL, is
8395 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8396 * soon as a path is passed with a status that satisfies this criteria.
8398 static const struct got_error *
8399 worktree_has_commitable_path(void *arg, unsigned char status,
8400 unsigned char staged_status, const char *path,
8401 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8402 struct got_object_id *commit_id, int dirfd, const char *de_name)
8404 struct wt_commitable_path_arg *a = arg;
8406 if (status == staged_status && (status == GOT_STATUS_DELETE))
8407 status = GOT_STATUS_NO_CHANGE;
8409 if (!(status == GOT_STATUS_NO_CHANGE ||
8410 status == GOT_STATUS_UNVERSIONED) ||
8411 staged_status != GOT_STATUS_NO_CHANGE) {
8412 if (a->commit_paths != NULL) {
8413 struct got_pathlist_entry *pe;
8415 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8416 if (strncmp(path, pe->path,
8417 pe->path_len) == 0) {
8418 *a->has_changes = 1;
8423 *a->has_changes = 1;
8425 if (*a->has_changes)
8426 return got_error(GOT_ERR_FILE_MODIFIED);
8433 * Check that the changeset of the commit identified by id is
8434 * comprised of at least one modified path that is being committed.
8436 static const struct got_error *
8437 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8438 struct got_object_id *id, struct got_worktree *worktree,
8439 struct got_repository *repo)
8441 const struct got_error *err;
8442 struct got_pathlist_head paths;
8443 struct got_commit_object *commit = NULL, *pcommit = NULL;
8444 struct got_tree_object *tree = NULL, *ptree = NULL;
8445 struct got_object_qid *pid;
8449 err = got_object_open_as_commit(&commit, repo, id);
8453 err = got_object_open_as_tree(&tree, repo,
8454 got_object_commit_get_tree_id(commit));
8458 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8460 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8464 err = got_object_open_as_tree(&ptree, repo,
8465 got_object_commit_get_tree_id(pcommit));
8470 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8471 got_diff_tree_collect_changed_paths, &paths, 0);
8475 err = got_worktree_status(worktree, &paths, repo, 0,
8476 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8477 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8479 * At least one changed path in the referenced commit is
8480 * modified in the work tree, that's all we need to know!
8486 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8488 got_object_commit_close(commit);
8490 got_object_commit_close(pcommit);
8492 got_object_tree_close(tree);
8494 got_object_tree_close(ptree);
8499 * Remove any "logmsg" reference comprised entirely of paths that have
8500 * been reverted in this work tree. If any path in the logmsg ref changeset
8501 * remains in a changed state in the worktree, do not remove the reference.
8503 static const struct got_error *
8504 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8506 const struct got_error *err;
8507 struct got_reflist_head refs;
8508 struct got_reflist_entry *re;
8509 struct got_commit_object *commit = NULL;
8510 struct got_object_id *commit_id = NULL;
8511 struct wt_commitable_path_arg wcpa;
8512 char *uuidstr = NULL;
8516 err = got_worktree_get_uuid(&uuidstr, worktree);
8520 err = got_ref_list(&refs, repo, "refs/got/worktree",
8521 got_ref_cmp_by_name, repo);
8525 TAILQ_FOREACH(re, &refs, entry) {
8526 const char *refname;
8527 int has_changes = 0;
8529 refname = got_ref_get_name(re->ref);
8531 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8532 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8533 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8534 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8535 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8536 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8540 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8541 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8545 err = got_repo_match_object_id(&commit_id, NULL, refname,
8546 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8550 err = got_object_open_as_commit(&commit, repo, commit_id);
8554 wcpa.commit_paths = NULL;
8555 wcpa.has_changes = &has_changes;
8557 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8563 err = got_ref_delete(re->ref, repo);
8568 got_object_commit_close(commit);
8577 got_ref_list_free(&refs);
8579 got_object_commit_close(commit);
8583 static const struct got_error *
8584 cmd_revert(int argc, char *argv[])
8586 const struct got_error *error = NULL;
8587 struct got_worktree *worktree = NULL;
8588 struct got_repository *repo = NULL;
8589 char *cwd = NULL, *path = NULL;
8590 struct got_pathlist_head paths;
8591 struct got_pathlist_entry *pe;
8592 int ch, can_recurse = 0, pflag = 0;
8593 FILE *patch_script_file = NULL;
8594 const char *patch_script_path = NULL;
8595 struct choose_patch_arg cpa;
8596 int *pack_fds = NULL;
8601 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8602 "unveil", NULL) == -1)
8606 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8609 patch_script_path = optarg;
8628 if (patch_script_path && !pflag)
8629 errx(1, "-F option can only be used together with -p option");
8631 cwd = getcwd(NULL, 0);
8633 error = got_error_from_errno("getcwd");
8637 error = got_repo_pack_fds_open(&pack_fds);
8641 error = got_worktree_open(&worktree, cwd);
8643 if (error->code == GOT_ERR_NOT_WORKTREE)
8644 error = wrap_not_worktree_error(error, "revert", cwd);
8648 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8653 if (patch_script_path) {
8654 patch_script_file = fopen(patch_script_path, "re");
8655 if (patch_script_file == NULL) {
8656 error = got_error_from_errno2("fopen",
8663 * XXX "c" perm needed on repo dir to delete merge references.
8665 error = apply_unveil(got_repo_get_path(repo), 0,
8666 got_worktree_get_root_path(worktree));
8670 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8677 TAILQ_FOREACH(pe, &paths, entry) {
8678 if (asprintf(&ondisk_path, "%s/%s",
8679 got_worktree_get_root_path(worktree),
8681 error = got_error_from_errno("asprintf");
8684 if (lstat(ondisk_path, &sb) == -1) {
8685 if (errno == ENOENT) {
8689 error = got_error_from_errno2("lstat",
8695 if (S_ISDIR(sb.st_mode)) {
8696 error = got_error_msg(GOT_ERR_BAD_PATH,
8697 "reverting directories requires -R option");
8703 cpa.patch_script_file = patch_script_file;
8704 cpa.action = "revert";
8705 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8706 pflag ? choose_patch : NULL, &cpa, repo);
8708 error = rm_logmsg_ref(worktree, repo);
8710 if (patch_script_file && fclose(patch_script_file) == EOF &&
8712 error = got_error_from_errno2("fclose", patch_script_path);
8714 const struct got_error *close_err = got_repo_close(repo);
8719 got_worktree_close(worktree);
8721 const struct got_error *pack_err =
8722 got_repo_pack_fds_close(pack_fds);
8726 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8735 fprintf(stderr, "usage: %s commit [-NnS] [-A author] [-F path] "
8736 "[-m message] [path ...]\n", getprogname());
8740 struct collect_commit_logmsg_arg {
8741 const char *cmdline_log;
8742 const char *prepared_log;
8743 const char *merged_log;
8744 int non_interactive;
8746 const char *worktree_path;
8747 const char *branch_name;
8748 const char *repo_path;
8753 static const struct got_error *
8754 read_prepared_logmsg(char **logmsg, const char *path)
8756 const struct got_error *err = NULL;
8762 memset(&sb, 0, sizeof(sb));
8764 f = fopen(path, "re");
8766 return got_error_from_errno2("fopen", path);
8768 if (fstat(fileno(f), &sb) == -1) {
8769 err = got_error_from_errno2("fstat", path);
8772 if (sb.st_size == 0) {
8773 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8777 *logmsg = malloc(sb.st_size + 1);
8778 if (*logmsg == NULL) {
8779 err = got_error_from_errno("malloc");
8783 r = fread(*logmsg, 1, sb.st_size, f);
8784 if (r != sb.st_size) {
8786 err = got_error_from_errno2("fread", path);
8788 err = got_error(GOT_ERR_IO);
8791 (*logmsg)[sb.st_size] = '\0';
8793 if (fclose(f) == EOF && err == NULL)
8794 err = got_error_from_errno2("fclose", path);
8802 static const struct got_error *
8803 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8804 const char *diff_path, char **logmsg, void *arg)
8806 char *initial_content = NULL;
8807 struct got_pathlist_entry *pe;
8808 const struct got_error *err = NULL;
8809 char *template = NULL;
8810 char *prepared_msg = NULL, *merged_msg = NULL;
8811 struct collect_commit_logmsg_arg *a = arg;
8812 int initial_content_len;
8816 /* if a message was specified on the command line, just use it */
8817 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8818 len = strlen(a->cmdline_log) + 1;
8819 *logmsg = malloc(len + 1);
8820 if (*logmsg == NULL)
8821 return got_error_from_errno("malloc");
8822 strlcpy(*logmsg, a->cmdline_log, len);
8824 } else if (a->prepared_log != NULL && a->non_interactive)
8825 return read_prepared_logmsg(logmsg, a->prepared_log);
8827 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8828 return got_error_from_errno("asprintf");
8830 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8834 if (a->prepared_log) {
8835 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8838 } else if (a->merged_log) {
8839 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8844 initial_content_len = asprintf(&initial_content,
8845 "%s%s\n# changes to be committed on branch %s:\n",
8846 prepared_msg ? prepared_msg : "",
8847 merged_msg ? merged_msg : "", a->branch_name);
8848 if (initial_content_len == -1) {
8849 err = got_error_from_errno("asprintf");
8853 if (write(fd, initial_content, initial_content_len) == -1) {
8854 err = got_error_from_errno2("write", a->logmsg_path);
8858 TAILQ_FOREACH(pe, commitable_paths, entry) {
8859 struct got_commitable *ct = pe->data;
8860 dprintf(fd, "# %c %s\n",
8861 got_commitable_get_status(ct),
8862 got_commitable_get_path(ct));
8866 dprintf(fd, "# detailed changes can be viewed in %s\n",
8870 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8871 initial_content_len, a->prepared_log ? 0 : 1);
8873 free(initial_content);
8878 if (fd != -1 && close(fd) == -1 && err == NULL)
8879 err = got_error_from_errno2("close", a->logmsg_path);
8881 /* Editor is done; we can now apply unveil(2) */
8883 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8891 static const struct got_error *
8892 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8893 const char *type, int has_content)
8895 const struct got_error *err = NULL;
8896 char *logmsg = NULL;
8898 err = got_object_commit_get_logmsg(&logmsg, commit);
8902 if (fprintf(f, "%s# log message of %s commit %s:%s",
8903 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8904 err = got_ferror(f, GOT_ERR_IO);
8911 * Lookup "logmsg" references of backed-out and cherrypicked commits
8912 * belonging to the current work tree. If found, and the worktree has
8913 * at least one modified file that was changed in the referenced commit,
8914 * add its log message to a new temporary file at *logmsg_path.
8915 * Add all refs found to matched_refs to be scheduled for removal on
8916 * successful commit.
8918 static const struct got_error *
8919 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8920 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8921 struct got_repository *repo)
8923 const struct got_error *err;
8924 struct got_commit_object *commit = NULL;
8925 struct got_object_id *id = NULL;
8926 struct got_reflist_head refs;
8927 struct got_reflist_entry *re, *re_match;
8929 char *uuidstr = NULL;
8930 int added_logmsg = 0;
8934 *logmsg_path = NULL;
8936 err = got_worktree_get_uuid(&uuidstr, worktree);
8940 err = got_ref_list(&refs, repo, "refs/got/worktree",
8941 got_ref_cmp_by_name, repo);
8945 TAILQ_FOREACH(re, &refs, entry) {
8946 const char *refname, *type;
8947 struct wt_commitable_path_arg wcpa;
8950 refname = got_ref_get_name(re->ref);
8952 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8953 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8954 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8955 type = "cherrypicked";
8956 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8957 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8958 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8959 type = "backed-out";
8963 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8964 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8968 err = got_repo_match_object_id(&id, NULL, refname,
8969 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8973 err = got_object_open_as_commit(&commit, repo, id);
8977 wcpa.commit_paths = paths;
8978 wcpa.has_changes = &add_logmsg;
8980 err = commit_path_changed_in_worktree(&wcpa, id,
8987 err = got_opentemp_named(logmsg_path, &f,
8988 "got-commit-logmsg", "");
8992 err = cat_logmsg(f, commit, refname, type,
8999 err = got_reflist_entry_dup(&re_match, re);
9002 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9005 got_object_commit_close(commit);
9014 got_ref_list_free(&refs);
9016 got_object_commit_close(commit);
9017 if (f && fclose(f) == EOF && err == NULL)
9018 err = got_error_from_errno("fclose");
9019 if (!added_logmsg) {
9020 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9021 err = got_error_from_errno2("unlink", *logmsg_path);
9022 *logmsg_path = NULL;
9027 static const struct got_error *
9028 cmd_commit(int argc, char *argv[])
9030 const struct got_error *error = NULL;
9031 struct got_worktree *worktree = NULL;
9032 struct got_repository *repo = NULL;
9033 char *cwd = NULL, *id_str = NULL;
9034 struct got_object_id *id = NULL;
9035 const char *logmsg = NULL;
9036 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9037 struct collect_commit_logmsg_arg cl_arg;
9038 const char *author = NULL;
9039 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9040 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9041 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9043 struct got_pathlist_head paths;
9044 struct got_reflist_head refs;
9045 struct got_reflist_entry *re;
9046 int *pack_fds = NULL;
9050 cl_arg.logmsg_path = NULL;
9053 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9054 "unveil", NULL) == -1)
9058 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9062 error = valid_author(author);
9068 option_conflict('F', 'm');
9069 prepared_logmsg = realpath(optarg, NULL);
9070 if (prepared_logmsg == NULL)
9071 return got_error_from_errno2("realpath",
9075 if (prepared_logmsg)
9076 option_conflict('m', 'F');
9080 non_interactive = 1;
9086 allow_bad_symlinks = 1;
9097 cwd = getcwd(NULL, 0);
9099 error = got_error_from_errno("getcwd");
9103 error = got_repo_pack_fds_open(&pack_fds);
9107 error = got_worktree_open(&worktree, cwd);
9109 if (error->code == GOT_ERR_NOT_WORKTREE)
9110 error = wrap_not_worktree_error(error, "commit", cwd);
9114 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9117 if (rebase_in_progress) {
9118 error = got_error(GOT_ERR_REBASING);
9122 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9127 error = get_gitconfig_path(&gitconfig_path);
9130 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9131 gitconfig_path, pack_fds);
9135 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9138 if (merge_in_progress) {
9139 error = got_error(GOT_ERR_MERGE_BUSY);
9143 error = get_author(&committer, repo, worktree);
9151 * unveil(2) traverses exec(2); if an editor is used we have
9152 * to apply unveil after the log message has been written.
9154 if (logmsg == NULL || strlen(logmsg) == 0)
9155 error = get_editor(&editor);
9157 error = apply_unveil(got_repo_get_path(repo), 0,
9158 got_worktree_get_root_path(worktree));
9162 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9166 if (prepared_logmsg == NULL) {
9167 error = lookup_logmsg_ref(&merged_logmsg,
9168 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9173 cl_arg.editor = editor;
9174 cl_arg.cmdline_log = logmsg;
9175 cl_arg.prepared_log = prepared_logmsg;
9176 cl_arg.merged_log = merged_logmsg;
9177 cl_arg.non_interactive = non_interactive;
9178 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9179 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9180 if (!histedit_in_progress) {
9181 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9182 error = got_error(GOT_ERR_COMMIT_BRANCH);
9185 cl_arg.branch_name += 11;
9187 cl_arg.repo_path = got_repo_get_path(repo);
9188 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9189 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9190 print_status, NULL, repo);
9192 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9193 cl_arg.logmsg_path != NULL)
9194 preserve_logmsg = 1;
9198 error = got_object_id_str(&id_str, id);
9201 printf("Created commit %s\n", id_str);
9203 TAILQ_FOREACH(re, &refs, entry) {
9204 error = got_ref_delete(re->ref, repo);
9210 if (preserve_logmsg) {
9211 fprintf(stderr, "%s: log message preserved in %s\n",
9212 getprogname(), cl_arg.logmsg_path);
9213 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9215 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9216 free(cl_arg.logmsg_path);
9217 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9218 error = got_error_from_errno2("unlink", merged_logmsg);
9219 free(merged_logmsg);
9221 const struct got_error *close_err = got_repo_close(repo);
9226 got_worktree_close(worktree);
9228 const struct got_error *pack_err =
9229 got_repo_pack_fds_close(pack_fds);
9233 got_ref_list_free(&refs);
9234 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9237 free(gitconfig_path);
9240 free(prepared_logmsg);
9247 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9248 "[-r repository-path] [-t tag] [remote-repository]\n",
9254 print_load_info(int print_colored, int print_found, int print_trees,
9255 int ncolored, int nfound, int ntrees)
9257 if (print_colored) {
9258 printf("%d commit%s colored", ncolored,
9259 ncolored == 1 ? "" : "s");
9262 printf("%s%d object%s found",
9263 ncolored > 0 ? "; " : "",
9264 nfound, nfound == 1 ? "" : "s");
9267 printf("; %d tree%s scanned", ntrees,
9268 ntrees == 1 ? "" : "s");
9272 struct got_send_progress_arg {
9273 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9280 int last_nobj_total;
9284 int printed_something;
9286 struct got_pathlist_head *delete_branches;
9289 static const struct got_error *
9290 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9291 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9292 int nobj_written, off_t bytes_sent, const char *refname,
9293 const char *errmsg, int success)
9295 struct got_send_progress_arg *a = arg;
9296 char scaled_packsize[FMT_SCALED_STRSIZE];
9297 char scaled_sent[FMT_SCALED_STRSIZE];
9298 int p_deltify = 0, p_written = 0, p_sent = 0;
9299 int print_colored = 0, print_found = 0, print_trees = 0;
9300 int print_searching = 0, print_total = 0;
9301 int print_deltify = 0, print_written = 0, print_sent = 0;
9303 if (a->verbosity < 0)
9307 const char *status = success ? "accepted" : "rejected";
9310 struct got_pathlist_entry *pe;
9311 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9312 const char *branchname = pe->path;
9313 if (got_path_cmp(branchname, refname,
9314 strlen(branchname), strlen(refname)) == 0) {
9316 a->sent_something = 1;
9322 if (a->printed_something)
9324 printf("Server has %s %s", status, refname);
9326 printf(": %s", errmsg);
9327 a->printed_something = 1;
9331 if (a->last_ncolored != ncolored) {
9333 a->last_ncolored = ncolored;
9336 if (a->last_nfound != nfound) {
9339 a->last_nfound = nfound;
9342 if (a->last_ntrees != ntrees) {
9346 a->last_ntrees = ntrees;
9349 if ((print_colored || print_found || print_trees) &&
9352 print_load_info(print_colored, print_found, print_trees,
9353 ncolored, nfound, ntrees);
9354 a->printed_something = 1;
9357 } else if (!a->loading_done) {
9359 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9361 a->loading_done = 1;
9364 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9365 return got_error_from_errno("fmt_scaled");
9366 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9367 return got_error_from_errno("fmt_scaled");
9369 if (a->last_ncommits != ncommits) {
9370 print_searching = 1;
9371 a->last_ncommits = ncommits;
9374 if (a->last_nobj_total != nobj_total) {
9375 print_searching = 1;
9377 a->last_nobj_total = nobj_total;
9380 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9381 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9382 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9383 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9384 return got_error(GOT_ERR_NO_SPACE);
9387 if (nobj_deltify > 0 || nobj_written > 0) {
9388 if (nobj_deltify > 0) {
9389 p_deltify = (nobj_deltify * 100) / nobj_total;
9390 if (p_deltify != a->last_p_deltify) {
9391 a->last_p_deltify = p_deltify;
9392 print_searching = 1;
9397 if (nobj_written > 0) {
9398 p_written = (nobj_written * 100) / nobj_total;
9399 if (p_written != a->last_p_written) {
9400 a->last_p_written = p_written;
9401 print_searching = 1;
9409 if (bytes_sent > 0) {
9410 p_sent = (bytes_sent * 100) / packfile_size;
9411 if (p_sent != a->last_p_sent) {
9412 a->last_p_sent = p_sent;
9413 print_searching = 1;
9419 a->sent_something = 1;
9422 if (print_searching || print_total || print_deltify || print_written ||
9425 if (print_searching)
9426 printf("packing %d reference%s", ncommits,
9427 ncommits == 1 ? "" : "s");
9429 printf("; %d object%s", nobj_total,
9430 nobj_total == 1 ? "" : "s");
9432 printf("; deltify: %d%%", p_deltify);
9434 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9435 scaled_packsize, p_sent);
9436 else if (print_written)
9437 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9438 scaled_packsize, p_written);
9439 if (print_searching || print_total || print_deltify ||
9440 print_written || print_sent) {
9441 a->printed_something = 1;
9447 static const struct got_error *
9448 cmd_send(int argc, char *argv[])
9450 const struct got_error *error = NULL;
9451 char *cwd = NULL, *repo_path = NULL;
9452 const char *remote_name;
9453 char *proto = NULL, *host = NULL, *port = NULL;
9454 char *repo_name = NULL, *server_path = NULL;
9455 const struct got_remote_repo *remotes, *remote = NULL;
9456 int nremotes, nbranches = 0, ndelete_branches = 0;
9457 struct got_repository *repo = NULL;
9458 struct got_worktree *worktree = NULL;
9459 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9460 struct got_pathlist_head branches;
9461 struct got_pathlist_head tags;
9462 struct got_reflist_head all_branches;
9463 struct got_reflist_head all_tags;
9464 struct got_pathlist_head delete_args;
9465 struct got_pathlist_head delete_branches;
9466 struct got_reflist_entry *re;
9467 struct got_pathlist_entry *pe;
9468 int i, ch, sendfd = -1, sendstatus;
9470 struct got_send_progress_arg spa;
9471 int verbosity = 0, overwrite_refs = 0;
9472 int send_all_branches = 0, send_all_tags = 0;
9473 struct got_reference *ref = NULL;
9474 int *pack_fds = NULL;
9476 TAILQ_INIT(&branches);
9478 TAILQ_INIT(&all_branches);
9479 TAILQ_INIT(&all_tags);
9480 TAILQ_INIT(&delete_args);
9481 TAILQ_INIT(&delete_branches);
9483 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9486 send_all_branches = 1;
9489 error = got_pathlist_append(&branches, optarg, NULL);
9495 error = got_pathlist_append(&delete_args, optarg, NULL);
9506 repo_path = realpath(optarg, NULL);
9507 if (repo_path == NULL)
9508 return got_error_from_errno2("realpath",
9510 got_path_strip_trailing_slashes(repo_path);
9516 error = got_pathlist_append(&tags, optarg, NULL);
9523 else if (verbosity < 3)
9534 if (send_all_branches && !TAILQ_EMPTY(&branches))
9535 option_conflict('a', 'b');
9536 if (send_all_tags && !TAILQ_EMPTY(&tags))
9537 option_conflict('T', 't');
9541 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9543 remote_name = argv[0];
9547 cwd = getcwd(NULL, 0);
9549 error = got_error_from_errno("getcwd");
9553 error = got_repo_pack_fds_open(&pack_fds);
9557 if (repo_path == NULL) {
9558 error = got_worktree_open(&worktree, cwd);
9559 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9565 strdup(got_worktree_get_repo_path(worktree));
9566 if (repo_path == NULL)
9567 error = got_error_from_errno("strdup");
9571 repo_path = strdup(cwd);
9572 if (repo_path == NULL) {
9573 error = got_error_from_errno("strdup");
9579 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9584 worktree_conf = got_worktree_get_gotconfig(worktree);
9585 if (worktree_conf) {
9586 got_gotconfig_get_remotes(&nremotes, &remotes,
9588 for (i = 0; i < nremotes; i++) {
9589 if (strcmp(remotes[i].name, remote_name) == 0) {
9590 remote = &remotes[i];
9596 if (remote == NULL) {
9597 repo_conf = got_repo_get_gotconfig(repo);
9599 got_gotconfig_get_remotes(&nremotes, &remotes,
9601 for (i = 0; i < nremotes; i++) {
9602 if (strcmp(remotes[i].name, remote_name) == 0) {
9603 remote = &remotes[i];
9609 if (remote == NULL) {
9610 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9611 for (i = 0; i < nremotes; i++) {
9612 if (strcmp(remotes[i].name, remote_name) == 0) {
9613 remote = &remotes[i];
9618 if (remote == NULL) {
9619 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9623 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9624 &repo_name, remote->send_url);
9628 if (strcmp(proto, "git") == 0) {
9630 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9631 "sendfd dns inet unveil", NULL) == -1)
9634 } else if (strcmp(proto, "git+ssh") == 0 ||
9635 strcmp(proto, "ssh") == 0) {
9637 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9638 "sendfd unveil", NULL) == -1)
9641 } else if (strcmp(proto, "http") == 0 ||
9642 strcmp(proto, "git+http") == 0) {
9643 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9646 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9650 error = got_dial_apply_unveil(proto);
9654 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9658 if (send_all_branches) {
9659 error = got_ref_list(&all_branches, repo, "refs/heads",
9660 got_ref_cmp_by_name, NULL);
9663 TAILQ_FOREACH(re, &all_branches, entry) {
9664 const char *branchname = got_ref_get_name(re->ref);
9665 error = got_pathlist_append(&branches,
9671 } else if (nbranches == 0) {
9672 for (i = 0; i < remote->nsend_branches; i++) {
9673 error = got_pathlist_append(&branches,
9674 remote->send_branches[i], NULL);
9680 if (send_all_tags) {
9681 error = got_ref_list(&all_tags, repo, "refs/tags",
9682 got_ref_cmp_by_name, NULL);
9685 TAILQ_FOREACH(re, &all_tags, entry) {
9686 const char *tagname = got_ref_get_name(re->ref);
9687 error = got_pathlist_append(&tags,
9695 * To prevent accidents only branches in refs/heads/ can be deleted
9696 * with 'got send -d'.
9697 * Deleting anything else requires local repository access or Git.
9699 TAILQ_FOREACH(pe, &delete_args, entry) {
9700 const char *branchname = pe->path;
9702 struct got_pathlist_entry *new;
9703 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9704 s = strdup(branchname);
9706 error = got_error_from_errno("strdup");
9710 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9711 error = got_error_from_errno("asprintf");
9715 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9716 if (error || new == NULL /* duplicate */)
9723 if (nbranches == 0 && ndelete_branches == 0) {
9724 struct got_reference *head_ref;
9726 error = got_ref_open(&head_ref, repo,
9727 got_worktree_get_head_ref_name(worktree), 0);
9729 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9732 if (got_ref_is_symbolic(head_ref)) {
9733 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9734 got_ref_close(head_ref);
9739 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9746 if (verbosity >= 0) {
9747 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9748 remote->name, proto, host,
9749 port ? ":" : "", port ? port : "",
9750 *server_path == '/' ? "" : "/", server_path);
9753 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9754 server_path, verbosity);
9758 memset(&spa, 0, sizeof(spa));
9759 spa.last_scaled_packsize[0] = '\0';
9760 spa.last_p_deltify = -1;
9761 spa.last_p_written = -1;
9762 spa.verbosity = verbosity;
9763 spa.delete_branches = &delete_branches;
9764 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9765 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9766 check_cancelled, NULL);
9767 if (spa.printed_something)
9771 if (!spa.sent_something && verbosity >= 0)
9772 printf("Already up-to-date\n");
9775 if (kill(sendpid, SIGTERM) == -1)
9776 error = got_error_from_errno("kill");
9777 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9778 error = got_error_from_errno("waitpid");
9780 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9781 error = got_error_from_errno("close");
9783 const struct got_error *close_err = got_repo_close(repo);
9788 got_worktree_close(worktree);
9790 const struct got_error *pack_err =
9791 got_repo_pack_fds_close(pack_fds);
9797 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9798 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9799 got_ref_list_free(&all_branches);
9800 got_ref_list_free(&all_tags);
9801 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9802 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9814 * Print and if delete is set delete all ref_prefix references.
9815 * If wanted_ref is not NULL, only print or delete this reference.
9817 static const struct got_error *
9818 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9819 const char *wanted_ref, int delete, struct got_worktree *worktree,
9820 struct got_repository *repo)
9822 const struct got_error *err;
9823 struct got_pathlist_head paths;
9824 struct got_reflist_head refs;
9825 struct got_reflist_entry *re;
9826 struct got_reflist_object_id_map *refs_idmap = NULL;
9827 struct got_commit_object *commit = NULL;
9828 struct got_object_id *id = NULL;
9829 const char *header_prefix;
9830 char *uuidstr = NULL;
9836 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9840 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9844 if (worktree != NULL) {
9845 err = got_worktree_get_uuid(&uuidstr, worktree);
9851 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9855 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9856 header_prefix = "backout";
9858 header_prefix = "cherrypick";
9860 TAILQ_FOREACH(re, &refs, entry) {
9861 const char *refname, *wt;
9863 refname = got_ref_get_name(re->ref);
9865 err = check_cancelled(NULL);
9869 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9870 refname += prefix_len + 1; /* skip '-' delimiter */
9876 if (worktree == NULL || strncmp(refname, uuidstr,
9877 GOT_WORKTREE_UUID_STRLEN) == 0)
9878 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9882 err = got_repo_match_object_id(&id, NULL, refname,
9883 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9887 err = got_object_open_as_commit(&commit, repo, id);
9892 found = strncmp(wanted_ref, refname,
9893 strlen(wanted_ref)) == 0;
9894 if (wanted_ref && !found) {
9895 struct got_reflist_head *ci_refs;
9897 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9901 char *refs_str = NULL;
9902 char const *r = NULL;
9904 err = build_refs_str(&refs_str, ci_refs, id,
9911 if (strncmp(r, wanted_ref,
9912 strlen(wanted_ref)) == 0) {
9924 if (wanted_ref == NULL || found) {
9926 err = got_ref_delete(re->ref, repo);
9929 printf("Deleted: ");
9930 err = print_commit_oneline(commit, id, repo,
9934 * Print paths modified by commit to help
9935 * associate commits with worktree changes.
9937 err = get_changed_paths(&paths, commit,
9942 err = print_commit(commit, id, repo, NULL,
9943 &paths, NULL, 0, 0, refs_idmap, NULL,
9945 got_pathlist_free(&paths,
9946 GOT_PATHLIST_FREE_ALL);
9948 if (worktree == NULL)
9949 printf("work tree: %.*s\n\n",
9950 GOT_WORKTREE_UUID_STRLEN, wt);
9956 got_object_commit_close(commit);
9962 if (wanted_ref != NULL && !found)
9963 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
9968 got_ref_list_free(&refs);
9969 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
9971 got_reflist_object_id_map_free(refs_idmap);
9973 got_object_commit_close(commit);
9978 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
9979 * identified by id for log messages to prepopulate the editor on commit.
9981 static const struct got_error *
9982 logmsg_ref(struct got_object_id *id, const char *prefix,
9983 struct got_worktree *worktree, struct got_repository *repo)
9985 const struct got_error *err = NULL;
9986 char *idstr, *ref = NULL, *refname = NULL;
9987 int histedit_in_progress;
9988 int rebase_in_progress, merge_in_progress;
9991 * Silenty refuse to create merge reference if any histedit, merge,
9992 * or rebase operation is in progress.
9994 err = got_worktree_histedit_in_progress(&histedit_in_progress,
9998 if (histedit_in_progress)
10001 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10004 if (rebase_in_progress)
10007 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10011 if (merge_in_progress)
10014 err = got_object_id_str(&idstr, id);
10018 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10022 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10023 err = got_error_from_errno("asprintf");
10027 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10037 usage_cherrypick(void)
10039 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10044 static const struct got_error *
10045 cmd_cherrypick(int argc, char *argv[])
10047 const struct got_error *error = NULL;
10048 struct got_worktree *worktree = NULL;
10049 struct got_repository *repo = NULL;
10050 char *cwd = NULL, *commit_id_str = NULL;
10051 struct got_object_id *commit_id = NULL;
10052 struct got_commit_object *commit = NULL;
10053 struct got_object_qid *pid;
10054 int ch, list_refs = 0, remove_refs = 0;
10055 struct got_update_progress_arg upa;
10056 int *pack_fds = NULL;
10059 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10060 "unveil", NULL) == -1)
10064 while ((ch = getopt(argc, argv, "lX")) != -1) {
10073 usage_cherrypick();
10081 if (list_refs || remove_refs) {
10082 if (argc != 0 && argc != 1)
10083 usage_cherrypick();
10084 } else if (argc != 1)
10085 usage_cherrypick();
10086 if (list_refs && remove_refs)
10087 option_conflict('l', 'X');
10089 cwd = getcwd(NULL, 0);
10091 error = got_error_from_errno("getcwd");
10095 error = got_repo_pack_fds_open(&pack_fds);
10099 error = got_worktree_open(&worktree, cwd);
10101 if (list_refs || remove_refs) {
10102 if (error->code != GOT_ERR_NOT_WORKTREE)
10105 if (error->code == GOT_ERR_NOT_WORKTREE)
10106 error = wrap_not_worktree_error(error,
10107 "cherrypick", cwd);
10112 error = got_repo_open(&repo,
10113 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10118 error = apply_unveil(got_repo_get_path(repo), 0,
10119 worktree ? got_worktree_get_root_path(worktree) : NULL);
10123 if (list_refs || remove_refs) {
10124 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10125 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10126 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10130 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10131 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10134 error = got_object_id_str(&commit_id_str, commit_id);
10138 error = got_object_open_as_commit(&commit, repo, commit_id);
10141 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10142 memset(&upa, 0, sizeof(upa));
10143 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10144 commit_id, repo, update_progress, &upa, check_cancelled,
10149 if (upa.did_something) {
10150 error = logmsg_ref(commit_id,
10151 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10154 printf("Merged commit %s\n", commit_id_str);
10156 print_merge_progress_stats(&upa);
10160 got_object_commit_close(commit);
10161 free(commit_id_str);
10163 got_worktree_close(worktree);
10165 const struct got_error *close_err = got_repo_close(repo);
10170 const struct got_error *pack_err =
10171 got_repo_pack_fds_close(pack_fds);
10180 usage_backout(void)
10182 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10186 static const struct got_error *
10187 cmd_backout(int argc, char *argv[])
10189 const struct got_error *error = NULL;
10190 struct got_worktree *worktree = NULL;
10191 struct got_repository *repo = NULL;
10192 char *cwd = NULL, *commit_id_str = NULL;
10193 struct got_object_id *commit_id = NULL;
10194 struct got_commit_object *commit = NULL;
10195 struct got_object_qid *pid;
10196 int ch, list_refs = 0, remove_refs = 0;
10197 struct got_update_progress_arg upa;
10198 int *pack_fds = NULL;
10201 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10202 "unveil", NULL) == -1)
10206 while ((ch = getopt(argc, argv, "lX")) != -1) {
10223 if (list_refs || remove_refs) {
10224 if (argc != 0 && argc != 1)
10226 } else if (argc != 1)
10228 if (list_refs && remove_refs)
10229 option_conflict('l', 'X');
10231 cwd = getcwd(NULL, 0);
10233 error = got_error_from_errno("getcwd");
10237 error = got_repo_pack_fds_open(&pack_fds);
10241 error = got_worktree_open(&worktree, cwd);
10243 if (list_refs || remove_refs) {
10244 if (error->code != GOT_ERR_NOT_WORKTREE)
10247 if (error->code == GOT_ERR_NOT_WORKTREE)
10248 error = wrap_not_worktree_error(error,
10254 error = got_repo_open(&repo,
10255 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10260 error = apply_unveil(got_repo_get_path(repo), 0,
10261 worktree ? got_worktree_get_root_path(worktree) : NULL);
10265 if (list_refs || remove_refs) {
10266 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10267 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10268 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10272 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10273 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10276 error = got_object_id_str(&commit_id_str, commit_id);
10280 error = got_object_open_as_commit(&commit, repo, commit_id);
10283 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10285 error = got_error(GOT_ERR_ROOT_COMMIT);
10289 memset(&upa, 0, sizeof(upa));
10290 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10291 repo, update_progress, &upa, check_cancelled, NULL);
10295 if (upa.did_something) {
10296 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10300 printf("Backed out commit %s\n", commit_id_str);
10302 print_merge_progress_stats(&upa);
10306 got_object_commit_close(commit);
10307 free(commit_id_str);
10309 got_worktree_close(worktree);
10311 const struct got_error *close_err = got_repo_close(repo);
10316 const struct got_error *pack_err =
10317 got_repo_pack_fds_close(pack_fds);
10327 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10332 trim_logmsg(char *logmsg, int limit)
10337 len = strlen(logmsg);
10340 logmsg[len] = '\0';
10341 nl = strchr(logmsg, '\n');
10346 static const struct got_error *
10347 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10349 const struct got_error *err;
10350 char *logmsg0 = NULL;
10353 err = got_object_commit_get_logmsg(&logmsg0, commit);
10358 while (isspace((unsigned char)s[0]))
10361 *logmsg = strdup(s);
10362 if (*logmsg == NULL) {
10363 err = got_error_from_errno("strdup");
10367 trim_logmsg(*logmsg, limit);
10373 static const struct got_error *
10374 show_rebase_merge_conflict(struct got_object_id *id,
10375 struct got_repository *repo)
10377 const struct got_error *err;
10378 struct got_commit_object *commit = NULL;
10379 char *id_str = NULL, *logmsg = NULL;
10381 err = got_object_open_as_commit(&commit, repo, id);
10385 err = got_object_id_str(&id_str, id);
10391 err = get_short_logmsg(&logmsg, 42, commit);
10395 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10398 got_object_commit_close(commit);
10403 static const struct got_error *
10404 show_rebase_progress(struct got_commit_object *commit,
10405 struct got_object_id *old_id, struct got_object_id *new_id)
10407 const struct got_error *err;
10408 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10410 err = got_object_id_str(&old_id_str, old_id);
10415 err = got_object_id_str(&new_id_str, new_id);
10420 old_id_str[12] = '\0';
10422 new_id_str[12] = '\0';
10424 err = get_short_logmsg(&logmsg, 42, commit);
10428 printf("%s -> %s: %s\n", old_id_str,
10429 new_id_str ? new_id_str : "no-op change", logmsg);
10437 static const struct got_error *
10438 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10439 struct got_reference *branch, struct got_reference *tmp_branch,
10440 struct got_repository *repo, int create_backup)
10442 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10443 return got_worktree_rebase_complete(worktree, fileindex,
10444 tmp_branch, branch, repo, create_backup);
10447 static const struct got_error *
10448 rebase_commit(struct got_pathlist_head *merged_paths,
10449 struct got_worktree *worktree, struct got_fileindex *fileindex,
10450 struct got_reference *tmp_branch, const char *committer,
10451 struct got_object_id *commit_id, struct got_repository *repo)
10453 const struct got_error *error;
10454 struct got_commit_object *commit;
10455 struct got_object_id *new_commit_id;
10457 error = got_object_open_as_commit(&commit, repo, commit_id);
10461 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10462 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10465 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10467 error = show_rebase_progress(commit, commit_id, NULL);
10469 error = show_rebase_progress(commit, commit_id, new_commit_id);
10470 free(new_commit_id);
10473 got_object_commit_close(commit);
10477 struct check_path_prefix_arg {
10478 const char *path_prefix;
10483 static const struct got_error *
10484 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10485 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10486 struct got_object_id *id1, struct got_object_id *id2,
10487 const char *path1, const char *path2,
10488 mode_t mode1, mode_t mode2, struct got_repository *repo)
10490 struct check_path_prefix_arg *a = arg;
10492 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10493 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10494 return got_error(a->errcode);
10499 static const struct got_error *
10500 check_path_prefix(struct got_object_id *parent_id,
10501 struct got_object_id *commit_id, const char *path_prefix,
10502 int errcode, struct got_repository *repo)
10504 const struct got_error *err;
10505 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10506 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10507 struct check_path_prefix_arg cpp_arg;
10509 if (got_path_is_root_dir(path_prefix))
10512 err = got_object_open_as_commit(&commit, repo, commit_id);
10516 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10520 err = got_object_open_as_tree(&tree1, repo,
10521 got_object_commit_get_tree_id(parent_commit));
10525 err = got_object_open_as_tree(&tree2, repo,
10526 got_object_commit_get_tree_id(commit));
10530 cpp_arg.path_prefix = path_prefix;
10531 while (cpp_arg.path_prefix[0] == '/')
10532 cpp_arg.path_prefix++;
10533 cpp_arg.len = strlen(cpp_arg.path_prefix);
10534 cpp_arg.errcode = errcode;
10535 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10536 check_path_prefix_in_diff, &cpp_arg, 0);
10539 got_object_tree_close(tree1);
10541 got_object_tree_close(tree2);
10543 got_object_commit_close(commit);
10545 got_object_commit_close(parent_commit);
10549 static const struct got_error *
10550 collect_commits(struct got_object_id_queue *commits,
10551 struct got_object_id *initial_commit_id,
10552 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10553 const char *path_prefix, int path_prefix_errcode,
10554 struct got_repository *repo)
10556 const struct got_error *err = NULL;
10557 struct got_commit_graph *graph = NULL;
10558 struct got_object_id parent_id, commit_id;
10559 struct got_object_qid *qid;
10561 err = got_commit_graph_open(&graph, "/", 1);
10565 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10566 check_cancelled, NULL);
10570 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10571 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10572 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10573 check_cancelled, NULL);
10575 if (err->code == GOT_ERR_ITER_COMPLETED) {
10576 err = got_error_msg(GOT_ERR_ANCESTRY,
10577 "ran out of commits to rebase before "
10578 "youngest common ancestor commit has "
10579 "been reached?!?");
10583 err = check_path_prefix(&parent_id, &commit_id,
10584 path_prefix, path_prefix_errcode, repo);
10588 err = got_object_qid_alloc(&qid, &commit_id);
10591 STAILQ_INSERT_HEAD(commits, qid, entry);
10593 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10597 got_commit_graph_close(graph);
10601 static const struct got_error *
10602 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10604 const struct got_error *err = NULL;
10605 time_t committer_time;
10607 char datebuf[11]; /* YYYY-MM-DD + NUL */
10608 char *author0 = NULL, *author, *smallerthan;
10609 char *logmsg0 = NULL, *logmsg, *newline;
10611 committer_time = got_object_commit_get_committer_time(commit);
10612 if (gmtime_r(&committer_time, &tm) == NULL)
10613 return got_error_from_errno("gmtime_r");
10614 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10615 return got_error(GOT_ERR_NO_SPACE);
10617 author0 = strdup(got_object_commit_get_author(commit));
10618 if (author0 == NULL)
10619 return got_error_from_errno("strdup");
10621 smallerthan = strchr(author, '<');
10622 if (smallerthan && smallerthan[1] != '\0')
10623 author = smallerthan + 1;
10624 author[strcspn(author, "@>")] = '\0';
10626 err = got_object_commit_get_logmsg(&logmsg0, commit);
10630 while (*logmsg == '\n')
10632 newline = strchr(logmsg, '\n');
10636 if (asprintf(brief_str, "%s %s %s",
10637 datebuf, author, logmsg) == -1)
10638 err = got_error_from_errno("asprintf");
10645 static const struct got_error *
10646 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10647 struct got_repository *repo)
10649 const struct got_error *err;
10652 err = got_object_id_str(&id_str, id);
10656 err = got_ref_delete(ref, repo);
10660 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10666 static const struct got_error *
10667 print_backup_ref(const char *branch_name, const char *new_id_str,
10668 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10669 struct got_reflist_object_id_map *refs_idmap,
10670 struct got_repository *repo)
10672 const struct got_error *err = NULL;
10673 struct got_reflist_head *refs;
10674 char *refs_str = NULL;
10675 struct got_object_id *new_commit_id = NULL;
10676 struct got_commit_object *new_commit = NULL;
10677 char *new_commit_brief_str = NULL;
10678 struct got_object_id *yca_id = NULL;
10679 struct got_commit_object *yca_commit = NULL;
10680 char *yca_id_str = NULL, *yca_brief_str = NULL;
10681 char *custom_refs_str;
10683 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10684 return got_error_from_errno("asprintf");
10686 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10687 0, 0, refs_idmap, custom_refs_str, NULL);
10691 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10695 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10697 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10702 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10706 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10710 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10711 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10715 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10716 refs_str ? " (" : "", refs_str ? refs_str : "",
10717 refs_str ? ")" : "", new_commit_brief_str);
10718 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10719 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10723 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10727 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10731 err = got_object_id_str(&yca_id_str, yca_id);
10735 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10737 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10741 printf("history forked at %s%s%s%s\n %s\n",
10743 refs_str ? " (" : "", refs_str ? refs_str : "",
10744 refs_str ? ")" : "", yca_brief_str);
10747 free(custom_refs_str);
10748 free(new_commit_id);
10752 free(yca_brief_str);
10754 got_object_commit_close(new_commit);
10756 got_object_commit_close(yca_commit);
10761 static const struct got_error *
10762 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10763 struct got_repository *repo)
10765 const struct got_error *err;
10766 struct got_reflist_head refs;
10767 struct got_reflist_entry *re;
10768 char *uuidstr = NULL;
10769 static char msg[160];
10773 err = got_worktree_get_uuid(&uuidstr, worktree);
10777 err = got_ref_list(&refs, repo, "refs/got/worktree",
10778 got_ref_cmp_by_name, repo);
10782 TAILQ_FOREACH(re, &refs, entry) {
10783 const char *cmd, *refname, *type;
10785 refname = got_ref_get_name(re->ref);
10787 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10788 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10789 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10790 cmd = "cherrypick";
10791 type = "cherrypicked";
10792 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10793 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10794 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10796 type = "backed-out";
10800 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10803 snprintf(msg, sizeof(msg),
10804 "work tree has references created by %s commits which "
10805 "must be removed with 'got %s -X' before running the %s "
10806 "command", type, cmd, caller);
10807 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10813 got_ref_list_free(&refs);
10817 static const struct got_error *
10818 process_backup_refs(const char *backup_ref_prefix,
10819 const char *wanted_branch_name,
10820 int delete, struct got_repository *repo)
10822 const struct got_error *err;
10823 struct got_reflist_head refs, backup_refs;
10824 struct got_reflist_entry *re;
10825 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10826 struct got_object_id *old_commit_id = NULL;
10827 char *branch_name = NULL;
10828 struct got_commit_object *old_commit = NULL;
10829 struct got_reflist_object_id_map *refs_idmap = NULL;
10830 int wanted_branch_found = 0;
10833 TAILQ_INIT(&backup_refs);
10835 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10839 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10843 if (wanted_branch_name) {
10844 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10845 wanted_branch_name += 11;
10848 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10849 got_ref_cmp_by_commit_timestamp_descending, repo);
10853 TAILQ_FOREACH(re, &backup_refs, entry) {
10854 const char *refname = got_ref_get_name(re->ref);
10857 err = check_cancelled(NULL);
10861 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10865 err = got_object_open_as_commit(&old_commit, repo,
10870 if (strncmp(backup_ref_prefix, refname,
10871 backup_ref_prefix_len) == 0)
10872 refname += backup_ref_prefix_len;
10874 while (refname[0] == '/')
10877 branch_name = strdup(refname);
10878 if (branch_name == NULL) {
10879 err = got_error_from_errno("strdup");
10882 slash = strrchr(branch_name, '/');
10885 refname += strlen(branch_name) + 1;
10888 if (wanted_branch_name == NULL ||
10889 strcmp(wanted_branch_name, branch_name) == 0) {
10890 wanted_branch_found = 1;
10892 err = delete_backup_ref(re->ref,
10893 old_commit_id, repo);
10895 err = print_backup_ref(branch_name, refname,
10896 old_commit_id, old_commit, refs_idmap,
10903 free(old_commit_id);
10904 old_commit_id = NULL;
10906 branch_name = NULL;
10907 got_object_commit_close(old_commit);
10911 if (wanted_branch_name && !wanted_branch_found) {
10912 err = got_error_fmt(GOT_ERR_NOT_REF,
10913 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10917 got_reflist_object_id_map_free(refs_idmap);
10918 got_ref_list_free(&refs);
10919 got_ref_list_free(&backup_refs);
10920 free(old_commit_id);
10923 got_object_commit_close(old_commit);
10927 static const struct got_error *
10928 abort_progress(void *arg, unsigned char status, const char *path)
10931 * Unversioned files should not clutter progress output when
10932 * an operation is aborted.
10934 if (status == GOT_STATUS_UNVERSIONED)
10937 return update_progress(arg, status, path);
10940 static const struct got_error *
10941 cmd_rebase(int argc, char *argv[])
10943 const struct got_error *error = NULL;
10944 struct got_worktree *worktree = NULL;
10945 struct got_repository *repo = NULL;
10946 struct got_fileindex *fileindex = NULL;
10947 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10948 struct got_reference *branch = NULL;
10949 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10950 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10951 struct got_object_id *resume_commit_id = NULL;
10952 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10953 struct got_object_id *head_commit_id = NULL;
10954 struct got_reference *head_ref = NULL;
10955 struct got_commit_object *commit = NULL;
10956 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10957 int histedit_in_progress = 0, merge_in_progress = 0;
10958 int create_backup = 1, list_backups = 0, delete_backups = 0;
10959 struct got_object_id_queue commits;
10960 struct got_pathlist_head merged_paths;
10961 const struct got_object_id_queue *parent_ids;
10962 struct got_object_qid *qid, *pid;
10963 struct got_update_progress_arg upa;
10964 int *pack_fds = NULL;
10966 STAILQ_INIT(&commits);
10967 TAILQ_INIT(&merged_paths);
10968 memset(&upa, 0, sizeof(upa));
10971 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10972 "unveil", NULL) == -1)
10976 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10982 continue_rebase = 1;
10988 delete_backups = 1;
10999 if (list_backups) {
11001 option_conflict('l', 'a');
11002 if (continue_rebase)
11003 option_conflict('l', 'c');
11004 if (delete_backups)
11005 option_conflict('l', 'X');
11006 if (argc != 0 && argc != 1)
11008 } else if (delete_backups) {
11010 option_conflict('X', 'a');
11011 if (continue_rebase)
11012 option_conflict('X', 'c');
11014 option_conflict('l', 'X');
11015 if (argc != 0 && argc != 1)
11018 if (abort_rebase && continue_rebase)
11020 else if (abort_rebase || continue_rebase) {
11023 } else if (argc != 1)
11027 cwd = getcwd(NULL, 0);
11029 error = got_error_from_errno("getcwd");
11033 error = got_repo_pack_fds_open(&pack_fds);
11037 error = got_worktree_open(&worktree, cwd);
11039 if (list_backups || delete_backups) {
11040 if (error->code != GOT_ERR_NOT_WORKTREE)
11043 if (error->code == GOT_ERR_NOT_WORKTREE)
11044 error = wrap_not_worktree_error(error,
11050 error = get_gitconfig_path(&gitconfig_path);
11053 error = got_repo_open(&repo,
11054 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11055 gitconfig_path, pack_fds);
11059 if (worktree != NULL && !list_backups && !delete_backups) {
11060 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11065 error = get_author(&committer, repo, worktree);
11066 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11069 error = apply_unveil(got_repo_get_path(repo), 0,
11070 worktree ? got_worktree_get_root_path(worktree) : NULL);
11074 if (list_backups || delete_backups) {
11075 error = process_backup_refs(
11076 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11077 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11078 goto done; /* nothing else to do */
11081 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11085 if (histedit_in_progress) {
11086 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11090 error = got_worktree_merge_in_progress(&merge_in_progress,
11094 if (merge_in_progress) {
11095 error = got_error(GOT_ERR_MERGE_BUSY);
11099 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11103 if (abort_rebase) {
11104 if (!rebase_in_progress) {
11105 error = got_error(GOT_ERR_NOT_REBASING);
11108 error = got_worktree_rebase_continue(&resume_commit_id,
11109 &new_base_branch, &tmp_branch, &branch, &fileindex,
11113 printf("Switching work tree to %s\n",
11114 got_ref_get_symref_target(new_base_branch));
11115 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11116 new_base_branch, abort_progress, &upa);
11119 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11120 print_merge_progress_stats(&upa);
11121 goto done; /* nothing else to do */
11124 if (continue_rebase) {
11125 if (!rebase_in_progress) {
11126 error = got_error(GOT_ERR_NOT_REBASING);
11129 error = got_worktree_rebase_continue(&resume_commit_id,
11130 &new_base_branch, &tmp_branch, &branch, &fileindex,
11135 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11136 committer, resume_commit_id, repo);
11140 yca_id = got_object_id_dup(resume_commit_id);
11141 if (yca_id == NULL) {
11142 error = got_error_from_errno("got_object_id_dup");
11146 error = got_ref_open(&branch, repo, argv[0], 0);
11149 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11150 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11151 "will not rebase a branch which lives outside "
11152 "the \"refs/heads/\" reference namespace");
11157 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11161 if (!continue_rebase) {
11162 struct got_object_id *base_commit_id;
11164 error = got_ref_open(&head_ref, repo,
11165 got_worktree_get_head_ref_name(worktree), 0);
11168 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11171 base_commit_id = got_worktree_get_base_commit_id(worktree);
11172 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11173 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11177 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11178 base_commit_id, branch_head_commit_id, 1, repo,
11179 check_cancelled, NULL);
11181 if (error->code == GOT_ERR_ANCESTRY) {
11182 error = got_error_msg(GOT_ERR_ANCESTRY,
11183 "specified branch shares no common "
11184 "ancestry with work tree's branch");
11189 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11191 if (error->code != GOT_ERR_ANCESTRY)
11195 struct got_pathlist_head paths;
11196 printf("%s is already based on %s\n",
11197 got_ref_get_name(branch),
11198 got_worktree_get_head_ref_name(worktree));
11199 error = switch_head_ref(branch, branch_head_commit_id,
11203 error = got_worktree_set_base_commit_id(worktree, repo,
11204 branch_head_commit_id);
11207 TAILQ_INIT(&paths);
11208 error = got_pathlist_append(&paths, "", NULL);
11211 error = got_worktree_checkout_files(worktree,
11212 &paths, repo, update_progress, &upa,
11213 check_cancelled, NULL);
11214 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11217 if (upa.did_something) {
11219 error = got_object_id_str(&id_str,
11220 branch_head_commit_id);
11223 printf("Updated to %s: %s\n",
11224 got_worktree_get_head_ref_name(worktree),
11228 printf("Already up-to-date\n");
11229 print_update_progress_stats(&upa);
11234 commit_id = branch_head_commit_id;
11235 error = got_object_open_as_commit(&commit, repo, commit_id);
11239 parent_ids = got_object_commit_get_parent_ids(commit);
11240 pid = STAILQ_FIRST(parent_ids);
11242 error = collect_commits(&commits, commit_id, &pid->id,
11243 yca_id, got_worktree_get_path_prefix(worktree),
11244 GOT_ERR_REBASE_PATH, repo);
11249 got_object_commit_close(commit);
11252 if (!continue_rebase) {
11253 error = got_worktree_rebase_prepare(&new_base_branch,
11254 &tmp_branch, &fileindex, worktree, branch, repo);
11259 if (STAILQ_EMPTY(&commits)) {
11260 if (continue_rebase) {
11261 error = rebase_complete(worktree, fileindex,
11262 branch, tmp_branch, repo, create_backup);
11265 /* Fast-forward the reference of the branch. */
11266 struct got_object_id *new_head_commit_id;
11268 error = got_ref_resolve(&new_head_commit_id, repo,
11272 error = got_object_id_str(&id_str, new_head_commit_id);
11275 printf("Forwarding %s to commit %s\n",
11276 got_ref_get_name(branch), id_str);
11278 error = got_ref_change_ref(branch,
11279 new_head_commit_id);
11282 /* No backup needed since objects did not change. */
11288 STAILQ_FOREACH(qid, &commits, entry) {
11290 commit_id = &qid->id;
11291 parent_id = pid ? &pid->id : yca_id;
11294 memset(&upa, 0, sizeof(upa));
11295 error = got_worktree_rebase_merge_files(&merged_paths,
11296 worktree, fileindex, parent_id, commit_id, repo,
11297 update_progress, &upa, check_cancelled, NULL);
11301 print_merge_progress_stats(&upa);
11302 if (upa.conflicts > 0 || upa.missing > 0 ||
11303 upa.not_deleted > 0 || upa.unversioned > 0) {
11304 if (upa.conflicts > 0) {
11305 error = show_rebase_merge_conflict(&qid->id,
11310 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11314 error = rebase_commit(&merged_paths, worktree, fileindex,
11315 tmp_branch, committer, commit_id, repo);
11316 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11321 if (upa.conflicts > 0 || upa.missing > 0 ||
11322 upa.not_deleted > 0 || upa.unversioned > 0) {
11323 error = got_worktree_rebase_postpone(worktree, fileindex);
11326 if (upa.conflicts > 0 && upa.missing == 0 &&
11327 upa.not_deleted == 0 && upa.unversioned == 0) {
11328 error = got_error_msg(GOT_ERR_CONFLICTS,
11329 "conflicts must be resolved before rebasing "
11331 } else if (upa.conflicts > 0) {
11332 error = got_error_msg(GOT_ERR_CONFLICTS,
11333 "conflicts must be resolved before rebasing "
11334 "can continue; changes destined for some "
11335 "files were not yet merged and should be "
11336 "merged manually if required before the "
11337 "rebase operation is continued");
11339 error = got_error_msg(GOT_ERR_CONFLICTS,
11340 "changes destined for some files were not "
11341 "yet merged and should be merged manually "
11342 "if required before the rebase operation "
11346 error = rebase_complete(worktree, fileindex, branch,
11347 tmp_branch, repo, create_backup);
11351 free(gitconfig_path);
11352 got_object_id_queue_free(&commits);
11353 free(branch_head_commit_id);
11354 free(resume_commit_id);
11355 free(head_commit_id);
11358 got_object_commit_close(commit);
11360 got_ref_close(branch);
11361 if (new_base_branch)
11362 got_ref_close(new_base_branch);
11364 got_ref_close(tmp_branch);
11366 got_ref_close(head_ref);
11368 got_worktree_close(worktree);
11370 const struct got_error *close_err = got_repo_close(repo);
11375 const struct got_error *pack_err =
11376 got_repo_pack_fds_close(pack_fds);
11384 usage_histedit(void)
11386 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11387 "[branch]\n", getprogname());
11391 #define GOT_HISTEDIT_PICK 'p'
11392 #define GOT_HISTEDIT_EDIT 'e'
11393 #define GOT_HISTEDIT_FOLD 'f'
11394 #define GOT_HISTEDIT_DROP 'd'
11395 #define GOT_HISTEDIT_MESG 'm'
11397 static const struct got_histedit_cmd {
11398 unsigned char code;
11401 } got_histedit_cmds[] = {
11402 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11403 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11404 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11406 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11407 { GOT_HISTEDIT_MESG, "mesg",
11408 "single-line log message for commit above (open editor if empty)" },
11411 struct got_histedit_list_entry {
11412 TAILQ_ENTRY(got_histedit_list_entry) entry;
11413 struct got_object_id *commit_id;
11414 const struct got_histedit_cmd *cmd;
11417 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11419 static const struct got_error *
11420 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11421 FILE *f, struct got_repository *repo)
11423 const struct got_error *err = NULL;
11424 char *logmsg = NULL, *id_str = NULL;
11425 struct got_commit_object *commit = NULL;
11428 err = got_object_open_as_commit(&commit, repo, commit_id);
11432 err = get_short_logmsg(&logmsg, 34, commit);
11436 err = got_object_id_str(&id_str, commit_id);
11440 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11442 err = got_ferror(f, GOT_ERR_IO);
11445 got_object_commit_close(commit);
11451 static const struct got_error *
11452 histedit_write_commit_list(struct got_object_id_queue *commits,
11453 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11454 int edit_only, struct got_repository *repo)
11456 const struct got_error *err = NULL;
11457 struct got_object_qid *qid;
11458 const char *histedit_cmd = NULL;
11460 if (STAILQ_EMPTY(commits))
11461 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11463 STAILQ_FOREACH(qid, commits, entry) {
11464 histedit_cmd = got_histedit_cmds[0].name;
11466 histedit_cmd = "drop";
11467 else if (edit_only)
11468 histedit_cmd = "edit";
11469 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11470 histedit_cmd = "fold";
11471 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11474 if (edit_logmsg_only) {
11475 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11477 err = got_ferror(f, GOT_ERR_IO);
11486 static const struct got_error *
11487 write_cmd_list(FILE *f, const char *branch_name,
11488 struct got_object_id_queue *commits)
11490 const struct got_error *err = NULL;
11494 struct got_object_qid *qid;
11496 qid = STAILQ_FIRST(commits);
11497 err = got_object_id_str(&id_str, &qid->id);
11502 "# Editing the history of branch '%s' starting at\n"
11504 "# Commits will be processed in order from top to "
11505 "bottom of this file.\n", branch_name, id_str);
11507 err = got_ferror(f, GOT_ERR_IO);
11511 n = fprintf(f, "# Available histedit commands:\n");
11513 err = got_ferror(f, GOT_ERR_IO);
11517 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11518 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11519 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11522 err = got_ferror(f, GOT_ERR_IO);
11531 static const struct got_error *
11532 histedit_syntax_error(int lineno)
11534 static char msg[42];
11537 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11539 if (ret < 0 || (size_t)ret >= sizeof(msg))
11540 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11542 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11545 static const struct got_error *
11546 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11547 char *logmsg, struct got_repository *repo)
11549 const struct got_error *err;
11550 struct got_commit_object *folded_commit = NULL;
11551 char *id_str, *folded_logmsg = NULL;
11553 err = got_object_id_str(&id_str, hle->commit_id);
11557 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11561 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11564 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11565 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11566 folded_logmsg) == -1) {
11567 err = got_error_from_errno("asprintf");
11571 got_object_commit_close(folded_commit);
11573 free(folded_logmsg);
11577 static struct got_histedit_list_entry *
11578 get_folded_commits(struct got_histedit_list_entry *hle)
11580 struct got_histedit_list_entry *prev, *folded = NULL;
11582 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11583 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11584 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11585 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11587 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11593 static const struct got_error *
11594 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11595 struct got_repository *repo)
11597 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11598 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11599 const struct got_error *err = NULL;
11600 struct got_commit_object *commit = NULL;
11603 struct got_histedit_list_entry *folded = NULL;
11605 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11609 folded = get_folded_commits(hle);
11611 while (folded != hle) {
11612 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11613 folded = TAILQ_NEXT(folded, entry);
11616 err = append_folded_commit_msg(&new_msg, folded,
11622 folded = TAILQ_NEXT(folded, entry);
11626 err = got_object_id_str(&id_str, hle->commit_id);
11629 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11632 logmsg_len = asprintf(&new_msg,
11633 "%s\n# original log message of commit %s: %s",
11634 logmsg ? logmsg : "", id_str, orig_logmsg);
11635 if (logmsg_len == -1) {
11636 err = got_error_from_errno("asprintf");
11642 err = got_object_id_str(&id_str, hle->commit_id);
11646 err = got_opentemp_named_fd(&logmsg_path, &fd,
11647 GOT_TMPDIR_STR "/got-logmsg", "");
11651 write(fd, logmsg, logmsg_len);
11654 err = get_editor(&editor);
11658 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11661 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11664 hle->logmsg = strdup(new_msg);
11665 if (hle->logmsg == NULL)
11666 err = got_error_from_errno("strdup");
11669 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11670 err = got_error_from_errno2("unlink", logmsg_path);
11676 got_object_commit_close(commit);
11680 static const struct got_error *
11681 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11682 FILE *f, struct got_repository *repo)
11684 const struct got_error *err = NULL;
11685 char *line = NULL, *p, *end;
11688 int lineno = 0, lastcmd = -1;
11689 const struct got_histedit_cmd *cmd;
11690 struct got_object_id *commit_id = NULL;
11691 struct got_histedit_list_entry *hle = NULL;
11694 len = getline(&line, &size, f);
11696 const struct got_error *getline_err;
11699 getline_err = got_error_from_errno("getline");
11700 err = got_ferror(f, getline_err->code);
11705 while (isspace((unsigned char)p[0]))
11707 if (p[0] == '#' || p[0] == '\0') {
11713 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11714 cmd = &got_histedit_cmds[i];
11715 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11716 isspace((unsigned char)p[strlen(cmd->name)])) {
11717 p += strlen(cmd->name);
11720 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11725 if (i == nitems(got_histedit_cmds)) {
11726 err = histedit_syntax_error(lineno);
11729 while (isspace((unsigned char)p[0]))
11731 if (cmd->code == GOT_HISTEDIT_MESG) {
11732 if (lastcmd != GOT_HISTEDIT_PICK &&
11733 lastcmd != GOT_HISTEDIT_EDIT) {
11734 err = got_error(GOT_ERR_HISTEDIT_CMD);
11737 if (p[0] == '\0') {
11738 err = histedit_edit_logmsg(hle, repo);
11742 hle->logmsg = strdup(p);
11743 if (hle->logmsg == NULL) {
11744 err = got_error_from_errno("strdup");
11750 lastcmd = cmd->code;
11754 while (end[0] && !isspace((unsigned char)end[0]))
11758 err = got_object_resolve_id_str(&commit_id, repo, p);
11760 /* override error code */
11761 err = histedit_syntax_error(lineno);
11765 hle = malloc(sizeof(*hle));
11767 err = got_error_from_errno("malloc");
11771 hle->commit_id = commit_id;
11772 hle->logmsg = NULL;
11776 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11777 lastcmd = cmd->code;
11785 static const struct got_error *
11786 histedit_check_script(struct got_histedit_list *histedit_cmds,
11787 struct got_object_id_queue *commits, struct got_repository *repo)
11789 const struct got_error *err = NULL;
11790 struct got_object_qid *qid;
11791 struct got_histedit_list_entry *hle;
11792 static char msg[92];
11795 if (TAILQ_EMPTY(histedit_cmds))
11796 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11797 "histedit script contains no commands");
11798 if (STAILQ_EMPTY(commits))
11799 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11801 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11802 struct got_histedit_list_entry *hle2;
11803 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11806 if (got_object_id_cmp(hle->commit_id,
11807 hle2->commit_id) != 0)
11809 err = got_object_id_str(&id_str, hle->commit_id);
11812 snprintf(msg, sizeof(msg), "commit %s is listed "
11813 "more than once in histedit script", id_str);
11815 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11819 STAILQ_FOREACH(qid, commits, entry) {
11820 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11821 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11825 err = got_object_id_str(&id_str, &qid->id);
11828 snprintf(msg, sizeof(msg),
11829 "commit %s missing from histedit script", id_str);
11831 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11835 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11836 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11837 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11838 "last commit in histedit script cannot be folded");
11843 static const struct got_error *
11844 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11845 const char *path, struct got_object_id_queue *commits,
11846 struct got_repository *repo)
11848 const struct got_error *err = NULL;
11852 err = get_editor(&editor);
11856 if (spawn_editor(editor, path) == -1) {
11857 err = got_error_from_errno("failed spawning editor");
11861 f = fopen(path, "re");
11863 err = got_error_from_errno("fopen");
11866 err = histedit_parse_list(histedit_cmds, f, repo);
11870 err = histedit_check_script(histedit_cmds, commits, repo);
11872 if (f && fclose(f) == EOF && err == NULL)
11873 err = got_error_from_errno("fclose");
11878 static const struct got_error *
11879 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11880 struct got_object_id_queue *, const char *, const char *,
11881 struct got_repository *);
11883 static const struct got_error *
11884 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11885 struct got_object_id_queue *commits, const char *branch_name,
11886 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11887 struct got_repository *repo)
11889 const struct got_error *err;
11893 err = got_opentemp_named(&path, &f, "got-histedit", "");
11897 err = write_cmd_list(f, branch_name, commits);
11901 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11902 fold_only, drop_only, edit_only, repo);
11906 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11908 err = histedit_parse_list(histedit_cmds, f, repo);
11910 if (fclose(f) == EOF) {
11911 err = got_error_from_errno("fclose");
11915 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11917 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11918 err->code != GOT_ERR_HISTEDIT_CMD)
11920 err = histedit_edit_list_retry(histedit_cmds, err,
11921 commits, path, branch_name, repo);
11925 if (f && fclose(f) == EOF && err == NULL)
11926 err = got_error_from_errno("fclose");
11927 if (path && unlink(path) != 0 && err == NULL)
11928 err = got_error_from_errno2("unlink", path);
11933 static const struct got_error *
11934 histedit_save_list(struct got_histedit_list *histedit_cmds,
11935 struct got_worktree *worktree, struct got_repository *repo)
11937 const struct got_error *err = NULL;
11940 struct got_histedit_list_entry *hle;
11941 struct got_commit_object *commit = NULL;
11943 err = got_worktree_get_histedit_script_path(&path, worktree);
11947 f = fopen(path, "we");
11949 err = got_error_from_errno2("fopen", path);
11952 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11953 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11959 int n = fprintf(f, "%c %s\n",
11960 GOT_HISTEDIT_MESG, hle->logmsg);
11962 err = got_ferror(f, GOT_ERR_IO);
11968 if (f && fclose(f) == EOF && err == NULL)
11969 err = got_error_from_errno("fclose");
11972 got_object_commit_close(commit);
11977 histedit_free_list(struct got_histedit_list *histedit_cmds)
11979 struct got_histedit_list_entry *hle;
11981 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11982 TAILQ_REMOVE(histedit_cmds, hle, entry);
11987 static const struct got_error *
11988 histedit_load_list(struct got_histedit_list *histedit_cmds,
11989 const char *path, struct got_repository *repo)
11991 const struct got_error *err = NULL;
11994 f = fopen(path, "re");
11996 err = got_error_from_errno2("fopen", path);
12000 err = histedit_parse_list(histedit_cmds, f, repo);
12002 if (f && fclose(f) == EOF && err == NULL)
12003 err = got_error_from_errno("fclose");
12007 static const struct got_error *
12008 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12009 const struct got_error *edit_err, struct got_object_id_queue *commits,
12010 const char *path, const char *branch_name, struct got_repository *repo)
12012 const struct got_error *err = NULL, *prev_err = edit_err;
12015 while (resp != 'c' && resp != 'r' && resp != 'a') {
12016 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12017 "or (a)bort: ", getprogname(), prev_err->msg);
12022 histedit_free_list(histedit_cmds);
12023 err = histedit_run_editor(histedit_cmds, path, commits,
12026 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12027 err->code != GOT_ERR_HISTEDIT_CMD)
12034 } else if (resp == 'r') {
12035 histedit_free_list(histedit_cmds);
12036 err = histedit_edit_script(histedit_cmds,
12037 commits, branch_name, 0, 0, 0, 0, repo);
12039 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12040 err->code != GOT_ERR_HISTEDIT_CMD)
12047 } else if (resp == 'a') {
12048 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12051 printf("invalid response '%c'\n", resp);
12057 static const struct got_error *
12058 histedit_complete(struct got_worktree *worktree,
12059 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12060 struct got_reference *branch, struct got_repository *repo)
12062 printf("Switching work tree to %s\n",
12063 got_ref_get_symref_target(branch));
12064 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12068 static const struct got_error *
12069 show_histedit_progress(struct got_commit_object *commit,
12070 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12072 const struct got_error *err;
12073 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12075 err = got_object_id_str(&old_id_str, hle->commit_id);
12080 err = got_object_id_str(&new_id_str, new_id);
12085 old_id_str[12] = '\0';
12087 new_id_str[12] = '\0';
12090 logmsg = strdup(hle->logmsg);
12091 if (logmsg == NULL) {
12092 err = got_error_from_errno("strdup");
12095 trim_logmsg(logmsg, 42);
12097 err = get_short_logmsg(&logmsg, 42, commit);
12102 switch (hle->cmd->code) {
12103 case GOT_HISTEDIT_PICK:
12104 case GOT_HISTEDIT_EDIT:
12105 printf("%s -> %s: %s\n", old_id_str,
12106 new_id_str ? new_id_str : "no-op change", logmsg);
12108 case GOT_HISTEDIT_DROP:
12109 case GOT_HISTEDIT_FOLD:
12110 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12122 static const struct got_error *
12123 histedit_commit(struct got_pathlist_head *merged_paths,
12124 struct got_worktree *worktree, struct got_fileindex *fileindex,
12125 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12126 const char *committer, struct got_repository *repo)
12128 const struct got_error *err;
12129 struct got_commit_object *commit;
12130 struct got_object_id *new_commit_id;
12132 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12133 && hle->logmsg == NULL) {
12134 err = histedit_edit_logmsg(hle, repo);
12139 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12143 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12144 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12145 hle->logmsg, repo);
12147 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12149 err = show_histedit_progress(commit, hle, NULL);
12151 err = show_histedit_progress(commit, hle, new_commit_id);
12152 free(new_commit_id);
12155 got_object_commit_close(commit);
12159 static const struct got_error *
12160 histedit_skip_commit(struct got_histedit_list_entry *hle,
12161 struct got_worktree *worktree, struct got_repository *repo)
12163 const struct got_error *error;
12164 struct got_commit_object *commit;
12166 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12171 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12175 error = show_histedit_progress(commit, hle, NULL);
12176 got_object_commit_close(commit);
12180 static const struct got_error *
12181 check_local_changes(void *arg, unsigned char status,
12182 unsigned char staged_status, const char *path,
12183 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12184 struct got_object_id *commit_id, int dirfd, const char *de_name)
12186 int *have_local_changes = arg;
12189 case GOT_STATUS_ADD:
12190 case GOT_STATUS_DELETE:
12191 case GOT_STATUS_MODIFY:
12192 case GOT_STATUS_CONFLICT:
12193 *have_local_changes = 1;
12194 return got_error(GOT_ERR_CANCELLED);
12199 switch (staged_status) {
12200 case GOT_STATUS_ADD:
12201 case GOT_STATUS_DELETE:
12202 case GOT_STATUS_MODIFY:
12203 *have_local_changes = 1;
12204 return got_error(GOT_ERR_CANCELLED);
12212 static const struct got_error *
12213 cmd_histedit(int argc, char *argv[])
12215 const struct got_error *error = NULL;
12216 struct got_worktree *worktree = NULL;
12217 struct got_fileindex *fileindex = NULL;
12218 struct got_repository *repo = NULL;
12219 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12220 struct got_reference *branch = NULL;
12221 struct got_reference *tmp_branch = NULL;
12222 struct got_object_id *resume_commit_id = NULL;
12223 struct got_object_id *base_commit_id = NULL;
12224 struct got_object_id *head_commit_id = NULL;
12225 struct got_commit_object *commit = NULL;
12226 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12227 struct got_update_progress_arg upa;
12228 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12229 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12230 int list_backups = 0, delete_backups = 0;
12231 const char *edit_script_path = NULL;
12232 struct got_object_id_queue commits;
12233 struct got_pathlist_head merged_paths;
12234 const struct got_object_id_queue *parent_ids;
12235 struct got_object_qid *pid;
12236 struct got_histedit_list histedit_cmds;
12237 struct got_histedit_list_entry *hle;
12238 int *pack_fds = NULL;
12240 STAILQ_INIT(&commits);
12241 TAILQ_INIT(&histedit_cmds);
12242 TAILQ_INIT(&merged_paths);
12243 memset(&upa, 0, sizeof(upa));
12246 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12247 "unveil", NULL) == -1)
12251 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12266 edit_script_path = optarg;
12275 edit_logmsg_only = 1;
12278 delete_backups = 1;
12289 if (abort_edit && continue_edit)
12290 option_conflict('a', 'c');
12291 if (edit_script_path && edit_logmsg_only)
12292 option_conflict('F', 'm');
12293 if (abort_edit && edit_logmsg_only)
12294 option_conflict('a', 'm');
12295 if (continue_edit && edit_logmsg_only)
12296 option_conflict('c', 'm');
12297 if (abort_edit && fold_only)
12298 option_conflict('a', 'f');
12299 if (continue_edit && fold_only)
12300 option_conflict('c', 'f');
12301 if (fold_only && edit_logmsg_only)
12302 option_conflict('f', 'm');
12303 if (edit_script_path && fold_only)
12304 option_conflict('F', 'f');
12305 if (abort_edit && edit_only)
12306 option_conflict('a', 'e');
12307 if (continue_edit && edit_only)
12308 option_conflict('c', 'e');
12309 if (edit_only && edit_logmsg_only)
12310 option_conflict('e', 'm');
12311 if (edit_script_path && edit_only)
12312 option_conflict('F', 'e');
12313 if (fold_only && edit_only)
12314 option_conflict('f', 'e');
12315 if (drop_only && abort_edit)
12316 option_conflict('d', 'a');
12317 if (drop_only && continue_edit)
12318 option_conflict('d', 'c');
12319 if (drop_only && edit_logmsg_only)
12320 option_conflict('d', 'm');
12321 if (drop_only && edit_only)
12322 option_conflict('d', 'e');
12323 if (drop_only && edit_script_path)
12324 option_conflict('d', 'F');
12325 if (drop_only && fold_only)
12326 option_conflict('d', 'f');
12327 if (list_backups) {
12329 option_conflict('l', 'a');
12331 option_conflict('l', 'c');
12332 if (edit_script_path)
12333 option_conflict('l', 'F');
12334 if (edit_logmsg_only)
12335 option_conflict('l', 'm');
12337 option_conflict('l', 'd');
12339 option_conflict('l', 'f');
12341 option_conflict('l', 'e');
12342 if (delete_backups)
12343 option_conflict('l', 'X');
12344 if (argc != 0 && argc != 1)
12346 } else if (delete_backups) {
12348 option_conflict('X', 'a');
12350 option_conflict('X', 'c');
12352 option_conflict('X', 'd');
12353 if (edit_script_path)
12354 option_conflict('X', 'F');
12355 if (edit_logmsg_only)
12356 option_conflict('X', 'm');
12358 option_conflict('X', 'f');
12360 option_conflict('X', 'e');
12362 option_conflict('X', 'l');
12363 if (argc != 0 && argc != 1)
12365 } else if (argc != 0)
12369 * This command cannot apply unveil(2) in all cases because the
12370 * user may choose to run an editor to edit the histedit script
12371 * and to edit individual commit log messages.
12372 * unveil(2) traverses exec(2); if an editor is used we have to
12373 * apply unveil after edit script and log messages have been written.
12374 * XXX TODO: Make use of unveil(2) where possible.
12377 cwd = getcwd(NULL, 0);
12379 error = got_error_from_errno("getcwd");
12383 error = got_repo_pack_fds_open(&pack_fds);
12387 error = got_worktree_open(&worktree, cwd);
12389 if (list_backups || delete_backups) {
12390 if (error->code != GOT_ERR_NOT_WORKTREE)
12393 if (error->code == GOT_ERR_NOT_WORKTREE)
12394 error = wrap_not_worktree_error(error,
12400 if (list_backups || delete_backups) {
12401 error = got_repo_open(&repo,
12402 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12406 error = apply_unveil(got_repo_get_path(repo), 0,
12407 worktree ? got_worktree_get_root_path(worktree) : NULL);
12410 error = process_backup_refs(
12411 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12412 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12413 goto done; /* nothing else to do */
12416 error = get_gitconfig_path(&gitconfig_path);
12419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12420 gitconfig_path, pack_fds);
12424 if (worktree != NULL && !list_backups && !delete_backups) {
12425 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12430 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12433 if (rebase_in_progress) {
12434 error = got_error(GOT_ERR_REBASING);
12438 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12442 if (merge_in_progress) {
12443 error = got_error(GOT_ERR_MERGE_BUSY);
12447 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12451 if (edit_in_progress && edit_logmsg_only) {
12452 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12453 "histedit operation is in progress in this "
12454 "work tree and must be continued or aborted "
12455 "before the -m option can be used");
12458 if (edit_in_progress && drop_only) {
12459 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12460 "histedit operation is in progress in this "
12461 "work tree and must be continued or aborted "
12462 "before the -d option can be used");
12465 if (edit_in_progress && fold_only) {
12466 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12467 "histedit operation is in progress in this "
12468 "work tree and must be continued or aborted "
12469 "before the -f option can be used");
12472 if (edit_in_progress && edit_only) {
12473 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12474 "histedit operation is in progress in this "
12475 "work tree and must be continued or aborted "
12476 "before the -e option can be used");
12480 if (edit_in_progress && abort_edit) {
12481 error = got_worktree_histedit_continue(&resume_commit_id,
12482 &tmp_branch, &branch, &base_commit_id, &fileindex,
12486 printf("Switching work tree to %s\n",
12487 got_ref_get_symref_target(branch));
12488 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12489 branch, base_commit_id, abort_progress, &upa);
12492 printf("Histedit of %s aborted\n",
12493 got_ref_get_symref_target(branch));
12494 print_merge_progress_stats(&upa);
12495 goto done; /* nothing else to do */
12496 } else if (abort_edit) {
12497 error = got_error(GOT_ERR_NOT_HISTEDIT);
12501 error = get_author(&committer, repo, worktree);
12505 if (continue_edit) {
12508 if (!edit_in_progress) {
12509 error = got_error(GOT_ERR_NOT_HISTEDIT);
12513 error = got_worktree_get_histedit_script_path(&path, worktree);
12517 error = histedit_load_list(&histedit_cmds, path, repo);
12522 error = got_worktree_histedit_continue(&resume_commit_id,
12523 &tmp_branch, &branch, &base_commit_id, &fileindex,
12528 error = got_ref_resolve(&head_commit_id, repo, branch);
12532 error = got_object_open_as_commit(&commit, repo,
12536 parent_ids = got_object_commit_get_parent_ids(commit);
12537 pid = STAILQ_FIRST(parent_ids);
12539 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12542 error = collect_commits(&commits, head_commit_id, &pid->id,
12543 base_commit_id, got_worktree_get_path_prefix(worktree),
12544 GOT_ERR_HISTEDIT_PATH, repo);
12545 got_object_commit_close(commit);
12550 if (edit_in_progress) {
12551 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12555 error = got_ref_open(&branch, repo,
12556 got_worktree_get_head_ref_name(worktree), 0);
12560 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12561 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12562 "will not edit commit history of a branch outside "
12563 "the \"refs/heads/\" reference namespace");
12567 error = got_ref_resolve(&head_commit_id, repo, branch);
12568 got_ref_close(branch);
12573 error = got_object_open_as_commit(&commit, repo,
12577 parent_ids = got_object_commit_get_parent_ids(commit);
12578 pid = STAILQ_FIRST(parent_ids);
12580 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12583 error = collect_commits(&commits, head_commit_id, &pid->id,
12584 got_worktree_get_base_commit_id(worktree),
12585 got_worktree_get_path_prefix(worktree),
12586 GOT_ERR_HISTEDIT_PATH, repo);
12587 got_object_commit_close(commit);
12592 if (STAILQ_EMPTY(&commits)) {
12593 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12597 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12598 &base_commit_id, &fileindex, worktree, repo);
12602 if (edit_script_path) {
12603 error = histedit_load_list(&histedit_cmds,
12604 edit_script_path, repo);
12606 got_worktree_histedit_abort(worktree, fileindex,
12607 repo, branch, base_commit_id,
12608 abort_progress, &upa);
12609 print_merge_progress_stats(&upa);
12613 const char *branch_name;
12614 branch_name = got_ref_get_symref_target(branch);
12615 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12617 error = histedit_edit_script(&histedit_cmds, &commits,
12618 branch_name, edit_logmsg_only, fold_only,
12619 drop_only, edit_only, repo);
12621 got_worktree_histedit_abort(worktree, fileindex,
12622 repo, branch, base_commit_id,
12623 abort_progress, &upa);
12624 print_merge_progress_stats(&upa);
12630 error = histedit_save_list(&histedit_cmds, worktree,
12633 got_worktree_histedit_abort(worktree, fileindex,
12634 repo, branch, base_commit_id,
12635 abort_progress, &upa);
12636 print_merge_progress_stats(&upa);
12642 error = histedit_check_script(&histedit_cmds, &commits, repo);
12646 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12647 if (resume_commit_id) {
12648 if (got_object_id_cmp(hle->commit_id,
12649 resume_commit_id) != 0)
12652 resume_commit_id = NULL;
12653 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12654 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12655 error = histedit_skip_commit(hle, worktree,
12660 struct got_pathlist_head paths;
12661 int have_changes = 0;
12663 TAILQ_INIT(&paths);
12664 error = got_pathlist_append(&paths, "", NULL);
12667 error = got_worktree_status(worktree, &paths,
12668 repo, 0, check_local_changes, &have_changes,
12669 check_cancelled, NULL);
12670 got_pathlist_free(&paths,
12671 GOT_PATHLIST_FREE_NONE);
12673 if (error->code != GOT_ERR_CANCELLED)
12675 if (sigint_received || sigpipe_received)
12678 if (have_changes) {
12679 error = histedit_commit(NULL, worktree,
12680 fileindex, tmp_branch, hle,
12685 error = got_object_open_as_commit(
12686 &commit, repo, hle->commit_id);
12689 error = show_histedit_progress(commit,
12691 got_object_commit_close(commit);
12700 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12701 error = histedit_skip_commit(hle, worktree, repo);
12707 error = got_object_open_as_commit(&commit, repo,
12711 parent_ids = got_object_commit_get_parent_ids(commit);
12712 pid = STAILQ_FIRST(parent_ids);
12714 error = got_worktree_histedit_merge_files(&merged_paths,
12715 worktree, fileindex, &pid->id, hle->commit_id, repo,
12716 update_progress, &upa, check_cancelled, NULL);
12719 got_object_commit_close(commit);
12722 print_merge_progress_stats(&upa);
12723 if (upa.conflicts > 0 || upa.missing > 0 ||
12724 upa.not_deleted > 0 || upa.unversioned > 0) {
12725 if (upa.conflicts > 0) {
12726 error = show_rebase_merge_conflict(
12727 hle->commit_id, repo);
12731 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12735 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12737 error = got_object_id_str(&id_str, hle->commit_id);
12740 printf("Stopping histedit for amending commit %s\n",
12743 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12744 error = got_worktree_histedit_postpone(worktree,
12749 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12750 error = histedit_skip_commit(hle, worktree, repo);
12756 error = histedit_commit(&merged_paths, worktree, fileindex,
12757 tmp_branch, hle, committer, repo);
12758 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12763 if (upa.conflicts > 0 || upa.missing > 0 ||
12764 upa.not_deleted > 0 || upa.unversioned > 0) {
12765 error = got_worktree_histedit_postpone(worktree, fileindex);
12768 if (upa.conflicts > 0 && upa.missing == 0 &&
12769 upa.not_deleted == 0 && upa.unversioned == 0) {
12770 error = got_error_msg(GOT_ERR_CONFLICTS,
12771 "conflicts must be resolved before histedit "
12773 } else if (upa.conflicts > 0) {
12774 error = got_error_msg(GOT_ERR_CONFLICTS,
12775 "conflicts must be resolved before histedit "
12776 "can continue; changes destined for some "
12777 "files were not yet merged and should be "
12778 "merged manually if required before the "
12779 "histedit operation is continued");
12781 error = got_error_msg(GOT_ERR_CONFLICTS,
12782 "changes destined for some files were not "
12783 "yet merged and should be merged manually "
12784 "if required before the histedit operation "
12788 error = histedit_complete(worktree, fileindex, tmp_branch,
12793 free(gitconfig_path);
12794 got_object_id_queue_free(&commits);
12795 histedit_free_list(&histedit_cmds);
12796 free(head_commit_id);
12797 free(base_commit_id);
12798 free(resume_commit_id);
12800 got_object_commit_close(commit);
12802 got_ref_close(branch);
12804 got_ref_close(tmp_branch);
12806 got_worktree_close(worktree);
12808 const struct got_error *close_err = got_repo_close(repo);
12813 const struct got_error *pack_err =
12814 got_repo_pack_fds_close(pack_fds);
12822 usage_integrate(void)
12824 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12828 static const struct got_error *
12829 cmd_integrate(int argc, char *argv[])
12831 const struct got_error *error = NULL;
12832 struct got_repository *repo = NULL;
12833 struct got_worktree *worktree = NULL;
12834 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12835 const char *branch_arg = NULL;
12836 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12837 struct got_fileindex *fileindex = NULL;
12838 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12840 struct got_update_progress_arg upa;
12841 int *pack_fds = NULL;
12844 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12845 "unveil", NULL) == -1)
12849 while ((ch = getopt(argc, argv, "")) != -1) {
12862 branch_arg = argv[0];
12864 cwd = getcwd(NULL, 0);
12866 error = got_error_from_errno("getcwd");
12870 error = got_repo_pack_fds_open(&pack_fds);
12874 error = got_worktree_open(&worktree, cwd);
12876 if (error->code == GOT_ERR_NOT_WORKTREE)
12877 error = wrap_not_worktree_error(error, "integrate",
12882 error = check_rebase_or_histedit_in_progress(worktree);
12886 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12891 error = apply_unveil(got_repo_get_path(repo), 0,
12892 got_worktree_get_root_path(worktree));
12896 error = check_merge_in_progress(worktree, repo);
12900 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12901 error = got_error_from_errno("asprintf");
12905 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12906 &base_branch_ref, worktree, refname, repo);
12910 refname = strdup(got_ref_get_name(branch_ref));
12911 if (refname == NULL) {
12912 error = got_error_from_errno("strdup");
12913 got_worktree_integrate_abort(worktree, fileindex, repo,
12914 branch_ref, base_branch_ref);
12917 base_refname = strdup(got_ref_get_name(base_branch_ref));
12918 if (base_refname == NULL) {
12919 error = got_error_from_errno("strdup");
12920 got_worktree_integrate_abort(worktree, fileindex, repo,
12921 branch_ref, base_branch_ref);
12924 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12925 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12926 got_worktree_integrate_abort(worktree, fileindex, repo,
12927 branch_ref, base_branch_ref);
12931 error = got_ref_resolve(&commit_id, repo, branch_ref);
12935 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12939 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12940 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12941 "specified branch has already been integrated");
12942 got_worktree_integrate_abort(worktree, fileindex, repo,
12943 branch_ref, base_branch_ref);
12947 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12949 if (error->code == GOT_ERR_ANCESTRY)
12950 error = got_error(GOT_ERR_REBASE_REQUIRED);
12951 got_worktree_integrate_abort(worktree, fileindex, repo,
12952 branch_ref, base_branch_ref);
12956 memset(&upa, 0, sizeof(upa));
12957 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12958 branch_ref, base_branch_ref, update_progress, &upa,
12959 check_cancelled, NULL);
12963 printf("Integrated %s into %s\n", refname, base_refname);
12964 print_update_progress_stats(&upa);
12967 const struct got_error *close_err = got_repo_close(repo);
12972 got_worktree_close(worktree);
12974 const struct got_error *pack_err =
12975 got_repo_pack_fds_close(pack_fds);
12980 free(base_commit_id);
12983 free(base_refname);
12990 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12994 static const struct got_error *
12995 cmd_merge(int argc, char *argv[])
12997 const struct got_error *error = NULL;
12998 struct got_worktree *worktree = NULL;
12999 struct got_repository *repo = NULL;
13000 struct got_fileindex *fileindex = NULL;
13001 char *cwd = NULL, *id_str = NULL, *author = NULL;
13002 struct got_reference *branch = NULL, *wt_branch = NULL;
13003 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13004 struct got_object_id *wt_branch_tip = NULL;
13005 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13006 int interrupt_merge = 0;
13007 struct got_update_progress_arg upa;
13008 struct got_object_id *merge_commit_id = NULL;
13009 char *branch_name = NULL;
13010 int *pack_fds = NULL;
13012 memset(&upa, 0, sizeof(upa));
13015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13016 "unveil", NULL) == -1)
13020 while ((ch = getopt(argc, argv, "acn")) != -1) {
13026 continue_merge = 1;
13029 interrupt_merge = 1;
13040 if (abort_merge && continue_merge)
13041 option_conflict('a', 'c');
13042 if (abort_merge || continue_merge) {
13045 } else if (argc != 1)
13048 cwd = getcwd(NULL, 0);
13050 error = got_error_from_errno("getcwd");
13054 error = got_repo_pack_fds_open(&pack_fds);
13058 error = got_worktree_open(&worktree, cwd);
13060 if (error->code == GOT_ERR_NOT_WORKTREE)
13061 error = wrap_not_worktree_error(error,
13066 error = got_repo_open(&repo,
13067 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13072 if (worktree != NULL) {
13073 error = worktree_has_logmsg_ref("merge", worktree, repo);
13078 error = apply_unveil(got_repo_get_path(repo), 0,
13079 worktree ? got_worktree_get_root_path(worktree) : NULL);
13083 error = check_rebase_or_histedit_in_progress(worktree);
13087 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13093 if (!merge_in_progress) {
13094 error = got_error(GOT_ERR_NOT_MERGING);
13097 error = got_worktree_merge_continue(&branch_name,
13098 &branch_tip, &fileindex, worktree, repo);
13101 error = got_worktree_merge_abort(worktree, fileindex, repo,
13102 abort_progress, &upa);
13105 printf("Merge of %s aborted\n", branch_name);
13106 goto done; /* nothing else to do */
13109 error = get_author(&author, repo, worktree);
13113 if (continue_merge) {
13114 if (!merge_in_progress) {
13115 error = got_error(GOT_ERR_NOT_MERGING);
13118 error = got_worktree_merge_continue(&branch_name,
13119 &branch_tip, &fileindex, worktree, repo);
13123 error = got_ref_open(&branch, repo, argv[0], 0);
13126 branch_name = strdup(got_ref_get_name(branch));
13127 if (branch_name == NULL) {
13128 error = got_error_from_errno("strdup");
13131 error = got_ref_resolve(&branch_tip, repo, branch);
13136 error = got_ref_open(&wt_branch, repo,
13137 got_worktree_get_head_ref_name(worktree), 0);
13140 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13143 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13144 wt_branch_tip, branch_tip, 0, repo,
13145 check_cancelled, NULL);
13146 if (error && error->code != GOT_ERR_ANCESTRY)
13149 if (!continue_merge) {
13150 error = check_path_prefix(wt_branch_tip, branch_tip,
13151 got_worktree_get_path_prefix(worktree),
13152 GOT_ERR_MERGE_PATH, repo);
13156 error = check_same_branch(wt_branch_tip, branch,
13159 if (error->code != GOT_ERR_ANCESTRY)
13163 static char msg[512];
13164 snprintf(msg, sizeof(msg),
13165 "cannot create a merge commit because "
13166 "%s is based on %s; %s can be integrated "
13167 "with 'got integrate' instead", branch_name,
13168 got_worktree_get_head_ref_name(worktree),
13170 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13174 error = got_worktree_merge_prepare(&fileindex, worktree,
13179 error = got_worktree_merge_branch(worktree, fileindex,
13180 yca_id, branch_tip, repo, update_progress, &upa,
13181 check_cancelled, NULL);
13184 print_merge_progress_stats(&upa);
13185 if (!upa.did_something) {
13186 error = got_worktree_merge_abort(worktree, fileindex,
13187 repo, abort_progress, &upa);
13190 printf("Already up-to-date\n");
13195 if (interrupt_merge) {
13196 error = got_worktree_merge_postpone(worktree, fileindex);
13199 printf("Merge of %s interrupted on request\n", branch_name);
13200 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13201 upa.not_deleted > 0 || upa.unversioned > 0) {
13202 error = got_worktree_merge_postpone(worktree, fileindex);
13205 if (upa.conflicts > 0 && upa.missing == 0 &&
13206 upa.not_deleted == 0 && upa.unversioned == 0) {
13207 error = got_error_msg(GOT_ERR_CONFLICTS,
13208 "conflicts must be resolved before merging "
13210 } else if (upa.conflicts > 0) {
13211 error = got_error_msg(GOT_ERR_CONFLICTS,
13212 "conflicts must be resolved before merging "
13213 "can continue; changes destined for some "
13214 "files were not yet merged and "
13215 "should be merged manually if required before the "
13216 "merge operation is continued");
13218 error = got_error_msg(GOT_ERR_CONFLICTS,
13219 "changes destined for some "
13220 "files were not yet merged and should be "
13221 "merged manually if required before the "
13222 "merge operation is continued");
13226 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13227 fileindex, author, NULL, 1, branch_tip, branch_name,
13228 repo, continue_merge ? print_status : NULL, NULL);
13231 error = got_worktree_merge_complete(worktree, fileindex, repo);
13234 error = got_object_id_str(&id_str, merge_commit_id);
13237 printf("Merged %s into %s: %s\n", branch_name,
13238 got_worktree_get_head_ref_name(worktree),
13244 free(merge_commit_id);
13250 got_ref_close(branch);
13252 got_ref_close(wt_branch);
13254 got_worktree_close(worktree);
13256 const struct got_error *close_err = got_repo_close(repo);
13261 const struct got_error *pack_err =
13262 got_repo_pack_fds_close(pack_fds);
13272 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13273 "[path ...]\n", getprogname());
13277 static const struct got_error *
13278 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13279 const char *path, struct got_object_id *blob_id,
13280 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13281 int dirfd, const char *de_name)
13283 const struct got_error *err = NULL;
13284 char *id_str = NULL;
13286 if (staged_status != GOT_STATUS_ADD &&
13287 staged_status != GOT_STATUS_MODIFY &&
13288 staged_status != GOT_STATUS_DELETE)
13291 if (staged_status == GOT_STATUS_ADD ||
13292 staged_status == GOT_STATUS_MODIFY)
13293 err = got_object_id_str(&id_str, staged_blob_id);
13295 err = got_object_id_str(&id_str, blob_id);
13299 printf("%s %c %s\n", id_str, staged_status, path);
13304 static const struct got_error *
13305 cmd_stage(int argc, char *argv[])
13307 const struct got_error *error = NULL;
13308 struct got_repository *repo = NULL;
13309 struct got_worktree *worktree = NULL;
13311 struct got_pathlist_head paths;
13312 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13313 FILE *patch_script_file = NULL;
13314 const char *patch_script_path = NULL;
13315 struct choose_patch_arg cpa;
13316 int *pack_fds = NULL;
13318 TAILQ_INIT(&paths);
13321 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13322 "unveil", NULL) == -1)
13326 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13329 patch_script_path = optarg;
13338 allow_bad_symlinks = 1;
13349 if (list_stage && (pflag || patch_script_path))
13350 errx(1, "-l option cannot be used with other options");
13351 if (patch_script_path && !pflag)
13352 errx(1, "-F option can only be used together with -p option");
13354 cwd = getcwd(NULL, 0);
13356 error = got_error_from_errno("getcwd");
13360 error = got_repo_pack_fds_open(&pack_fds);
13364 error = got_worktree_open(&worktree, cwd);
13366 if (error->code == GOT_ERR_NOT_WORKTREE)
13367 error = wrap_not_worktree_error(error, "stage", cwd);
13371 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13376 if (patch_script_path) {
13377 patch_script_file = fopen(patch_script_path, "re");
13378 if (patch_script_file == NULL) {
13379 error = got_error_from_errno2("fopen",
13380 patch_script_path);
13384 error = apply_unveil(got_repo_get_path(repo), 0,
13385 got_worktree_get_root_path(worktree));
13389 error = check_merge_in_progress(worktree, repo);
13393 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13398 error = got_worktree_status(worktree, &paths, repo, 0,
13399 print_stage, NULL, check_cancelled, NULL);
13401 cpa.patch_script_file = patch_script_file;
13402 cpa.action = "stage";
13403 error = got_worktree_stage(worktree, &paths,
13404 pflag ? NULL : print_status, NULL,
13405 pflag ? choose_patch : NULL, &cpa,
13406 allow_bad_symlinks, repo);
13409 if (patch_script_file && fclose(patch_script_file) == EOF &&
13411 error = got_error_from_errno2("fclose", patch_script_path);
13413 const struct got_error *close_err = got_repo_close(repo);
13418 got_worktree_close(worktree);
13420 const struct got_error *pack_err =
13421 got_repo_pack_fds_close(pack_fds);
13425 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13431 usage_unstage(void)
13433 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13434 "[path ...]\n", getprogname());
13439 static const struct got_error *
13440 cmd_unstage(int argc, char *argv[])
13442 const struct got_error *error = NULL;
13443 struct got_repository *repo = NULL;
13444 struct got_worktree *worktree = NULL;
13446 struct got_pathlist_head paths;
13448 struct got_update_progress_arg upa;
13449 FILE *patch_script_file = NULL;
13450 const char *patch_script_path = NULL;
13451 struct choose_patch_arg cpa;
13452 int *pack_fds = NULL;
13454 TAILQ_INIT(&paths);
13457 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13458 "unveil", NULL) == -1)
13462 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13465 patch_script_path = optarg;
13479 if (patch_script_path && !pflag)
13480 errx(1, "-F option can only be used together with -p option");
13482 cwd = getcwd(NULL, 0);
13484 error = got_error_from_errno("getcwd");
13488 error = got_repo_pack_fds_open(&pack_fds);
13492 error = got_worktree_open(&worktree, cwd);
13494 if (error->code == GOT_ERR_NOT_WORKTREE)
13495 error = wrap_not_worktree_error(error, "unstage", cwd);
13499 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13504 if (patch_script_path) {
13505 patch_script_file = fopen(patch_script_path, "re");
13506 if (patch_script_file == NULL) {
13507 error = got_error_from_errno2("fopen",
13508 patch_script_path);
13513 error = apply_unveil(got_repo_get_path(repo), 0,
13514 got_worktree_get_root_path(worktree));
13518 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13522 cpa.patch_script_file = patch_script_file;
13523 cpa.action = "unstage";
13524 memset(&upa, 0, sizeof(upa));
13525 error = got_worktree_unstage(worktree, &paths, update_progress,
13526 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13528 print_merge_progress_stats(&upa);
13530 if (patch_script_file && fclose(patch_script_file) == EOF &&
13532 error = got_error_from_errno2("fclose", patch_script_path);
13534 const struct got_error *close_err = got_repo_close(repo);
13539 got_worktree_close(worktree);
13541 const struct got_error *pack_err =
13542 got_repo_pack_fds_close(pack_fds);
13546 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13554 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13555 "arg ...\n", getprogname());
13559 static const struct got_error *
13560 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13562 const struct got_error *err;
13563 struct got_blob_object *blob;
13566 fd = got_opentempfd();
13568 return got_error_from_errno("got_opentempfd");
13570 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13574 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13576 if (fd != -1 && close(fd) == -1 && err == NULL)
13577 err = got_error_from_errno("close");
13579 got_object_blob_close(blob);
13583 static const struct got_error *
13584 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13586 const struct got_error *err;
13587 struct got_tree_object *tree;
13590 err = got_object_open_as_tree(&tree, repo, id);
13594 nentries = got_object_tree_get_nentries(tree);
13595 for (i = 0; i < nentries; i++) {
13596 struct got_tree_entry *te;
13598 if (sigint_received || sigpipe_received)
13600 te = got_object_tree_get_entry(tree, i);
13601 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13604 fprintf(outfile, "%s %.7o %s\n", id_str,
13605 got_tree_entry_get_mode(te),
13606 got_tree_entry_get_name(te));
13610 got_object_tree_close(tree);
13614 static const struct got_error *
13615 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13617 const struct got_error *err;
13618 struct got_commit_object *commit;
13619 const struct got_object_id_queue *parent_ids;
13620 struct got_object_qid *pid;
13621 char *id_str = NULL;
13622 const char *logmsg = NULL;
13625 err = got_object_open_as_commit(&commit, repo, id);
13629 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13633 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13634 parent_ids = got_object_commit_get_parent_ids(commit);
13635 fprintf(outfile, "numparents %d\n",
13636 got_object_commit_get_nparents(commit));
13637 STAILQ_FOREACH(pid, parent_ids, entry) {
13639 err = got_object_id_str(&pid_str, &pid->id);
13642 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13645 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13646 got_object_commit_get_author_gmtoff(commit));
13647 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13648 got_object_commit_get_author(commit),
13649 (long long)got_object_commit_get_author_time(commit),
13652 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13653 got_object_commit_get_committer_gmtoff(commit));
13654 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13655 got_object_commit_get_committer(commit),
13656 (long long)got_object_commit_get_committer_time(commit),
13659 logmsg = got_object_commit_get_logmsg_raw(commit);
13660 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13661 fprintf(outfile, "%s", logmsg);
13664 got_object_commit_close(commit);
13668 static const struct got_error *
13669 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13671 const struct got_error *err;
13672 struct got_tag_object *tag;
13673 char *id_str = NULL;
13674 const char *tagmsg = NULL;
13677 err = got_object_open_as_tag(&tag, repo, id);
13681 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13685 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13687 switch (got_object_tag_get_object_type(tag)) {
13688 case GOT_OBJ_TYPE_BLOB:
13689 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13690 GOT_OBJ_LABEL_BLOB);
13692 case GOT_OBJ_TYPE_TREE:
13693 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13694 GOT_OBJ_LABEL_TREE);
13696 case GOT_OBJ_TYPE_COMMIT:
13697 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13698 GOT_OBJ_LABEL_COMMIT);
13700 case GOT_OBJ_TYPE_TAG:
13701 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13702 GOT_OBJ_LABEL_TAG);
13708 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13709 got_object_tag_get_name(tag));
13711 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13712 got_object_tag_get_tagger_gmtoff(tag));
13713 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13714 got_object_tag_get_tagger(tag),
13715 (long long)got_object_tag_get_tagger_time(tag),
13718 tagmsg = got_object_tag_get_message(tag);
13719 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13720 fprintf(outfile, "%s", tagmsg);
13723 got_object_tag_close(tag);
13727 static const struct got_error *
13728 cmd_cat(int argc, char *argv[])
13730 const struct got_error *error;
13731 struct got_repository *repo = NULL;
13732 struct got_worktree *worktree = NULL;
13733 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13734 const char *commit_id_str = NULL;
13735 struct got_object_id *id = NULL, *commit_id = NULL;
13736 struct got_commit_object *commit = NULL;
13737 int ch, obj_type, i, force_path = 0;
13738 struct got_reflist_head refs;
13739 int *pack_fds = NULL;
13744 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13749 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13752 commit_id_str = optarg;
13758 repo_path = realpath(optarg, NULL);
13759 if (repo_path == NULL)
13760 return got_error_from_errno2("realpath",
13762 got_path_strip_trailing_slashes(repo_path);
13773 cwd = getcwd(NULL, 0);
13775 error = got_error_from_errno("getcwd");
13779 error = got_repo_pack_fds_open(&pack_fds);
13783 if (repo_path == NULL) {
13784 error = got_worktree_open(&worktree, cwd);
13785 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13788 repo_path = strdup(
13789 got_worktree_get_repo_path(worktree));
13790 if (repo_path == NULL) {
13791 error = got_error_from_errno("strdup");
13795 /* Release work tree lock. */
13796 got_worktree_close(worktree);
13801 if (repo_path == NULL) {
13802 repo_path = strdup(cwd);
13803 if (repo_path == NULL)
13804 return got_error_from_errno("strdup");
13807 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13812 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13816 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13820 if (commit_id_str == NULL)
13821 commit_id_str = GOT_REF_HEAD;
13822 error = got_repo_match_object_id(&commit_id, NULL,
13823 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13827 error = got_object_open_as_commit(&commit, repo, commit_id);
13831 for (i = 0; i < argc; i++) {
13833 error = got_object_id_by_path(&id, repo, commit,
13838 error = got_repo_match_object_id(&id, &label, argv[i],
13839 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13842 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13843 error->code != GOT_ERR_NOT_REF)
13845 error = got_object_id_by_path(&id, repo,
13852 error = got_object_get_type(&obj_type, repo, id);
13856 switch (obj_type) {
13857 case GOT_OBJ_TYPE_BLOB:
13858 error = cat_blob(id, repo, stdout);
13860 case GOT_OBJ_TYPE_TREE:
13861 error = cat_tree(id, repo, stdout);
13863 case GOT_OBJ_TYPE_COMMIT:
13864 error = cat_commit(id, repo, stdout);
13866 case GOT_OBJ_TYPE_TAG:
13867 error = cat_tag(id, repo, stdout);
13870 error = got_error(GOT_ERR_OBJ_TYPE);
13885 got_object_commit_close(commit);
13887 got_worktree_close(worktree);
13889 const struct got_error *close_err = got_repo_close(repo);
13894 const struct got_error *pack_err =
13895 got_repo_pack_fds_close(pack_fds);
13900 got_ref_list_free(&refs);
13907 fprintf(stderr, "usage: %s info [path ...]\n",
13912 static const struct got_error *
13913 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13914 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13915 struct got_object_id *commit_id)
13917 const struct got_error *err = NULL;
13918 char *id_str = NULL;
13920 struct tm mytm, *tm;
13921 struct got_pathlist_head *paths = arg;
13922 struct got_pathlist_entry *pe;
13925 * Clear error indication from any of the path arguments which
13926 * would cause this file index entry to be displayed.
13928 TAILQ_FOREACH(pe, paths, entry) {
13929 if (got_path_cmp(path, pe->path, strlen(path),
13930 pe->path_len) == 0 ||
13931 got_path_is_child(path, pe->path, pe->path_len))
13932 pe->data = NULL; /* no error */
13935 printf(GOT_COMMIT_SEP_STR);
13937 printf("symlink: %s\n", path);
13938 else if (S_ISREG(mode)) {
13939 printf("file: %s\n", path);
13940 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13941 } else if (S_ISDIR(mode))
13942 printf("directory: %s\n", path);
13944 printf("something: %s\n", path);
13946 tm = localtime_r(&mtime, &mytm);
13949 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13950 return got_error(GOT_ERR_NO_SPACE);
13951 printf("timestamp: %s\n", datebuf);
13954 err = got_object_id_str(&id_str, blob_id);
13957 printf("based on blob: %s\n", id_str);
13961 if (staged_blob_id) {
13962 err = got_object_id_str(&id_str, staged_blob_id);
13965 printf("based on staged blob: %s\n", id_str);
13970 err = got_object_id_str(&id_str, commit_id);
13973 printf("based on commit: %s\n", id_str);
13980 static const struct got_error *
13981 cmd_info(int argc, char *argv[])
13983 const struct got_error *error = NULL;
13984 struct got_worktree *worktree = NULL;
13985 char *cwd = NULL, *id_str = NULL;
13986 struct got_pathlist_head paths;
13987 char *uuidstr = NULL;
13988 int ch, show_files = 0;
13990 TAILQ_INIT(&paths);
13993 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13998 while ((ch = getopt(argc, argv, "")) != -1) {
14009 cwd = getcwd(NULL, 0);
14011 error = got_error_from_errno("getcwd");
14015 error = got_worktree_open(&worktree, cwd);
14017 if (error->code == GOT_ERR_NOT_WORKTREE)
14018 error = wrap_not_worktree_error(error, "info", cwd);
14023 /* Remove "wpath cpath proc exec sendfd" promises. */
14024 if (pledge("stdio rpath flock unveil", NULL) == -1)
14027 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14032 error = get_worktree_paths_from_argv(&paths, argc, argv,
14039 error = got_object_id_str(&id_str,
14040 got_worktree_get_base_commit_id(worktree));
14044 error = got_worktree_get_uuid(&uuidstr, worktree);
14048 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14049 printf("work tree base commit: %s\n", id_str);
14050 printf("work tree path prefix: %s\n",
14051 got_worktree_get_path_prefix(worktree));
14052 printf("work tree branch reference: %s\n",
14053 got_worktree_get_head_ref_name(worktree));
14054 printf("work tree UUID: %s\n", uuidstr);
14055 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14058 struct got_pathlist_entry *pe;
14059 TAILQ_FOREACH(pe, &paths, entry) {
14060 if (pe->path_len == 0)
14063 * Assume this path will fail. This will be corrected
14064 * in print_path_info() in case the path does suceeed.
14066 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14068 error = got_worktree_path_info(worktree, &paths,
14069 print_path_info, &paths, check_cancelled, NULL);
14072 TAILQ_FOREACH(pe, &paths, entry) {
14073 if (pe->data != NULL) {
14074 const struct got_error *perr;
14077 error = got_error_fmt(perr->code, "%s",
14085 got_worktree_close(worktree);
14086 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);