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>
20 #include <sys/types.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static volatile sig_atomic_t sigint_received;
65 static volatile sig_atomic_t sigpipe_received;
68 catch_sigint(int signo)
74 catch_sigpipe(int signo)
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 const char *cmd_alias;
87 __dead static void usage(int, int);
88 __dead static void usage_init(void);
89 __dead static void usage_import(void);
90 __dead static void usage_clone(void);
91 __dead static void usage_fetch(void);
92 __dead static void usage_checkout(void);
93 __dead static void usage_update(void);
94 __dead static void usage_log(void);
95 __dead static void usage_diff(void);
96 __dead static void usage_blame(void);
97 __dead static void usage_tree(void);
98 __dead static void usage_status(void);
99 __dead static void usage_ref(void);
100 __dead static void usage_branch(void);
101 __dead static void usage_tag(void);
102 __dead static void usage_add(void);
103 __dead static void usage_remove(void);
104 __dead static void usage_revert(void);
105 __dead static void usage_commit(void);
106 __dead static void usage_send(void);
107 __dead static void usage_cherrypick(void);
108 __dead static void usage_backout(void);
109 __dead static void usage_rebase(void);
110 __dead static void usage_histedit(void);
111 __dead static void usage_integrate(void);
112 __dead static void usage_stage(void);
113 __dead static void usage_unstage(void);
114 __dead static void usage_cat(void);
115 __dead static void usage_info(void);
117 static const struct got_error* cmd_init(int, char *[]);
118 static const struct got_error* cmd_import(int, char *[]);
119 static const struct got_error* cmd_clone(int, char *[]);
120 static const struct got_error* cmd_fetch(int, char *[]);
121 static const struct got_error* cmd_checkout(int, char *[]);
122 static const struct got_error* cmd_update(int, char *[]);
123 static const struct got_error* cmd_log(int, char *[]);
124 static const struct got_error* cmd_diff(int, char *[]);
125 static const struct got_error* cmd_blame(int, char *[]);
126 static const struct got_error* cmd_tree(int, char *[]);
127 static const struct got_error* cmd_status(int, char *[]);
128 static const struct got_error* cmd_ref(int, char *[]);
129 static const struct got_error* cmd_branch(int, char *[]);
130 static const struct got_error* cmd_tag(int, char *[]);
131 static const struct got_error* cmd_add(int, char *[]);
132 static const struct got_error* cmd_remove(int, char *[]);
133 static const struct got_error* cmd_revert(int, char *[]);
134 static const struct got_error* cmd_commit(int, char *[]);
135 static const struct got_error* cmd_send(int, char *[]);
136 static const struct got_error* cmd_cherrypick(int, char *[]);
137 static const struct got_error* cmd_backout(int, char *[]);
138 static const struct got_error* cmd_rebase(int, char *[]);
139 static const struct got_error* cmd_histedit(int, char *[]);
140 static const struct got_error* cmd_integrate(int, char *[]);
141 static const struct got_error* cmd_stage(int, char *[]);
142 static const struct got_error* cmd_unstage(int, char *[]);
143 static const struct got_error* cmd_cat(int, char *[]);
144 static const struct got_error* cmd_info(int, char *[]);
146 static struct got_cmd got_commands[] = {
147 { "init", cmd_init, usage_init, "" },
148 { "import", cmd_import, usage_import, "im" },
149 { "clone", cmd_clone, usage_clone, "cl" },
150 { "fetch", cmd_fetch, usage_fetch, "fe" },
151 { "checkout", cmd_checkout, usage_checkout, "co" },
152 { "update", cmd_update, usage_update, "up" },
153 { "log", cmd_log, usage_log, "" },
154 { "diff", cmd_diff, usage_diff, "di" },
155 { "blame", cmd_blame, usage_blame, "bl" },
156 { "tree", cmd_tree, usage_tree, "tr" },
157 { "status", cmd_status, usage_status, "st" },
158 { "ref", cmd_ref, usage_ref, "" },
159 { "branch", cmd_branch, usage_branch, "br" },
160 { "tag", cmd_tag, usage_tag, "" },
161 { "add", cmd_add, usage_add, "" },
162 { "remove", cmd_remove, usage_remove, "rm" },
163 { "revert", cmd_revert, usage_revert, "rv" },
164 { "commit", cmd_commit, usage_commit, "ci" },
165 { "send", cmd_send, usage_send, "se" },
166 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
167 { "backout", cmd_backout, usage_backout, "bo" },
168 { "rebase", cmd_rebase, usage_rebase, "rb" },
169 { "histedit", cmd_histedit, usage_histedit, "he" },
170 { "integrate", cmd_integrate, usage_integrate,"ig" },
171 { "stage", cmd_stage, usage_stage, "sg" },
172 { "unstage", cmd_unstage, usage_unstage, "ug" },
173 { "cat", cmd_cat, usage_cat, "" },
174 { "info", cmd_info, usage_info, "" },
178 list_commands(FILE *fp)
182 fprintf(fp, "commands:");
183 for (i = 0; i < nitems(got_commands); i++) {
184 struct got_cmd *cmd = &got_commands[i];
185 fprintf(fp, " %s", cmd->cmd_name);
191 option_conflict(char a, char b)
193 errx(1, "-%c and -%c options are mutually exclusive", a, b);
197 main(int argc, char *argv[])
202 int hflag = 0, Vflag = 0;
203 static struct option longopts[] = {
204 { "version", no_argument, NULL, 'V' },
208 setlocale(LC_CTYPE, "");
210 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
230 got_version_print_str();
235 usage(hflag, hflag ? 0 : 1);
237 signal(SIGINT, catch_sigint);
238 signal(SIGPIPE, catch_sigpipe);
240 for (i = 0; i < nitems(got_commands); i++) {
241 const struct got_error *error;
243 cmd = &got_commands[i];
245 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
246 strcmp(cmd->cmd_alias, argv[0]) != 0)
250 got_commands[i].cmd_usage();
252 error = got_commands[i].cmd_main(argc, argv);
253 if (error && error->code != GOT_ERR_CANCELLED &&
254 error->code != GOT_ERR_PRIVSEP_EXIT &&
255 !(sigpipe_received &&
256 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
258 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
259 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
266 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
267 list_commands(stderr);
272 usage(int hflag, int status)
274 FILE *fp = (status == 0) ? stdout : stderr;
276 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
283 static const struct got_error *
284 get_editor(char **abspath)
286 const struct got_error *err = NULL;
291 editor = getenv("VISUAL");
293 editor = getenv("EDITOR");
296 err = got_path_find_prog(abspath, editor);
301 if (*abspath == NULL) {
302 *abspath = strdup("/bin/ed");
303 if (*abspath == NULL)
304 return got_error_from_errno("strdup");
310 static const struct got_error *
311 apply_unveil(const char *repo_path, int repo_read_only,
312 const char *worktree_path)
314 const struct got_error *err;
317 if (unveil("gmon.out", "rwc") != 0)
318 return got_error_from_errno2("unveil", "gmon.out");
320 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
321 return got_error_from_errno2("unveil", repo_path);
323 if (worktree_path && unveil(worktree_path, "rwc") != 0)
324 return got_error_from_errno2("unveil", worktree_path);
326 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
327 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
329 err = got_privsep_unveil_exec_helpers();
333 if (unveil(NULL, NULL) != 0)
334 return got_error_from_errno("unveil");
342 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
346 static const struct got_error *
347 cmd_init(int argc, char *argv[])
349 const struct got_error *error = NULL;
350 char *repo_path = NULL;
353 while ((ch = getopt(argc, argv, "")) != -1) {
365 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
371 repo_path = strdup(argv[0]);
372 if (repo_path == NULL)
373 return got_error_from_errno("strdup");
375 got_path_strip_trailing_slashes(repo_path);
377 error = got_path_mkdir(repo_path);
379 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
382 error = apply_unveil(repo_path, 0, NULL);
386 error = got_repo_init(repo_path);
395 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
396 "[-r repository-path] [-I pattern] path\n", getprogname());
401 spawn_editor(const char *editor, const char *file)
404 sig_t sighup, sigint, sigquit;
407 sighup = signal(SIGHUP, SIG_IGN);
408 sigint = signal(SIGINT, SIG_IGN);
409 sigquit = signal(SIGQUIT, SIG_IGN);
411 switch (pid = fork()) {
415 execl(editor, editor, file, (char *)NULL);
419 while (waitpid(pid, &st, 0) == -1)
424 (void)signal(SIGHUP, sighup);
425 (void)signal(SIGINT, sigint);
426 (void)signal(SIGQUIT, sigquit);
428 if (!WIFEXITED(st)) {
433 return WEXITSTATUS(st);
436 static const struct got_error *
437 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
438 const char *initial_content, size_t initial_content_len,
439 int require_modification)
441 const struct got_error *err = NULL;
447 size_t len, logmsg_len;
448 char *initial_content_stripped = NULL, *buf = NULL, *s;
452 if (stat(logmsg_path, &st) == -1)
453 return got_error_from_errno2("stat", logmsg_path);
455 if (spawn_editor(editor, logmsg_path) == -1)
456 return got_error_from_errno("failed spawning editor");
458 if (stat(logmsg_path, &st2) == -1)
459 return got_error_from_errno("stat");
461 if (require_modification &&
462 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
463 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
464 "no changes made to commit message, aborting");
467 * Set up a stripped version of the initial content without comments
468 * and blank lines. We need this in order to check if the message
469 * has in fact been edited.
471 initial_content_stripped = malloc(initial_content_len + 1);
472 if (initial_content_stripped == NULL)
473 return got_error_from_errno("malloc");
474 initial_content_stripped[0] = '\0';
476 buf = strdup(initial_content);
478 err = got_error_from_errno("strdup");
483 while ((line = strsep(&s, "\n")) != NULL) {
484 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
485 continue; /* remove comments and leading empty lines */
486 len = strlcat(initial_content_stripped, line,
487 initial_content_len + 1);
488 if (len >= initial_content_len + 1) {
489 err = got_error(GOT_ERR_NO_SPACE);
493 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
494 initial_content_stripped[len - 1] = '\0';
498 logmsg_len = st2.st_size;
499 *logmsg = malloc(logmsg_len + 1);
501 return got_error_from_errno("malloc");
504 fp = fopen(logmsg_path, "r");
506 err = got_error_from_errno("fopen");
511 while ((linelen = getline(&line, &linesize, fp)) != -1) {
512 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
513 continue; /* remove comments and leading empty lines */
514 len = strlcat(*logmsg, line, logmsg_len + 1);
515 if (len >= logmsg_len + 1) {
516 err = got_error(GOT_ERR_NO_SPACE);
522 err = got_ferror(fp, GOT_ERR_IO);
525 while (len > 0 && (*logmsg)[len - 1] == '\n') {
526 (*logmsg)[len - 1] = '\0';
531 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
532 "commit message cannot be empty, aborting");
535 if (require_modification &&
536 strcmp(*logmsg, initial_content_stripped) == 0)
537 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
538 "no changes made to commit message, aborting");
540 free(initial_content_stripped);
542 if (fp && fclose(fp) == EOF && err == NULL)
543 err = got_error_from_errno("fclose");
551 static const struct got_error *
552 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
553 const char *path_dir, const char *branch_name)
555 char *initial_content = NULL;
556 const struct got_error *err = NULL;
557 int initial_content_len;
560 initial_content_len = asprintf(&initial_content,
561 "\n# %s to be imported to branch %s\n", path_dir,
563 if (initial_content_len == -1)
564 return got_error_from_errno("asprintf");
566 err = got_opentemp_named_fd(logmsg_path, &fd,
567 GOT_TMPDIR_STR "/got-importmsg");
571 if (write(fd, initial_content, initial_content_len) == -1) {
572 err = got_error_from_errno2("write", *logmsg_path);
576 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
577 initial_content_len, 1);
579 if (fd != -1 && close(fd) == -1 && err == NULL)
580 err = got_error_from_errno2("close", *logmsg_path);
581 free(initial_content);
589 static const struct got_error *
590 import_progress(void *arg, const char *path)
592 printf("A %s\n", path);
596 static const struct got_error *
597 get_author(char **author, struct got_repository *repo,
598 struct got_worktree *worktree)
600 const struct got_error *err = NULL;
601 const char *got_author = NULL, *name, *email;
602 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
607 worktree_conf = got_worktree_get_gotconfig(worktree);
608 repo_conf = got_repo_get_gotconfig(repo);
611 * Priority of potential author information sources, from most
612 * significant to least significant:
613 * 1) work tree's .got/got.conf file
614 * 2) repository's got.conf file
615 * 3) repository's git config file
616 * 4) environment variables
617 * 5) global git config files (in user's home directory or /etc)
621 got_author = got_gotconfig_get_author(worktree_conf);
622 if (got_author == NULL)
623 got_author = got_gotconfig_get_author(repo_conf);
624 if (got_author == NULL) {
625 name = got_repo_get_gitconfig_author_name(repo);
626 email = got_repo_get_gitconfig_author_email(repo);
628 if (asprintf(author, "%s <%s>", name, email) == -1)
629 return got_error_from_errno("asprintf");
633 got_author = getenv("GOT_AUTHOR");
634 if (got_author == NULL) {
635 name = got_repo_get_global_gitconfig_author_name(repo);
636 email = got_repo_get_global_gitconfig_author_email(
639 if (asprintf(author, "%s <%s>", name, email)
641 return got_error_from_errno("asprintf");
644 /* TODO: Look up user in password database? */
645 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
649 *author = strdup(got_author);
651 return got_error_from_errno("strdup");
654 * Really dumb email address check; we're only doing this to
655 * avoid git's object parser breaking on commits we create.
657 while (*got_author && *got_author != '<')
659 if (*got_author != '<') {
660 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
663 while (*got_author && *got_author != '@')
665 if (*got_author != '@') {
666 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
669 while (*got_author && *got_author != '>')
671 if (*got_author != '>')
672 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
681 static const struct got_error *
682 get_gitconfig_path(char **gitconfig_path)
684 const char *homedir = getenv("HOME");
686 *gitconfig_path = NULL;
688 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
689 return got_error_from_errno("asprintf");
695 static const struct got_error *
696 cmd_import(int argc, char *argv[])
698 const struct got_error *error = NULL;
699 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
700 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
701 const char *branch_name = "main";
702 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
703 struct got_repository *repo = NULL;
704 struct got_reference *branch_ref = NULL, *head_ref = NULL;
705 struct got_object_id *new_commit_id = NULL;
707 struct got_pathlist_head ignores;
708 struct got_pathlist_entry *pe;
709 int preserve_logmsg = 0;
711 TAILQ_INIT(&ignores);
713 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
716 branch_name = optarg;
719 logmsg = strdup(optarg);
720 if (logmsg == NULL) {
721 error = got_error_from_errno("strdup");
726 repo_path = realpath(optarg, NULL);
727 if (repo_path == NULL) {
728 error = got_error_from_errno2("realpath",
734 if (optarg[0] == '\0')
736 error = got_pathlist_insert(&pe, &ignores, optarg,
751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
759 if (repo_path == NULL) {
760 repo_path = getcwd(NULL, 0);
761 if (repo_path == NULL)
762 return got_error_from_errno("getcwd");
764 got_path_strip_trailing_slashes(repo_path);
765 error = get_gitconfig_path(&gitconfig_path);
768 error = got_repo_open(&repo, repo_path, gitconfig_path);
772 error = get_author(&author, repo, NULL);
777 * Don't let the user create a branch name with a leading '-'.
778 * While technically a valid reference name, this case is usually
779 * an unintended typo.
781 if (branch_name[0] == '-')
782 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
784 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
785 error = got_error_from_errno("asprintf");
789 error = got_ref_open(&branch_ref, repo, refname, 0);
791 if (error->code != GOT_ERR_NOT_REF)
794 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
795 "import target branch already exists");
799 path_dir = realpath(argv[0], NULL);
800 if (path_dir == NULL) {
801 error = got_error_from_errno2("realpath", argv[0]);
804 got_path_strip_trailing_slashes(path_dir);
807 * unveil(2) traverses exec(2); if an editor is used we have
808 * to apply unveil after the log message has been written.
810 if (logmsg == NULL || strlen(logmsg) == 0) {
811 error = get_editor(&editor);
815 error = collect_import_msg(&logmsg, &logmsg_path, editor,
818 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
825 if (unveil(path_dir, "r") != 0) {
826 error = got_error_from_errno2("unveil", path_dir);
832 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
839 error = got_repo_import(&new_commit_id, path_dir, logmsg,
840 author, &ignores, repo, import_progress, NULL);
847 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
854 error = got_ref_write(branch_ref, repo);
861 error = got_object_id_str(&id_str, new_commit_id);
868 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
870 if (error->code != GOT_ERR_NOT_REF) {
876 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
884 error = got_ref_write(head_ref, repo);
892 printf("Created branch %s with commit %s\n",
893 got_ref_get_name(branch_ref), id_str);
895 if (preserve_logmsg) {
896 fprintf(stderr, "%s: log message preserved in %s\n",
897 getprogname(), logmsg_path);
898 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
899 error = got_error_from_errno2("unlink", logmsg_path);
908 free(gitconfig_path);
910 got_ref_close(branch_ref);
912 got_ref_close(head_ref);
919 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
920 "[-R reference] repository-url [directory]\n", getprogname());
924 struct got_fetch_progress_arg {
925 char last_scaled_size[FMT_SCALED_STRSIZE];
930 struct got_repository *repo;
935 struct got_pathlist_head *symrefs;
936 struct got_pathlist_head *wanted_branches;
937 struct got_pathlist_head *wanted_refs;
941 const char *remote_repo_path;
943 int fetch_all_branches;
944 int mirror_references;
948 /* XXX forward declaration */
949 static const struct got_error *
950 create_config_files(const char *proto, const char *host, const char *port,
951 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
952 int mirror_references, struct got_pathlist_head *symrefs,
953 struct got_pathlist_head *wanted_branches,
954 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
956 static const struct got_error *
957 fetch_progress(void *arg, const char *message, off_t packfile_size,
958 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
960 const struct got_error *err = NULL;
961 struct got_fetch_progress_arg *a = arg;
962 char scaled_size[FMT_SCALED_STRSIZE];
963 int p_indexed, p_resolved;
964 int print_size = 0, print_indexed = 0, print_resolved = 0;
967 * In order to allow a failed clone to be resumed with 'got fetch'
968 * we try to create configuration files as soon as possible.
969 * Once the server has sent information about its default branch
970 * we have all required information.
972 if (a->create_configs && !a->configs_created &&
973 !TAILQ_EMPTY(a->config_info.symrefs)) {
974 err = create_config_files(a->config_info.proto,
975 a->config_info.host, a->config_info.port,
976 a->config_info.remote_repo_path,
977 a->config_info.git_url,
978 a->config_info.fetch_all_branches,
979 a->config_info.mirror_references,
980 a->config_info.symrefs,
981 a->config_info.wanted_branches,
982 a->config_info.wanted_refs, a->repo);
985 a->configs_created = 1;
988 if (a->verbosity < 0)
991 if (message && message[0] != '\0') {
992 printf("\rserver: %s", message);
997 if (packfile_size > 0 || nobj_indexed > 0) {
998 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
999 (a->last_scaled_size[0] == '\0' ||
1000 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1002 if (strlcpy(a->last_scaled_size, scaled_size,
1003 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1004 return got_error(GOT_ERR_NO_SPACE);
1006 if (nobj_indexed > 0) {
1007 p_indexed = (nobj_indexed * 100) / nobj_total;
1008 if (p_indexed != a->last_p_indexed) {
1009 a->last_p_indexed = p_indexed;
1014 if (nobj_resolved > 0) {
1015 p_resolved = (nobj_resolved * 100) /
1016 (nobj_total - nobj_loose);
1017 if (p_resolved != a->last_p_resolved) {
1018 a->last_p_resolved = p_resolved;
1026 if (print_size || print_indexed || print_resolved)
1029 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1031 printf("; indexing %d%%", p_indexed);
1033 printf("; resolving deltas %d%%", p_resolved);
1034 if (print_size || print_indexed || print_resolved)
1040 static const struct got_error *
1041 create_symref(const char *refname, struct got_reference *target_ref,
1042 int verbosity, struct got_repository *repo)
1044 const struct got_error *err;
1045 struct got_reference *head_symref;
1047 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1051 err = got_ref_write(head_symref, repo);
1052 if (err == NULL && verbosity > 0) {
1053 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1054 got_ref_get_name(target_ref));
1056 got_ref_close(head_symref);
1060 static const struct got_error *
1061 list_remote_refs(struct got_pathlist_head *symrefs,
1062 struct got_pathlist_head *refs)
1064 const struct got_error *err;
1065 struct got_pathlist_entry *pe;
1067 TAILQ_FOREACH(pe, symrefs, entry) {
1068 const char *refname = pe->path;
1069 const char *targetref = pe->data;
1071 printf("%s: %s\n", refname, targetref);
1074 TAILQ_FOREACH(pe, refs, entry) {
1075 const char *refname = pe->path;
1076 struct got_object_id *id = pe->data;
1079 err = got_object_id_str(&id_str, id);
1082 printf("%s: %s\n", refname, id_str);
1089 static const struct got_error *
1090 create_ref(const char *refname, struct got_object_id *id,
1091 int verbosity, struct got_repository *repo)
1093 const struct got_error *err = NULL;
1094 struct got_reference *ref;
1097 err = got_object_id_str(&id_str, id);
1101 err = got_ref_alloc(&ref, refname, id);
1105 err = got_ref_write(ref, repo);
1108 if (err == NULL && verbosity >= 0)
1109 printf("Created reference %s: %s\n", refname, id_str);
1116 match_wanted_ref(const char *refname, const char *wanted_ref)
1118 if (strncmp(refname, "refs/", 5) != 0)
1123 * Prevent fetching of references that won't make any
1124 * sense outside of the remote repository's context.
1126 if (strncmp(refname, "got/", 4) == 0)
1128 if (strncmp(refname, "remotes/", 8) == 0)
1131 if (strncmp(wanted_ref, "refs/", 5) == 0)
1134 /* Allow prefix match. */
1135 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1138 /* Allow exact match. */
1139 return (strcmp(refname, wanted_ref) == 0);
1143 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1145 struct got_pathlist_entry *pe;
1147 TAILQ_FOREACH(pe, wanted_refs, entry) {
1148 if (match_wanted_ref(refname, pe->path))
1155 static const struct got_error *
1156 create_wanted_ref(const char *refname, struct got_object_id *id,
1157 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1159 const struct got_error *err;
1160 char *remote_refname;
1162 if (strncmp("refs/", refname, 5) == 0)
1165 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1166 remote_repo_name, refname) == -1)
1167 return got_error_from_errno("asprintf");
1169 err = create_ref(remote_refname, id, verbosity, repo);
1170 free(remote_refname);
1174 static const struct got_error *
1175 create_gotconfig(const char *proto, const char *host, const char *port,
1176 const char *remote_repo_path, const char *default_branch,
1177 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1178 struct got_pathlist_head *wanted_refs, int mirror_references,
1179 struct got_repository *repo)
1181 const struct got_error *err = NULL;
1182 char *gotconfig_path = NULL;
1183 char *gotconfig = NULL;
1184 FILE *gotconfig_file = NULL;
1185 const char *branchname = NULL;
1186 char *branches = NULL, *refs = NULL;
1189 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1190 struct got_pathlist_entry *pe;
1191 TAILQ_FOREACH(pe, wanted_branches, entry) {
1193 branchname = pe->path;
1194 if (strncmp(branchname, "refs/heads/", 11) == 0)
1196 if (asprintf(&s, "%s\"%s\" ",
1197 branches ? branches : "", branchname) == -1) {
1198 err = got_error_from_errno("asprintf");
1204 } else if (!fetch_all_branches && default_branch) {
1205 branchname = default_branch;
1206 if (strncmp(branchname, "refs/heads/", 11) == 0)
1208 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1209 err = got_error_from_errno("asprintf");
1213 if (!TAILQ_EMPTY(wanted_refs)) {
1214 struct got_pathlist_entry *pe;
1215 TAILQ_FOREACH(pe, wanted_refs, entry) {
1217 const char *refname = pe->path;
1218 if (strncmp(refname, "refs/", 5) == 0)
1220 if (asprintf(&s, "%s\"%s\" ",
1221 refs ? refs : "", refname) == -1) {
1222 err = got_error_from_errno("asprintf");
1230 /* Create got.conf(5). */
1231 gotconfig_path = got_repo_get_path_gotconfig(repo);
1232 if (gotconfig_path == NULL) {
1233 err = got_error_from_errno("got_repo_get_path_gotconfig");
1236 gotconfig_file = fopen(gotconfig_path, "a");
1237 if (gotconfig_file == NULL) {
1238 err = got_error_from_errno2("fopen", gotconfig_path);
1241 if (asprintf(&gotconfig,
1246 "\trepository \"%s\"\n"
1252 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1253 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1254 remote_repo_path, branches ? "\tbranch { " : "",
1255 branches ? branches : "", branches ? "}\n" : "",
1256 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1257 mirror_references ? "\tmirror-references yes\n" : "",
1258 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1259 err = got_error_from_errno("asprintf");
1262 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1263 if (n != strlen(gotconfig)) {
1264 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1269 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1270 err = got_error_from_errno2("fclose", gotconfig_path);
1271 free(gotconfig_path);
1276 static const struct got_error *
1277 create_gitconfig(const char *git_url, const char *default_branch,
1278 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1279 struct got_pathlist_head *wanted_refs, int mirror_references,
1280 struct got_repository *repo)
1282 const struct got_error *err = NULL;
1283 char *gitconfig_path = NULL;
1284 char *gitconfig = NULL;
1285 FILE *gitconfig_file = NULL;
1286 char *branches = NULL, *refs = NULL;
1287 const char *branchname;
1290 /* Create a config file Git can understand. */
1291 gitconfig_path = got_repo_get_path_gitconfig(repo);
1292 if (gitconfig_path == NULL) {
1293 err = got_error_from_errno("got_repo_get_path_gitconfig");
1296 gitconfig_file = fopen(gitconfig_path, "a");
1297 if (gitconfig_file == NULL) {
1298 err = got_error_from_errno2("fopen", gitconfig_path);
1301 if (fetch_all_branches) {
1302 if (mirror_references) {
1303 if (asprintf(&branches,
1304 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1305 err = got_error_from_errno("asprintf");
1308 } else if (asprintf(&branches,
1309 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1310 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1311 err = got_error_from_errno("asprintf");
1314 } else if (!TAILQ_EMPTY(wanted_branches)) {
1315 struct got_pathlist_entry *pe;
1316 TAILQ_FOREACH(pe, wanted_branches, entry) {
1318 branchname = pe->path;
1319 if (strncmp(branchname, "refs/heads/", 11) == 0)
1321 if (mirror_references) {
1323 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1324 branches ? branches : "",
1325 branchname, branchname) == -1) {
1326 err = got_error_from_errno("asprintf");
1329 } else if (asprintf(&s,
1330 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1331 branches ? branches : "",
1332 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1333 branchname) == -1) {
1334 err = got_error_from_errno("asprintf");
1342 * If the server specified a default branch, use just that one.
1343 * Otherwise fall back to fetching all branches on next fetch.
1345 if (default_branch) {
1346 branchname = default_branch;
1347 if (strncmp(branchname, "refs/heads/", 11) == 0)
1350 branchname = "*"; /* fall back to all branches */
1351 if (mirror_references) {
1352 if (asprintf(&branches,
1353 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1354 branchname, branchname) == -1) {
1355 err = got_error_from_errno("asprintf");
1358 } else if (asprintf(&branches,
1359 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1360 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1361 branchname) == -1) {
1362 err = got_error_from_errno("asprintf");
1366 if (!TAILQ_EMPTY(wanted_refs)) {
1367 struct got_pathlist_entry *pe;
1368 TAILQ_FOREACH(pe, wanted_refs, entry) {
1370 const char *refname = pe->path;
1371 if (strncmp(refname, "refs/", 5) == 0)
1373 if (mirror_references) {
1375 "%s\tfetch = refs/%s:refs/%s\n",
1376 refs ? refs : "", refname, refname) == -1) {
1377 err = got_error_from_errno("asprintf");
1380 } else if (asprintf(&s,
1381 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1383 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1385 err = got_error_from_errno("asprintf");
1393 if (asprintf(&gitconfig,
1398 "\tfetch = refs/tags/*:refs/tags/*\n",
1399 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1400 refs ? refs : "") == -1) {
1401 err = got_error_from_errno("asprintf");
1404 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1405 if (n != strlen(gitconfig)) {
1406 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1410 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1411 err = got_error_from_errno2("fclose", gitconfig_path);
1412 free(gitconfig_path);
1417 static const struct got_error *
1418 create_config_files(const char *proto, const char *host, const char *port,
1419 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1420 int mirror_references, struct got_pathlist_head *symrefs,
1421 struct got_pathlist_head *wanted_branches,
1422 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1424 const struct got_error *err = NULL;
1425 const char *default_branch = NULL;
1426 struct got_pathlist_entry *pe;
1429 * If we asked for a set of wanted branches then use the first
1432 if (!TAILQ_EMPTY(wanted_branches)) {
1433 pe = TAILQ_FIRST(wanted_branches);
1434 default_branch = pe->path;
1436 /* First HEAD ref listed by server is the default branch. */
1437 TAILQ_FOREACH(pe, symrefs, entry) {
1438 const char *refname = pe->path;
1439 const char *target = pe->data;
1441 if (strcmp(refname, GOT_REF_HEAD) != 0)
1444 default_branch = target;
1449 /* Create got.conf(5). */
1450 err = create_gotconfig(proto, host, port, remote_repo_path,
1451 default_branch, fetch_all_branches, wanted_branches,
1452 wanted_refs, mirror_references, repo);
1456 /* Create a config file Git can understand. */
1457 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1458 wanted_branches, wanted_refs, mirror_references, repo);
1461 static const struct got_error *
1462 cmd_clone(int argc, char *argv[])
1464 const struct got_error *error = NULL;
1465 const char *uri, *dirname;
1466 char *proto, *host, *port, *repo_name, *server_path;
1467 char *default_destdir = NULL, *id_str = NULL;
1468 const char *repo_path;
1469 struct got_repository *repo = NULL;
1470 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1471 struct got_pathlist_entry *pe;
1472 struct got_object_id *pack_hash = NULL;
1473 int ch, fetchfd = -1, fetchstatus;
1474 pid_t fetchpid = -1;
1475 struct got_fetch_progress_arg fpa;
1476 char *git_url = NULL;
1477 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1478 int list_refs_only = 0;
1481 TAILQ_INIT(&symrefs);
1482 TAILQ_INIT(&wanted_branches);
1483 TAILQ_INIT(&wanted_refs);
1485 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1488 fetch_all_branches = 1;
1491 error = got_pathlist_append(&wanted_branches,
1500 mirror_references = 1;
1505 else if (verbosity < 3)
1512 error = got_pathlist_append(&wanted_refs,
1525 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1526 option_conflict('a', 'b');
1527 if (list_refs_only) {
1528 if (!TAILQ_EMPTY(&wanted_branches))
1529 option_conflict('l', 'b');
1530 if (fetch_all_branches)
1531 option_conflict('l', 'a');
1532 if (mirror_references)
1533 option_conflict('l', 'm');
1534 if (!TAILQ_EMPTY(&wanted_refs))
1535 option_conflict('l', 'R');
1547 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1552 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1553 host, port ? ":" : "", port ? port : "",
1554 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1555 error = got_error_from_errno("asprintf");
1559 if (strcmp(proto, "git") == 0) {
1561 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 "sendfd dns inet unveil", NULL) == -1)
1565 } else if (strcmp(proto, "git+ssh") == 0 ||
1566 strcmp(proto, "ssh") == 0) {
1568 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 "sendfd unveil", NULL) == -1)
1572 } else if (strcmp(proto, "http") == 0 ||
1573 strcmp(proto, "git+http") == 0) {
1574 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1577 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1580 if (dirname == NULL) {
1581 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1582 error = got_error_from_errno("asprintf");
1585 repo_path = default_destdir;
1587 repo_path = dirname;
1589 if (!list_refs_only) {
1590 error = got_path_mkdir(repo_path);
1592 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1593 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1595 if (!got_path_dir_is_empty(repo_path)) {
1596 error = got_error_path(repo_path,
1597 GOT_ERR_DIR_NOT_EMPTY);
1602 error = got_dial_apply_unveil(proto);
1606 error = apply_unveil(repo_path, 0, NULL);
1611 printf("Connecting to %s%s%s\n", host,
1612 port ? ":" : "", port ? port : "");
1614 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1615 server_path, verbosity);
1619 if (!list_refs_only) {
1620 error = got_repo_init(repo_path);
1623 error = got_repo_open(&repo, repo_path, NULL);
1628 fpa.last_scaled_size[0] = '\0';
1629 fpa.last_p_indexed = -1;
1630 fpa.last_p_resolved = -1;
1631 fpa.verbosity = verbosity;
1632 fpa.create_configs = 1;
1633 fpa.configs_created = 0;
1635 fpa.config_info.symrefs = &symrefs;
1636 fpa.config_info.wanted_branches = &wanted_branches;
1637 fpa.config_info.wanted_refs = &wanted_refs;
1638 fpa.config_info.proto = proto;
1639 fpa.config_info.host = host;
1640 fpa.config_info.port = port;
1641 fpa.config_info.remote_repo_path = server_path;
1642 fpa.config_info.git_url = git_url;
1643 fpa.config_info.fetch_all_branches = fetch_all_branches;
1644 fpa.config_info.mirror_references = mirror_references;
1645 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1646 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1647 fetch_all_branches, &wanted_branches, &wanted_refs,
1648 list_refs_only, verbosity, fetchfd, repo,
1649 fetch_progress, &fpa);
1653 if (list_refs_only) {
1654 error = list_remote_refs(&symrefs, &refs);
1658 error = got_object_id_str(&id_str, pack_hash);
1662 printf("\nFetched %s.pack\n", id_str);
1665 /* Set up references provided with the pack file. */
1666 TAILQ_FOREACH(pe, &refs, entry) {
1667 const char *refname = pe->path;
1668 struct got_object_id *id = pe->data;
1669 char *remote_refname;
1671 if (is_wanted_ref(&wanted_refs, refname) &&
1672 !mirror_references) {
1673 error = create_wanted_ref(refname, id,
1674 GOT_FETCH_DEFAULT_REMOTE_NAME,
1675 verbosity - 1, repo);
1681 error = create_ref(refname, id, verbosity - 1, repo);
1685 if (mirror_references)
1688 if (strncmp("refs/heads/", refname, 11) != 0)
1691 if (asprintf(&remote_refname,
1692 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1693 refname + 11) == -1) {
1694 error = got_error_from_errno("asprintf");
1697 error = create_ref(remote_refname, id, verbosity - 1, repo);
1698 free(remote_refname);
1703 /* Set the HEAD reference if the server provided one. */
1704 TAILQ_FOREACH(pe, &symrefs, entry) {
1705 struct got_reference *target_ref;
1706 const char *refname = pe->path;
1707 const char *target = pe->data;
1708 char *remote_refname = NULL, *remote_target = NULL;
1710 if (strcmp(refname, GOT_REF_HEAD) != 0)
1713 error = got_ref_open(&target_ref, repo, target, 0);
1715 if (error->code == GOT_ERR_NOT_REF) {
1722 error = create_symref(refname, target_ref, verbosity, repo);
1723 got_ref_close(target_ref);
1727 if (mirror_references)
1730 if (strncmp("refs/heads/", target, 11) != 0)
1733 if (asprintf(&remote_refname,
1734 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1736 error = got_error_from_errno("asprintf");
1739 if (asprintf(&remote_target,
1740 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1741 target + 11) == -1) {
1742 error = got_error_from_errno("asprintf");
1743 free(remote_refname);
1746 error = got_ref_open(&target_ref, repo, remote_target, 0);
1748 free(remote_refname);
1749 free(remote_target);
1750 if (error->code == GOT_ERR_NOT_REF) {
1756 error = create_symref(remote_refname, target_ref,
1757 verbosity - 1, repo);
1758 free(remote_refname);
1759 free(remote_target);
1760 got_ref_close(target_ref);
1766 * We failed to set the HEAD reference. If we asked for
1767 * a set of wanted branches use the first of one of those
1768 * which could be fetched instead.
1770 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1771 const char *target = pe->path;
1772 struct got_reference *target_ref;
1774 error = got_ref_open(&target_ref, repo, target, 0);
1776 if (error->code == GOT_ERR_NOT_REF) {
1783 error = create_symref(GOT_REF_HEAD, target_ref,
1785 got_ref_close(target_ref);
1793 printf("Created %s repository '%s'\n",
1794 mirror_references ? "mirrored" : "cloned", repo_path);
1797 if (kill(fetchpid, SIGTERM) == -1)
1798 error = got_error_from_errno("kill");
1799 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1800 error = got_error_from_errno("waitpid");
1802 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1803 error = got_error_from_errno("close");
1805 const struct got_error *close_err = got_repo_close(repo);
1809 TAILQ_FOREACH(pe, &refs, entry) {
1810 free((void *)pe->path);
1813 got_pathlist_free(&refs);
1814 TAILQ_FOREACH(pe, &symrefs, entry) {
1815 free((void *)pe->path);
1818 got_pathlist_free(&symrefs);
1819 got_pathlist_free(&wanted_branches);
1820 got_pathlist_free(&wanted_refs);
1827 free(default_destdir);
1832 static const struct got_error *
1833 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1834 int replace_tags, int verbosity, struct got_repository *repo)
1836 const struct got_error *err = NULL;
1837 char *new_id_str = NULL;
1838 struct got_object_id *old_id = NULL;
1840 err = got_object_id_str(&new_id_str, new_id);
1844 if (!replace_tags &&
1845 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1846 err = got_ref_resolve(&old_id, repo, ref);
1849 if (got_object_id_cmp(old_id, new_id) == 0)
1851 if (verbosity >= 0) {
1852 printf("Rejecting update of existing tag %s: %s\n",
1853 got_ref_get_name(ref), new_id_str);
1858 if (got_ref_is_symbolic(ref)) {
1859 if (verbosity >= 0) {
1860 printf("Replacing reference %s: %s\n",
1861 got_ref_get_name(ref),
1862 got_ref_get_symref_target(ref));
1864 err = got_ref_change_symref_to_ref(ref, new_id);
1867 err = got_ref_write(ref, repo);
1871 err = got_ref_resolve(&old_id, repo, ref);
1874 if (got_object_id_cmp(old_id, new_id) == 0)
1877 err = got_ref_change_ref(ref, new_id);
1880 err = got_ref_write(ref, repo);
1886 printf("Updated %s: %s\n", got_ref_get_name(ref),
1894 static const struct got_error *
1895 update_symref(const char *refname, struct got_reference *target_ref,
1896 int verbosity, struct got_repository *repo)
1898 const struct got_error *err = NULL, *unlock_err;
1899 struct got_reference *symref;
1900 int symref_is_locked = 0;
1902 err = got_ref_open(&symref, repo, refname, 1);
1904 if (err->code != GOT_ERR_NOT_REF)
1906 err = got_ref_alloc_symref(&symref, refname, target_ref);
1910 err = got_ref_write(symref, repo);
1915 printf("Created reference %s: %s\n",
1916 got_ref_get_name(symref),
1917 got_ref_get_symref_target(symref));
1919 symref_is_locked = 1;
1921 if (strcmp(got_ref_get_symref_target(symref),
1922 got_ref_get_name(target_ref)) == 0)
1925 err = got_ref_change_symref(symref,
1926 got_ref_get_name(target_ref));
1930 err = got_ref_write(symref, repo);
1935 printf("Updated %s: %s\n", got_ref_get_name(symref),
1936 got_ref_get_symref_target(symref));
1940 if (symref_is_locked) {
1941 unlock_err = got_ref_unlock(symref);
1942 if (unlock_err && err == NULL)
1945 got_ref_close(symref);
1952 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1953 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1954 "[remote-repository-name]\n",
1959 static const struct got_error *
1960 delete_missing_ref(struct got_reference *ref,
1961 int verbosity, struct got_repository *repo)
1963 const struct got_error *err = NULL;
1964 struct got_object_id *id = NULL;
1965 char *id_str = NULL;
1967 if (got_ref_is_symbolic(ref)) {
1968 err = got_ref_delete(ref, repo);
1971 if (verbosity >= 0) {
1972 printf("Deleted %s: %s\n",
1973 got_ref_get_name(ref),
1974 got_ref_get_symref_target(ref));
1977 err = got_ref_resolve(&id, repo, ref);
1980 err = got_object_id_str(&id_str, id);
1984 err = got_ref_delete(ref, repo);
1987 if (verbosity >= 0) {
1988 printf("Deleted %s: %s\n",
1989 got_ref_get_name(ref), id_str);
1998 static const struct got_error *
1999 delete_missing_refs(struct got_pathlist_head *their_refs,
2000 struct got_pathlist_head *their_symrefs,
2001 const struct got_remote_repo *remote,
2002 int verbosity, struct got_repository *repo)
2004 const struct got_error *err = NULL, *unlock_err;
2005 struct got_reflist_head my_refs;
2006 struct got_reflist_entry *re;
2007 struct got_pathlist_entry *pe;
2008 char *remote_namespace = NULL;
2009 char *local_refname = NULL;
2011 TAILQ_INIT(&my_refs);
2013 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2015 return got_error_from_errno("asprintf");
2017 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2021 TAILQ_FOREACH(re, &my_refs, entry) {
2022 const char *refname = got_ref_get_name(re->ref);
2023 const char *their_refname;
2025 if (remote->mirror_references) {
2026 their_refname = refname;
2028 if (strncmp(refname, remote_namespace,
2029 strlen(remote_namespace)) == 0) {
2030 if (strcmp(refname + strlen(remote_namespace),
2033 if (asprintf(&local_refname, "refs/heads/%s",
2034 refname + strlen(remote_namespace)) == -1) {
2035 err = got_error_from_errno("asprintf");
2038 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2041 their_refname = local_refname;
2044 TAILQ_FOREACH(pe, their_refs, entry) {
2045 if (strcmp(their_refname, pe->path) == 0)
2051 TAILQ_FOREACH(pe, their_symrefs, entry) {
2052 if (strcmp(their_refname, pe->path) == 0)
2058 err = delete_missing_ref(re->ref, verbosity, repo);
2062 if (local_refname) {
2063 struct got_reference *ref;
2064 err = got_ref_open(&ref, repo, local_refname, 1);
2066 if (err->code != GOT_ERR_NOT_REF)
2068 free(local_refname);
2069 local_refname = NULL;
2072 err = delete_missing_ref(ref, verbosity, repo);
2075 unlock_err = got_ref_unlock(ref);
2077 if (unlock_err && err == NULL) {
2082 free(local_refname);
2083 local_refname = NULL;
2087 free(remote_namespace);
2088 free(local_refname);
2092 static const struct got_error *
2093 update_wanted_ref(const char *refname, struct got_object_id *id,
2094 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2096 const struct got_error *err, *unlock_err;
2097 char *remote_refname;
2098 struct got_reference *ref;
2100 if (strncmp("refs/", refname, 5) == 0)
2103 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2104 remote_repo_name, refname) == -1)
2105 return got_error_from_errno("asprintf");
2107 err = got_ref_open(&ref, repo, remote_refname, 1);
2109 if (err->code != GOT_ERR_NOT_REF)
2111 err = create_ref(remote_refname, id, verbosity, repo);
2113 err = update_ref(ref, id, 0, verbosity, repo);
2114 unlock_err = got_ref_unlock(ref);
2115 if (unlock_err && err == NULL)
2120 free(remote_refname);
2124 static const struct got_error *
2125 delete_ref(struct got_repository *repo, struct got_reference *ref)
2127 const struct got_error *err = NULL;
2128 struct got_object_id *id = NULL;
2129 char *id_str = NULL;
2132 if (got_ref_is_symbolic(ref)) {
2133 target = got_ref_get_symref_target(ref);
2135 err = got_ref_resolve(&id, repo, ref);
2138 err = got_object_id_str(&id_str, id);
2144 err = got_ref_delete(ref, repo);
2148 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2155 static const struct got_error *
2156 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2158 const struct got_error *err = NULL;
2159 struct got_reflist_head refs;
2160 struct got_reflist_entry *re;
2165 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2166 err = got_error_from_errno("asprintf");
2169 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2173 TAILQ_FOREACH(re, &refs, entry)
2174 delete_ref(repo, re->ref);
2176 got_ref_list_free(&refs);
2180 static const struct got_error *
2181 cmd_fetch(int argc, char *argv[])
2183 const struct got_error *error = NULL, *unlock_err;
2184 char *cwd = NULL, *repo_path = NULL;
2185 const char *remote_name;
2186 char *proto = NULL, *host = NULL, *port = NULL;
2187 char *repo_name = NULL, *server_path = NULL;
2188 const struct got_remote_repo *remotes, *remote = NULL;
2190 char *id_str = NULL;
2191 struct got_repository *repo = NULL;
2192 struct got_worktree *worktree = NULL;
2193 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2194 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2195 struct got_pathlist_entry *pe;
2196 struct got_object_id *pack_hash = NULL;
2197 int i, ch, fetchfd = -1, fetchstatus;
2198 pid_t fetchpid = -1;
2199 struct got_fetch_progress_arg fpa;
2200 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2201 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2204 TAILQ_INIT(&symrefs);
2205 TAILQ_INIT(&wanted_branches);
2206 TAILQ_INIT(&wanted_refs);
2208 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2211 fetch_all_branches = 1;
2214 error = got_pathlist_append(&wanted_branches,
2226 repo_path = realpath(optarg, NULL);
2227 if (repo_path == NULL)
2228 return got_error_from_errno2("realpath",
2230 got_path_strip_trailing_slashes(repo_path);
2238 else if (verbosity < 3)
2245 error = got_pathlist_append(&wanted_refs,
2261 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2262 option_conflict('a', 'b');
2263 if (list_refs_only) {
2264 if (!TAILQ_EMPTY(&wanted_branches))
2265 option_conflict('l', 'b');
2266 if (fetch_all_branches)
2267 option_conflict('l', 'a');
2269 option_conflict('l', 'd');
2271 option_conflict('l', 'X');
2273 if (delete_remote) {
2274 if (fetch_all_branches)
2275 option_conflict('X', 'a');
2276 if (!TAILQ_EMPTY(&wanted_branches))
2277 option_conflict('X', 'b');
2279 option_conflict('X', 'd');
2281 option_conflict('X', 't');
2282 if (!TAILQ_EMPTY(&wanted_refs))
2283 option_conflict('X', 'R');
2288 errx(1, "-X option requires a remote name");
2289 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2290 } else if (argc == 1)
2291 remote_name = argv[0];
2295 cwd = getcwd(NULL, 0);
2297 error = got_error_from_errno("getcwd");
2301 if (repo_path == NULL) {
2302 error = got_worktree_open(&worktree, cwd);
2303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2309 strdup(got_worktree_get_repo_path(worktree));
2310 if (repo_path == NULL)
2311 error = got_error_from_errno("strdup");
2315 repo_path = strdup(cwd);
2316 if (repo_path == NULL) {
2317 error = got_error_from_errno("strdup");
2323 error = got_repo_open(&repo, repo_path, NULL);
2327 if (delete_remote) {
2328 error = delete_refs_for_remote(repo, remote_name);
2329 goto done; /* nothing else to do */
2333 worktree_conf = got_worktree_get_gotconfig(worktree);
2334 if (worktree_conf) {
2335 got_gotconfig_get_remotes(&nremotes, &remotes,
2337 for (i = 0; i < nremotes; i++) {
2338 if (strcmp(remotes[i].name, remote_name) == 0) {
2339 remote = &remotes[i];
2345 if (remote == NULL) {
2346 repo_conf = got_repo_get_gotconfig(repo);
2348 got_gotconfig_get_remotes(&nremotes, &remotes,
2350 for (i = 0; i < nremotes; i++) {
2351 if (strcmp(remotes[i].name, remote_name) == 0) {
2352 remote = &remotes[i];
2358 if (remote == NULL) {
2359 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2360 for (i = 0; i < nremotes; i++) {
2361 if (strcmp(remotes[i].name, remote_name) == 0) {
2362 remote = &remotes[i];
2367 if (remote == NULL) {
2368 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2372 if (TAILQ_EMPTY(&wanted_branches)) {
2373 if (!fetch_all_branches)
2374 fetch_all_branches = remote->fetch_all_branches;
2375 for (i = 0; i < remote->nfetch_branches; i++) {
2376 got_pathlist_append(&wanted_branches,
2377 remote->fetch_branches[i], NULL);
2380 if (TAILQ_EMPTY(&wanted_refs)) {
2381 for (i = 0; i < remote->nfetch_refs; i++) {
2382 got_pathlist_append(&wanted_refs,
2383 remote->fetch_refs[i], NULL);
2387 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2388 &repo_name, remote->fetch_url);
2392 if (strcmp(proto, "git") == 0) {
2394 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2395 "sendfd dns inet unveil", NULL) == -1)
2398 } else if (strcmp(proto, "git+ssh") == 0 ||
2399 strcmp(proto, "ssh") == 0) {
2401 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2402 "sendfd unveil", NULL) == -1)
2405 } else if (strcmp(proto, "http") == 0 ||
2406 strcmp(proto, "git+http") == 0) {
2407 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2410 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2414 error = got_dial_apply_unveil(proto);
2418 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2423 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2424 port ? ":" : "", port ? port : "");
2426 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2427 server_path, verbosity);
2431 fpa.last_scaled_size[0] = '\0';
2432 fpa.last_p_indexed = -1;
2433 fpa.last_p_resolved = -1;
2434 fpa.verbosity = verbosity;
2436 fpa.create_configs = 0;
2437 fpa.configs_created = 0;
2438 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2439 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2440 remote->mirror_references, fetch_all_branches, &wanted_branches,
2441 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2442 fetch_progress, &fpa);
2446 if (list_refs_only) {
2447 error = list_remote_refs(&symrefs, &refs);
2451 if (pack_hash == NULL) {
2453 printf("Already up-to-date\n");
2454 } else if (verbosity >= 0) {
2455 error = got_object_id_str(&id_str, pack_hash);
2458 printf("\nFetched %s.pack\n", id_str);
2463 /* Update references provided with the pack file. */
2464 TAILQ_FOREACH(pe, &refs, entry) {
2465 const char *refname = pe->path;
2466 struct got_object_id *id = pe->data;
2467 struct got_reference *ref;
2468 char *remote_refname;
2470 if (is_wanted_ref(&wanted_refs, refname) &&
2471 !remote->mirror_references) {
2472 error = update_wanted_ref(refname, id,
2473 remote->name, verbosity, repo);
2479 if (remote->mirror_references ||
2480 strncmp("refs/tags/", refname, 10) == 0) {
2481 error = got_ref_open(&ref, repo, refname, 1);
2483 if (error->code != GOT_ERR_NOT_REF)
2485 error = create_ref(refname, id, verbosity,
2490 error = update_ref(ref, id, replace_tags,
2492 unlock_err = got_ref_unlock(ref);
2493 if (unlock_err && error == NULL)
2499 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2500 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2501 remote_name, refname + 11) == -1) {
2502 error = got_error_from_errno("asprintf");
2506 error = got_ref_open(&ref, repo, remote_refname, 1);
2508 if (error->code != GOT_ERR_NOT_REF)
2510 error = create_ref(remote_refname, id,
2515 error = update_ref(ref, id, replace_tags,
2517 unlock_err = got_ref_unlock(ref);
2518 if (unlock_err && error == NULL)
2525 /* Also create a local branch if none exists yet. */
2526 error = got_ref_open(&ref, repo, refname, 1);
2528 if (error->code != GOT_ERR_NOT_REF)
2530 error = create_ref(refname, id, verbosity,
2535 unlock_err = got_ref_unlock(ref);
2536 if (unlock_err && error == NULL)
2543 error = delete_missing_refs(&refs, &symrefs, remote,
2549 if (!remote->mirror_references) {
2550 /* Update remote HEAD reference if the server provided one. */
2551 TAILQ_FOREACH(pe, &symrefs, entry) {
2552 struct got_reference *target_ref;
2553 const char *refname = pe->path;
2554 const char *target = pe->data;
2555 char *remote_refname = NULL, *remote_target = NULL;
2557 if (strcmp(refname, GOT_REF_HEAD) != 0)
2560 if (strncmp("refs/heads/", target, 11) != 0)
2563 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2564 remote->name, refname) == -1) {
2565 error = got_error_from_errno("asprintf");
2568 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2569 remote->name, target + 11) == -1) {
2570 error = got_error_from_errno("asprintf");
2571 free(remote_refname);
2575 error = got_ref_open(&target_ref, repo, remote_target,
2578 free(remote_refname);
2579 free(remote_target);
2580 if (error->code == GOT_ERR_NOT_REF) {
2586 error = update_symref(remote_refname, target_ref,
2588 free(remote_refname);
2589 free(remote_target);
2590 got_ref_close(target_ref);
2597 if (kill(fetchpid, SIGTERM) == -1)
2598 error = got_error_from_errno("kill");
2599 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2600 error = got_error_from_errno("waitpid");
2602 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2603 error = got_error_from_errno("close");
2605 const struct got_error *close_err = got_repo_close(repo);
2610 got_worktree_close(worktree);
2611 TAILQ_FOREACH(pe, &refs, entry) {
2612 free((void *)pe->path);
2615 got_pathlist_free(&refs);
2616 TAILQ_FOREACH(pe, &symrefs, entry) {
2617 free((void *)pe->path);
2620 got_pathlist_free(&symrefs);
2621 got_pathlist_free(&wanted_branches);
2622 got_pathlist_free(&wanted_refs);
2637 usage_checkout(void)
2639 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2645 show_worktree_base_ref_warning(void)
2647 fprintf(stderr, "%s: warning: could not create a reference "
2648 "to the work tree's base commit; the commit could be "
2649 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2650 "repository writable and running 'got update' will prevent this\n",
2654 struct got_checkout_progress_arg {
2655 const char *worktree_path;
2656 int had_base_commit_ref_error;
2659 static const struct got_error *
2660 checkout_progress(void *arg, unsigned char status, const char *path)
2662 struct got_checkout_progress_arg *a = arg;
2664 /* Base commit bump happens silently. */
2665 if (status == GOT_STATUS_BUMP_BASE)
2668 if (status == GOT_STATUS_BASE_REF_ERR) {
2669 a->had_base_commit_ref_error = 1;
2673 while (path[0] == '/')
2676 printf("%c %s/%s\n", status, a->worktree_path, path);
2680 static const struct got_error *
2681 check_cancelled(void *arg)
2683 if (sigint_received || sigpipe_received)
2684 return got_error(GOT_ERR_CANCELLED);
2688 static const struct got_error *
2689 check_linear_ancestry(struct got_object_id *commit_id,
2690 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2691 struct got_repository *repo)
2693 const struct got_error *err = NULL;
2694 struct got_object_id *yca_id;
2696 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2697 commit_id, base_commit_id, repo, check_cancelled, NULL);
2702 return got_error(GOT_ERR_ANCESTRY);
2705 * Require a straight line of history between the target commit
2706 * and the work tree's base commit.
2708 * Non-linear situations such as this require a rebase:
2710 * (commit) D F (base_commit)
2718 * 'got update' only handles linear cases:
2719 * Update forwards in time: A (base/yca) - B - C - D (commit)
2720 * Update backwards in time: D (base) - C - B - A (commit/yca)
2722 if (allow_forwards_in_time_only) {
2723 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2724 return got_error(GOT_ERR_ANCESTRY);
2725 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2726 got_object_id_cmp(base_commit_id, yca_id) != 0)
2727 return got_error(GOT_ERR_ANCESTRY);
2733 static const struct got_error *
2734 check_same_branch(struct got_object_id *commit_id,
2735 struct got_reference *head_ref, struct got_object_id *yca_id,
2736 struct got_repository *repo)
2738 const struct got_error *err = NULL;
2739 struct got_commit_graph *graph = NULL;
2740 struct got_object_id *head_commit_id = NULL;
2741 int is_same_branch = 0;
2743 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2747 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2751 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2756 err = got_commit_graph_open(&graph, "/", 1);
2760 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2761 check_cancelled, NULL);
2766 struct got_object_id *id;
2767 err = got_commit_graph_iter_next(&id, graph, repo,
2768 check_cancelled, NULL);
2770 if (err->code == GOT_ERR_ITER_COMPLETED)
2776 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2778 if (got_object_id_cmp(id, commit_id) == 0) {
2786 got_commit_graph_close(graph);
2787 free(head_commit_id);
2788 if (!err && !is_same_branch)
2789 err = got_error(GOT_ERR_ANCESTRY);
2793 static const struct got_error *
2794 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2796 static char msg[512];
2797 const char *branch_name;
2799 if (got_ref_is_symbolic(ref))
2800 branch_name = got_ref_get_symref_target(ref);
2802 branch_name = got_ref_get_name(ref);
2804 if (strncmp("refs/heads/", branch_name, 11) == 0)
2807 snprintf(msg, sizeof(msg),
2808 "target commit is not contained in branch '%s'; "
2809 "the branch to use must be specified with -b; "
2810 "if necessary a new branch can be created for "
2811 "this commit with 'got branch -c %s BRANCH_NAME'",
2812 branch_name, commit_id_str);
2814 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2817 static const struct got_error *
2818 cmd_checkout(int argc, char *argv[])
2820 const struct got_error *error = NULL;
2821 struct got_repository *repo = NULL;
2822 struct got_reference *head_ref = NULL;
2823 struct got_worktree *worktree = NULL;
2824 char *repo_path = NULL;
2825 char *worktree_path = NULL;
2826 const char *path_prefix = "";
2827 const char *branch_name = GOT_REF_HEAD;
2828 char *commit_id_str = NULL;
2830 int ch, same_path_prefix, allow_nonempty = 0;
2831 struct got_pathlist_head paths;
2832 struct got_checkout_progress_arg cpa;
2836 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2839 branch_name = optarg;
2842 commit_id_str = strdup(optarg);
2843 if (commit_id_str == NULL)
2844 return got_error_from_errno("strdup");
2850 path_prefix = optarg;
2862 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2863 "unveil", NULL) == -1)
2867 char *base, *dotgit;
2869 repo_path = realpath(argv[0], NULL);
2870 if (repo_path == NULL)
2871 return got_error_from_errno2("realpath", argv[0]);
2872 cwd = getcwd(NULL, 0);
2874 error = got_error_from_errno("getcwd");
2881 error = got_path_basename(&base, path);
2884 dotgit = strstr(base, ".git");
2887 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2888 error = got_error_from_errno("asprintf");
2893 } else if (argc == 2) {
2894 repo_path = realpath(argv[0], NULL);
2895 if (repo_path == NULL) {
2896 error = got_error_from_errno2("realpath", argv[0]);
2899 worktree_path = realpath(argv[1], NULL);
2900 if (worktree_path == NULL) {
2901 if (errno != ENOENT) {
2902 error = got_error_from_errno2("realpath",
2906 worktree_path = strdup(argv[1]);
2907 if (worktree_path == NULL) {
2908 error = got_error_from_errno("strdup");
2915 got_path_strip_trailing_slashes(repo_path);
2916 got_path_strip_trailing_slashes(worktree_path);
2918 error = got_repo_open(&repo, repo_path, NULL);
2922 /* Pre-create work tree path for unveil(2) */
2923 error = got_path_mkdir(worktree_path);
2925 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2926 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2928 if (!allow_nonempty &&
2929 !got_path_dir_is_empty(worktree_path)) {
2930 error = got_error_path(worktree_path,
2931 GOT_ERR_DIR_NOT_EMPTY);
2936 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2940 error = got_ref_open(&head_ref, repo, branch_name, 0);
2944 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2945 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2948 error = got_worktree_open(&worktree, worktree_path);
2952 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2956 if (!same_path_prefix) {
2957 error = got_error(GOT_ERR_PATH_PREFIX);
2961 if (commit_id_str) {
2962 struct got_object_id *commit_id;
2963 struct got_reflist_head refs;
2965 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2969 error = got_repo_match_object_id(&commit_id, NULL,
2970 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2971 got_ref_list_free(&refs);
2974 error = check_linear_ancestry(commit_id,
2975 got_worktree_get_base_commit_id(worktree), 0, repo);
2976 if (error != NULL) {
2978 if (error->code == GOT_ERR_ANCESTRY) {
2979 error = checkout_ancestry_error(
2980 head_ref, commit_id_str);
2984 error = check_same_branch(commit_id, head_ref, NULL, repo);
2986 if (error->code == GOT_ERR_ANCESTRY) {
2987 error = checkout_ancestry_error(
2988 head_ref, commit_id_str);
2992 error = got_worktree_set_base_commit_id(worktree, repo,
2999 error = got_pathlist_append(&paths, "", NULL);
3002 cpa.worktree_path = worktree_path;
3003 cpa.had_base_commit_ref_error = 0;
3004 error = got_worktree_checkout_files(worktree, &paths, repo,
3005 checkout_progress, &cpa, check_cancelled, NULL);
3009 printf("Now shut up and hack\n");
3010 if (cpa.had_base_commit_ref_error)
3011 show_worktree_base_ref_warning();
3013 got_pathlist_free(&paths);
3014 free(commit_id_str);
3016 free(worktree_path);
3021 struct got_update_progress_arg {
3029 print_update_progress_stats(struct got_update_progress_arg *upa)
3031 if (!upa->did_something)
3034 if (upa->conflicts > 0)
3035 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3036 if (upa->obstructed > 0)
3037 printf("File paths obstructed by a non-regular file: %d\n",
3039 if (upa->not_updated > 0)
3040 printf("Files not updated because of existing merge "
3041 "conflicts: %d\n", upa->not_updated);
3047 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3052 static const struct got_error *
3053 update_progress(void *arg, unsigned char status, const char *path)
3055 struct got_update_progress_arg *upa = arg;
3057 if (status == GOT_STATUS_EXISTS ||
3058 status == GOT_STATUS_BASE_REF_ERR)
3061 upa->did_something = 1;
3063 /* Base commit bump happens silently. */
3064 if (status == GOT_STATUS_BUMP_BASE)
3067 if (status == GOT_STATUS_CONFLICT)
3069 if (status == GOT_STATUS_OBSTRUCTED)
3071 if (status == GOT_STATUS_CANNOT_UPDATE)
3074 while (path[0] == '/')
3076 printf("%c %s\n", status, path);
3080 static const struct got_error *
3081 switch_head_ref(struct got_reference *head_ref,
3082 struct got_object_id *commit_id, struct got_worktree *worktree,
3083 struct got_repository *repo)
3085 const struct got_error *err = NULL;
3087 int ref_has_moved = 0;
3089 /* Trivial case: switching between two different references. */
3090 if (strcmp(got_ref_get_name(head_ref),
3091 got_worktree_get_head_ref_name(worktree)) != 0) {
3092 printf("Switching work tree from %s to %s\n",
3093 got_worktree_get_head_ref_name(worktree),
3094 got_ref_get_name(head_ref));
3095 return got_worktree_set_head_ref(worktree, head_ref);
3098 err = check_linear_ancestry(commit_id,
3099 got_worktree_get_base_commit_id(worktree), 0, repo);
3101 if (err->code != GOT_ERR_ANCESTRY)
3108 /* Switching to a rebased branch with the same reference name. */
3109 err = got_object_id_str(&base_id_str,
3110 got_worktree_get_base_commit_id(worktree));
3113 printf("Reference %s now points at a different branch\n",
3114 got_worktree_get_head_ref_name(worktree));
3115 printf("Switching work tree from %s to %s\n", base_id_str,
3116 got_worktree_get_head_ref_name(worktree));
3120 static const struct got_error *
3121 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3123 const struct got_error *err;
3126 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3130 return got_error(GOT_ERR_REBASING);
3132 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3136 return got_error(GOT_ERR_HISTEDIT_BUSY);
3141 static const struct got_error *
3142 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3143 char *argv[], struct got_worktree *worktree)
3145 const struct got_error *err = NULL;
3152 return got_error_from_errno("strdup");
3153 return got_pathlist_append(paths, path, NULL);
3156 for (i = 0; i < argc; i++) {
3157 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3160 err = got_pathlist_append(paths, path, NULL);
3170 static const struct got_error *
3171 wrap_not_worktree_error(const struct got_error *orig_err,
3172 const char *cmdname, const char *path)
3174 const struct got_error *err;
3175 struct got_repository *repo;
3176 static char msg[512];
3178 err = got_repo_open(&repo, path, NULL);
3182 snprintf(msg, sizeof(msg),
3183 "'got %s' needs a work tree in addition to a git repository\n"
3184 "Work trees can be checked out from this Git repository with "
3186 "The got(1) manual page contains more information.", cmdname);
3187 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3188 got_repo_close(repo);
3192 static const struct got_error *
3193 cmd_update(int argc, char *argv[])
3195 const struct got_error *error = NULL;
3196 struct got_repository *repo = NULL;
3197 struct got_worktree *worktree = NULL;
3198 char *worktree_path = NULL;
3199 struct got_object_id *commit_id = NULL;
3200 char *commit_id_str = NULL;
3201 const char *branch_name = NULL;
3202 struct got_reference *head_ref = NULL;
3203 struct got_pathlist_head paths;
3204 struct got_pathlist_entry *pe;
3206 struct got_update_progress_arg upa;
3210 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3213 branch_name = optarg;
3216 commit_id_str = strdup(optarg);
3217 if (commit_id_str == NULL)
3218 return got_error_from_errno("strdup");
3230 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3231 "unveil", NULL) == -1)
3234 worktree_path = getcwd(NULL, 0);
3235 if (worktree_path == NULL) {
3236 error = got_error_from_errno("getcwd");
3239 error = got_worktree_open(&worktree, worktree_path);
3241 if (error->code == GOT_ERR_NOT_WORKTREE)
3242 error = wrap_not_worktree_error(error, "update",
3247 error = check_rebase_or_histedit_in_progress(worktree);
3251 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3256 error = apply_unveil(got_repo_get_path(repo), 0,
3257 got_worktree_get_root_path(worktree));
3261 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3265 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3266 got_worktree_get_head_ref_name(worktree), 0);
3269 if (commit_id_str == NULL) {
3270 error = got_ref_resolve(&commit_id, repo, head_ref);
3273 error = got_object_id_str(&commit_id_str, commit_id);
3277 struct got_reflist_head refs;
3279 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3283 error = got_repo_match_object_id(&commit_id, NULL,
3284 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3285 got_ref_list_free(&refs);
3286 free(commit_id_str);
3287 commit_id_str = NULL;
3290 error = got_object_id_str(&commit_id_str, commit_id);
3296 struct got_object_id *head_commit_id;
3297 TAILQ_FOREACH(pe, &paths, entry) {
3298 if (pe->path_len == 0)
3300 error = got_error_msg(GOT_ERR_BAD_PATH,
3301 "switching between branches requires that "
3302 "the entire work tree gets updated");
3305 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3308 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3310 free(head_commit_id);
3313 error = check_same_branch(commit_id, head_ref, NULL, repo);
3316 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3320 error = check_linear_ancestry(commit_id,
3321 got_worktree_get_base_commit_id(worktree), 0, repo);
3322 if (error != NULL) {
3323 if (error->code == GOT_ERR_ANCESTRY)
3324 error = got_error(GOT_ERR_BRANCH_MOVED);
3327 error = check_same_branch(commit_id, head_ref, NULL, repo);
3332 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3334 error = got_worktree_set_base_commit_id(worktree, repo,
3340 memset(&upa, 0, sizeof(upa));
3341 error = got_worktree_checkout_files(worktree, &paths, repo,
3342 update_progress, &upa, check_cancelled, NULL);
3346 if (upa.did_something)
3347 printf("Updated to commit %s\n", commit_id_str);
3349 printf("Already up-to-date\n");
3350 print_update_progress_stats(&upa);
3352 free(worktree_path);
3353 TAILQ_FOREACH(pe, &paths, entry)
3354 free((char *)pe->path);
3355 got_pathlist_free(&paths);
3357 free(commit_id_str);
3361 static const struct got_error *
3362 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3363 const char *path, int diff_context, int ignore_whitespace,
3364 int force_text_diff, struct got_repository *repo)
3366 const struct got_error *err = NULL;
3367 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3370 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3375 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3379 while (path[0] == '/')
3381 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3382 diff_context, ignore_whitespace, force_text_diff, stdout);
3385 got_object_blob_close(blob1);
3386 got_object_blob_close(blob2);
3390 static const struct got_error *
3391 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3392 const char *path, int diff_context, int ignore_whitespace,
3393 int force_text_diff, struct got_repository *repo)
3395 const struct got_error *err = NULL;
3396 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3397 struct got_diff_blob_output_unidiff_arg arg;
3400 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3405 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3409 arg.diff_context = diff_context;
3410 arg.ignore_whitespace = ignore_whitespace;
3411 arg.force_text_diff = force_text_diff;
3412 arg.outfile = stdout;
3413 arg.line_offsets = NULL;
3415 while (path[0] == '/')
3417 err = got_diff_tree(tree1, tree2, path, path, repo,
3418 got_diff_blob_output_unidiff, &arg, 1);
3421 got_object_tree_close(tree1);
3423 got_object_tree_close(tree2);
3427 static const struct got_error *
3428 get_changed_paths(struct got_pathlist_head *paths,
3429 struct got_commit_object *commit, struct got_repository *repo)
3431 const struct got_error *err = NULL;
3432 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3433 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3434 struct got_object_qid *qid;
3436 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3438 struct got_commit_object *pcommit;
3439 err = got_object_open_as_commit(&pcommit, repo,
3444 tree_id1 = got_object_id_dup(
3445 got_object_commit_get_tree_id(pcommit));
3446 if (tree_id1 == NULL) {
3447 got_object_commit_close(pcommit);
3448 return got_error_from_errno("got_object_id_dup");
3450 got_object_commit_close(pcommit);
3455 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3460 tree_id2 = got_object_commit_get_tree_id(commit);
3461 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3465 err = got_diff_tree(tree1, tree2, "", "", repo,
3466 got_diff_tree_collect_changed_paths, paths, 0);
3469 got_object_tree_close(tree1);
3471 got_object_tree_close(tree2);
3476 static const struct got_error *
3477 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3478 const char *path, int diff_context, struct got_repository *repo)
3480 const struct got_error *err = NULL;
3481 struct got_commit_object *pcommit = NULL;
3482 char *id_str1 = NULL, *id_str2 = NULL;
3483 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3484 struct got_object_qid *qid;
3486 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3488 err = got_object_open_as_commit(&pcommit, repo,
3494 if (path && path[0] != '\0') {
3496 err = got_object_id_by_path(&obj_id2, repo, id, path);
3499 err = got_object_id_str(&id_str2, obj_id2);
3505 err = got_object_id_by_path(&obj_id1, repo,
3508 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3513 err = got_object_id_str(&id_str1, obj_id1);
3520 err = got_object_get_type(&obj_type, repo, obj_id2);
3525 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3527 case GOT_OBJ_TYPE_BLOB:
3528 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3531 case GOT_OBJ_TYPE_TREE:
3532 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3536 err = got_error(GOT_ERR_OBJ_TYPE);
3542 obj_id2 = got_object_commit_get_tree_id(commit);
3543 err = got_object_id_str(&id_str2, obj_id2);
3547 obj_id1 = got_object_commit_get_tree_id(pcommit);
3548 err = got_object_id_str(&id_str1, obj_id1);
3552 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3554 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3561 got_object_commit_close(pcommit);
3566 get_datestr(time_t *time, char *datebuf)
3568 struct tm mytm, *tm;
3571 tm = gmtime_r(time, &mytm);
3574 s = asctime_r(tm, datebuf);
3577 p = strchr(s, '\n');
3583 static const struct got_error *
3584 match_logmsg(int *have_match, struct got_object_id *id,
3585 struct got_commit_object *commit, regex_t *regex)
3587 const struct got_error *err = NULL;
3588 regmatch_t regmatch;
3589 char *id_str = NULL, *logmsg = NULL;
3593 err = got_object_id_str(&id_str, id);
3597 err = got_object_commit_get_logmsg(&logmsg, commit);
3601 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3610 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3613 regmatch_t regmatch;
3614 struct got_pathlist_entry *pe;
3618 TAILQ_FOREACH(pe, changed_paths, entry) {
3619 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3626 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3628 static const struct got_error*
3629 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3630 struct got_object_id *id, struct got_repository *repo)
3632 static const struct got_error *err = NULL;
3633 struct got_reflist_entry *re;
3639 TAILQ_FOREACH(re, refs, entry) {
3640 struct got_tag_object *tag = NULL;
3641 struct got_object_id *ref_id;
3644 name = got_ref_get_name(re->ref);
3645 if (strcmp(name, GOT_REF_HEAD) == 0)
3647 if (strncmp(name, "refs/", 5) == 0)
3649 if (strncmp(name, "got/", 4) == 0)
3651 if (strncmp(name, "heads/", 6) == 0)
3653 if (strncmp(name, "remotes/", 8) == 0) {
3655 s = strstr(name, "/" GOT_REF_HEAD);
3656 if (s != NULL && s[strlen(s)] == '\0')
3659 err = got_ref_resolve(&ref_id, repo, re->ref);
3662 if (strncmp(name, "tags/", 5) == 0) {
3663 err = got_object_open_as_tag(&tag, repo, ref_id);
3665 if (err->code != GOT_ERR_OBJ_TYPE) {
3669 /* Ref points at something other than a tag. */
3674 cmp = got_object_id_cmp(tag ?
3675 got_object_tag_get_object_id(tag) : ref_id, id);
3678 got_object_tag_close(tag);
3682 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3683 s ? ", " : "", name) == -1) {
3684 err = got_error_from_errno("asprintf");
3695 static const struct got_error *
3696 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3697 struct got_repository *repo, const char *path,
3698 struct got_pathlist_head *changed_paths, int show_patch,
3699 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3700 const char *custom_refs_str)
3702 const struct got_error *err = NULL;
3703 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3705 time_t committer_time;
3706 const char *author, *committer;
3707 char *refs_str = NULL;
3709 err = got_object_id_str(&id_str, id);
3713 if (custom_refs_str == NULL) {
3714 struct got_reflist_head *refs;
3715 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3717 err = build_refs_str(&refs_str, refs, id, repo);
3723 printf(GOT_COMMIT_SEP_STR);
3724 if (custom_refs_str)
3725 printf("commit %s (%s)\n", id_str, custom_refs_str);
3727 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3728 refs_str ? refs_str : "", refs_str ? ")" : "");
3733 printf("from: %s\n", got_object_commit_get_author(commit));
3734 committer_time = got_object_commit_get_committer_time(commit);
3735 datestr = get_datestr(&committer_time, datebuf);
3737 printf("date: %s UTC\n", datestr);
3738 author = got_object_commit_get_author(commit);
3739 committer = got_object_commit_get_committer(commit);
3740 if (strcmp(author, committer) != 0)
3741 printf("via: %s\n", committer);
3742 if (got_object_commit_get_nparents(commit) > 1) {
3743 const struct got_object_id_queue *parent_ids;
3744 struct got_object_qid *qid;
3746 parent_ids = got_object_commit_get_parent_ids(commit);
3747 STAILQ_FOREACH(qid, parent_ids, entry) {
3748 err = got_object_id_str(&id_str, qid->id);
3751 printf("parent %d: %s\n", n++, id_str);
3757 err = got_object_commit_get_logmsg(&logmsg0, commit);
3763 line = strsep(&logmsg, "\n");
3765 printf(" %s\n", line);
3769 if (changed_paths) {
3770 struct got_pathlist_entry *pe;
3771 TAILQ_FOREACH(pe, changed_paths, entry) {
3772 struct got_diff_changed_path *cp = pe->data;
3773 printf(" %c %s\n", cp->status, pe->path);
3778 err = print_patch(commit, id, path, diff_context, repo);
3783 if (fflush(stdout) != 0 && err == NULL)
3784 err = got_error_from_errno("fflush");
3791 static const struct got_error *
3792 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3793 struct got_repository *repo, const char *path, int show_changed_paths,
3794 int show_patch, const char *search_pattern, int diff_context, int limit,
3795 int log_branches, int reverse_display_order,
3796 struct got_reflist_object_id_map *refs_idmap)
3798 const struct got_error *err;
3799 struct got_commit_graph *graph;
3802 struct got_object_id_queue reversed_commits;
3803 struct got_object_qid *qid;
3804 struct got_commit_object *commit;
3805 struct got_pathlist_head changed_paths;
3806 struct got_pathlist_entry *pe;
3808 STAILQ_INIT(&reversed_commits);
3809 TAILQ_INIT(&changed_paths);
3811 if (search_pattern && regcomp(®ex, search_pattern,
3812 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3813 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3815 err = got_commit_graph_open(&graph, path, !log_branches);
3818 err = got_commit_graph_iter_start(graph, root_id, repo,
3819 check_cancelled, NULL);
3823 struct got_object_id *id;
3825 if (sigint_received || sigpipe_received)
3828 err = got_commit_graph_iter_next(&id, graph, repo,
3829 check_cancelled, NULL);
3831 if (err->code == GOT_ERR_ITER_COMPLETED)
3838 err = got_object_open_as_commit(&commit, repo, id);
3842 if (show_changed_paths && !reverse_display_order) {
3843 err = get_changed_paths(&changed_paths, commit, repo);
3848 if (search_pattern) {
3849 err = match_logmsg(&have_match, id, commit, ®ex);
3851 got_object_commit_close(commit);
3854 if (have_match == 0 && show_changed_paths)
3855 match_changed_paths(&have_match,
3856 &changed_paths, ®ex);
3857 if (have_match == 0) {
3858 got_object_commit_close(commit);
3859 TAILQ_FOREACH(pe, &changed_paths, entry) {
3860 free((char *)pe->path);
3863 got_pathlist_free(&changed_paths);
3868 if (reverse_display_order) {
3869 err = got_object_qid_alloc(&qid, id);
3872 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3873 got_object_commit_close(commit);
3875 err = print_commit(commit, id, repo, path,
3876 show_changed_paths ? &changed_paths : NULL,
3877 show_patch, diff_context, refs_idmap, NULL);
3878 got_object_commit_close(commit);
3882 if ((limit && --limit == 0) ||
3883 (end_id && got_object_id_cmp(id, end_id) == 0))
3886 TAILQ_FOREACH(pe, &changed_paths, entry) {
3887 free((char *)pe->path);
3890 got_pathlist_free(&changed_paths);
3892 if (reverse_display_order) {
3893 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3894 err = got_object_open_as_commit(&commit, repo, qid->id);
3897 if (show_changed_paths) {
3898 err = get_changed_paths(&changed_paths,
3903 err = print_commit(commit, qid->id, repo, path,
3904 show_changed_paths ? &changed_paths : NULL,
3905 show_patch, diff_context, refs_idmap, NULL);
3906 got_object_commit_close(commit);
3909 TAILQ_FOREACH(pe, &changed_paths, entry) {
3910 free((char *)pe->path);
3913 got_pathlist_free(&changed_paths);
3917 while (!STAILQ_EMPTY(&reversed_commits)) {
3918 qid = STAILQ_FIRST(&reversed_commits);
3919 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3920 got_object_qid_free(qid);
3922 TAILQ_FOREACH(pe, &changed_paths, entry) {
3923 free((char *)pe->path);
3926 got_pathlist_free(&changed_paths);
3929 got_commit_graph_close(graph);
3936 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3937 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3938 "[-R] [path]\n", getprogname());
3943 get_default_log_limit(void)
3945 const char *got_default_log_limit;
3949 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3950 if (got_default_log_limit == NULL)
3952 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3958 static const struct got_error *
3959 cmd_log(int argc, char *argv[])
3961 const struct got_error *error;
3962 struct got_repository *repo = NULL;
3963 struct got_worktree *worktree = NULL;
3964 struct got_object_id *start_id = NULL, *end_id = NULL;
3965 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3966 const char *start_commit = NULL, *end_commit = NULL;
3967 const char *search_pattern = NULL;
3968 int diff_context = -1, ch;
3969 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3970 int reverse_display_order = 0;
3972 struct got_reflist_head refs;
3973 struct got_reflist_object_id_map *refs_idmap = NULL;
3978 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3984 limit = get_default_log_limit();
3986 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3992 show_changed_paths = 1;
3995 start_commit = optarg;
3998 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4001 err(1, "-C option %s", errstr);
4004 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4006 err(1, "-l option %s", errstr);
4012 repo_path = realpath(optarg, NULL);
4013 if (repo_path == NULL)
4014 return got_error_from_errno2("realpath",
4016 got_path_strip_trailing_slashes(repo_path);
4019 reverse_display_order = 1;
4022 search_pattern = optarg;
4025 end_commit = optarg;
4036 if (diff_context == -1)
4038 else if (!show_patch)
4039 errx(1, "-C requires -p");
4041 cwd = getcwd(NULL, 0);
4043 error = got_error_from_errno("getcwd");
4047 if (repo_path == NULL) {
4048 error = got_worktree_open(&worktree, cwd);
4049 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4056 error = got_worktree_resolve_path(&path, worktree,
4061 path = strdup(argv[0]);
4063 error = got_error_from_errno("strdup");
4067 } else if (argc != 0)
4070 if (repo_path == NULL) {
4071 repo_path = worktree ?
4072 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4074 if (repo_path == NULL) {
4075 error = got_error_from_errno("strdup");
4079 error = got_repo_open(&repo, repo_path, NULL);
4083 error = apply_unveil(got_repo_get_path(repo), 1,
4084 worktree ? got_worktree_get_root_path(worktree) : NULL);
4088 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4092 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4096 if (start_commit == NULL) {
4097 struct got_reference *head_ref;
4098 struct got_commit_object *commit = NULL;
4099 error = got_ref_open(&head_ref, repo,
4100 worktree ? got_worktree_get_head_ref_name(worktree)
4104 error = got_ref_resolve(&start_id, repo, head_ref);
4105 got_ref_close(head_ref);
4108 error = got_object_open_as_commit(&commit, repo,
4112 got_object_commit_close(commit);
4114 error = got_repo_match_object_id(&start_id, NULL,
4115 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4119 if (end_commit != NULL) {
4120 error = got_repo_match_object_id(&end_id, NULL,
4121 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4128 * If a path was specified on the command line it was resolved
4129 * to a path in the work tree above. Prepend the work tree's
4130 * path prefix to obtain the corresponding in-repository path.
4134 prefix = got_worktree_get_path_prefix(worktree);
4135 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4136 (path[0] != '\0') ? "/" : "", path) == -1) {
4137 error = got_error_from_errno("asprintf");
4142 error = got_repo_map_path(&in_repo_path, repo,
4148 path = in_repo_path;
4151 error = print_commits(start_id, end_id, repo, path ? path : "",
4152 show_changed_paths, show_patch, search_pattern, diff_context,
4153 limit, log_branches, reverse_display_order, refs_idmap);
4159 got_worktree_close(worktree);
4161 const struct got_error *close_err = got_repo_close(repo);
4166 got_reflist_object_id_map_free(refs_idmap);
4167 got_ref_list_free(&refs);
4174 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4175 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4179 struct print_diff_arg {
4180 struct got_repository *repo;
4181 struct got_worktree *worktree;
4186 int ignore_whitespace;
4187 int force_text_diff;
4191 * Create a file which contains the target path of a symlink so we can feed
4192 * it as content to the diff engine.
4194 static const struct got_error *
4195 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4196 const char *abspath)
4198 const struct got_error *err = NULL;
4199 char target_path[PATH_MAX];
4200 ssize_t target_len, outlen;
4205 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4206 if (target_len == -1)
4207 return got_error_from_errno2("readlinkat", abspath);
4209 target_len = readlink(abspath, target_path, PATH_MAX);
4210 if (target_len == -1)
4211 return got_error_from_errno2("readlink", abspath);
4214 *fd = got_opentempfd();
4216 return got_error_from_errno("got_opentempfd");
4218 outlen = write(*fd, target_path, target_len);
4220 err = got_error_from_errno("got_opentempfd");
4224 if (lseek(*fd, 0, SEEK_SET) == -1) {
4225 err = got_error_from_errno2("lseek", abspath);
4236 static const struct got_error *
4237 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4238 const char *path, struct got_object_id *blob_id,
4239 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4240 int dirfd, const char *de_name)
4242 struct print_diff_arg *a = arg;
4243 const struct got_error *err = NULL;
4244 struct got_blob_object *blob1 = NULL;
4247 char *abspath = NULL, *label1 = NULL;
4250 if (a->diff_staged) {
4251 if (staged_status != GOT_STATUS_MODIFY &&
4252 staged_status != GOT_STATUS_ADD &&
4253 staged_status != GOT_STATUS_DELETE)
4256 if (staged_status == GOT_STATUS_DELETE)
4258 if (status == GOT_STATUS_NONEXISTENT)
4259 return got_error_set_errno(ENOENT, path);
4260 if (status != GOT_STATUS_MODIFY &&
4261 status != GOT_STATUS_ADD &&
4262 status != GOT_STATUS_DELETE &&
4263 status != GOT_STATUS_CONFLICT)
4267 if (!a->header_shown) {
4268 printf("diff %s %s%s\n", a->id_str,
4269 got_worktree_get_root_path(a->worktree),
4270 a->diff_staged ? " (staged changes)" : "");
4271 a->header_shown = 1;
4274 if (a->diff_staged) {
4275 const char *label1 = NULL, *label2 = NULL;
4276 switch (staged_status) {
4277 case GOT_STATUS_MODIFY:
4281 case GOT_STATUS_ADD:
4284 case GOT_STATUS_DELETE:
4288 return got_error(GOT_ERR_FILE_STATUS);
4290 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4291 staged_blob_id, label1, label2, a->diff_context,
4292 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4295 if (staged_status == GOT_STATUS_ADD ||
4296 staged_status == GOT_STATUS_MODIFY) {
4298 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4302 err = got_object_id_str(&id_str, staged_blob_id);
4305 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4306 err = got_error_from_errno("asprintf");
4311 } else if (status != GOT_STATUS_ADD) {
4312 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4317 if (status != GOT_STATUS_DELETE) {
4318 if (asprintf(&abspath, "%s/%s",
4319 got_worktree_get_root_path(a->worktree), path) == -1) {
4320 err = got_error_from_errno("asprintf");
4325 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4327 if (errno != ELOOP) {
4328 err = got_error_from_errno2("openat",
4332 err = get_symlink_target_file(&fd, dirfd,
4338 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4340 if (errno != ELOOP) {
4341 err = got_error_from_errno2("open",
4345 err = get_symlink_target_file(&fd, dirfd,
4351 if (fstat(fd, &sb) == -1) {
4352 err = got_error_from_errno2("fstat", abspath);
4355 f2 = fdopen(fd, "r");
4357 err = got_error_from_errno2("fdopen", abspath);
4364 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4365 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4368 got_object_blob_close(blob1);
4369 if (f2 && fclose(f2) == EOF && err == NULL)
4370 err = got_error_from_errno("fclose");
4371 if (fd != -1 && close(fd) == -1 && err == NULL)
4372 err = got_error_from_errno("close");
4377 static const struct got_error *
4378 cmd_diff(int argc, char *argv[])
4380 const struct got_error *error;
4381 struct got_repository *repo = NULL;
4382 struct got_worktree *worktree = NULL;
4383 char *cwd = NULL, *repo_path = NULL;
4384 struct got_object_id *id1 = NULL, *id2 = NULL;
4385 const char *id_str1 = NULL, *id_str2 = NULL;
4386 char *label1 = NULL, *label2 = NULL;
4388 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4389 int force_text_diff = 0;
4392 struct got_reflist_head refs;
4397 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4402 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4405 force_text_diff = 1;
4408 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4411 err(1, "-C option %s", errstr);
4414 repo_path = realpath(optarg, NULL);
4415 if (repo_path == NULL)
4416 return got_error_from_errno2("realpath",
4418 got_path_strip_trailing_slashes(repo_path);
4424 ignore_whitespace = 1;
4435 cwd = getcwd(NULL, 0);
4437 error = got_error_from_errno("getcwd");
4443 "-r option can't be used when diffing a work tree");
4444 error = got_worktree_open(&worktree, cwd);
4446 if (error->code == GOT_ERR_NOT_WORKTREE)
4447 error = wrap_not_worktree_error(error, "diff",
4451 repo_path = strdup(got_worktree_get_repo_path(worktree));
4452 if (repo_path == NULL) {
4453 error = got_error_from_errno("strdup");
4457 error = got_worktree_resolve_path(&path, worktree,
4464 error = got_error_from_errno("strdup");
4468 } else if (argc == 2) {
4470 errx(1, "-s option can't be used when diffing "
4471 "objects in repository");
4474 if (repo_path == NULL) {
4475 error = got_worktree_open(&worktree, cwd);
4476 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4478 repo_path = strdup(worktree ?
4479 got_worktree_get_repo_path(worktree) : cwd);
4480 if (repo_path == NULL) {
4481 error = got_error_from_errno("strdup");
4488 error = got_repo_open(&repo, repo_path, NULL);
4493 error = apply_unveil(got_repo_get_path(repo), 1,
4494 worktree ? got_worktree_get_root_path(worktree) : NULL);
4499 struct print_diff_arg arg;
4500 struct got_pathlist_head paths;
4505 error = got_object_id_str(&id_str,
4506 got_worktree_get_base_commit_id(worktree));
4510 arg.worktree = worktree;
4511 arg.diff_context = diff_context;
4512 arg.id_str = id_str;
4513 arg.header_shown = 0;
4514 arg.diff_staged = diff_staged;
4515 arg.ignore_whitespace = ignore_whitespace;
4516 arg.force_text_diff = force_text_diff;
4518 error = got_pathlist_append(&paths, path, NULL);
4522 error = got_worktree_status(worktree, &paths, repo, 0,
4523 print_diff, &arg, check_cancelled, NULL);
4525 got_pathlist_free(&paths);
4529 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4533 error = got_repo_match_object_id(&id1, &label1, id_str1,
4534 GOT_OBJ_TYPE_ANY, &refs, repo);
4538 error = got_repo_match_object_id(&id2, &label2, id_str2,
4539 GOT_OBJ_TYPE_ANY, &refs, repo);
4543 error = got_object_get_type(&type1, repo, id1);
4547 error = got_object_get_type(&type2, repo, id2);
4551 if (type1 != type2) {
4552 error = got_error(GOT_ERR_OBJ_TYPE);
4557 case GOT_OBJ_TYPE_BLOB:
4558 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4559 NULL, NULL, diff_context, ignore_whitespace,
4560 force_text_diff, repo, stdout);
4562 case GOT_OBJ_TYPE_TREE:
4563 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4564 "", "", diff_context, ignore_whitespace, force_text_diff,
4567 case GOT_OBJ_TYPE_COMMIT:
4568 printf("diff %s %s\n", label1, label2);
4569 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4570 diff_context, ignore_whitespace, force_text_diff, repo,
4574 error = got_error(GOT_ERR_OBJ_TYPE);
4583 got_worktree_close(worktree);
4585 const struct got_error *close_err = got_repo_close(repo);
4589 got_ref_list_free(&refs);
4597 "usage: %s blame [-c commit] [-r repository-path] path\n",
4606 char datebuf[11]; /* YYYY-MM-DD + NUL */
4609 struct blame_cb_args {
4610 struct blame_line *lines;
4614 off_t *line_offsets;
4616 struct got_repository *repo;
4619 static const struct got_error *
4620 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4622 const struct got_error *err = NULL;
4623 struct blame_cb_args *a = arg;
4624 struct blame_line *bline;
4626 size_t linesize = 0;
4627 struct got_commit_object *commit = NULL;
4630 time_t committer_time;
4632 if (nlines != a->nlines ||
4633 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4634 return got_error(GOT_ERR_RANGE);
4636 if (sigint_received)
4637 return got_error(GOT_ERR_ITER_COMPLETED);
4640 return NULL; /* no change in this commit */
4642 /* Annotate this line. */
4643 bline = &a->lines[lineno - 1];
4644 if (bline->annotated)
4646 err = got_object_id_str(&bline->id_str, id);
4650 err = got_object_open_as_commit(&commit, a->repo, id);
4654 bline->committer = strdup(got_object_commit_get_committer(commit));
4655 if (bline->committer == NULL) {
4656 err = got_error_from_errno("strdup");
4660 committer_time = got_object_commit_get_committer_time(commit);
4661 if (gmtime_r(&committer_time, &tm) == NULL)
4662 return got_error_from_errno("gmtime_r");
4663 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4665 err = got_error(GOT_ERR_NO_SPACE);
4668 bline->annotated = 1;
4670 /* Print lines annotated so far. */
4671 bline = &a->lines[a->lineno_cur - 1];
4672 if (!bline->annotated)
4675 offset = a->line_offsets[a->lineno_cur - 1];
4676 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4677 err = got_error_from_errno("fseeko");
4681 while (bline->annotated) {
4682 char *smallerthan, *at, *nl, *committer;
4685 if (getline(&line, &linesize, a->f) == -1) {
4687 err = got_error_from_errno("getline");
4691 committer = bline->committer;
4692 smallerthan = strchr(committer, '<');
4693 if (smallerthan && smallerthan[1] != '\0')
4694 committer = smallerthan + 1;
4695 at = strchr(committer, '@');
4698 len = strlen(committer);
4700 committer[8] = '\0';
4702 nl = strchr(line, '\n');
4705 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4706 bline->id_str, bline->datebuf, committer, line);
4709 bline = &a->lines[a->lineno_cur - 1];
4713 got_object_commit_close(commit);
4718 static const struct got_error *
4719 cmd_blame(int argc, char *argv[])
4721 const struct got_error *error;
4722 struct got_repository *repo = NULL;
4723 struct got_worktree *worktree = NULL;
4724 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4725 char *link_target = NULL;
4726 struct got_object_id *obj_id = NULL;
4727 struct got_object_id *commit_id = NULL;
4728 struct got_blob_object *blob = NULL;
4729 char *commit_id_str = NULL;
4730 struct blame_cb_args bca;
4731 int ch, obj_type, i;
4734 memset(&bca, 0, sizeof(bca));
4737 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4742 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4745 commit_id_str = optarg;
4748 repo_path = realpath(optarg, NULL);
4749 if (repo_path == NULL)
4750 return got_error_from_errno2("realpath",
4752 got_path_strip_trailing_slashes(repo_path);
4768 cwd = getcwd(NULL, 0);
4770 error = got_error_from_errno("getcwd");
4773 if (repo_path == NULL) {
4774 error = got_worktree_open(&worktree, cwd);
4775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4781 strdup(got_worktree_get_repo_path(worktree));
4782 if (repo_path == NULL) {
4783 error = got_error_from_errno("strdup");
4788 repo_path = strdup(cwd);
4789 if (repo_path == NULL) {
4790 error = got_error_from_errno("strdup");
4796 error = got_repo_open(&repo, repo_path, NULL);
4801 const char *prefix = got_worktree_get_path_prefix(worktree);
4804 error = got_worktree_resolve_path(&p, worktree, path);
4807 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4808 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4810 error = got_error_from_errno("asprintf");
4815 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4817 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4820 error = got_repo_map_path(&in_repo_path, repo, path);
4825 if (commit_id_str == NULL) {
4826 struct got_reference *head_ref;
4827 error = got_ref_open(&head_ref, repo, worktree ?
4828 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4831 error = got_ref_resolve(&commit_id, repo, head_ref);
4832 got_ref_close(head_ref);
4836 struct got_reflist_head refs;
4838 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4842 error = got_repo_match_object_id(&commit_id, NULL,
4843 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4844 got_ref_list_free(&refs);
4849 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4854 error = got_object_id_by_path(&obj_id, repo, commit_id,
4855 link_target ? link_target : in_repo_path);
4859 error = got_object_get_type(&obj_type, repo, obj_id);
4863 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4864 error = got_error_path(link_target ? link_target : in_repo_path,
4869 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4872 bca.f = got_opentemp();
4873 if (bca.f == NULL) {
4874 error = got_error_from_errno("got_opentemp");
4877 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4878 &bca.line_offsets, bca.f, blob);
4879 if (error || bca.nlines == 0)
4882 /* Don't include \n at EOF in the blame line count. */
4883 if (bca.line_offsets[bca.nlines - 1] == filesize)
4886 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4887 if (bca.lines == NULL) {
4888 error = got_error_from_errno("calloc");
4892 bca.nlines_prec = 0;
4900 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4901 repo, blame_cb, &bca, check_cancelled, NULL);
4910 got_object_blob_close(blob);
4912 got_worktree_close(worktree);
4914 const struct got_error *close_err = got_repo_close(repo);
4919 for (i = 0; i < bca.nlines; i++) {
4920 struct blame_line *bline = &bca.lines[i];
4921 free(bline->id_str);
4922 free(bline->committer);
4926 free(bca.line_offsets);
4927 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4928 error = got_error_from_errno("fclose");
4936 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4941 static const struct got_error *
4942 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4943 const char *root_path, struct got_repository *repo)
4945 const struct got_error *err = NULL;
4946 int is_root_path = (strcmp(path, root_path) == 0);
4947 const char *modestr = "";
4948 mode_t mode = got_tree_entry_get_mode(te);
4949 char *link_target = NULL;
4951 path += strlen(root_path);
4952 while (path[0] == '/')
4955 if (got_object_tree_entry_is_submodule(te))
4957 else if (S_ISLNK(mode)) {
4960 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4963 for (i = 0; i < strlen(link_target); i++) {
4964 if (!isprint((unsigned char)link_target[i]))
4965 link_target[i] = '?';
4970 else if (S_ISDIR(mode))
4972 else if (mode & S_IXUSR)
4975 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4976 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4977 link_target ? " -> ": "", link_target ? link_target : "");
4983 static const struct got_error *
4984 print_tree(const char *path, struct got_object_id *commit_id,
4985 int show_ids, int recurse, const char *root_path,
4986 struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 struct got_object_id *tree_id = NULL;
4990 struct got_tree_object *tree = NULL;
4993 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4997 err = got_object_open_as_tree(&tree, repo, tree_id);
5000 nentries = got_object_tree_get_nentries(tree);
5001 for (i = 0; i < nentries; i++) {
5002 struct got_tree_entry *te;
5005 if (sigint_received || sigpipe_received)
5008 te = got_object_tree_get_entry(tree, i);
5011 err = got_object_id_str(&id_str,
5012 got_tree_entry_get_id(te));
5015 if (asprintf(&id, "%s ", id_str) == -1) {
5016 err = got_error_from_errno("asprintf");
5022 err = print_entry(te, id, path, root_path, repo);
5027 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5029 if (asprintf(&child_path, "%s%s%s", path,
5030 path[0] == '/' && path[1] == '\0' ? "" : "/",
5031 got_tree_entry_get_name(te)) == -1) {
5032 err = got_error_from_errno("asprintf");
5035 err = print_tree(child_path, commit_id, show_ids, 1,
5044 got_object_tree_close(tree);
5049 static const struct got_error *
5050 cmd_tree(int argc, char *argv[])
5052 const struct got_error *error;
5053 struct got_repository *repo = NULL;
5054 struct got_worktree *worktree = NULL;
5055 const char *path, *refname = NULL;
5056 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5057 struct got_object_id *commit_id = NULL;
5058 char *commit_id_str = NULL;
5059 int show_ids = 0, recurse = 0;
5063 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5068 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5071 commit_id_str = optarg;
5074 repo_path = realpath(optarg, NULL);
5075 if (repo_path == NULL)
5076 return got_error_from_errno2("realpath",
5078 got_path_strip_trailing_slashes(repo_path);
5102 cwd = getcwd(NULL, 0);
5104 error = got_error_from_errno("getcwd");
5107 if (repo_path == NULL) {
5108 error = got_worktree_open(&worktree, cwd);
5109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5115 strdup(got_worktree_get_repo_path(worktree));
5116 if (repo_path == NULL)
5117 error = got_error_from_errno("strdup");
5121 repo_path = strdup(cwd);
5122 if (repo_path == NULL) {
5123 error = got_error_from_errno("strdup");
5129 error = got_repo_open(&repo, repo_path, NULL);
5134 const char *prefix = got_worktree_get_path_prefix(worktree);
5139 error = got_worktree_resolve_path(&p, worktree, path);
5142 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5143 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5145 error = got_error_from_errno("asprintf");
5150 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5154 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5159 error = got_repo_map_path(&in_repo_path, repo, path);
5164 if (commit_id_str == NULL) {
5165 struct got_reference *head_ref;
5167 refname = got_worktree_get_head_ref_name(worktree);
5169 refname = GOT_REF_HEAD;
5170 error = got_ref_open(&head_ref, repo, refname, 0);
5173 error = got_ref_resolve(&commit_id, repo, head_ref);
5174 got_ref_close(head_ref);
5178 struct got_reflist_head refs;
5180 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5184 error = got_repo_match_object_id(&commit_id, NULL,
5185 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5186 got_ref_list_free(&refs);
5191 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5192 in_repo_path, repo);
5199 got_worktree_close(worktree);
5201 const struct got_error *close_err = got_repo_close(repo);
5211 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5216 static const struct got_error *
5217 print_status(void *arg, unsigned char status, unsigned char staged_status,
5218 const char *path, struct got_object_id *blob_id,
5219 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5220 int dirfd, const char *de_name)
5222 if (status == staged_status && (status == GOT_STATUS_DELETE))
5223 status = GOT_STATUS_NO_CHANGE;
5225 char *status_codes = arg;
5226 size_t ncodes = strlen(status_codes);
5228 for (i = 0; i < ncodes ; i++) {
5229 if (status == status_codes[i] ||
5230 staged_status == status_codes[i])
5236 printf("%c%c %s\n", status, staged_status, path);
5240 static const struct got_error *
5241 cmd_status(int argc, char *argv[])
5243 const struct got_error *error = NULL;
5244 struct got_repository *repo = NULL;
5245 struct got_worktree *worktree = NULL;
5246 char *cwd = NULL, *status_codes = NULL;;
5247 struct got_pathlist_head paths;
5248 struct got_pathlist_entry *pe;
5249 int ch, i, no_ignores = 0;
5253 while ((ch = getopt(argc, argv, "Is:")) != -1) {
5259 for (i = 0; i < strlen(optarg); i++) {
5260 switch (optarg[i]) {
5261 case GOT_STATUS_MODIFY:
5262 case GOT_STATUS_ADD:
5263 case GOT_STATUS_DELETE:
5264 case GOT_STATUS_CONFLICT:
5265 case GOT_STATUS_MISSING:
5266 case GOT_STATUS_OBSTRUCTED:
5267 case GOT_STATUS_UNVERSIONED:
5268 case GOT_STATUS_MODE_CHANGE:
5269 case GOT_STATUS_NONEXISTENT:
5272 errx(1, "invalid status code '%c'",
5276 status_codes = optarg;
5288 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5292 cwd = getcwd(NULL, 0);
5294 error = got_error_from_errno("getcwd");
5298 error = got_worktree_open(&worktree, cwd);
5300 if (error->code == GOT_ERR_NOT_WORKTREE)
5301 error = wrap_not_worktree_error(error, "status", cwd);
5305 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5310 error = apply_unveil(got_repo_get_path(repo), 1,
5311 got_worktree_get_root_path(worktree));
5315 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5319 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5320 print_status, status_codes, check_cancelled, NULL);
5322 TAILQ_FOREACH(pe, &paths, entry)
5323 free((char *)pe->path);
5324 got_pathlist_free(&paths);
5333 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5339 static const struct got_error *
5340 list_refs(struct got_repository *repo, const char *refname)
5342 static const struct got_error *err = NULL;
5343 struct got_reflist_head refs;
5344 struct got_reflist_entry *re;
5347 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5351 TAILQ_FOREACH(re, &refs, entry) {
5353 refstr = got_ref_to_str(re->ref);
5355 return got_error_from_errno("got_ref_to_str");
5356 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5360 got_ref_list_free(&refs);
5364 static const struct got_error *
5365 delete_ref_by_name(struct got_repository *repo, const char *refname)
5367 const struct got_error *err;
5368 struct got_reference *ref;
5370 err = got_ref_open(&ref, repo, refname, 0);
5374 err = delete_ref(repo, ref);
5379 static const struct got_error *
5380 add_ref(struct got_repository *repo, const char *refname, const char *target)
5382 const struct got_error *err = NULL;
5383 struct got_object_id *id;
5384 struct got_reference *ref = NULL;
5387 * Don't let the user create a reference name with a leading '-'.
5388 * While technically a valid reference name, this case is usually
5389 * an unintended typo.
5391 if (refname[0] == '-')
5392 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5394 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5397 struct got_reference *target_ref;
5399 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5401 err = got_ref_open(&target_ref, repo, target, 0);
5404 err = got_ref_resolve(&id, repo, target_ref);
5405 got_ref_close(target_ref);
5410 err = got_ref_alloc(&ref, refname, id);
5414 err = got_ref_write(ref, repo);
5422 static const struct got_error *
5423 add_symref(struct got_repository *repo, const char *refname, const char *target)
5425 const struct got_error *err = NULL;
5426 struct got_reference *ref = NULL;
5427 struct got_reference *target_ref = NULL;
5430 * Don't let the user create a reference name with a leading '-'.
5431 * While technically a valid reference name, this case is usually
5432 * an unintended typo.
5434 if (refname[0] == '-')
5435 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5437 err = got_ref_open(&target_ref, repo, target, 0);
5441 err = got_ref_alloc_symref(&ref, refname, target_ref);
5445 err = got_ref_write(ref, repo);
5448 got_ref_close(target_ref);
5454 static const struct got_error *
5455 cmd_ref(int argc, char *argv[])
5457 const struct got_error *error = NULL;
5458 struct got_repository *repo = NULL;
5459 struct got_worktree *worktree = NULL;
5460 char *cwd = NULL, *repo_path = NULL;
5461 int ch, do_list = 0, do_delete = 0;
5462 const char *obj_arg = NULL, *symref_target= NULL;
5463 char *refname = NULL;
5465 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5474 repo_path = realpath(optarg, NULL);
5475 if (repo_path == NULL)
5476 return got_error_from_errno2("realpath",
5478 got_path_strip_trailing_slashes(repo_path);
5484 symref_target = optarg;
5492 if (obj_arg && do_list)
5493 option_conflict('c', 'l');
5494 if (obj_arg && do_delete)
5495 option_conflict('c', 'd');
5496 if (obj_arg && symref_target)
5497 option_conflict('c', 's');
5498 if (symref_target && do_delete)
5499 option_conflict('s', 'd');
5500 if (symref_target && do_list)
5501 option_conflict('s', 'l');
5502 if (do_delete && do_list)
5503 option_conflict('d', 'l');
5509 if (argc != 0 && argc != 1)
5512 refname = strdup(argv[0]);
5513 if (refname == NULL) {
5514 error = got_error_from_errno("strdup");
5521 refname = strdup(argv[0]);
5522 if (refname == NULL) {
5523 error = got_error_from_errno("strdup");
5529 got_path_strip_trailing_slashes(refname);
5533 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5537 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5538 "sendfd unveil", NULL) == -1)
5542 cwd = getcwd(NULL, 0);
5544 error = got_error_from_errno("getcwd");
5548 if (repo_path == NULL) {
5549 error = got_worktree_open(&worktree, cwd);
5550 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5556 strdup(got_worktree_get_repo_path(worktree));
5557 if (repo_path == NULL)
5558 error = got_error_from_errno("strdup");
5562 repo_path = strdup(cwd);
5563 if (repo_path == NULL) {
5564 error = got_error_from_errno("strdup");
5570 error = got_repo_open(&repo, repo_path, NULL);
5574 error = apply_unveil(got_repo_get_path(repo), do_list,
5575 worktree ? got_worktree_get_root_path(worktree) : NULL);
5580 error = list_refs(repo, refname);
5582 error = delete_ref_by_name(repo, refname);
5583 else if (symref_target)
5584 error = add_symref(repo, refname, symref_target);
5586 if (obj_arg == NULL)
5588 error = add_ref(repo, refname, obj_arg);
5593 const struct got_error *close_err = got_repo_close(repo);
5598 got_worktree_close(worktree);
5608 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5609 "[name]\n", getprogname());
5613 static const struct got_error *
5614 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5615 struct got_reference *ref)
5617 const struct got_error *err = NULL;
5618 const char *refname, *marker = " ";
5621 refname = got_ref_get_name(ref);
5622 if (worktree && strcmp(refname,
5623 got_worktree_get_head_ref_name(worktree)) == 0) {
5624 struct got_object_id *id = NULL;
5626 err = got_ref_resolve(&id, repo, ref);
5629 if (got_object_id_cmp(id,
5630 got_worktree_get_base_commit_id(worktree)) == 0)
5637 if (strncmp(refname, "refs/heads/", 11) == 0)
5639 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5641 if (strncmp(refname, "refs/remotes/", 13) == 0)
5644 refstr = got_ref_to_str(ref);
5646 return got_error_from_errno("got_ref_to_str");
5648 printf("%s%s: %s\n", marker, refname, refstr);
5653 static const struct got_error *
5654 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5656 const char *refname;
5658 if (worktree == NULL)
5659 return got_error(GOT_ERR_NOT_WORKTREE);
5661 refname = got_worktree_get_head_ref_name(worktree);
5663 if (strncmp(refname, "refs/heads/", 11) == 0)
5665 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5668 printf("%s\n", refname);
5673 static const struct got_error *
5674 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5676 static const struct got_error *err = NULL;
5677 struct got_reflist_head refs;
5678 struct got_reflist_entry *re;
5679 struct got_reference *temp_ref = NULL;
5680 int rebase_in_progress, histedit_in_progress;
5685 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5690 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5695 if (rebase_in_progress || histedit_in_progress) {
5696 err = got_ref_open(&temp_ref, repo,
5697 got_worktree_get_head_ref_name(worktree), 0);
5700 list_branch(repo, worktree, temp_ref);
5701 got_ref_close(temp_ref);
5705 err = got_ref_list(&refs, repo, "refs/heads",
5706 got_ref_cmp_by_name, NULL);
5710 TAILQ_FOREACH(re, &refs, entry)
5711 list_branch(repo, worktree, re->ref);
5713 got_ref_list_free(&refs);
5715 err = got_ref_list(&refs, repo, "refs/remotes",
5716 got_ref_cmp_by_name, NULL);
5720 TAILQ_FOREACH(re, &refs, entry)
5721 list_branch(repo, worktree, re->ref);
5723 got_ref_list_free(&refs);
5728 static const struct got_error *
5729 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5730 const char *branch_name)
5732 const struct got_error *err = NULL;
5733 struct got_reference *ref = NULL;
5734 char *refname, *remote_refname = NULL;
5736 if (strncmp(branch_name, "refs/", 5) == 0)
5738 if (strncmp(branch_name, "heads/", 6) == 0)
5740 else if (strncmp(branch_name, "remotes/", 8) == 0)
5743 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5744 return got_error_from_errno("asprintf");
5746 if (asprintf(&remote_refname, "refs/remotes/%s",
5747 branch_name) == -1) {
5748 err = got_error_from_errno("asprintf");
5752 err = got_ref_open(&ref, repo, refname, 0);
5754 const struct got_error *err2;
5755 if (err->code != GOT_ERR_NOT_REF)
5758 * Keep 'err' intact such that if neither branch exists
5759 * we report "refs/heads" rather than "refs/remotes" in
5760 * our error message.
5762 err2 = got_ref_open(&ref, repo, remote_refname, 0);
5769 strcmp(got_worktree_get_head_ref_name(worktree),
5770 got_ref_get_name(ref)) == 0) {
5771 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5772 "will not delete this work tree's current branch");
5776 err = delete_ref(repo, ref);
5781 free(remote_refname);
5785 static const struct got_error *
5786 add_branch(struct got_repository *repo, const char *branch_name,
5787 struct got_object_id *base_commit_id)
5789 const struct got_error *err = NULL;
5790 struct got_reference *ref = NULL;
5791 char *base_refname = NULL, *refname = NULL;
5794 * Don't let the user create a branch name with a leading '-'.
5795 * While technically a valid reference name, this case is usually
5796 * an unintended typo.
5798 if (branch_name[0] == '-')
5799 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5801 if (strncmp(branch_name, "refs/heads/", 11) == 0)
5804 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5805 err = got_error_from_errno("asprintf");
5809 err = got_ref_open(&ref, repo, refname, 0);
5811 err = got_error(GOT_ERR_BRANCH_EXISTS);
5813 } else if (err->code != GOT_ERR_NOT_REF)
5816 err = got_ref_alloc(&ref, refname, base_commit_id);
5820 err = got_ref_write(ref, repo);
5829 static const struct got_error *
5830 cmd_branch(int argc, char *argv[])
5832 const struct got_error *error = NULL;
5833 struct got_repository *repo = NULL;
5834 struct got_worktree *worktree = NULL;
5835 char *cwd = NULL, *repo_path = NULL;
5836 int ch, do_list = 0, do_show = 0, do_update = 1;
5837 const char *delref = NULL, *commit_id_arg = NULL;
5838 struct got_reference *ref = NULL;
5839 struct got_pathlist_head paths;
5840 struct got_pathlist_entry *pe;
5841 struct got_object_id *commit_id = NULL;
5842 char *commit_id_str = NULL;
5846 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5849 commit_id_arg = optarg;
5855 repo_path = realpath(optarg, NULL);
5856 if (repo_path == NULL)
5857 return got_error_from_errno2("realpath",
5859 got_path_strip_trailing_slashes(repo_path);
5873 if (do_list && delref)
5874 option_conflict('l', 'd');
5879 if (!do_list && !delref && argc == 0)
5882 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5883 errx(1, "-c option can only be used when creating a branch");
5885 if (do_list || delref) {
5888 } else if (!do_show && argc != 1)
5892 if (do_list || do_show) {
5893 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5897 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5898 "sendfd unveil", NULL) == -1)
5902 cwd = getcwd(NULL, 0);
5904 error = got_error_from_errno("getcwd");
5908 if (repo_path == NULL) {
5909 error = got_worktree_open(&worktree, cwd);
5910 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5916 strdup(got_worktree_get_repo_path(worktree));
5917 if (repo_path == NULL)
5918 error = got_error_from_errno("strdup");
5922 repo_path = strdup(cwd);
5923 if (repo_path == NULL) {
5924 error = got_error_from_errno("strdup");
5930 error = got_repo_open(&repo, repo_path, NULL);
5934 error = apply_unveil(got_repo_get_path(repo), do_list,
5935 worktree ? got_worktree_get_root_path(worktree) : NULL);
5940 error = show_current_branch(repo, worktree);
5942 error = list_branches(repo, worktree);
5944 error = delete_branch(repo, worktree, delref);
5946 struct got_reflist_head refs;
5948 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5952 if (commit_id_arg == NULL)
5953 commit_id_arg = worktree ?
5954 got_worktree_get_head_ref_name(worktree) :
5956 error = got_repo_match_object_id(&commit_id, NULL,
5957 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5958 got_ref_list_free(&refs);
5961 error = add_branch(repo, argv[0], commit_id);
5964 if (worktree && do_update) {
5965 struct got_update_progress_arg upa;
5966 char *branch_refname = NULL;
5968 error = got_object_id_str(&commit_id_str, commit_id);
5971 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5975 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5977 error = got_error_from_errno("asprintf");
5980 error = got_ref_open(&ref, repo, branch_refname, 0);
5981 free(branch_refname);
5984 error = switch_head_ref(ref, commit_id, worktree,
5988 error = got_worktree_set_base_commit_id(worktree, repo,
5992 memset(&upa, 0, sizeof(upa));
5993 error = got_worktree_checkout_files(worktree, &paths,
5994 repo, update_progress, &upa, check_cancelled,
5998 if (upa.did_something)
5999 printf("Updated to commit %s\n", commit_id_str);
6000 print_update_progress_stats(&upa);
6007 const struct got_error *close_err = got_repo_close(repo);
6012 got_worktree_close(worktree);
6016 free(commit_id_str);
6017 TAILQ_FOREACH(pe, &paths, entry)
6018 free((char *)pe->path);
6019 got_pathlist_free(&paths);
6028 "usage: %s tag [-c commit] [-r repository] [-l] "
6029 "[-m message] name\n", getprogname());
6034 static const struct got_error *
6035 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6037 const struct got_error *err = NULL;
6038 struct got_reflist_entry *re, *se, *new;
6039 struct got_object_id *re_id, *se_id;
6040 struct got_tag_object *re_tag, *se_tag;
6041 time_t re_time, se_time;
6043 STAILQ_FOREACH(re, tags, entry) {
6044 se = STAILQ_FIRST(sorted);
6046 err = got_reflist_entry_dup(&new, re);
6049 STAILQ_INSERT_HEAD(sorted, new, entry);
6052 err = got_ref_resolve(&re_id, repo, re->ref);
6055 err = got_object_open_as_tag(&re_tag, repo, re_id);
6059 re_time = got_object_tag_get_tagger_time(re_tag);
6060 got_object_tag_close(re_tag);
6064 err = got_ref_resolve(&se_id, repo, re->ref);
6067 err = got_object_open_as_tag(&se_tag, repo, se_id);
6071 se_time = got_object_tag_get_tagger_time(se_tag);
6072 got_object_tag_close(se_tag);
6074 if (se_time > re_time) {
6075 err = got_reflist_entry_dup(&new, re);
6078 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6081 se = STAILQ_NEXT(se, entry);
6090 static const struct got_error *
6091 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6093 static const struct got_error *err = NULL;
6094 struct got_reflist_head refs;
6095 struct got_reflist_entry *re;
6099 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6103 TAILQ_FOREACH(re, &refs, entry) {
6104 const char *refname;
6105 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6109 struct got_object_id *id;
6110 struct got_tag_object *tag;
6111 struct got_commit_object *commit = NULL;
6113 refname = got_ref_get_name(re->ref);
6114 if (strncmp(refname, "refs/tags/", 10) != 0)
6117 refstr = got_ref_to_str(re->ref);
6118 if (refstr == NULL) {
6119 err = got_error_from_errno("got_ref_to_str");
6122 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6125 err = got_ref_resolve(&id, repo, re->ref);
6128 err = got_object_open_as_tag(&tag, repo, id);
6130 if (err->code != GOT_ERR_OBJ_TYPE) {
6134 /* "lightweight" tag */
6135 err = got_object_open_as_commit(&commit, repo, id);
6140 tagger = got_object_commit_get_committer(commit);
6142 got_object_commit_get_committer_time(commit);
6143 err = got_object_id_str(&id_str, id);
6149 tagger = got_object_tag_get_tagger(tag);
6150 tagger_time = got_object_tag_get_tagger_time(tag);
6151 err = got_object_id_str(&id_str,
6152 got_object_tag_get_object_id(tag));
6156 printf("from: %s\n", tagger);
6157 datestr = get_datestr(&tagger_time, datebuf);
6159 printf("date: %s UTC\n", datestr);
6161 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6163 switch (got_object_tag_get_object_type(tag)) {
6164 case GOT_OBJ_TYPE_BLOB:
6165 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6168 case GOT_OBJ_TYPE_TREE:
6169 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6172 case GOT_OBJ_TYPE_COMMIT:
6173 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6176 case GOT_OBJ_TYPE_TAG:
6177 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6186 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6189 got_object_commit_close(commit);
6191 tagmsg0 = strdup(got_object_tag_get_message(tag));
6192 got_object_tag_close(tag);
6193 if (tagmsg0 == NULL) {
6194 err = got_error_from_errno("strdup");
6201 line = strsep(&tagmsg, "\n");
6203 printf(" %s\n", line);
6208 got_ref_list_free(&refs);
6212 static const struct got_error *
6213 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6214 const char *tag_name, const char *repo_path)
6216 const struct got_error *err = NULL;
6217 char *template = NULL, *initial_content = NULL;
6218 char *editor = NULL;
6219 int initial_content_len;
6222 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6223 err = got_error_from_errno("asprintf");
6227 initial_content_len = asprintf(&initial_content,
6228 "\n# tagging commit %s as %s\n",
6229 commit_id_str, tag_name);
6230 if (initial_content_len == -1) {
6231 err = got_error_from_errno("asprintf");
6235 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6239 if (write(fd, initial_content, initial_content_len) == -1) {
6240 err = got_error_from_errno2("write", *tagmsg_path);
6244 err = get_editor(&editor);
6247 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6248 initial_content_len, 1);
6250 free(initial_content);
6254 if (fd != -1 && close(fd) == -1 && err == NULL)
6255 err = got_error_from_errno2("close", *tagmsg_path);
6257 /* Editor is done; we can now apply unveil(2) */
6259 err = apply_unveil(repo_path, 0, NULL);
6267 static const struct got_error *
6268 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6269 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6271 const struct got_error *err = NULL;
6272 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6273 char *label = NULL, *commit_id_str = NULL;
6274 struct got_reference *ref = NULL;
6275 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6276 char *tagmsg_path = NULL, *tag_id_str = NULL;
6277 int preserve_tagmsg = 0;
6278 struct got_reflist_head refs;
6283 * Don't let the user create a tag name with a leading '-'.
6284 * While technically a valid reference name, this case is usually
6285 * an unintended typo.
6287 if (tag_name[0] == '-')
6288 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6290 err = get_author(&tagger, repo, worktree);
6294 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6298 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6299 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6303 err = got_object_id_str(&commit_id_str, commit_id);
6307 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6308 refname = strdup(tag_name);
6309 if (refname == NULL) {
6310 err = got_error_from_errno("strdup");
6314 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6315 err = got_error_from_errno("asprintf");
6319 err = got_ref_open(&ref, repo, refname, 0);
6321 err = got_error(GOT_ERR_TAG_EXISTS);
6323 } else if (err->code != GOT_ERR_NOT_REF)
6326 if (tagmsg_arg == NULL) {
6327 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6328 tag_name, got_repo_get_path(repo));
6330 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6331 tagmsg_path != NULL)
6332 preserve_tagmsg = 1;
6337 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6338 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6341 preserve_tagmsg = 1;
6345 err = got_ref_alloc(&ref, refname, tag_id);
6348 preserve_tagmsg = 1;
6352 err = got_ref_write(ref, repo);
6355 preserve_tagmsg = 1;
6359 err = got_object_id_str(&tag_id_str, tag_id);
6362 preserve_tagmsg = 1;
6365 printf("Created tag %s\n", tag_id_str);
6367 if (preserve_tagmsg) {
6368 fprintf(stderr, "%s: tag message preserved in %s\n",
6369 getprogname(), tagmsg_path);
6370 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6371 err = got_error_from_errno2("unlink", tagmsg_path);
6376 free(commit_id_str);
6381 got_ref_list_free(&refs);
6385 static const struct got_error *
6386 cmd_tag(int argc, char *argv[])
6388 const struct got_error *error = NULL;
6389 struct got_repository *repo = NULL;
6390 struct got_worktree *worktree = NULL;
6391 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6392 char *gitconfig_path = NULL;
6393 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6394 int ch, do_list = 0;
6396 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6399 commit_id_arg = optarg;
6405 repo_path = realpath(optarg, NULL);
6406 if (repo_path == NULL)
6407 return got_error_from_errno2("realpath",
6409 got_path_strip_trailing_slashes(repo_path);
6424 if (commit_id_arg != NULL)
6426 "-c option can only be used when creating a tag");
6428 option_conflict('l', 'm');
6431 } else if (argc != 1)
6438 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6442 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6443 "sendfd unveil", NULL) == -1)
6447 cwd = getcwd(NULL, 0);
6449 error = got_error_from_errno("getcwd");
6453 if (repo_path == NULL) {
6454 error = got_worktree_open(&worktree, cwd);
6455 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6461 strdup(got_worktree_get_repo_path(worktree));
6462 if (repo_path == NULL)
6463 error = got_error_from_errno("strdup");
6467 repo_path = strdup(cwd);
6468 if (repo_path == NULL) {
6469 error = got_error_from_errno("strdup");
6476 error = got_repo_open(&repo, repo_path, NULL);
6479 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6482 error = list_tags(repo, worktree);
6484 error = get_gitconfig_path(&gitconfig_path);
6487 error = got_repo_open(&repo, repo_path, gitconfig_path);
6492 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6497 if (commit_id_arg == NULL) {
6498 struct got_reference *head_ref;
6499 struct got_object_id *commit_id;
6500 error = got_ref_open(&head_ref, repo,
6501 worktree ? got_worktree_get_head_ref_name(worktree)
6505 error = got_ref_resolve(&commit_id, repo, head_ref);
6506 got_ref_close(head_ref);
6509 error = got_object_id_str(&commit_id_str, commit_id);
6515 error = add_tag(repo, worktree, tag_name,
6516 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6520 const struct got_error *close_err = got_repo_close(repo);
6525 got_worktree_close(worktree);
6528 free(gitconfig_path);
6529 free(commit_id_str);
6536 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6541 static const struct got_error *
6542 add_progress(void *arg, unsigned char status, const char *path)
6544 while (path[0] == '/')
6546 printf("%c %s\n", status, path);
6550 static const struct got_error *
6551 cmd_add(int argc, char *argv[])
6553 const struct got_error *error = NULL;
6554 struct got_repository *repo = NULL;
6555 struct got_worktree *worktree = NULL;
6557 struct got_pathlist_head paths;
6558 struct got_pathlist_entry *pe;
6559 int ch, can_recurse = 0, no_ignores = 0;
6563 while ((ch = getopt(argc, argv, "IR")) != -1) {
6581 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6588 cwd = getcwd(NULL, 0);
6590 error = got_error_from_errno("getcwd");
6594 error = got_worktree_open(&worktree, cwd);
6596 if (error->code == GOT_ERR_NOT_WORKTREE)
6597 error = wrap_not_worktree_error(error, "add", cwd);
6601 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6606 error = apply_unveil(got_repo_get_path(repo), 1,
6607 got_worktree_get_root_path(worktree));
6611 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6618 TAILQ_FOREACH(pe, &paths, entry) {
6619 if (asprintf(&ondisk_path, "%s/%s",
6620 got_worktree_get_root_path(worktree),
6622 error = got_error_from_errno("asprintf");
6625 if (lstat(ondisk_path, &sb) == -1) {
6626 if (errno == ENOENT) {
6630 error = got_error_from_errno2("lstat",
6636 if (S_ISDIR(sb.st_mode)) {
6637 error = got_error_msg(GOT_ERR_BAD_PATH,
6638 "adding directories requires -R option");
6644 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6645 NULL, repo, no_ignores);
6648 const struct got_error *close_err = got_repo_close(repo);
6653 got_worktree_close(worktree);
6654 TAILQ_FOREACH(pe, &paths, entry)
6655 free((char *)pe->path);
6656 got_pathlist_free(&paths);
6664 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6665 "path ...\n", getprogname());
6669 static const struct got_error *
6670 print_remove_status(void *arg, unsigned char status,
6671 unsigned char staged_status, const char *path)
6673 while (path[0] == '/')
6675 if (status == GOT_STATUS_NONEXISTENT)
6677 if (status == staged_status && (status == GOT_STATUS_DELETE))
6678 status = GOT_STATUS_NO_CHANGE;
6679 printf("%c%c %s\n", status, staged_status, path);
6683 static const struct got_error *
6684 cmd_remove(int argc, char *argv[])
6686 const struct got_error *error = NULL;
6687 struct got_worktree *worktree = NULL;
6688 struct got_repository *repo = NULL;
6689 const char *status_codes = NULL;
6691 struct got_pathlist_head paths;
6692 struct got_pathlist_entry *pe;
6693 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6697 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6700 delete_local_mods = 1;
6709 for (i = 0; i < strlen(optarg); i++) {
6710 switch (optarg[i]) {
6711 case GOT_STATUS_MODIFY:
6712 delete_local_mods = 1;
6714 case GOT_STATUS_MISSING:
6717 errx(1, "invalid status code '%c'",
6721 status_codes = optarg;
6733 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6740 cwd = getcwd(NULL, 0);
6742 error = got_error_from_errno("getcwd");
6745 error = got_worktree_open(&worktree, cwd);
6747 if (error->code == GOT_ERR_NOT_WORKTREE)
6748 error = wrap_not_worktree_error(error, "remove", cwd);
6752 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6757 error = apply_unveil(got_repo_get_path(repo), 1,
6758 got_worktree_get_root_path(worktree));
6762 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6769 TAILQ_FOREACH(pe, &paths, entry) {
6770 if (asprintf(&ondisk_path, "%s/%s",
6771 got_worktree_get_root_path(worktree),
6773 error = got_error_from_errno("asprintf");
6776 if (lstat(ondisk_path, &sb) == -1) {
6777 if (errno == ENOENT) {
6781 error = got_error_from_errno2("lstat",
6787 if (S_ISDIR(sb.st_mode)) {
6788 error = got_error_msg(GOT_ERR_BAD_PATH,
6789 "removing directories requires -R option");
6795 error = got_worktree_schedule_delete(worktree, &paths,
6796 delete_local_mods, status_codes, print_remove_status, NULL,
6797 repo, keep_on_disk);
6800 const struct got_error *close_err = got_repo_close(repo);
6805 got_worktree_close(worktree);
6806 TAILQ_FOREACH(pe, &paths, entry)
6807 free((char *)pe->path);
6808 got_pathlist_free(&paths);
6816 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6817 "path ...\n", getprogname());
6821 static const struct got_error *
6822 revert_progress(void *arg, unsigned char status, const char *path)
6824 if (status == GOT_STATUS_UNVERSIONED)
6827 while (path[0] == '/')
6829 printf("%c %s\n", status, path);
6833 struct choose_patch_arg {
6834 FILE *patch_script_file;
6838 static const struct got_error *
6839 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6840 int nchanges, const char *action)
6843 size_t linesize = 0;
6847 case GOT_STATUS_ADD:
6848 printf("A %s\n%s this addition? [y/n] ", path, action);
6850 case GOT_STATUS_DELETE:
6851 printf("D %s\n%s this deletion? [y/n] ", path, action);
6853 case GOT_STATUS_MODIFY:
6854 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6855 return got_error_from_errno("fseek");
6856 printf(GOT_COMMIT_SEP_STR);
6857 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6859 if (ferror(patch_file))
6860 return got_error_from_errno("getline");
6861 printf(GOT_COMMIT_SEP_STR);
6862 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6863 path, n, nchanges, action);
6866 return got_error_path(path, GOT_ERR_FILE_STATUS);
6872 static const struct got_error *
6873 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6874 FILE *patch_file, int n, int nchanges)
6876 const struct got_error *err = NULL;
6878 size_t linesize = 0;
6881 struct choose_patch_arg *a = arg;
6883 *choice = GOT_PATCH_CHOICE_NONE;
6885 if (a->patch_script_file) {
6887 err = show_change(status, path, patch_file, n, nchanges,
6891 linelen = getline(&line, &linesize, a->patch_script_file);
6892 if (linelen == -1) {
6893 if (ferror(a->patch_script_file))
6894 return got_error_from_errno("getline");
6897 nl = strchr(line, '\n');
6900 if (strcmp(line, "y") == 0) {
6901 *choice = GOT_PATCH_CHOICE_YES;
6903 } else if (strcmp(line, "n") == 0) {
6904 *choice = GOT_PATCH_CHOICE_NO;
6906 } else if (strcmp(line, "q") == 0 &&
6907 status == GOT_STATUS_MODIFY) {
6908 *choice = GOT_PATCH_CHOICE_QUIT;
6911 printf("invalid response '%s'\n", line);
6916 while (resp != 'y' && resp != 'n' && resp != 'q') {
6917 err = show_change(status, path, patch_file, n, nchanges,
6924 if (status == GOT_STATUS_MODIFY) {
6925 if (resp != 'y' && resp != 'n' && resp != 'q') {
6926 printf("invalid response '%c'\n", resp);
6929 } else if (resp != 'y' && resp != 'n') {
6930 printf("invalid response '%c'\n", resp);
6936 *choice = GOT_PATCH_CHOICE_YES;
6937 else if (resp == 'n')
6938 *choice = GOT_PATCH_CHOICE_NO;
6939 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6940 *choice = GOT_PATCH_CHOICE_QUIT;
6946 static const struct got_error *
6947 cmd_revert(int argc, char *argv[])
6949 const struct got_error *error = NULL;
6950 struct got_worktree *worktree = NULL;
6951 struct got_repository *repo = NULL;
6952 char *cwd = NULL, *path = NULL;
6953 struct got_pathlist_head paths;
6954 struct got_pathlist_entry *pe;
6955 int ch, can_recurse = 0, pflag = 0;
6956 FILE *patch_script_file = NULL;
6957 const char *patch_script_path = NULL;
6958 struct choose_patch_arg cpa;
6962 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6968 patch_script_path = optarg;
6983 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6984 "unveil", NULL) == -1)
6989 if (patch_script_path && !pflag)
6990 errx(1, "-F option can only be used together with -p option");
6992 cwd = getcwd(NULL, 0);
6994 error = got_error_from_errno("getcwd");
6997 error = got_worktree_open(&worktree, cwd);
6999 if (error->code == GOT_ERR_NOT_WORKTREE)
7000 error = wrap_not_worktree_error(error, "revert", cwd);
7004 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7009 if (patch_script_path) {
7010 patch_script_file = fopen(patch_script_path, "r");
7011 if (patch_script_file == NULL) {
7012 error = got_error_from_errno2("fopen",
7017 error = apply_unveil(got_repo_get_path(repo), 1,
7018 got_worktree_get_root_path(worktree));
7022 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7029 TAILQ_FOREACH(pe, &paths, entry) {
7030 if (asprintf(&ondisk_path, "%s/%s",
7031 got_worktree_get_root_path(worktree),
7033 error = got_error_from_errno("asprintf");
7036 if (lstat(ondisk_path, &sb) == -1) {
7037 if (errno == ENOENT) {
7041 error = got_error_from_errno2("lstat",
7047 if (S_ISDIR(sb.st_mode)) {
7048 error = got_error_msg(GOT_ERR_BAD_PATH,
7049 "reverting directories requires -R option");
7055 cpa.patch_script_file = patch_script_file;
7056 cpa.action = "revert";
7057 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7058 pflag ? choose_patch : NULL, &cpa, repo);
7060 if (patch_script_file && fclose(patch_script_file) == EOF &&
7062 error = got_error_from_errno2("fclose", patch_script_path);
7064 const struct got_error *close_err = got_repo_close(repo);
7069 got_worktree_close(worktree);
7078 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7079 "[path ...]\n", getprogname());
7083 struct collect_commit_logmsg_arg {
7084 const char *cmdline_log;
7085 const char *prepared_log;
7086 int non_interactive;
7088 const char *worktree_path;
7089 const char *branch_name;
7090 const char *repo_path;
7095 static const struct got_error *
7096 read_prepared_logmsg(char **logmsg, const char *path)
7098 const struct got_error *err = NULL;
7104 memset(&sb, 0, sizeof(sb));
7106 f = fopen(path, "r");
7108 return got_error_from_errno2("fopen", path);
7110 if (fstat(fileno(f), &sb) == -1) {
7111 err = got_error_from_errno2("fstat", path);
7114 if (sb.st_size == 0) {
7115 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7119 *logmsg = malloc(sb.st_size + 1);
7120 if (*logmsg == NULL) {
7121 err = got_error_from_errno("malloc");
7125 r = fread(*logmsg, 1, sb.st_size, f);
7126 if (r != sb.st_size) {
7128 err = got_error_from_errno2("fread", path);
7130 err = got_error(GOT_ERR_IO);
7133 (*logmsg)[sb.st_size] = '\0';
7135 if (fclose(f) == EOF && err == NULL)
7136 err = got_error_from_errno2("fclose", path);
7145 static const struct got_error *
7146 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7149 char *initial_content = NULL;
7150 struct got_pathlist_entry *pe;
7151 const struct got_error *err = NULL;
7152 char *template = NULL;
7153 struct collect_commit_logmsg_arg *a = arg;
7154 int initial_content_len;
7158 /* if a message was specified on the command line, just use it */
7159 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7160 len = strlen(a->cmdline_log) + 1;
7161 *logmsg = malloc(len + 1);
7162 if (*logmsg == NULL)
7163 return got_error_from_errno("malloc");
7164 strlcpy(*logmsg, a->cmdline_log, len);
7166 } else if (a->prepared_log != NULL && a->non_interactive)
7167 return read_prepared_logmsg(logmsg, a->prepared_log);
7169 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7170 return got_error_from_errno("asprintf");
7172 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7176 if (a->prepared_log) {
7178 err = read_prepared_logmsg(&msg, a->prepared_log);
7181 if (write(fd, msg, strlen(msg)) == -1) {
7182 err = got_error_from_errno2("write", a->logmsg_path);
7189 initial_content_len = asprintf(&initial_content,
7190 "\n# changes to be committed on branch %s:\n",
7192 if (initial_content_len == -1) {
7193 err = got_error_from_errno("asprintf");
7197 if (write(fd, initial_content, initial_content_len) == -1) {
7198 err = got_error_from_errno2("write", a->logmsg_path);
7202 TAILQ_FOREACH(pe, commitable_paths, entry) {
7203 struct got_commitable *ct = pe->data;
7204 dprintf(fd, "# %c %s\n",
7205 got_commitable_get_status(ct),
7206 got_commitable_get_path(ct));
7209 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7210 initial_content_len, a->prepared_log ? 0 : 1);
7212 free(initial_content);
7215 if (fd != -1 && close(fd) == -1 && err == NULL)
7216 err = got_error_from_errno2("close", a->logmsg_path);
7218 /* Editor is done; we can now apply unveil(2) */
7220 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7228 static const struct got_error *
7229 cmd_commit(int argc, char *argv[])
7231 const struct got_error *error = NULL;
7232 struct got_worktree *worktree = NULL;
7233 struct got_repository *repo = NULL;
7234 char *cwd = NULL, *id_str = NULL;
7235 struct got_object_id *id = NULL;
7236 const char *logmsg = NULL;
7237 char *prepared_logmsg = NULL;
7238 struct collect_commit_logmsg_arg cl_arg;
7239 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7240 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7241 int allow_bad_symlinks = 0, non_interactive = 0;
7242 struct got_pathlist_head paths;
7245 cl_arg.logmsg_path = NULL;
7247 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7251 option_conflict('F', 'm');
7252 prepared_logmsg = realpath(optarg, NULL);
7253 if (prepared_logmsg == NULL)
7254 return got_error_from_errno2("realpath",
7258 if (prepared_logmsg)
7259 option_conflict('m', 'F');
7263 non_interactive = 1;
7266 allow_bad_symlinks = 1;
7278 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7279 "unveil", NULL) == -1)
7282 cwd = getcwd(NULL, 0);
7284 error = got_error_from_errno("getcwd");
7287 error = got_worktree_open(&worktree, cwd);
7289 if (error->code == GOT_ERR_NOT_WORKTREE)
7290 error = wrap_not_worktree_error(error, "commit", cwd);
7294 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7297 if (rebase_in_progress) {
7298 error = got_error(GOT_ERR_REBASING);
7302 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7307 error = get_gitconfig_path(&gitconfig_path);
7310 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7315 error = get_author(&author, repo, worktree);
7320 * unveil(2) traverses exec(2); if an editor is used we have
7321 * to apply unveil after the log message has been written.
7323 if (logmsg == NULL || strlen(logmsg) == 0)
7324 error = get_editor(&editor);
7326 error = apply_unveil(got_repo_get_path(repo), 0,
7327 got_worktree_get_root_path(worktree));
7331 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7335 cl_arg.editor = editor;
7336 cl_arg.cmdline_log = logmsg;
7337 cl_arg.prepared_log = prepared_logmsg;
7338 cl_arg.non_interactive = non_interactive;
7339 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7340 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7341 if (!histedit_in_progress) {
7342 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7343 error = got_error(GOT_ERR_COMMIT_BRANCH);
7346 cl_arg.branch_name += 11;
7348 cl_arg.repo_path = got_repo_get_path(repo);
7349 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7350 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7351 print_status, NULL, repo);
7353 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7354 cl_arg.logmsg_path != NULL)
7355 preserve_logmsg = 1;
7359 error = got_object_id_str(&id_str, id);
7362 printf("Created commit %s\n", id_str);
7364 if (preserve_logmsg) {
7365 fprintf(stderr, "%s: log message preserved in %s\n",
7366 getprogname(), cl_arg.logmsg_path);
7367 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7369 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7370 free(cl_arg.logmsg_path);
7372 const struct got_error *close_err = got_repo_close(repo);
7377 got_worktree_close(worktree);
7380 free(gitconfig_path);
7383 free(prepared_logmsg);
7390 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7391 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7392 "[remote-repository]\n", getprogname());
7396 struct got_send_progress_arg {
7397 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7400 int last_nobj_total;
7404 int printed_something;
7406 struct got_pathlist_head *delete_branches;
7409 static const struct got_error *
7410 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7411 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7414 struct got_send_progress_arg *a = arg;
7415 char scaled_packsize[FMT_SCALED_STRSIZE];
7416 char scaled_sent[FMT_SCALED_STRSIZE];
7417 int p_deltify = 0, p_written = 0, p_sent = 0;
7418 int print_searching = 0, print_total = 0;
7419 int print_deltify = 0, print_written = 0, print_sent = 0;
7421 if (a->verbosity < 0)
7425 const char *status = success ? "accepted" : "rejected";
7428 struct got_pathlist_entry *pe;
7429 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7430 const char *branchname = pe->path;
7431 if (got_path_cmp(branchname, refname,
7432 strlen(branchname), strlen(refname)) == 0) {
7434 a->sent_something = 1;
7440 if (a->printed_something)
7442 printf("Server has %s %s", status, refname);
7443 a->printed_something = 1;
7447 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7448 return got_error_from_errno("fmt_scaled");
7449 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7450 return got_error_from_errno("fmt_scaled");
7452 if (a->last_ncommits != ncommits) {
7453 print_searching = 1;
7454 a->last_ncommits = ncommits;
7457 if (a->last_nobj_total != nobj_total) {
7458 print_searching = 1;
7460 a->last_nobj_total = nobj_total;
7463 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7464 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7465 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7466 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7467 return got_error(GOT_ERR_NO_SPACE);
7470 if (nobj_deltify > 0 || nobj_written > 0) {
7471 if (nobj_deltify > 0) {
7472 p_deltify = (nobj_deltify * 100) / nobj_total;
7473 if (p_deltify != a->last_p_deltify) {
7474 a->last_p_deltify = p_deltify;
7475 print_searching = 1;
7480 if (nobj_written > 0) {
7481 p_written = (nobj_written * 100) / nobj_total;
7482 if (p_written != a->last_p_written) {
7483 a->last_p_written = p_written;
7484 print_searching = 1;
7492 if (bytes_sent > 0) {
7493 p_sent = (bytes_sent * 100) / packfile_size;
7494 if (p_sent != a->last_p_sent) {
7495 a->last_p_sent = p_sent;
7496 print_searching = 1;
7502 a->sent_something = 1;
7505 if (print_searching || print_total || print_deltify || print_written ||
7508 if (print_searching)
7509 printf("packing %d reference%s", ncommits,
7510 ncommits == 1 ? "" : "s");
7512 printf("; %d object%s", nobj_total,
7513 nobj_total == 1 ? "" : "s");
7515 printf("; deltify: %d%%", p_deltify);
7517 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7518 scaled_packsize, p_sent);
7519 else if (print_written)
7520 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7521 scaled_packsize, p_written);
7522 if (print_searching || print_total || print_deltify ||
7523 print_written || print_sent) {
7524 a->printed_something = 1;
7530 static const struct got_error *
7531 cmd_send(int argc, char *argv[])
7533 const struct got_error *error = NULL;
7534 char *cwd = NULL, *repo_path = NULL;
7535 const char *remote_name;
7536 char *proto = NULL, *host = NULL, *port = NULL;
7537 char *repo_name = NULL, *server_path = NULL;
7538 const struct got_remote_repo *remotes, *remote = NULL;
7539 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7540 struct got_repository *repo = NULL;
7541 struct got_worktree *worktree = NULL;
7542 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7543 struct got_pathlist_head branches;
7544 struct got_pathlist_head tags;
7545 struct got_reflist_head all_branches;
7546 struct got_reflist_head all_tags;
7547 struct got_pathlist_head delete_args;
7548 struct got_pathlist_head delete_branches;
7549 struct got_reflist_entry *re;
7550 struct got_pathlist_entry *pe;
7551 int i, ch, sendfd = -1, sendstatus;
7553 struct got_send_progress_arg spa;
7554 int verbosity = 0, overwrite_refs = 0;
7555 int send_all_branches = 0, send_all_tags = 0;
7556 struct got_reference *ref = NULL;
7558 TAILQ_INIT(&branches);
7560 TAILQ_INIT(&all_branches);
7561 TAILQ_INIT(&all_tags);
7562 TAILQ_INIT(&delete_args);
7563 TAILQ_INIT(&delete_branches);
7565 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7568 send_all_branches = 1;
7571 error = got_pathlist_append(&branches, optarg, NULL);
7577 error = got_pathlist_append(&delete_args, optarg, NULL);
7585 repo_path = realpath(optarg, NULL);
7586 if (repo_path == NULL)
7587 return got_error_from_errno2("realpath",
7589 got_path_strip_trailing_slashes(repo_path);
7592 error = got_pathlist_append(&tags, optarg, NULL);
7603 else if (verbosity < 3)
7617 if (send_all_branches && !TAILQ_EMPTY(&branches))
7618 option_conflict('a', 'b');
7619 if (send_all_tags && !TAILQ_EMPTY(&tags))
7620 option_conflict('T', 't');
7624 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7626 remote_name = argv[0];
7630 cwd = getcwd(NULL, 0);
7632 error = got_error_from_errno("getcwd");
7636 if (repo_path == NULL) {
7637 error = got_worktree_open(&worktree, cwd);
7638 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7644 strdup(got_worktree_get_repo_path(worktree));
7645 if (repo_path == NULL)
7646 error = got_error_from_errno("strdup");
7650 repo_path = strdup(cwd);
7651 if (repo_path == NULL) {
7652 error = got_error_from_errno("strdup");
7658 error = got_repo_open(&repo, repo_path, NULL);
7663 worktree_conf = got_worktree_get_gotconfig(worktree);
7664 if (worktree_conf) {
7665 got_gotconfig_get_remotes(&nremotes, &remotes,
7667 for (i = 0; i < nremotes; i++) {
7668 if (strcmp(remotes[i].name, remote_name) == 0) {
7669 remote = &remotes[i];
7675 if (remote == NULL) {
7676 repo_conf = got_repo_get_gotconfig(repo);
7678 got_gotconfig_get_remotes(&nremotes, &remotes,
7680 for (i = 0; i < nremotes; i++) {
7681 if (strcmp(remotes[i].name, remote_name) == 0) {
7682 remote = &remotes[i];
7688 if (remote == NULL) {
7689 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7690 for (i = 0; i < nremotes; i++) {
7691 if (strcmp(remotes[i].name, remote_name) == 0) {
7692 remote = &remotes[i];
7697 if (remote == NULL) {
7698 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7702 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7703 &repo_name, remote->send_url);
7707 if (strcmp(proto, "git") == 0) {
7709 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7710 "sendfd dns inet unveil", NULL) == -1)
7713 } else if (strcmp(proto, "git+ssh") == 0 ||
7714 strcmp(proto, "ssh") == 0) {
7716 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7717 "sendfd unveil", NULL) == -1)
7720 } else if (strcmp(proto, "http") == 0 ||
7721 strcmp(proto, "git+http") == 0) {
7722 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7725 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7729 error = got_dial_apply_unveil(proto);
7733 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7737 if (send_all_branches) {
7738 error = got_ref_list(&all_branches, repo, "refs/heads",
7739 got_ref_cmp_by_name, NULL);
7742 TAILQ_FOREACH(re, &all_branches, entry) {
7743 const char *branchname = got_ref_get_name(re->ref);
7744 error = got_pathlist_append(&branches,
7750 } else if (nbranches == 0) {
7751 for (i = 0; i < remote->nsend_branches; i++) {
7752 got_pathlist_append(&branches,
7753 remote->send_branches[i], NULL);
7757 if (send_all_tags) {
7758 error = got_ref_list(&all_tags, repo, "refs/tags",
7759 got_ref_cmp_by_name, NULL);
7762 TAILQ_FOREACH(re, &all_tags, entry) {
7763 const char *tagname = got_ref_get_name(re->ref);
7764 error = got_pathlist_append(&tags,
7773 * To prevent accidents only branches in refs/heads/ can be deleted
7774 * with 'got send -d'.
7775 * Deleting anything else requires local repository access or Git.
7777 TAILQ_FOREACH(pe, &delete_args, entry) {
7778 const char *branchname = pe->path;
7780 struct got_pathlist_entry *new;
7781 if (strncmp(branchname, "refs/heads/", 11) == 0) {
7782 s = strdup(branchname);
7784 error = got_error_from_errno("strdup");
7788 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7789 error = got_error_from_errno("asprintf");
7793 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7794 if (error || new == NULL /* duplicate */)
7801 if (nbranches == 0 && ndelete_branches == 0) {
7802 struct got_reference *head_ref;
7804 error = got_ref_open(&head_ref, repo,
7805 got_worktree_get_head_ref_name(worktree), 0);
7807 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7810 if (got_ref_is_symbolic(head_ref)) {
7811 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7812 got_ref_close(head_ref);
7817 error = got_pathlist_append(&branches, got_ref_get_name(ref),
7825 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7826 port ? ":" : "", port ? port : "");
7828 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7829 server_path, verbosity);
7833 memset(&spa, 0, sizeof(spa));
7834 spa.last_scaled_packsize[0] = '\0';
7835 spa.last_p_deltify = -1;
7836 spa.last_p_written = -1;
7837 spa.verbosity = verbosity;
7838 spa.delete_branches = &delete_branches;
7839 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7840 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7841 check_cancelled, NULL);
7842 if (spa.printed_something)
7846 if (!spa.sent_something && verbosity >= 0)
7847 printf("Already up-to-date\n");
7850 if (kill(sendpid, SIGTERM) == -1)
7851 error = got_error_from_errno("kill");
7852 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7853 error = got_error_from_errno("waitpid");
7855 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7856 error = got_error_from_errno("close");
7858 const struct got_error *close_err = got_repo_close(repo);
7863 got_worktree_close(worktree);
7866 got_pathlist_free(&branches);
7867 got_pathlist_free(&tags);
7868 got_ref_list_free(&all_branches);
7869 got_ref_list_free(&all_tags);
7870 got_pathlist_free(&delete_args);
7871 TAILQ_FOREACH(pe, &delete_branches, entry)
7872 free((char *)pe->path);
7873 got_pathlist_free(&delete_branches);
7885 usage_cherrypick(void)
7887 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7891 static const struct got_error *
7892 cmd_cherrypick(int argc, char *argv[])
7894 const struct got_error *error = NULL;
7895 struct got_worktree *worktree = NULL;
7896 struct got_repository *repo = NULL;
7897 char *cwd = NULL, *commit_id_str = NULL;
7898 struct got_object_id *commit_id = NULL;
7899 struct got_commit_object *commit = NULL;
7900 struct got_object_qid *pid;
7902 struct got_update_progress_arg upa;
7904 while ((ch = getopt(argc, argv, "")) != -1) {
7916 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7917 "unveil", NULL) == -1)
7923 cwd = getcwd(NULL, 0);
7925 error = got_error_from_errno("getcwd");
7928 error = got_worktree_open(&worktree, cwd);
7930 if (error->code == GOT_ERR_NOT_WORKTREE)
7931 error = wrap_not_worktree_error(error, "cherrypick",
7936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7941 error = apply_unveil(got_repo_get_path(repo), 0,
7942 got_worktree_get_root_path(worktree));
7946 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7947 GOT_OBJ_TYPE_COMMIT, repo);
7948 if (error != NULL) {
7949 struct got_reference *ref;
7950 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7952 error = got_ref_open(&ref, repo, argv[0], 0);
7955 error = got_ref_resolve(&commit_id, repo, ref);
7960 error = got_object_id_str(&commit_id_str, commit_id);
7964 error = got_object_open_as_commit(&commit, repo, commit_id);
7967 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7968 memset(&upa, 0, sizeof(upa));
7969 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7970 commit_id, repo, update_progress, &upa, check_cancelled,
7975 if (upa.did_something)
7976 printf("Merged commit %s\n", commit_id_str);
7977 print_update_progress_stats(&upa);
7980 got_object_commit_close(commit);
7981 free(commit_id_str);
7983 got_worktree_close(worktree);
7985 const struct got_error *close_err = got_repo_close(repo);
7995 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7999 static const struct got_error *
8000 cmd_backout(int argc, char *argv[])
8002 const struct got_error *error = NULL;
8003 struct got_worktree *worktree = NULL;
8004 struct got_repository *repo = NULL;
8005 char *cwd = NULL, *commit_id_str = NULL;
8006 struct got_object_id *commit_id = NULL;
8007 struct got_commit_object *commit = NULL;
8008 struct got_object_qid *pid;
8010 struct got_update_progress_arg upa;
8012 while ((ch = getopt(argc, argv, "")) != -1) {
8024 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8025 "unveil", NULL) == -1)
8031 cwd = getcwd(NULL, 0);
8033 error = got_error_from_errno("getcwd");
8036 error = got_worktree_open(&worktree, cwd);
8038 if (error->code == GOT_ERR_NOT_WORKTREE)
8039 error = wrap_not_worktree_error(error, "backout", cwd);
8043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8048 error = apply_unveil(got_repo_get_path(repo), 0,
8049 got_worktree_get_root_path(worktree));
8053 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8054 GOT_OBJ_TYPE_COMMIT, repo);
8055 if (error != NULL) {
8056 struct got_reference *ref;
8057 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8059 error = got_ref_open(&ref, repo, argv[0], 0);
8062 error = got_ref_resolve(&commit_id, repo, ref);
8067 error = got_object_id_str(&commit_id_str, commit_id);
8071 error = got_object_open_as_commit(&commit, repo, commit_id);
8074 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8076 error = got_error(GOT_ERR_ROOT_COMMIT);
8080 memset(&upa, 0, sizeof(upa));
8081 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8082 update_progress, &upa, check_cancelled, NULL);
8086 if (upa.did_something)
8087 printf("Backed out commit %s\n", commit_id_str);
8088 print_update_progress_stats(&upa);
8091 got_object_commit_close(commit);
8092 free(commit_id_str);
8094 got_worktree_close(worktree);
8096 const struct got_error *close_err = got_repo_close(repo);
8106 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8112 trim_logmsg(char *logmsg, int limit)
8117 len = strlen(logmsg);
8121 nl = strchr(logmsg, '\n');
8126 static const struct got_error *
8127 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8129 const struct got_error *err;
8130 char *logmsg0 = NULL;
8133 err = got_object_commit_get_logmsg(&logmsg0, commit);
8138 while (isspace((unsigned char)s[0]))
8141 *logmsg = strdup(s);
8142 if (*logmsg == NULL) {
8143 err = got_error_from_errno("strdup");
8147 trim_logmsg(*logmsg, limit);
8153 static const struct got_error *
8154 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8156 const struct got_error *err;
8157 struct got_commit_object *commit = NULL;
8158 char *id_str = NULL, *logmsg = NULL;
8160 err = got_object_open_as_commit(&commit, repo, id);
8164 err = got_object_id_str(&id_str, id);
8170 err = get_short_logmsg(&logmsg, 42, commit);
8174 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8177 got_object_commit_close(commit);
8182 static const struct got_error *
8183 show_rebase_progress(struct got_commit_object *commit,
8184 struct got_object_id *old_id, struct got_object_id *new_id)
8186 const struct got_error *err;
8187 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8189 err = got_object_id_str(&old_id_str, old_id);
8194 err = got_object_id_str(&new_id_str, new_id);
8199 old_id_str[12] = '\0';
8201 new_id_str[12] = '\0';
8203 err = get_short_logmsg(&logmsg, 42, commit);
8207 printf("%s -> %s: %s\n", old_id_str,
8208 new_id_str ? new_id_str : "no-op change", logmsg);
8216 static const struct got_error *
8217 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8218 struct got_reference *branch, struct got_reference *new_base_branch,
8219 struct got_reference *tmp_branch, struct got_repository *repo,
8222 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8223 return got_worktree_rebase_complete(worktree, fileindex,
8224 new_base_branch, tmp_branch, branch, repo, create_backup);
8227 static const struct got_error *
8228 rebase_commit(struct got_pathlist_head *merged_paths,
8229 struct got_worktree *worktree, struct got_fileindex *fileindex,
8230 struct got_reference *tmp_branch,
8231 struct got_object_id *commit_id, struct got_repository *repo)
8233 const struct got_error *error;
8234 struct got_commit_object *commit;
8235 struct got_object_id *new_commit_id;
8237 error = got_object_open_as_commit(&commit, repo, commit_id);
8241 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8242 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8244 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8246 error = show_rebase_progress(commit, commit_id, NULL);
8248 error = show_rebase_progress(commit, commit_id, new_commit_id);
8249 free(new_commit_id);
8252 got_object_commit_close(commit);
8256 struct check_path_prefix_arg {
8257 const char *path_prefix;
8262 static const struct got_error *
8263 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8264 struct got_blob_object *blob2, struct got_object_id *id1,
8265 struct got_object_id *id2, const char *path1, const char *path2,
8266 mode_t mode1, mode_t mode2, struct got_repository *repo)
8268 struct check_path_prefix_arg *a = arg;
8270 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8271 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8272 return got_error(a->errcode);
8277 static const struct got_error *
8278 check_path_prefix(struct got_object_id *parent_id,
8279 struct got_object_id *commit_id, const char *path_prefix,
8280 int errcode, struct got_repository *repo)
8282 const struct got_error *err;
8283 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8284 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8285 struct check_path_prefix_arg cpp_arg;
8287 if (got_path_is_root_dir(path_prefix))
8290 err = got_object_open_as_commit(&commit, repo, commit_id);
8294 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8298 err = got_object_open_as_tree(&tree1, repo,
8299 got_object_commit_get_tree_id(parent_commit));
8303 err = got_object_open_as_tree(&tree2, repo,
8304 got_object_commit_get_tree_id(commit));
8308 cpp_arg.path_prefix = path_prefix;
8309 while (cpp_arg.path_prefix[0] == '/')
8310 cpp_arg.path_prefix++;
8311 cpp_arg.len = strlen(cpp_arg.path_prefix);
8312 cpp_arg.errcode = errcode;
8313 err = got_diff_tree(tree1, tree2, "", "", repo,
8314 check_path_prefix_in_diff, &cpp_arg, 0);
8317 got_object_tree_close(tree1);
8319 got_object_tree_close(tree2);
8321 got_object_commit_close(commit);
8323 got_object_commit_close(parent_commit);
8327 static const struct got_error *
8328 collect_commits(struct got_object_id_queue *commits,
8329 struct got_object_id *initial_commit_id,
8330 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8331 const char *path_prefix, int path_prefix_errcode,
8332 struct got_repository *repo)
8334 const struct got_error *err = NULL;
8335 struct got_commit_graph *graph = NULL;
8336 struct got_object_id *parent_id = NULL;
8337 struct got_object_qid *qid;
8338 struct got_object_id *commit_id = initial_commit_id;
8340 err = got_commit_graph_open(&graph, "/", 1);
8344 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8345 check_cancelled, NULL);
8348 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8349 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8350 check_cancelled, NULL);
8352 if (err->code == GOT_ERR_ITER_COMPLETED) {
8353 err = got_error_msg(GOT_ERR_ANCESTRY,
8354 "ran out of commits to rebase before "
8355 "youngest common ancestor commit has "
8360 err = check_path_prefix(parent_id, commit_id,
8361 path_prefix, path_prefix_errcode, repo);
8365 err = got_object_qid_alloc(&qid, commit_id);
8368 STAILQ_INSERT_HEAD(commits, qid, entry);
8369 commit_id = parent_id;
8373 got_commit_graph_close(graph);
8377 static const struct got_error *
8378 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8380 const struct got_error *err = NULL;
8381 time_t committer_time;
8383 char datebuf[11]; /* YYYY-MM-DD + NUL */
8384 char *author0 = NULL, *author, *smallerthan;
8385 char *logmsg0 = NULL, *logmsg, *newline;
8387 committer_time = got_object_commit_get_committer_time(commit);
8388 if (gmtime_r(&committer_time, &tm) == NULL)
8389 return got_error_from_errno("gmtime_r");
8390 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8391 return got_error(GOT_ERR_NO_SPACE);
8393 author0 = strdup(got_object_commit_get_author(commit));
8394 if (author0 == NULL)
8395 return got_error_from_errno("strdup");
8397 smallerthan = strchr(author, '<');
8398 if (smallerthan && smallerthan[1] != '\0')
8399 author = smallerthan + 1;
8400 author[strcspn(author, "@>")] = '\0';
8402 err = got_object_commit_get_logmsg(&logmsg0, commit);
8406 while (*logmsg == '\n')
8408 newline = strchr(logmsg, '\n');
8412 if (asprintf(brief_str, "%s %s %s",
8413 datebuf, author, logmsg) == -1)
8414 err = got_error_from_errno("asprintf");
8421 static const struct got_error *
8422 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8423 struct got_repository *repo)
8425 const struct got_error *err;
8428 err = got_object_id_str(&id_str, id);
8432 err = got_ref_delete(ref, repo);
8436 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8442 static const struct got_error *
8443 print_backup_ref(const char *branch_name, const char *new_id_str,
8444 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8445 struct got_reflist_object_id_map *refs_idmap,
8446 struct got_repository *repo)
8448 const struct got_error *err = NULL;
8449 struct got_reflist_head *refs;
8450 char *refs_str = NULL;
8451 struct got_object_id *new_commit_id = NULL;
8452 struct got_commit_object *new_commit = NULL;
8453 char *new_commit_brief_str = NULL;
8454 struct got_object_id *yca_id = NULL;
8455 struct got_commit_object *yca_commit = NULL;
8456 char *yca_id_str = NULL, *yca_brief_str = NULL;
8457 char *custom_refs_str;
8459 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8460 return got_error_from_errno("asprintf");
8462 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8463 0, 0, refs_idmap, custom_refs_str);
8467 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8471 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8473 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8478 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8482 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8486 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8487 old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8491 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8492 refs_str ? " (" : "", refs_str ? refs_str : "",
8493 refs_str ? ")" : "", new_commit_brief_str);
8494 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8495 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8499 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8503 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8507 err = got_object_id_str(&yca_id_str, yca_id);
8511 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8513 err = build_refs_str(&refs_str, refs, yca_id, repo);
8517 printf("history forked at %s%s%s%s\n %s\n",
8519 refs_str ? " (" : "", refs_str ? refs_str : "",
8520 refs_str ? ")" : "", yca_brief_str);
8523 free(custom_refs_str);
8524 free(new_commit_id);
8528 free(yca_brief_str);
8530 got_object_commit_close(new_commit);
8532 got_object_commit_close(yca_commit);
8537 static const struct got_error *
8538 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8539 int delete, struct got_repository *repo)
8541 const struct got_error *err;
8542 struct got_reflist_head refs, backup_refs;
8543 struct got_reflist_entry *re;
8544 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8545 struct got_object_id *old_commit_id = NULL;
8546 char *branch_name = NULL;
8547 struct got_commit_object *old_commit = NULL;
8548 struct got_reflist_object_id_map *refs_idmap = NULL;
8549 int wanted_branch_found = 0;
8552 TAILQ_INIT(&backup_refs);
8554 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8558 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8562 if (wanted_branch_name) {
8563 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8564 wanted_branch_name += 11;
8567 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8568 got_ref_cmp_by_commit_timestamp_descending, repo);
8572 TAILQ_FOREACH(re, &backup_refs, entry) {
8573 const char *refname = got_ref_get_name(re->ref);
8576 err = check_cancelled(NULL);
8580 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8584 err = got_object_open_as_commit(&old_commit, repo,
8589 if (strncmp(backup_ref_prefix, refname,
8590 backup_ref_prefix_len) == 0)
8591 refname += backup_ref_prefix_len;
8593 while (refname[0] == '/')
8596 branch_name = strdup(refname);
8597 if (branch_name == NULL) {
8598 err = got_error_from_errno("strdup");
8601 slash = strrchr(branch_name, '/');
8604 refname += strlen(branch_name) + 1;
8607 if (wanted_branch_name == NULL ||
8608 strcmp(wanted_branch_name, branch_name) == 0) {
8609 wanted_branch_found = 1;
8611 err = delete_backup_ref(re->ref,
8612 old_commit_id, repo);
8614 err = print_backup_ref(branch_name, refname,
8615 old_commit_id, old_commit, refs_idmap,
8622 free(old_commit_id);
8623 old_commit_id = NULL;
8626 got_object_commit_close(old_commit);
8630 if (wanted_branch_name && !wanted_branch_found) {
8631 err = got_error_fmt(GOT_ERR_NOT_REF,
8632 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8636 got_reflist_object_id_map_free(refs_idmap);
8637 got_ref_list_free(&refs);
8638 got_ref_list_free(&backup_refs);
8639 free(old_commit_id);
8642 got_object_commit_close(old_commit);
8646 static const struct got_error *
8647 cmd_rebase(int argc, char *argv[])
8649 const struct got_error *error = NULL;
8650 struct got_worktree *worktree = NULL;
8651 struct got_repository *repo = NULL;
8652 struct got_fileindex *fileindex = NULL;
8654 struct got_reference *branch = NULL;
8655 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8656 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8657 struct got_object_id *resume_commit_id = NULL;
8658 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8659 struct got_commit_object *commit = NULL;
8660 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8661 int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8662 int delete_backups = 0;
8663 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8664 struct got_object_id_queue commits;
8665 struct got_pathlist_head merged_paths;
8666 const struct got_object_id_queue *parent_ids;
8667 struct got_object_qid *qid, *pid;
8669 STAILQ_INIT(&commits);
8670 TAILQ_INIT(&merged_paths);
8672 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8678 continue_rebase = 1;
8696 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8697 "unveil", NULL) == -1)
8702 option_conflict('l', 'a');
8703 if (continue_rebase)
8704 option_conflict('l', 'c');
8706 option_conflict('l', 'X');
8707 if (argc != 0 && argc != 1)
8709 } else if (delete_backups) {
8711 option_conflict('X', 'a');
8712 if (continue_rebase)
8713 option_conflict('X', 'c');
8715 option_conflict('l', 'X');
8716 if (argc != 0 && argc != 1)
8719 if (abort_rebase && continue_rebase)
8721 else if (abort_rebase || continue_rebase) {
8724 } else if (argc != 1)
8728 cwd = getcwd(NULL, 0);
8730 error = got_error_from_errno("getcwd");
8733 error = got_worktree_open(&worktree, cwd);
8735 if (list_backups || delete_backups) {
8736 if (error->code != GOT_ERR_NOT_WORKTREE)
8739 if (error->code == GOT_ERR_NOT_WORKTREE)
8740 error = wrap_not_worktree_error(error,
8746 error = got_repo_open(&repo,
8747 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8751 error = apply_unveil(got_repo_get_path(repo), 0,
8752 worktree ? got_worktree_get_root_path(worktree) : NULL);
8756 if (list_backups || delete_backups) {
8757 error = process_backup_refs(
8758 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8759 argc == 1 ? argv[0] : NULL, delete_backups, repo);
8760 goto done; /* nothing else to do */
8763 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8767 if (histedit_in_progress) {
8768 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8772 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8777 struct got_update_progress_arg upa;
8778 if (!rebase_in_progress) {
8779 error = got_error(GOT_ERR_NOT_REBASING);
8782 error = got_worktree_rebase_continue(&resume_commit_id,
8783 &new_base_branch, &tmp_branch, &branch, &fileindex,
8787 printf("Switching work tree to %s\n",
8788 got_ref_get_symref_target(new_base_branch));
8789 memset(&upa, 0, sizeof(upa));
8790 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8791 new_base_branch, update_progress, &upa);
8794 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8795 print_update_progress_stats(&upa);
8796 goto done; /* nothing else to do */
8799 if (continue_rebase) {
8800 if (!rebase_in_progress) {
8801 error = got_error(GOT_ERR_NOT_REBASING);
8804 error = got_worktree_rebase_continue(&resume_commit_id,
8805 &new_base_branch, &tmp_branch, &branch, &fileindex,
8810 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8811 resume_commit_id, repo);
8815 yca_id = got_object_id_dup(resume_commit_id);
8816 if (yca_id == NULL) {
8817 error = got_error_from_errno("got_object_id_dup");
8821 error = got_ref_open(&branch, repo, argv[0], 0);
8826 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8830 if (!continue_rebase) {
8831 struct got_object_id *base_commit_id;
8833 base_commit_id = got_worktree_get_base_commit_id(worktree);
8834 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8835 base_commit_id, branch_head_commit_id, repo,
8836 check_cancelled, NULL);
8839 if (yca_id == NULL) {
8840 error = got_error_msg(GOT_ERR_ANCESTRY,
8841 "specified branch shares no common ancestry "
8842 "with work tree's branch");
8846 error = check_same_branch(base_commit_id, branch, yca_id, repo);
8848 if (error->code != GOT_ERR_ANCESTRY)
8852 static char msg[128];
8853 snprintf(msg, sizeof(msg),
8854 "%s is already based on %s",
8855 got_ref_get_name(branch),
8856 got_worktree_get_head_ref_name(worktree));
8857 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8860 error = got_worktree_rebase_prepare(&new_base_branch,
8861 &tmp_branch, &fileindex, worktree, branch, repo);
8866 commit_id = branch_head_commit_id;
8867 error = got_object_open_as_commit(&commit, repo, commit_id);
8871 parent_ids = got_object_commit_get_parent_ids(commit);
8872 pid = STAILQ_FIRST(parent_ids);
8874 if (!continue_rebase) {
8875 struct got_update_progress_arg upa;
8876 memset(&upa, 0, sizeof(upa));
8877 error = got_worktree_rebase_abort(worktree, fileindex,
8878 repo, new_base_branch, update_progress, &upa);
8881 printf("Rebase of %s aborted\n",
8882 got_ref_get_name(branch));
8883 print_update_progress_stats(&upa);
8886 error = got_error(GOT_ERR_EMPTY_REBASE);
8889 error = collect_commits(&commits, commit_id, pid->id,
8890 yca_id, got_worktree_get_path_prefix(worktree),
8891 GOT_ERR_REBASE_PATH, repo);
8892 got_object_commit_close(commit);
8897 if (STAILQ_EMPTY(&commits)) {
8898 if (continue_rebase) {
8899 error = rebase_complete(worktree, fileindex,
8900 branch, new_base_branch, tmp_branch, repo,
8904 /* Fast-forward the reference of the branch. */
8905 struct got_object_id *new_head_commit_id;
8907 error = got_ref_resolve(&new_head_commit_id, repo,
8911 error = got_object_id_str(&id_str, new_head_commit_id);
8912 printf("Forwarding %s to commit %s\n",
8913 got_ref_get_name(branch), id_str);
8915 error = got_ref_change_ref(branch,
8916 new_head_commit_id);
8919 /* No backup needed since objects did not change. */
8925 STAILQ_FOREACH(qid, &commits, entry) {
8926 struct got_update_progress_arg upa;
8928 commit_id = qid->id;
8929 parent_id = pid ? pid->id : yca_id;
8932 memset(&upa, 0, sizeof(upa));
8933 error = got_worktree_rebase_merge_files(&merged_paths,
8934 worktree, fileindex, parent_id, commit_id, repo,
8935 update_progress, &upa, check_cancelled, NULL);
8939 print_update_progress_stats(&upa);
8940 if (upa.conflicts > 0)
8941 rebase_status = GOT_STATUS_CONFLICT;
8943 if (rebase_status == GOT_STATUS_CONFLICT) {
8944 error = show_rebase_merge_conflict(qid->id, repo);
8947 got_worktree_rebase_pathlist_free(&merged_paths);
8951 error = rebase_commit(&merged_paths, worktree, fileindex,
8952 tmp_branch, commit_id, repo);
8953 got_worktree_rebase_pathlist_free(&merged_paths);
8958 if (rebase_status == GOT_STATUS_CONFLICT) {
8959 error = got_worktree_rebase_postpone(worktree, fileindex);
8962 error = got_error_msg(GOT_ERR_CONFLICTS,
8963 "conflicts must be resolved before rebasing can continue");
8965 error = rebase_complete(worktree, fileindex, branch,
8966 new_base_branch, tmp_branch, repo, create_backup);
8968 got_object_id_queue_free(&commits);
8969 free(branch_head_commit_id);
8970 free(resume_commit_id);
8973 got_object_commit_close(commit);
8975 got_ref_close(branch);
8976 if (new_base_branch)
8977 got_ref_close(new_base_branch);
8979 got_ref_close(tmp_branch);
8981 got_worktree_close(worktree);
8983 const struct got_error *close_err = got_repo_close(repo);
8991 usage_histedit(void)
8993 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8994 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8999 #define GOT_HISTEDIT_PICK 'p'
9000 #define GOT_HISTEDIT_EDIT 'e'
9001 #define GOT_HISTEDIT_FOLD 'f'
9002 #define GOT_HISTEDIT_DROP 'd'
9003 #define GOT_HISTEDIT_MESG 'm'
9005 static struct got_histedit_cmd {
9009 } got_histedit_cmds[] = {
9010 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9011 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9012 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9014 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9015 { GOT_HISTEDIT_MESG, "mesg",
9016 "single-line log message for commit above (open editor if empty)" },
9019 struct got_histedit_list_entry {
9020 TAILQ_ENTRY(got_histedit_list_entry) entry;
9021 struct got_object_id *commit_id;
9022 const struct got_histedit_cmd *cmd;
9025 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9027 static const struct got_error *
9028 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9029 FILE *f, struct got_repository *repo)
9031 const struct got_error *err = NULL;
9032 char *logmsg = NULL, *id_str = NULL;
9033 struct got_commit_object *commit = NULL;
9036 err = got_object_open_as_commit(&commit, repo, commit_id);
9040 err = get_short_logmsg(&logmsg, 34, commit);
9044 err = got_object_id_str(&id_str, commit_id);
9048 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9050 err = got_ferror(f, GOT_ERR_IO);
9053 got_object_commit_close(commit);
9059 static const struct got_error *
9060 histedit_write_commit_list(struct got_object_id_queue *commits,
9061 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9063 const struct got_error *err = NULL;
9064 struct got_object_qid *qid;
9065 const char *histedit_cmd = NULL;
9067 if (STAILQ_EMPTY(commits))
9068 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9070 STAILQ_FOREACH(qid, commits, entry) {
9071 histedit_cmd = got_histedit_cmds[0].name;
9072 if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9073 histedit_cmd = "fold";
9074 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9077 if (edit_logmsg_only) {
9078 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9080 err = got_ferror(f, GOT_ERR_IO);
9089 static const struct got_error *
9090 write_cmd_list(FILE *f, const char *branch_name,
9091 struct got_object_id_queue *commits)
9093 const struct got_error *err = NULL;
9097 struct got_object_qid *qid;
9099 qid = STAILQ_FIRST(commits);
9100 err = got_object_id_str(&id_str, qid->id);
9105 "# Editing the history of branch '%s' starting at\n"
9107 "# Commits will be processed in order from top to "
9108 "bottom of this file.\n", branch_name, id_str);
9110 err = got_ferror(f, GOT_ERR_IO);
9114 n = fprintf(f, "# Available histedit commands:\n");
9116 err = got_ferror(f, GOT_ERR_IO);
9120 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9121 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9122 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9125 err = got_ferror(f, GOT_ERR_IO);
9134 static const struct got_error *
9135 histedit_syntax_error(int lineno)
9137 static char msg[42];
9140 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9142 if (ret == -1 || ret >= sizeof(msg))
9143 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9145 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9148 static const struct got_error *
9149 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9150 char *logmsg, struct got_repository *repo)
9152 const struct got_error *err;
9153 struct got_commit_object *folded_commit = NULL;
9154 char *id_str, *folded_logmsg = NULL;
9156 err = got_object_id_str(&id_str, hle->commit_id);
9160 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9164 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9167 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9168 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9169 folded_logmsg) == -1) {
9170 err = got_error_from_errno("asprintf");
9174 got_object_commit_close(folded_commit);
9176 free(folded_logmsg);
9180 static struct got_histedit_list_entry *
9181 get_folded_commits(struct got_histedit_list_entry *hle)
9183 struct got_histedit_list_entry *prev, *folded = NULL;
9185 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9186 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9187 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9188 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9190 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9196 static const struct got_error *
9197 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9198 struct got_repository *repo)
9200 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9201 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9202 const struct got_error *err = NULL;
9203 struct got_commit_object *commit = NULL;
9206 struct got_histedit_list_entry *folded = NULL;
9208 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9212 folded = get_folded_commits(hle);
9214 while (folded != hle) {
9215 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9216 folded = TAILQ_NEXT(folded, entry);
9219 err = append_folded_commit_msg(&new_msg, folded,
9225 folded = TAILQ_NEXT(folded, entry);
9229 err = got_object_id_str(&id_str, hle->commit_id);
9232 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9235 logmsg_len = asprintf(&new_msg,
9236 "%s\n# original log message of commit %s: %s",
9237 logmsg ? logmsg : "", id_str, orig_logmsg);
9238 if (logmsg_len == -1) {
9239 err = got_error_from_errno("asprintf");
9245 err = got_object_id_str(&id_str, hle->commit_id);
9249 err = got_opentemp_named_fd(&logmsg_path, &fd,
9250 GOT_TMPDIR_STR "/got-logmsg");
9254 write(fd, logmsg, logmsg_len);
9257 err = get_editor(&editor);
9261 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9264 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9267 hle->logmsg = strdup(new_msg);
9268 if (hle->logmsg == NULL)
9269 err = got_error_from_errno("strdup");
9272 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9273 err = got_error_from_errno2("unlink", logmsg_path);
9279 got_object_commit_close(commit);
9283 static const struct got_error *
9284 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9285 FILE *f, struct got_repository *repo)
9287 const struct got_error *err = NULL;
9288 char *line = NULL, *p, *end;
9292 const struct got_histedit_cmd *cmd;
9293 struct got_object_id *commit_id = NULL;
9294 struct got_histedit_list_entry *hle = NULL;
9297 len = getline(&line, &size, f);
9299 const struct got_error *getline_err;
9302 getline_err = got_error_from_errno("getline");
9303 err = got_ferror(f, getline_err->code);
9308 while (isspace((unsigned char)p[0]))
9310 if (p[0] == '#' || p[0] == '\0') {
9316 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9317 cmd = &got_histedit_cmds[i];
9318 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9319 isspace((unsigned char)p[strlen(cmd->name)])) {
9320 p += strlen(cmd->name);
9323 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9328 if (i == nitems(got_histedit_cmds)) {
9329 err = histedit_syntax_error(lineno);
9332 while (isspace((unsigned char)p[0]))
9334 if (cmd->code == GOT_HISTEDIT_MESG) {
9335 if (hle == NULL || hle->logmsg != NULL) {
9336 err = got_error(GOT_ERR_HISTEDIT_CMD);
9340 err = histedit_edit_logmsg(hle, repo);
9344 hle->logmsg = strdup(p);
9345 if (hle->logmsg == NULL) {
9346 err = got_error_from_errno("strdup");
9355 while (end[0] && !isspace((unsigned char)end[0]))
9359 err = got_object_resolve_id_str(&commit_id, repo, p);
9361 /* override error code */
9362 err = histedit_syntax_error(lineno);
9366 hle = malloc(sizeof(*hle));
9368 err = got_error_from_errno("malloc");
9372 hle->commit_id = commit_id;
9377 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9385 static const struct got_error *
9386 histedit_check_script(struct got_histedit_list *histedit_cmds,
9387 struct got_object_id_queue *commits, struct got_repository *repo)
9389 const struct got_error *err = NULL;
9390 struct got_object_qid *qid;
9391 struct got_histedit_list_entry *hle;
9392 static char msg[92];
9395 if (TAILQ_EMPTY(histedit_cmds))
9396 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9397 "histedit script contains no commands");
9398 if (STAILQ_EMPTY(commits))
9399 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9401 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9402 struct got_histedit_list_entry *hle2;
9403 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9406 if (got_object_id_cmp(hle->commit_id,
9407 hle2->commit_id) != 0)
9409 err = got_object_id_str(&id_str, hle->commit_id);
9412 snprintf(msg, sizeof(msg), "commit %s is listed "
9413 "more than once in histedit script", id_str);
9415 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9419 STAILQ_FOREACH(qid, commits, entry) {
9420 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9421 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9425 err = got_object_id_str(&id_str, qid->id);
9428 snprintf(msg, sizeof(msg),
9429 "commit %s missing from histedit script", id_str);
9431 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9435 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9436 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9437 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9438 "last commit in histedit script cannot be folded");
9443 static const struct got_error *
9444 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9445 const char *path, struct got_object_id_queue *commits,
9446 struct got_repository *repo)
9448 const struct got_error *err = NULL;
9452 err = get_editor(&editor);
9456 if (spawn_editor(editor, path) == -1) {
9457 err = got_error_from_errno("failed spawning editor");
9461 f = fopen(path, "r");
9463 err = got_error_from_errno("fopen");
9466 err = histedit_parse_list(histedit_cmds, f, repo);
9470 err = histedit_check_script(histedit_cmds, commits, repo);
9472 if (f && fclose(f) == EOF && err == NULL)
9473 err = got_error_from_errno("fclose");
9478 static const struct got_error *
9479 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9480 struct got_object_id_queue *, const char *, const char *,
9481 struct got_repository *);
9483 static const struct got_error *
9484 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9485 struct got_object_id_queue *commits, const char *branch_name,
9486 int edit_logmsg_only, int fold_only, struct got_repository *repo)
9488 const struct got_error *err;
9492 err = got_opentemp_named(&path, &f, "got-histedit");
9496 err = write_cmd_list(f, branch_name, commits);
9500 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9505 if (edit_logmsg_only || fold_only) {
9507 err = histedit_parse_list(histedit_cmds, f, repo);
9509 if (fclose(f) == EOF) {
9510 err = got_error_from_errno("fclose");
9514 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9516 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9517 err->code != GOT_ERR_HISTEDIT_CMD)
9519 err = histedit_edit_list_retry(histedit_cmds, err,
9520 commits, path, branch_name, repo);
9524 if (f && fclose(f) == EOF && err == NULL)
9525 err = got_error_from_errno("fclose");
9526 if (path && unlink(path) != 0 && err == NULL)
9527 err = got_error_from_errno2("unlink", path);
9532 static const struct got_error *
9533 histedit_save_list(struct got_histedit_list *histedit_cmds,
9534 struct got_worktree *worktree, struct got_repository *repo)
9536 const struct got_error *err = NULL;
9539 struct got_histedit_list_entry *hle;
9540 struct got_commit_object *commit = NULL;
9542 err = got_worktree_get_histedit_script_path(&path, worktree);
9546 f = fopen(path, "w");
9548 err = got_error_from_errno2("fopen", path);
9551 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9552 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9558 int n = fprintf(f, "%c %s\n",
9559 GOT_HISTEDIT_MESG, hle->logmsg);
9561 err = got_ferror(f, GOT_ERR_IO);
9567 if (f && fclose(f) == EOF && err == NULL)
9568 err = got_error_from_errno("fclose");
9571 got_object_commit_close(commit);
9576 histedit_free_list(struct got_histedit_list *histedit_cmds)
9578 struct got_histedit_list_entry *hle;
9580 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9581 TAILQ_REMOVE(histedit_cmds, hle, entry);
9586 static const struct got_error *
9587 histedit_load_list(struct got_histedit_list *histedit_cmds,
9588 const char *path, struct got_repository *repo)
9590 const struct got_error *err = NULL;
9593 f = fopen(path, "r");
9595 err = got_error_from_errno2("fopen", path);
9599 err = histedit_parse_list(histedit_cmds, f, repo);
9601 if (f && fclose(f) == EOF && err == NULL)
9602 err = got_error_from_errno("fclose");
9606 static const struct got_error *
9607 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9608 const struct got_error *edit_err, struct got_object_id_queue *commits,
9609 const char *path, const char *branch_name, struct got_repository *repo)
9611 const struct got_error *err = NULL, *prev_err = edit_err;
9614 while (resp != 'c' && resp != 'r' && resp != 'a') {
9615 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9616 "or (a)bort: ", getprogname(), prev_err->msg);
9621 histedit_free_list(histedit_cmds);
9622 err = histedit_run_editor(histedit_cmds, path, commits,
9625 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9626 err->code != GOT_ERR_HISTEDIT_CMD)
9633 } else if (resp == 'r') {
9634 histedit_free_list(histedit_cmds);
9635 err = histedit_edit_script(histedit_cmds,
9636 commits, branch_name, 0, 0, repo);
9638 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9639 err->code != GOT_ERR_HISTEDIT_CMD)
9646 } else if (resp == 'a') {
9647 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9650 printf("invalid response '%c'\n", resp);
9656 static const struct got_error *
9657 histedit_complete(struct got_worktree *worktree,
9658 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9659 struct got_reference *branch, struct got_repository *repo)
9661 printf("Switching work tree to %s\n",
9662 got_ref_get_symref_target(branch));
9663 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9667 static const struct got_error *
9668 show_histedit_progress(struct got_commit_object *commit,
9669 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9671 const struct got_error *err;
9672 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9674 err = got_object_id_str(&old_id_str, hle->commit_id);
9679 err = got_object_id_str(&new_id_str, new_id);
9684 old_id_str[12] = '\0';
9686 new_id_str[12] = '\0';
9689 logmsg = strdup(hle->logmsg);
9690 if (logmsg == NULL) {
9691 err = got_error_from_errno("strdup");
9694 trim_logmsg(logmsg, 42);
9696 err = get_short_logmsg(&logmsg, 42, commit);
9701 switch (hle->cmd->code) {
9702 case GOT_HISTEDIT_PICK:
9703 case GOT_HISTEDIT_EDIT:
9704 printf("%s -> %s: %s\n", old_id_str,
9705 new_id_str ? new_id_str : "no-op change", logmsg);
9707 case GOT_HISTEDIT_DROP:
9708 case GOT_HISTEDIT_FOLD:
9709 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9721 static const struct got_error *
9722 histedit_commit(struct got_pathlist_head *merged_paths,
9723 struct got_worktree *worktree, struct got_fileindex *fileindex,
9724 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9725 struct got_repository *repo)
9727 const struct got_error *err;
9728 struct got_commit_object *commit;
9729 struct got_object_id *new_commit_id;
9731 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9732 && hle->logmsg == NULL) {
9733 err = histedit_edit_logmsg(hle, repo);
9738 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9742 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9743 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9746 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9748 err = show_histedit_progress(commit, hle, NULL);
9750 err = show_histedit_progress(commit, hle, new_commit_id);
9751 free(new_commit_id);
9754 got_object_commit_close(commit);
9758 static const struct got_error *
9759 histedit_skip_commit(struct got_histedit_list_entry *hle,
9760 struct got_worktree *worktree, struct got_repository *repo)
9762 const struct got_error *error;
9763 struct got_commit_object *commit;
9765 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9770 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9774 error = show_histedit_progress(commit, hle, NULL);
9775 got_object_commit_close(commit);
9779 static const struct got_error *
9780 check_local_changes(void *arg, unsigned char status,
9781 unsigned char staged_status, const char *path,
9782 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9783 struct got_object_id *commit_id, int dirfd, const char *de_name)
9785 int *have_local_changes = arg;
9788 case GOT_STATUS_ADD:
9789 case GOT_STATUS_DELETE:
9790 case GOT_STATUS_MODIFY:
9791 case GOT_STATUS_CONFLICT:
9792 *have_local_changes = 1;
9793 return got_error(GOT_ERR_CANCELLED);
9798 switch (staged_status) {
9799 case GOT_STATUS_ADD:
9800 case GOT_STATUS_DELETE:
9801 case GOT_STATUS_MODIFY:
9802 *have_local_changes = 1;
9803 return got_error(GOT_ERR_CANCELLED);
9811 static const struct got_error *
9812 cmd_histedit(int argc, char *argv[])
9814 const struct got_error *error = NULL;
9815 struct got_worktree *worktree = NULL;
9816 struct got_fileindex *fileindex = NULL;
9817 struct got_repository *repo = NULL;
9819 struct got_reference *branch = NULL;
9820 struct got_reference *tmp_branch = NULL;
9821 struct got_object_id *resume_commit_id = NULL;
9822 struct got_object_id *base_commit_id = NULL;
9823 struct got_object_id *head_commit_id = NULL;
9824 struct got_commit_object *commit = NULL;
9825 int ch, rebase_in_progress = 0;
9826 struct got_update_progress_arg upa;
9827 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9828 int edit_logmsg_only = 0, fold_only = 0;
9829 int list_backups = 0, delete_backups = 0;
9830 const char *edit_script_path = NULL;
9831 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9832 struct got_object_id_queue commits;
9833 struct got_pathlist_head merged_paths;
9834 const struct got_object_id_queue *parent_ids;
9835 struct got_object_qid *pid;
9836 struct got_histedit_list histedit_cmds;
9837 struct got_histedit_list_entry *hle;
9839 STAILQ_INIT(&commits);
9840 TAILQ_INIT(&histedit_cmds);
9841 TAILQ_INIT(&merged_paths);
9842 memset(&upa, 0, sizeof(upa));
9844 while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9856 edit_script_path = optarg;
9859 edit_logmsg_only = 1;
9877 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9878 "unveil", NULL) == -1)
9881 if (abort_edit && continue_edit)
9882 option_conflict('a', 'c');
9883 if (edit_script_path && edit_logmsg_only)
9884 option_conflict('F', 'm');
9885 if (abort_edit && edit_logmsg_only)
9886 option_conflict('a', 'm');
9887 if (continue_edit && edit_logmsg_only)
9888 option_conflict('c', 'm');
9889 if (abort_edit && fold_only)
9890 option_conflict('a', 'f');
9891 if (continue_edit && fold_only)
9892 option_conflict('c', 'f');
9893 if (fold_only && edit_logmsg_only)
9894 option_conflict('f', 'm');
9895 if (edit_script_path && fold_only)
9896 option_conflict('F', 'f');
9899 option_conflict('l', 'a');
9901 option_conflict('l', 'c');
9902 if (edit_script_path)
9903 option_conflict('l', 'F');
9904 if (edit_logmsg_only)
9905 option_conflict('l', 'm');
9907 option_conflict('l', 'f');
9909 option_conflict('l', 'X');
9910 if (argc != 0 && argc != 1)
9912 } else if (delete_backups) {
9914 option_conflict('X', 'a');
9916 option_conflict('X', 'c');
9917 if (edit_script_path)
9918 option_conflict('X', 'F');
9919 if (edit_logmsg_only)
9920 option_conflict('X', 'm');
9922 option_conflict('X', 'f');
9924 option_conflict('X', 'l');
9925 if (argc != 0 && argc != 1)
9927 } else if (argc != 0)
9931 * This command cannot apply unveil(2) in all cases because the
9932 * user may choose to run an editor to edit the histedit script
9933 * and to edit individual commit log messages.
9934 * unveil(2) traverses exec(2); if an editor is used we have to
9935 * apply unveil after edit script and log messages have been written.
9936 * XXX TODO: Make use of unveil(2) where possible.
9939 cwd = getcwd(NULL, 0);
9941 error = got_error_from_errno("getcwd");
9944 error = got_worktree_open(&worktree, cwd);
9946 if (list_backups || delete_backups) {
9947 if (error->code != GOT_ERR_NOT_WORKTREE)
9950 if (error->code == GOT_ERR_NOT_WORKTREE)
9951 error = wrap_not_worktree_error(error,
9957 if (list_backups || delete_backups) {
9958 error = got_repo_open(&repo,
9959 worktree ? got_worktree_get_repo_path(worktree) : cwd,
9963 error = apply_unveil(got_repo_get_path(repo), 0,
9964 worktree ? got_worktree_get_root_path(worktree) : NULL);
9967 error = process_backup_refs(
9968 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9969 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9970 goto done; /* nothing else to do */
9973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9978 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9981 if (rebase_in_progress) {
9982 error = got_error(GOT_ERR_REBASING);
9986 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9990 if (edit_in_progress && edit_logmsg_only) {
9991 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9992 "histedit operation is in progress in this "
9993 "work tree and must be continued or aborted "
9994 "before the -m option can be used");
9997 if (edit_in_progress && fold_only) {
9998 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9999 "histedit operation is in progress in this "
10000 "work tree and must be continued or aborted "
10001 "before the -f option can be used");
10005 if (edit_in_progress && abort_edit) {
10006 error = got_worktree_histedit_continue(&resume_commit_id,
10007 &tmp_branch, &branch, &base_commit_id, &fileindex,
10011 printf("Switching work tree to %s\n",
10012 got_ref_get_symref_target(branch));
10013 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10014 branch, base_commit_id, update_progress, &upa);
10017 printf("Histedit of %s aborted\n",
10018 got_ref_get_symref_target(branch));
10019 print_update_progress_stats(&upa);
10020 goto done; /* nothing else to do */
10021 } else if (abort_edit) {
10022 error = got_error(GOT_ERR_NOT_HISTEDIT);
10026 if (continue_edit) {
10029 if (!edit_in_progress) {
10030 error = got_error(GOT_ERR_NOT_HISTEDIT);
10034 error = got_worktree_get_histedit_script_path(&path, worktree);
10038 error = histedit_load_list(&histedit_cmds, path, repo);
10043 error = got_worktree_histedit_continue(&resume_commit_id,
10044 &tmp_branch, &branch, &base_commit_id, &fileindex,
10049 error = got_ref_resolve(&head_commit_id, repo, branch);
10053 error = got_object_open_as_commit(&commit, repo,
10057 parent_ids = got_object_commit_get_parent_ids(commit);
10058 pid = STAILQ_FIRST(parent_ids);
10060 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10063 error = collect_commits(&commits, head_commit_id, pid->id,
10064 base_commit_id, got_worktree_get_path_prefix(worktree),
10065 GOT_ERR_HISTEDIT_PATH, repo);
10066 got_object_commit_close(commit);
10071 if (edit_in_progress) {
10072 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10076 error = got_ref_open(&branch, repo,
10077 got_worktree_get_head_ref_name(worktree), 0);
10081 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10082 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10083 "will not edit commit history of a branch outside "
10084 "the \"refs/heads/\" reference namespace");
10088 error = got_ref_resolve(&head_commit_id, repo, branch);
10089 got_ref_close(branch);
10094 error = got_object_open_as_commit(&commit, repo,
10098 parent_ids = got_object_commit_get_parent_ids(commit);
10099 pid = STAILQ_FIRST(parent_ids);
10101 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10104 error = collect_commits(&commits, head_commit_id, pid->id,
10105 got_worktree_get_base_commit_id(worktree),
10106 got_worktree_get_path_prefix(worktree),
10107 GOT_ERR_HISTEDIT_PATH, repo);
10108 got_object_commit_close(commit);
10113 if (STAILQ_EMPTY(&commits)) {
10114 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10118 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10119 &base_commit_id, &fileindex, worktree, repo);
10123 if (edit_script_path) {
10124 error = histedit_load_list(&histedit_cmds,
10125 edit_script_path, repo);
10127 got_worktree_histedit_abort(worktree, fileindex,
10128 repo, branch, base_commit_id,
10129 update_progress, &upa);
10130 print_update_progress_stats(&upa);
10134 const char *branch_name;
10135 branch_name = got_ref_get_symref_target(branch);
10136 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10138 error = histedit_edit_script(&histedit_cmds, &commits,
10139 branch_name, edit_logmsg_only, fold_only, repo);
10141 got_worktree_histedit_abort(worktree, fileindex,
10142 repo, branch, base_commit_id,
10143 update_progress, &upa);
10144 print_update_progress_stats(&upa);
10150 error = histedit_save_list(&histedit_cmds, worktree,
10153 got_worktree_histedit_abort(worktree, fileindex,
10154 repo, branch, base_commit_id,
10155 update_progress, &upa);
10156 print_update_progress_stats(&upa);
10162 error = histedit_check_script(&histedit_cmds, &commits, repo);
10166 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10167 if (resume_commit_id) {
10168 if (got_object_id_cmp(hle->commit_id,
10169 resume_commit_id) != 0)
10172 resume_commit_id = NULL;
10173 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10174 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10175 error = histedit_skip_commit(hle, worktree,
10180 struct got_pathlist_head paths;
10181 int have_changes = 0;
10183 TAILQ_INIT(&paths);
10184 error = got_pathlist_append(&paths, "", NULL);
10187 error = got_worktree_status(worktree, &paths,
10188 repo, 0, check_local_changes, &have_changes,
10189 check_cancelled, NULL);
10190 got_pathlist_free(&paths);
10192 if (error->code != GOT_ERR_CANCELLED)
10194 if (sigint_received || sigpipe_received)
10197 if (have_changes) {
10198 error = histedit_commit(NULL, worktree,
10199 fileindex, tmp_branch, hle, repo);
10203 error = got_object_open_as_commit(
10204 &commit, repo, hle->commit_id);
10207 error = show_histedit_progress(commit,
10209 got_object_commit_close(commit);
10218 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10219 error = histedit_skip_commit(hle, worktree, repo);
10225 error = got_object_open_as_commit(&commit, repo,
10229 parent_ids = got_object_commit_get_parent_ids(commit);
10230 pid = STAILQ_FIRST(parent_ids);
10232 error = got_worktree_histedit_merge_files(&merged_paths,
10233 worktree, fileindex, pid->id, hle->commit_id, repo,
10234 update_progress, &upa, check_cancelled, NULL);
10237 got_object_commit_close(commit);
10240 print_update_progress_stats(&upa);
10241 if (upa.conflicts > 0)
10242 rebase_status = GOT_STATUS_CONFLICT;
10244 if (rebase_status == GOT_STATUS_CONFLICT) {
10245 error = show_rebase_merge_conflict(hle->commit_id,
10249 got_worktree_rebase_pathlist_free(&merged_paths);
10253 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10255 error = got_object_id_str(&id_str, hle->commit_id);
10258 printf("Stopping histedit for amending commit %s\n",
10261 got_worktree_rebase_pathlist_free(&merged_paths);
10262 error = got_worktree_histedit_postpone(worktree,
10267 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10268 error = histedit_skip_commit(hle, worktree, repo);
10274 error = histedit_commit(&merged_paths, worktree, fileindex,
10275 tmp_branch, hle, repo);
10276 got_worktree_rebase_pathlist_free(&merged_paths);
10281 if (rebase_status == GOT_STATUS_CONFLICT) {
10282 error = got_worktree_histedit_postpone(worktree, fileindex);
10285 error = got_error_msg(GOT_ERR_CONFLICTS,
10286 "conflicts must be resolved before histedit can continue");
10288 error = histedit_complete(worktree, fileindex, tmp_branch,
10291 got_object_id_queue_free(&commits);
10292 histedit_free_list(&histedit_cmds);
10293 free(head_commit_id);
10294 free(base_commit_id);
10295 free(resume_commit_id);
10297 got_object_commit_close(commit);
10299 got_ref_close(branch);
10301 got_ref_close(tmp_branch);
10303 got_worktree_close(worktree);
10305 const struct got_error *close_err = got_repo_close(repo);
10313 usage_integrate(void)
10315 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10319 static const struct got_error *
10320 cmd_integrate(int argc, char *argv[])
10322 const struct got_error *error = NULL;
10323 struct got_repository *repo = NULL;
10324 struct got_worktree *worktree = NULL;
10325 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10326 const char *branch_arg = NULL;
10327 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10328 struct got_fileindex *fileindex = NULL;
10329 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10331 struct got_update_progress_arg upa;
10333 while ((ch = getopt(argc, argv, "")) != -1) {
10346 branch_arg = argv[0];
10348 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10349 "unveil", NULL) == -1)
10352 cwd = getcwd(NULL, 0);
10354 error = got_error_from_errno("getcwd");
10358 error = got_worktree_open(&worktree, cwd);
10360 if (error->code == GOT_ERR_NOT_WORKTREE)
10361 error = wrap_not_worktree_error(error, "integrate",
10366 error = check_rebase_or_histedit_in_progress(worktree);
10370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10375 error = apply_unveil(got_repo_get_path(repo), 0,
10376 got_worktree_get_root_path(worktree));
10380 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10381 error = got_error_from_errno("asprintf");
10385 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10386 &base_branch_ref, worktree, refname, repo);
10390 refname = strdup(got_ref_get_name(branch_ref));
10391 if (refname == NULL) {
10392 error = got_error_from_errno("strdup");
10393 got_worktree_integrate_abort(worktree, fileindex, repo,
10394 branch_ref, base_branch_ref);
10397 base_refname = strdup(got_ref_get_name(base_branch_ref));
10398 if (base_refname == NULL) {
10399 error = got_error_from_errno("strdup");
10400 got_worktree_integrate_abort(worktree, fileindex, repo,
10401 branch_ref, base_branch_ref);
10405 error = got_ref_resolve(&commit_id, repo, branch_ref);
10409 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10413 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10414 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10415 "specified branch has already been integrated");
10416 got_worktree_integrate_abort(worktree, fileindex, repo,
10417 branch_ref, base_branch_ref);
10421 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10423 if (error->code == GOT_ERR_ANCESTRY)
10424 error = got_error(GOT_ERR_REBASE_REQUIRED);
10425 got_worktree_integrate_abort(worktree, fileindex, repo,
10426 branch_ref, base_branch_ref);
10430 memset(&upa, 0, sizeof(upa));
10431 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10432 branch_ref, base_branch_ref, update_progress, &upa,
10433 check_cancelled, NULL);
10437 printf("Integrated %s into %s\n", refname, base_refname);
10438 print_update_progress_stats(&upa);
10441 const struct got_error *close_err = got_repo_close(repo);
10446 got_worktree_close(worktree);
10448 free(base_commit_id);
10451 free(base_refname);
10458 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10459 "[-S] [file-path ...]\n",
10464 static const struct got_error *
10465 print_stage(void *arg, unsigned char status, unsigned char staged_status,
10466 const char *path, struct got_object_id *blob_id,
10467 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10468 int dirfd, const char *de_name)
10470 const struct got_error *err = NULL;
10471 char *id_str = NULL;
10473 if (staged_status != GOT_STATUS_ADD &&
10474 staged_status != GOT_STATUS_MODIFY &&
10475 staged_status != GOT_STATUS_DELETE)
10478 if (staged_status == GOT_STATUS_ADD ||
10479 staged_status == GOT_STATUS_MODIFY)
10480 err = got_object_id_str(&id_str, staged_blob_id);
10482 err = got_object_id_str(&id_str, blob_id);
10486 printf("%s %c %s\n", id_str, staged_status, path);
10491 static const struct got_error *
10492 cmd_stage(int argc, char *argv[])
10494 const struct got_error *error = NULL;
10495 struct got_repository *repo = NULL;
10496 struct got_worktree *worktree = NULL;
10498 struct got_pathlist_head paths;
10499 struct got_pathlist_entry *pe;
10500 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10501 FILE *patch_script_file = NULL;
10502 const char *patch_script_path = NULL;
10503 struct choose_patch_arg cpa;
10505 TAILQ_INIT(&paths);
10507 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10516 patch_script_path = optarg;
10519 allow_bad_symlinks = 1;
10531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10532 "unveil", NULL) == -1)
10535 if (list_stage && (pflag || patch_script_path))
10536 errx(1, "-l option cannot be used with other options");
10537 if (patch_script_path && !pflag)
10538 errx(1, "-F option can only be used together with -p option");
10540 cwd = getcwd(NULL, 0);
10542 error = got_error_from_errno("getcwd");
10546 error = got_worktree_open(&worktree, cwd);
10548 if (error->code == GOT_ERR_NOT_WORKTREE)
10549 error = wrap_not_worktree_error(error, "stage", cwd);
10553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10558 if (patch_script_path) {
10559 patch_script_file = fopen(patch_script_path, "r");
10560 if (patch_script_file == NULL) {
10561 error = got_error_from_errno2("fopen",
10562 patch_script_path);
10566 error = apply_unveil(got_repo_get_path(repo), 0,
10567 got_worktree_get_root_path(worktree));
10571 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10576 error = got_worktree_status(worktree, &paths, repo, 0,
10577 print_stage, NULL, check_cancelled, NULL);
10579 cpa.patch_script_file = patch_script_file;
10580 cpa.action = "stage";
10581 error = got_worktree_stage(worktree, &paths,
10582 pflag ? NULL : print_status, NULL,
10583 pflag ? choose_patch : NULL, &cpa,
10584 allow_bad_symlinks, repo);
10587 if (patch_script_file && fclose(patch_script_file) == EOF &&
10589 error = got_error_from_errno2("fclose", patch_script_path);
10591 const struct got_error *close_err = got_repo_close(repo);
10596 got_worktree_close(worktree);
10597 TAILQ_FOREACH(pe, &paths, entry)
10598 free((char *)pe->path);
10599 got_pathlist_free(&paths);
10605 usage_unstage(void)
10607 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10608 "[file-path ...]\n",
10614 static const struct got_error *
10615 cmd_unstage(int argc, char *argv[])
10617 const struct got_error *error = NULL;
10618 struct got_repository *repo = NULL;
10619 struct got_worktree *worktree = NULL;
10621 struct got_pathlist_head paths;
10622 struct got_pathlist_entry *pe;
10624 struct got_update_progress_arg upa;
10625 FILE *patch_script_file = NULL;
10626 const char *patch_script_path = NULL;
10627 struct choose_patch_arg cpa;
10629 TAILQ_INIT(&paths);
10631 while ((ch = getopt(argc, argv, "pF:")) != -1) {
10637 patch_script_path = optarg;
10649 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10650 "unveil", NULL) == -1)
10653 if (patch_script_path && !pflag)
10654 errx(1, "-F option can only be used together with -p option");
10656 cwd = getcwd(NULL, 0);
10658 error = got_error_from_errno("getcwd");
10662 error = got_worktree_open(&worktree, cwd);
10664 if (error->code == GOT_ERR_NOT_WORKTREE)
10665 error = wrap_not_worktree_error(error, "unstage", cwd);
10669 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10674 if (patch_script_path) {
10675 patch_script_file = fopen(patch_script_path, "r");
10676 if (patch_script_file == NULL) {
10677 error = got_error_from_errno2("fopen",
10678 patch_script_path);
10683 error = apply_unveil(got_repo_get_path(repo), 0,
10684 got_worktree_get_root_path(worktree));
10688 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10692 cpa.patch_script_file = patch_script_file;
10693 cpa.action = "unstage";
10694 memset(&upa, 0, sizeof(upa));
10695 error = got_worktree_unstage(worktree, &paths, update_progress,
10696 &upa, pflag ? choose_patch : NULL, &cpa, repo);
10698 print_update_progress_stats(&upa);
10700 if (patch_script_file && fclose(patch_script_file) == EOF &&
10702 error = got_error_from_errno2("fclose", patch_script_path);
10704 const struct got_error *close_err = got_repo_close(repo);
10709 got_worktree_close(worktree);
10710 TAILQ_FOREACH(pe, &paths, entry)
10711 free((char *)pe->path);
10712 got_pathlist_free(&paths);
10720 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10721 "arg1 [arg2 ...]\n", getprogname());
10725 static const struct got_error *
10726 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10728 const struct got_error *err;
10729 struct got_blob_object *blob;
10731 err = got_object_open_as_blob(&blob, repo, id, 8192);
10735 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10736 got_object_blob_close(blob);
10740 static const struct got_error *
10741 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10743 const struct got_error *err;
10744 struct got_tree_object *tree;
10747 err = got_object_open_as_tree(&tree, repo, id);
10751 nentries = got_object_tree_get_nentries(tree);
10752 for (i = 0; i < nentries; i++) {
10753 struct got_tree_entry *te;
10755 if (sigint_received || sigpipe_received)
10757 te = got_object_tree_get_entry(tree, i);
10758 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10761 fprintf(outfile, "%s %.7o %s\n", id_str,
10762 got_tree_entry_get_mode(te),
10763 got_tree_entry_get_name(te));
10767 got_object_tree_close(tree);
10771 static const struct got_error *
10772 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10774 const struct got_error *err;
10775 struct got_commit_object *commit;
10776 const struct got_object_id_queue *parent_ids;
10777 struct got_object_qid *pid;
10778 char *id_str = NULL;
10779 const char *logmsg = NULL;
10781 err = got_object_open_as_commit(&commit, repo, id);
10785 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10789 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10790 parent_ids = got_object_commit_get_parent_ids(commit);
10791 fprintf(outfile, "numparents %d\n",
10792 got_object_commit_get_nparents(commit));
10793 STAILQ_FOREACH(pid, parent_ids, entry) {
10795 err = got_object_id_str(&pid_str, pid->id);
10798 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10801 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10802 got_object_commit_get_author(commit),
10803 (long long)got_object_commit_get_author_time(commit));
10805 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10806 got_object_commit_get_author(commit),
10807 (long long)got_object_commit_get_committer_time(commit));
10809 logmsg = got_object_commit_get_logmsg_raw(commit);
10810 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10811 fprintf(outfile, "%s", logmsg);
10814 got_object_commit_close(commit);
10818 static const struct got_error *
10819 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10821 const struct got_error *err;
10822 struct got_tag_object *tag;
10823 char *id_str = NULL;
10824 const char *tagmsg = NULL;
10826 err = got_object_open_as_tag(&tag, repo, id);
10830 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10834 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10836 switch (got_object_tag_get_object_type(tag)) {
10837 case GOT_OBJ_TYPE_BLOB:
10838 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10839 GOT_OBJ_LABEL_BLOB);
10841 case GOT_OBJ_TYPE_TREE:
10842 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10843 GOT_OBJ_LABEL_TREE);
10845 case GOT_OBJ_TYPE_COMMIT:
10846 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10847 GOT_OBJ_LABEL_COMMIT);
10849 case GOT_OBJ_TYPE_TAG:
10850 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10851 GOT_OBJ_LABEL_TAG);
10857 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10858 got_object_tag_get_name(tag));
10860 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10861 got_object_tag_get_tagger(tag),
10862 (long long)got_object_tag_get_tagger_time(tag));
10864 tagmsg = got_object_tag_get_message(tag);
10865 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10866 fprintf(outfile, "%s", tagmsg);
10869 got_object_tag_close(tag);
10873 static const struct got_error *
10874 cmd_cat(int argc, char *argv[])
10876 const struct got_error *error;
10877 struct got_repository *repo = NULL;
10878 struct got_worktree *worktree = NULL;
10879 char *cwd = NULL, *repo_path = NULL, *label = NULL;
10880 const char *commit_id_str = NULL;
10881 struct got_object_id *id = NULL, *commit_id = NULL;
10882 int ch, obj_type, i, force_path = 0;
10883 struct got_reflist_head refs;
10888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10893 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10896 commit_id_str = optarg;
10899 repo_path = realpath(optarg, NULL);
10900 if (repo_path == NULL)
10901 return got_error_from_errno2("realpath",
10903 got_path_strip_trailing_slashes(repo_path);
10917 cwd = getcwd(NULL, 0);
10919 error = got_error_from_errno("getcwd");
10922 error = got_worktree_open(&worktree, cwd);
10923 if (error && error->code != GOT_ERR_NOT_WORKTREE)
10926 if (repo_path == NULL) {
10927 repo_path = strdup(
10928 got_worktree_get_repo_path(worktree));
10929 if (repo_path == NULL) {
10930 error = got_error_from_errno("strdup");
10936 if (repo_path == NULL) {
10937 repo_path = getcwd(NULL, 0);
10938 if (repo_path == NULL)
10939 return got_error_from_errno("getcwd");
10942 error = got_repo_open(&repo, repo_path, NULL);
10947 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10951 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10955 if (commit_id_str == NULL)
10956 commit_id_str = GOT_REF_HEAD;
10957 error = got_repo_match_object_id(&commit_id, NULL,
10958 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10962 for (i = 0; i < argc; i++) {
10964 error = got_object_id_by_path(&id, repo, commit_id,
10969 error = got_repo_match_object_id(&id, &label, argv[i],
10970 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10973 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10974 error->code != GOT_ERR_NOT_REF)
10976 error = got_object_id_by_path(&id, repo,
10977 commit_id, argv[i]);
10983 error = got_object_get_type(&obj_type, repo, id);
10987 switch (obj_type) {
10988 case GOT_OBJ_TYPE_BLOB:
10989 error = cat_blob(id, repo, stdout);
10991 case GOT_OBJ_TYPE_TREE:
10992 error = cat_tree(id, repo, stdout);
10994 case GOT_OBJ_TYPE_COMMIT:
10995 error = cat_commit(id, repo, stdout);
10997 case GOT_OBJ_TYPE_TAG:
10998 error = cat_tag(id, repo, stdout);
11001 error = got_error(GOT_ERR_OBJ_TYPE);
11016 got_worktree_close(worktree);
11018 const struct got_error *close_err = got_repo_close(repo);
11022 got_ref_list_free(&refs);
11029 fprintf(stderr, "usage: %s info [path ...]\n",
11034 static const struct got_error *
11035 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11036 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11037 struct got_object_id *commit_id)
11039 const struct got_error *err = NULL;
11040 char *id_str = NULL;
11042 struct tm mytm, *tm;
11043 struct got_pathlist_head *paths = arg;
11044 struct got_pathlist_entry *pe;
11047 * Clear error indication from any of the path arguments which
11048 * would cause this file index entry to be displayed.
11050 TAILQ_FOREACH(pe, paths, entry) {
11051 if (got_path_cmp(path, pe->path, strlen(path),
11052 pe->path_len) == 0 ||
11053 got_path_is_child(path, pe->path, pe->path_len))
11054 pe->data = NULL; /* no error */
11057 printf(GOT_COMMIT_SEP_STR);
11059 printf("symlink: %s\n", path);
11060 else if (S_ISREG(mode)) {
11061 printf("file: %s\n", path);
11062 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11063 } else if (S_ISDIR(mode))
11064 printf("directory: %s\n", path);
11066 printf("something: %s\n", path);
11068 tm = localtime_r(&mtime, &mytm);
11071 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11072 return got_error(GOT_ERR_NO_SPACE);
11073 printf("timestamp: %s\n", datebuf);
11076 err = got_object_id_str(&id_str, blob_id);
11079 printf("based on blob: %s\n", id_str);
11083 if (staged_blob_id) {
11084 err = got_object_id_str(&id_str, staged_blob_id);
11087 printf("based on staged blob: %s\n", id_str);
11092 err = got_object_id_str(&id_str, commit_id);
11095 printf("based on commit: %s\n", id_str);
11102 static const struct got_error *
11103 cmd_info(int argc, char *argv[])
11105 const struct got_error *error = NULL;
11106 struct got_worktree *worktree = NULL;
11107 char *cwd = NULL, *id_str = NULL;
11108 struct got_pathlist_head paths;
11109 struct got_pathlist_entry *pe;
11110 char *uuidstr = NULL;
11111 int ch, show_files = 0;
11113 TAILQ_INIT(&paths);
11115 while ((ch = getopt(argc, argv, "")) != -1) {
11127 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11131 cwd = getcwd(NULL, 0);
11133 error = got_error_from_errno("getcwd");
11137 error = got_worktree_open(&worktree, cwd);
11139 if (error->code == GOT_ERR_NOT_WORKTREE)
11140 error = wrap_not_worktree_error(error, "info", cwd);
11144 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11149 error = get_worktree_paths_from_argv(&paths, argc, argv,
11156 error = got_object_id_str(&id_str,
11157 got_worktree_get_base_commit_id(worktree));
11161 error = got_worktree_get_uuid(&uuidstr, worktree);
11165 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11166 printf("work tree base commit: %s\n", id_str);
11167 printf("work tree path prefix: %s\n",
11168 got_worktree_get_path_prefix(worktree));
11169 printf("work tree branch reference: %s\n",
11170 got_worktree_get_head_ref_name(worktree));
11171 printf("work tree UUID: %s\n", uuidstr);
11172 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11175 struct got_pathlist_entry *pe;
11176 TAILQ_FOREACH(pe, &paths, entry) {
11177 if (pe->path_len == 0)
11180 * Assume this path will fail. This will be corrected
11181 * in print_path_info() in case the path does suceeed.
11183 pe->data = (void *)got_error_path(pe->path,
11186 error = got_worktree_path_info(worktree, &paths,
11187 print_path_info, &paths, check_cancelled, NULL);
11190 TAILQ_FOREACH(pe, &paths, entry) {
11191 if (pe->data != NULL) {
11192 error = pe->data; /* bad path */
11198 TAILQ_FOREACH(pe, &paths, entry)
11199 free((char *)pe->path);
11200 got_pathlist_free(&paths);