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] [-q] repository-path [worktree-path]\n",
2646 show_worktree_base_ref_warning(void)
2648 fprintf(stderr, "%s: warning: could not create a reference "
2649 "to the work tree's base commit; the commit could be "
2650 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2651 "repository writable and running 'got update' will prevent this\n",
2655 struct got_checkout_progress_arg {
2656 const char *worktree_path;
2657 int had_base_commit_ref_error;
2661 static const struct got_error *
2662 checkout_progress(void *arg, unsigned char status, const char *path)
2664 struct got_checkout_progress_arg *a = arg;
2666 /* Base commit bump happens silently. */
2667 if (status == GOT_STATUS_BUMP_BASE)
2670 if (status == GOT_STATUS_BASE_REF_ERR) {
2671 a->had_base_commit_ref_error = 1;
2675 while (path[0] == '/')
2678 if (a->verbosity >= 0)
2679 printf("%c %s/%s\n", status, a->worktree_path, path);
2684 static const struct got_error *
2685 check_cancelled(void *arg)
2687 if (sigint_received || sigpipe_received)
2688 return got_error(GOT_ERR_CANCELLED);
2692 static const struct got_error *
2693 check_linear_ancestry(struct got_object_id *commit_id,
2694 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2695 struct got_repository *repo)
2697 const struct got_error *err = NULL;
2698 struct got_object_id *yca_id;
2700 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2701 commit_id, base_commit_id, repo, check_cancelled, NULL);
2706 return got_error(GOT_ERR_ANCESTRY);
2709 * Require a straight line of history between the target commit
2710 * and the work tree's base commit.
2712 * Non-linear situations such as this require a rebase:
2714 * (commit) D F (base_commit)
2722 * 'got update' only handles linear cases:
2723 * Update forwards in time: A (base/yca) - B - C - D (commit)
2724 * Update backwards in time: D (base) - C - B - A (commit/yca)
2726 if (allow_forwards_in_time_only) {
2727 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2728 return got_error(GOT_ERR_ANCESTRY);
2729 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2730 got_object_id_cmp(base_commit_id, yca_id) != 0)
2731 return got_error(GOT_ERR_ANCESTRY);
2737 static const struct got_error *
2738 check_same_branch(struct got_object_id *commit_id,
2739 struct got_reference *head_ref, struct got_object_id *yca_id,
2740 struct got_repository *repo)
2742 const struct got_error *err = NULL;
2743 struct got_commit_graph *graph = NULL;
2744 struct got_object_id *head_commit_id = NULL;
2745 int is_same_branch = 0;
2747 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2751 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2755 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2760 err = got_commit_graph_open(&graph, "/", 1);
2764 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2765 check_cancelled, NULL);
2770 struct got_object_id *id;
2771 err = got_commit_graph_iter_next(&id, graph, repo,
2772 check_cancelled, NULL);
2774 if (err->code == GOT_ERR_ITER_COMPLETED)
2780 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2782 if (got_object_id_cmp(id, commit_id) == 0) {
2790 got_commit_graph_close(graph);
2791 free(head_commit_id);
2792 if (!err && !is_same_branch)
2793 err = got_error(GOT_ERR_ANCESTRY);
2797 static const struct got_error *
2798 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2800 static char msg[512];
2801 const char *branch_name;
2803 if (got_ref_is_symbolic(ref))
2804 branch_name = got_ref_get_symref_target(ref);
2806 branch_name = got_ref_get_name(ref);
2808 if (strncmp("refs/heads/", branch_name, 11) == 0)
2811 snprintf(msg, sizeof(msg),
2812 "target commit is not contained in branch '%s'; "
2813 "the branch to use must be specified with -b; "
2814 "if necessary a new branch can be created for "
2815 "this commit with 'got branch -c %s BRANCH_NAME'",
2816 branch_name, commit_id_str);
2818 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2821 static const struct got_error *
2822 cmd_checkout(int argc, char *argv[])
2824 const struct got_error *error = NULL;
2825 struct got_repository *repo = NULL;
2826 struct got_reference *head_ref = NULL, *ref = NULL;
2827 struct got_worktree *worktree = NULL;
2828 char *repo_path = NULL;
2829 char *worktree_path = NULL;
2830 const char *path_prefix = "";
2831 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2832 char *commit_id_str = NULL;
2833 struct got_object_id *commit_id = NULL;
2835 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2836 struct got_pathlist_head paths;
2837 struct got_checkout_progress_arg cpa;
2841 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2844 branch_name = optarg;
2847 commit_id_str = strdup(optarg);
2848 if (commit_id_str == NULL)
2849 return got_error_from_errno("strdup");
2855 path_prefix = optarg;
2870 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2871 "unveil", NULL) == -1)
2875 char *base, *dotgit;
2877 repo_path = realpath(argv[0], NULL);
2878 if (repo_path == NULL)
2879 return got_error_from_errno2("realpath", argv[0]);
2880 cwd = getcwd(NULL, 0);
2882 error = got_error_from_errno("getcwd");
2889 error = got_path_basename(&base, path);
2892 dotgit = strstr(base, ".git");
2895 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2896 error = got_error_from_errno("asprintf");
2901 } else if (argc == 2) {
2902 repo_path = realpath(argv[0], NULL);
2903 if (repo_path == NULL) {
2904 error = got_error_from_errno2("realpath", argv[0]);
2907 worktree_path = realpath(argv[1], NULL);
2908 if (worktree_path == NULL) {
2909 if (errno != ENOENT) {
2910 error = got_error_from_errno2("realpath",
2914 worktree_path = strdup(argv[1]);
2915 if (worktree_path == NULL) {
2916 error = got_error_from_errno("strdup");
2923 got_path_strip_trailing_slashes(repo_path);
2924 got_path_strip_trailing_slashes(worktree_path);
2926 error = got_repo_open(&repo, repo_path, NULL);
2930 /* Pre-create work tree path for unveil(2) */
2931 error = got_path_mkdir(worktree_path);
2933 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2934 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2936 if (!allow_nonempty &&
2937 !got_path_dir_is_empty(worktree_path)) {
2938 error = got_error_path(worktree_path,
2939 GOT_ERR_DIR_NOT_EMPTY);
2944 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2948 error = got_ref_open(&head_ref, repo, branch_name, 0);
2952 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2953 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2956 error = got_worktree_open(&worktree, worktree_path);
2960 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2964 if (!same_path_prefix) {
2965 error = got_error(GOT_ERR_PATH_PREFIX);
2969 if (commit_id_str) {
2970 struct got_reflist_head refs;
2972 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2976 error = got_repo_match_object_id(&commit_id, NULL,
2977 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2978 got_ref_list_free(&refs);
2981 error = check_linear_ancestry(commit_id,
2982 got_worktree_get_base_commit_id(worktree), 0, repo);
2983 if (error != NULL) {
2985 if (error->code == GOT_ERR_ANCESTRY) {
2986 error = checkout_ancestry_error(
2987 head_ref, commit_id_str);
2991 error = check_same_branch(commit_id, head_ref, NULL, repo);
2993 if (error->code == GOT_ERR_ANCESTRY) {
2994 error = checkout_ancestry_error(
2995 head_ref, commit_id_str);
2999 error = got_worktree_set_base_commit_id(worktree, repo,
3003 /* Expand potentially abbreviated commit ID string. */
3004 free(commit_id_str);
3005 error = got_object_id_str(&commit_id_str, commit_id);
3009 commit_id = got_object_id_dup(
3010 got_worktree_get_base_commit_id(worktree));
3011 if (commit_id == NULL) {
3012 error = got_error_from_errno("got_object_id_dup");
3015 error = got_object_id_str(&commit_id_str, commit_id);
3020 error = got_pathlist_append(&paths, "", NULL);
3023 cpa.worktree_path = worktree_path;
3024 cpa.had_base_commit_ref_error = 0;
3025 cpa.verbosity = verbosity;
3026 error = got_worktree_checkout_files(worktree, &paths, repo,
3027 checkout_progress, &cpa, check_cancelled, NULL);
3031 if (got_ref_is_symbolic(head_ref)) {
3032 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3035 refname = got_ref_get_name(ref);
3037 refname = got_ref_get_name(head_ref);
3038 printf("Checked out %s: %s\n", refname, commit_id_str);
3039 printf("Now shut up and hack\n");
3040 if (cpa.had_base_commit_ref_error)
3041 show_worktree_base_ref_warning();
3044 got_ref_close(head_ref);
3047 got_pathlist_free(&paths);
3048 free(commit_id_str);
3051 free(worktree_path);
3056 struct got_update_progress_arg {
3065 print_update_progress_stats(struct got_update_progress_arg *upa)
3067 if (!upa->did_something)
3070 if (upa->conflicts > 0)
3071 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3072 if (upa->obstructed > 0)
3073 printf("File paths obstructed by a non-regular file: %d\n",
3075 if (upa->not_updated > 0)
3076 printf("Files not updated because of existing merge "
3077 "conflicts: %d\n", upa->not_updated);
3083 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3089 static const struct got_error *
3090 update_progress(void *arg, unsigned char status, const char *path)
3092 struct got_update_progress_arg *upa = arg;
3094 if (status == GOT_STATUS_EXISTS ||
3095 status == GOT_STATUS_BASE_REF_ERR)
3098 upa->did_something = 1;
3100 /* Base commit bump happens silently. */
3101 if (status == GOT_STATUS_BUMP_BASE)
3104 if (status == GOT_STATUS_CONFLICT)
3106 if (status == GOT_STATUS_OBSTRUCTED)
3108 if (status == GOT_STATUS_CANNOT_UPDATE)
3111 while (path[0] == '/')
3113 if (upa->verbosity >= 0)
3114 printf("%c %s\n", status, path);
3119 static const struct got_error *
3120 switch_head_ref(struct got_reference *head_ref,
3121 struct got_object_id *commit_id, struct got_worktree *worktree,
3122 struct got_repository *repo)
3124 const struct got_error *err = NULL;
3126 int ref_has_moved = 0;
3128 /* Trivial case: switching between two different references. */
3129 if (strcmp(got_ref_get_name(head_ref),
3130 got_worktree_get_head_ref_name(worktree)) != 0) {
3131 printf("Switching work tree from %s to %s\n",
3132 got_worktree_get_head_ref_name(worktree),
3133 got_ref_get_name(head_ref));
3134 return got_worktree_set_head_ref(worktree, head_ref);
3137 err = check_linear_ancestry(commit_id,
3138 got_worktree_get_base_commit_id(worktree), 0, repo);
3140 if (err->code != GOT_ERR_ANCESTRY)
3147 /* Switching to a rebased branch with the same reference name. */
3148 err = got_object_id_str(&base_id_str,
3149 got_worktree_get_base_commit_id(worktree));
3152 printf("Reference %s now points at a different branch\n",
3153 got_worktree_get_head_ref_name(worktree));
3154 printf("Switching work tree from %s to %s\n", base_id_str,
3155 got_worktree_get_head_ref_name(worktree));
3159 static const struct got_error *
3160 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3162 const struct got_error *err;
3165 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3169 return got_error(GOT_ERR_REBASING);
3171 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3175 return got_error(GOT_ERR_HISTEDIT_BUSY);
3180 static const struct got_error *
3181 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3182 char *argv[], struct got_worktree *worktree)
3184 const struct got_error *err = NULL;
3191 return got_error_from_errno("strdup");
3192 return got_pathlist_append(paths, path, NULL);
3195 for (i = 0; i < argc; i++) {
3196 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3199 err = got_pathlist_append(paths, path, NULL);
3209 static const struct got_error *
3210 wrap_not_worktree_error(const struct got_error *orig_err,
3211 const char *cmdname, const char *path)
3213 const struct got_error *err;
3214 struct got_repository *repo;
3215 static char msg[512];
3217 err = got_repo_open(&repo, path, NULL);
3221 snprintf(msg, sizeof(msg),
3222 "'got %s' needs a work tree in addition to a git repository\n"
3223 "Work trees can be checked out from this Git repository with "
3225 "The got(1) manual page contains more information.", cmdname);
3226 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3227 got_repo_close(repo);
3231 static const struct got_error *
3232 cmd_update(int argc, char *argv[])
3234 const struct got_error *error = NULL;
3235 struct got_repository *repo = NULL;
3236 struct got_worktree *worktree = NULL;
3237 char *worktree_path = NULL;
3238 struct got_object_id *commit_id = NULL;
3239 char *commit_id_str = NULL;
3240 const char *branch_name = NULL;
3241 struct got_reference *head_ref = NULL;
3242 struct got_pathlist_head paths;
3243 struct got_pathlist_entry *pe;
3244 int ch, verbosity = 0;
3245 struct got_update_progress_arg upa;
3249 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3252 branch_name = optarg;
3255 commit_id_str = strdup(optarg);
3256 if (commit_id_str == NULL)
3257 return got_error_from_errno("strdup");
3272 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3273 "unveil", NULL) == -1)
3276 worktree_path = getcwd(NULL, 0);
3277 if (worktree_path == NULL) {
3278 error = got_error_from_errno("getcwd");
3281 error = got_worktree_open(&worktree, worktree_path);
3283 if (error->code == GOT_ERR_NOT_WORKTREE)
3284 error = wrap_not_worktree_error(error, "update",
3289 error = check_rebase_or_histedit_in_progress(worktree);
3293 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3298 error = apply_unveil(got_repo_get_path(repo), 0,
3299 got_worktree_get_root_path(worktree));
3303 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3307 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3308 got_worktree_get_head_ref_name(worktree), 0);
3311 if (commit_id_str == NULL) {
3312 error = got_ref_resolve(&commit_id, repo, head_ref);
3315 error = got_object_id_str(&commit_id_str, commit_id);
3319 struct got_reflist_head refs;
3321 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3325 error = got_repo_match_object_id(&commit_id, NULL,
3326 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3327 got_ref_list_free(&refs);
3328 free(commit_id_str);
3329 commit_id_str = NULL;
3332 error = got_object_id_str(&commit_id_str, commit_id);
3338 struct got_object_id *head_commit_id;
3339 TAILQ_FOREACH(pe, &paths, entry) {
3340 if (pe->path_len == 0)
3342 error = got_error_msg(GOT_ERR_BAD_PATH,
3343 "switching between branches requires that "
3344 "the entire work tree gets updated");
3347 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3350 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3352 free(head_commit_id);
3355 error = check_same_branch(commit_id, head_ref, NULL, repo);
3358 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3362 error = check_linear_ancestry(commit_id,
3363 got_worktree_get_base_commit_id(worktree), 0, repo);
3364 if (error != NULL) {
3365 if (error->code == GOT_ERR_ANCESTRY)
3366 error = got_error(GOT_ERR_BRANCH_MOVED);
3369 error = check_same_branch(commit_id, head_ref, NULL, repo);
3374 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3376 error = got_worktree_set_base_commit_id(worktree, repo,
3382 memset(&upa, 0, sizeof(upa));
3383 upa.verbosity = verbosity;
3384 error = got_worktree_checkout_files(worktree, &paths, repo,
3385 update_progress, &upa, check_cancelled, NULL);
3389 if (upa.did_something)
3390 printf("Updated to commit %s\n", commit_id_str);
3392 printf("Already up-to-date\n");
3393 print_update_progress_stats(&upa);
3395 free(worktree_path);
3396 TAILQ_FOREACH(pe, &paths, entry)
3397 free((char *)pe->path);
3398 got_pathlist_free(&paths);
3400 free(commit_id_str);
3404 static const struct got_error *
3405 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3406 const char *path, int diff_context, int ignore_whitespace,
3407 int force_text_diff, struct got_repository *repo)
3409 const struct got_error *err = NULL;
3410 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3413 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3418 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3422 while (path[0] == '/')
3424 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3425 diff_context, ignore_whitespace, force_text_diff, stdout);
3428 got_object_blob_close(blob1);
3429 got_object_blob_close(blob2);
3433 static const struct got_error *
3434 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3435 const char *path, int diff_context, int ignore_whitespace,
3436 int force_text_diff, struct got_repository *repo)
3438 const struct got_error *err = NULL;
3439 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3440 struct got_diff_blob_output_unidiff_arg arg;
3443 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3448 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3452 arg.diff_context = diff_context;
3453 arg.ignore_whitespace = ignore_whitespace;
3454 arg.force_text_diff = force_text_diff;
3455 arg.outfile = stdout;
3456 arg.line_offsets = NULL;
3458 while (path[0] == '/')
3460 err = got_diff_tree(tree1, tree2, path, path, repo,
3461 got_diff_blob_output_unidiff, &arg, 1);
3464 got_object_tree_close(tree1);
3466 got_object_tree_close(tree2);
3470 static const struct got_error *
3471 get_changed_paths(struct got_pathlist_head *paths,
3472 struct got_commit_object *commit, struct got_repository *repo)
3474 const struct got_error *err = NULL;
3475 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3476 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3477 struct got_object_qid *qid;
3479 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3481 struct got_commit_object *pcommit;
3482 err = got_object_open_as_commit(&pcommit, repo,
3487 tree_id1 = got_object_id_dup(
3488 got_object_commit_get_tree_id(pcommit));
3489 if (tree_id1 == NULL) {
3490 got_object_commit_close(pcommit);
3491 return got_error_from_errno("got_object_id_dup");
3493 got_object_commit_close(pcommit);
3498 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3503 tree_id2 = got_object_commit_get_tree_id(commit);
3504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3508 err = got_diff_tree(tree1, tree2, "", "", repo,
3509 got_diff_tree_collect_changed_paths, paths, 0);
3512 got_object_tree_close(tree1);
3514 got_object_tree_close(tree2);
3519 static const struct got_error *
3520 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3521 const char *path, int diff_context, struct got_repository *repo)
3523 const struct got_error *err = NULL;
3524 struct got_commit_object *pcommit = NULL;
3525 char *id_str1 = NULL, *id_str2 = NULL;
3526 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3527 struct got_object_qid *qid;
3529 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3531 err = got_object_open_as_commit(&pcommit, repo,
3537 if (path && path[0] != '\0') {
3539 err = got_object_id_by_path(&obj_id2, repo, id, path);
3542 err = got_object_id_str(&id_str2, obj_id2);
3548 err = got_object_id_by_path(&obj_id1, repo,
3551 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3556 err = got_object_id_str(&id_str1, obj_id1);
3563 err = got_object_get_type(&obj_type, repo, obj_id2);
3568 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3570 case GOT_OBJ_TYPE_BLOB:
3571 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3574 case GOT_OBJ_TYPE_TREE:
3575 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3579 err = got_error(GOT_ERR_OBJ_TYPE);
3585 obj_id2 = got_object_commit_get_tree_id(commit);
3586 err = got_object_id_str(&id_str2, obj_id2);
3590 obj_id1 = got_object_commit_get_tree_id(pcommit);
3591 err = got_object_id_str(&id_str1, obj_id1);
3595 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3597 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3604 got_object_commit_close(pcommit);
3609 get_datestr(time_t *time, char *datebuf)
3611 struct tm mytm, *tm;
3614 tm = gmtime_r(time, &mytm);
3617 s = asctime_r(tm, datebuf);
3620 p = strchr(s, '\n');
3626 static const struct got_error *
3627 match_logmsg(int *have_match, struct got_object_id *id,
3628 struct got_commit_object *commit, regex_t *regex)
3630 const struct got_error *err = NULL;
3631 regmatch_t regmatch;
3632 char *id_str = NULL, *logmsg = NULL;
3636 err = got_object_id_str(&id_str, id);
3640 err = got_object_commit_get_logmsg(&logmsg, commit);
3644 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3653 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3656 regmatch_t regmatch;
3657 struct got_pathlist_entry *pe;
3661 TAILQ_FOREACH(pe, changed_paths, entry) {
3662 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3669 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3671 static const struct got_error*
3672 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3673 struct got_object_id *id, struct got_repository *repo)
3675 static const struct got_error *err = NULL;
3676 struct got_reflist_entry *re;
3682 TAILQ_FOREACH(re, refs, entry) {
3683 struct got_tag_object *tag = NULL;
3684 struct got_object_id *ref_id;
3687 name = got_ref_get_name(re->ref);
3688 if (strcmp(name, GOT_REF_HEAD) == 0)
3690 if (strncmp(name, "refs/", 5) == 0)
3692 if (strncmp(name, "got/", 4) == 0)
3694 if (strncmp(name, "heads/", 6) == 0)
3696 if (strncmp(name, "remotes/", 8) == 0) {
3698 s = strstr(name, "/" GOT_REF_HEAD);
3699 if (s != NULL && s[strlen(s)] == '\0')
3702 err = got_ref_resolve(&ref_id, repo, re->ref);
3705 if (strncmp(name, "tags/", 5) == 0) {
3706 err = got_object_open_as_tag(&tag, repo, ref_id);
3708 if (err->code != GOT_ERR_OBJ_TYPE) {
3712 /* Ref points at something other than a tag. */
3717 cmp = got_object_id_cmp(tag ?
3718 got_object_tag_get_object_id(tag) : ref_id, id);
3721 got_object_tag_close(tag);
3725 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3726 s ? ", " : "", name) == -1) {
3727 err = got_error_from_errno("asprintf");
3738 static const struct got_error *
3739 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3740 struct got_repository *repo, const char *path,
3741 struct got_pathlist_head *changed_paths, int show_patch,
3742 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3743 const char *custom_refs_str)
3745 const struct got_error *err = NULL;
3746 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3748 time_t committer_time;
3749 const char *author, *committer;
3750 char *refs_str = NULL;
3752 err = got_object_id_str(&id_str, id);
3756 if (custom_refs_str == NULL) {
3757 struct got_reflist_head *refs;
3758 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3760 err = build_refs_str(&refs_str, refs, id, repo);
3766 printf(GOT_COMMIT_SEP_STR);
3767 if (custom_refs_str)
3768 printf("commit %s (%s)\n", id_str, custom_refs_str);
3770 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3771 refs_str ? refs_str : "", refs_str ? ")" : "");
3776 printf("from: %s\n", got_object_commit_get_author(commit));
3777 committer_time = got_object_commit_get_committer_time(commit);
3778 datestr = get_datestr(&committer_time, datebuf);
3780 printf("date: %s UTC\n", datestr);
3781 author = got_object_commit_get_author(commit);
3782 committer = got_object_commit_get_committer(commit);
3783 if (strcmp(author, committer) != 0)
3784 printf("via: %s\n", committer);
3785 if (got_object_commit_get_nparents(commit) > 1) {
3786 const struct got_object_id_queue *parent_ids;
3787 struct got_object_qid *qid;
3789 parent_ids = got_object_commit_get_parent_ids(commit);
3790 STAILQ_FOREACH(qid, parent_ids, entry) {
3791 err = got_object_id_str(&id_str, qid->id);
3794 printf("parent %d: %s\n", n++, id_str);
3800 err = got_object_commit_get_logmsg(&logmsg0, commit);
3806 line = strsep(&logmsg, "\n");
3808 printf(" %s\n", line);
3812 if (changed_paths) {
3813 struct got_pathlist_entry *pe;
3814 TAILQ_FOREACH(pe, changed_paths, entry) {
3815 struct got_diff_changed_path *cp = pe->data;
3816 printf(" %c %s\n", cp->status, pe->path);
3821 err = print_patch(commit, id, path, diff_context, repo);
3826 if (fflush(stdout) != 0 && err == NULL)
3827 err = got_error_from_errno("fflush");
3834 static const struct got_error *
3835 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3836 struct got_repository *repo, const char *path, int show_changed_paths,
3837 int show_patch, const char *search_pattern, int diff_context, int limit,
3838 int log_branches, int reverse_display_order,
3839 struct got_reflist_object_id_map *refs_idmap)
3841 const struct got_error *err;
3842 struct got_commit_graph *graph;
3845 struct got_object_id_queue reversed_commits;
3846 struct got_object_qid *qid;
3847 struct got_commit_object *commit;
3848 struct got_pathlist_head changed_paths;
3849 struct got_pathlist_entry *pe;
3851 STAILQ_INIT(&reversed_commits);
3852 TAILQ_INIT(&changed_paths);
3854 if (search_pattern && regcomp(®ex, search_pattern,
3855 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3856 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3858 err = got_commit_graph_open(&graph, path, !log_branches);
3861 err = got_commit_graph_iter_start(graph, root_id, repo,
3862 check_cancelled, NULL);
3866 struct got_object_id *id;
3868 if (sigint_received || sigpipe_received)
3871 err = got_commit_graph_iter_next(&id, graph, repo,
3872 check_cancelled, NULL);
3874 if (err->code == GOT_ERR_ITER_COMPLETED)
3881 err = got_object_open_as_commit(&commit, repo, id);
3885 if (show_changed_paths && !reverse_display_order) {
3886 err = get_changed_paths(&changed_paths, commit, repo);
3891 if (search_pattern) {
3892 err = match_logmsg(&have_match, id, commit, ®ex);
3894 got_object_commit_close(commit);
3897 if (have_match == 0 && show_changed_paths)
3898 match_changed_paths(&have_match,
3899 &changed_paths, ®ex);
3900 if (have_match == 0) {
3901 got_object_commit_close(commit);
3902 TAILQ_FOREACH(pe, &changed_paths, entry) {
3903 free((char *)pe->path);
3906 got_pathlist_free(&changed_paths);
3911 if (reverse_display_order) {
3912 err = got_object_qid_alloc(&qid, id);
3915 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3916 got_object_commit_close(commit);
3918 err = print_commit(commit, id, repo, path,
3919 show_changed_paths ? &changed_paths : NULL,
3920 show_patch, diff_context, refs_idmap, NULL);
3921 got_object_commit_close(commit);
3925 if ((limit && --limit == 0) ||
3926 (end_id && got_object_id_cmp(id, end_id) == 0))
3929 TAILQ_FOREACH(pe, &changed_paths, entry) {
3930 free((char *)pe->path);
3933 got_pathlist_free(&changed_paths);
3935 if (reverse_display_order) {
3936 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3937 err = got_object_open_as_commit(&commit, repo, qid->id);
3940 if (show_changed_paths) {
3941 err = get_changed_paths(&changed_paths,
3946 err = print_commit(commit, qid->id, repo, path,
3947 show_changed_paths ? &changed_paths : NULL,
3948 show_patch, diff_context, refs_idmap, NULL);
3949 got_object_commit_close(commit);
3952 TAILQ_FOREACH(pe, &changed_paths, entry) {
3953 free((char *)pe->path);
3956 got_pathlist_free(&changed_paths);
3960 while (!STAILQ_EMPTY(&reversed_commits)) {
3961 qid = STAILQ_FIRST(&reversed_commits);
3962 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3963 got_object_qid_free(qid);
3965 TAILQ_FOREACH(pe, &changed_paths, entry) {
3966 free((char *)pe->path);
3969 got_pathlist_free(&changed_paths);
3972 got_commit_graph_close(graph);
3979 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3980 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3981 "[-R] [path]\n", getprogname());
3986 get_default_log_limit(void)
3988 const char *got_default_log_limit;
3992 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3993 if (got_default_log_limit == NULL)
3995 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4001 static const struct got_error *
4002 cmd_log(int argc, char *argv[])
4004 const struct got_error *error;
4005 struct got_repository *repo = NULL;
4006 struct got_worktree *worktree = NULL;
4007 struct got_object_id *start_id = NULL, *end_id = NULL;
4008 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4009 const char *start_commit = NULL, *end_commit = NULL;
4010 const char *search_pattern = NULL;
4011 int diff_context = -1, ch;
4012 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4013 int reverse_display_order = 0;
4015 struct got_reflist_head refs;
4016 struct got_reflist_object_id_map *refs_idmap = NULL;
4021 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4027 limit = get_default_log_limit();
4029 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4035 show_changed_paths = 1;
4038 start_commit = optarg;
4041 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4044 err(1, "-C option %s", errstr);
4047 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4049 err(1, "-l option %s", errstr);
4055 repo_path = realpath(optarg, NULL);
4056 if (repo_path == NULL)
4057 return got_error_from_errno2("realpath",
4059 got_path_strip_trailing_slashes(repo_path);
4062 reverse_display_order = 1;
4065 search_pattern = optarg;
4068 end_commit = optarg;
4079 if (diff_context == -1)
4081 else if (!show_patch)
4082 errx(1, "-C requires -p");
4084 cwd = getcwd(NULL, 0);
4086 error = got_error_from_errno("getcwd");
4090 if (repo_path == NULL) {
4091 error = got_worktree_open(&worktree, cwd);
4092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4099 error = got_worktree_resolve_path(&path, worktree,
4104 path = strdup(argv[0]);
4106 error = got_error_from_errno("strdup");
4110 } else if (argc != 0)
4113 if (repo_path == NULL) {
4114 repo_path = worktree ?
4115 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4117 if (repo_path == NULL) {
4118 error = got_error_from_errno("strdup");
4122 error = got_repo_open(&repo, repo_path, NULL);
4126 error = apply_unveil(got_repo_get_path(repo), 1,
4127 worktree ? got_worktree_get_root_path(worktree) : NULL);
4131 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4135 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4139 if (start_commit == NULL) {
4140 struct got_reference *head_ref;
4141 struct got_commit_object *commit = NULL;
4142 error = got_ref_open(&head_ref, repo,
4143 worktree ? got_worktree_get_head_ref_name(worktree)
4147 error = got_ref_resolve(&start_id, repo, head_ref);
4148 got_ref_close(head_ref);
4151 error = got_object_open_as_commit(&commit, repo,
4155 got_object_commit_close(commit);
4157 error = got_repo_match_object_id(&start_id, NULL,
4158 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4162 if (end_commit != NULL) {
4163 error = got_repo_match_object_id(&end_id, NULL,
4164 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4171 * If a path was specified on the command line it was resolved
4172 * to a path in the work tree above. Prepend the work tree's
4173 * path prefix to obtain the corresponding in-repository path.
4177 prefix = got_worktree_get_path_prefix(worktree);
4178 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4179 (path[0] != '\0') ? "/" : "", path) == -1) {
4180 error = got_error_from_errno("asprintf");
4185 error = got_repo_map_path(&in_repo_path, repo,
4191 path = in_repo_path;
4194 error = print_commits(start_id, end_id, repo, path ? path : "",
4195 show_changed_paths, show_patch, search_pattern, diff_context,
4196 limit, log_branches, reverse_display_order, refs_idmap);
4202 got_worktree_close(worktree);
4204 const struct got_error *close_err = got_repo_close(repo);
4209 got_reflist_object_id_map_free(refs_idmap);
4210 got_ref_list_free(&refs);
4217 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4218 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4222 struct print_diff_arg {
4223 struct got_repository *repo;
4224 struct got_worktree *worktree;
4229 int ignore_whitespace;
4230 int force_text_diff;
4234 * Create a file which contains the target path of a symlink so we can feed
4235 * it as content to the diff engine.
4237 static const struct got_error *
4238 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4239 const char *abspath)
4241 const struct got_error *err = NULL;
4242 char target_path[PATH_MAX];
4243 ssize_t target_len, outlen;
4248 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4249 if (target_len == -1)
4250 return got_error_from_errno2("readlinkat", abspath);
4252 target_len = readlink(abspath, target_path, PATH_MAX);
4253 if (target_len == -1)
4254 return got_error_from_errno2("readlink", abspath);
4257 *fd = got_opentempfd();
4259 return got_error_from_errno("got_opentempfd");
4261 outlen = write(*fd, target_path, target_len);
4263 err = got_error_from_errno("got_opentempfd");
4267 if (lseek(*fd, 0, SEEK_SET) == -1) {
4268 err = got_error_from_errno2("lseek", abspath);
4279 static const struct got_error *
4280 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4281 const char *path, struct got_object_id *blob_id,
4282 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4283 int dirfd, const char *de_name)
4285 struct print_diff_arg *a = arg;
4286 const struct got_error *err = NULL;
4287 struct got_blob_object *blob1 = NULL;
4290 char *abspath = NULL, *label1 = NULL;
4293 if (a->diff_staged) {
4294 if (staged_status != GOT_STATUS_MODIFY &&
4295 staged_status != GOT_STATUS_ADD &&
4296 staged_status != GOT_STATUS_DELETE)
4299 if (staged_status == GOT_STATUS_DELETE)
4301 if (status == GOT_STATUS_NONEXISTENT)
4302 return got_error_set_errno(ENOENT, path);
4303 if (status != GOT_STATUS_MODIFY &&
4304 status != GOT_STATUS_ADD &&
4305 status != GOT_STATUS_DELETE &&
4306 status != GOT_STATUS_CONFLICT)
4310 if (!a->header_shown) {
4311 printf("diff %s %s%s\n", a->id_str,
4312 got_worktree_get_root_path(a->worktree),
4313 a->diff_staged ? " (staged changes)" : "");
4314 a->header_shown = 1;
4317 if (a->diff_staged) {
4318 const char *label1 = NULL, *label2 = NULL;
4319 switch (staged_status) {
4320 case GOT_STATUS_MODIFY:
4324 case GOT_STATUS_ADD:
4327 case GOT_STATUS_DELETE:
4331 return got_error(GOT_ERR_FILE_STATUS);
4333 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4334 staged_blob_id, label1, label2, a->diff_context,
4335 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4338 if (staged_status == GOT_STATUS_ADD ||
4339 staged_status == GOT_STATUS_MODIFY) {
4341 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4345 err = got_object_id_str(&id_str, staged_blob_id);
4348 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4349 err = got_error_from_errno("asprintf");
4354 } else if (status != GOT_STATUS_ADD) {
4355 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4360 if (status != GOT_STATUS_DELETE) {
4361 if (asprintf(&abspath, "%s/%s",
4362 got_worktree_get_root_path(a->worktree), path) == -1) {
4363 err = got_error_from_errno("asprintf");
4368 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4370 if (errno != ELOOP) {
4371 err = got_error_from_errno2("openat",
4375 err = get_symlink_target_file(&fd, dirfd,
4381 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4383 if (errno != ELOOP) {
4384 err = got_error_from_errno2("open",
4388 err = get_symlink_target_file(&fd, dirfd,
4394 if (fstat(fd, &sb) == -1) {
4395 err = got_error_from_errno2("fstat", abspath);
4398 f2 = fdopen(fd, "r");
4400 err = got_error_from_errno2("fdopen", abspath);
4407 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4408 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4411 got_object_blob_close(blob1);
4412 if (f2 && fclose(f2) == EOF && err == NULL)
4413 err = got_error_from_errno("fclose");
4414 if (fd != -1 && close(fd) == -1 && err == NULL)
4415 err = got_error_from_errno("close");
4420 static const struct got_error *
4421 cmd_diff(int argc, char *argv[])
4423 const struct got_error *error;
4424 struct got_repository *repo = NULL;
4425 struct got_worktree *worktree = NULL;
4426 char *cwd = NULL, *repo_path = NULL;
4427 struct got_object_id *id1 = NULL, *id2 = NULL;
4428 const char *id_str1 = NULL, *id_str2 = NULL;
4429 char *label1 = NULL, *label2 = NULL;
4431 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4432 int force_text_diff = 0;
4435 struct got_reflist_head refs;
4440 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4445 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4448 force_text_diff = 1;
4451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4454 err(1, "-C option %s", errstr);
4457 repo_path = realpath(optarg, NULL);
4458 if (repo_path == NULL)
4459 return got_error_from_errno2("realpath",
4461 got_path_strip_trailing_slashes(repo_path);
4467 ignore_whitespace = 1;
4478 cwd = getcwd(NULL, 0);
4480 error = got_error_from_errno("getcwd");
4486 "-r option can't be used when diffing a work tree");
4487 error = got_worktree_open(&worktree, cwd);
4489 if (error->code == GOT_ERR_NOT_WORKTREE)
4490 error = wrap_not_worktree_error(error, "diff",
4494 repo_path = strdup(got_worktree_get_repo_path(worktree));
4495 if (repo_path == NULL) {
4496 error = got_error_from_errno("strdup");
4500 error = got_worktree_resolve_path(&path, worktree,
4507 error = got_error_from_errno("strdup");
4511 } else if (argc == 2) {
4513 errx(1, "-s option can't be used when diffing "
4514 "objects in repository");
4517 if (repo_path == NULL) {
4518 error = got_worktree_open(&worktree, cwd);
4519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4521 repo_path = strdup(worktree ?
4522 got_worktree_get_repo_path(worktree) : cwd);
4523 if (repo_path == NULL) {
4524 error = got_error_from_errno("strdup");
4531 error = got_repo_open(&repo, repo_path, NULL);
4536 error = apply_unveil(got_repo_get_path(repo), 1,
4537 worktree ? got_worktree_get_root_path(worktree) : NULL);
4542 struct print_diff_arg arg;
4543 struct got_pathlist_head paths;
4548 error = got_object_id_str(&id_str,
4549 got_worktree_get_base_commit_id(worktree));
4553 arg.worktree = worktree;
4554 arg.diff_context = diff_context;
4555 arg.id_str = id_str;
4556 arg.header_shown = 0;
4557 arg.diff_staged = diff_staged;
4558 arg.ignore_whitespace = ignore_whitespace;
4559 arg.force_text_diff = force_text_diff;
4561 error = got_pathlist_append(&paths, path, NULL);
4565 error = got_worktree_status(worktree, &paths, repo, 0,
4566 print_diff, &arg, check_cancelled, NULL);
4568 got_pathlist_free(&paths);
4572 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4576 error = got_repo_match_object_id(&id1, &label1, id_str1,
4577 GOT_OBJ_TYPE_ANY, &refs, repo);
4581 error = got_repo_match_object_id(&id2, &label2, id_str2,
4582 GOT_OBJ_TYPE_ANY, &refs, repo);
4586 error = got_object_get_type(&type1, repo, id1);
4590 error = got_object_get_type(&type2, repo, id2);
4594 if (type1 != type2) {
4595 error = got_error(GOT_ERR_OBJ_TYPE);
4600 case GOT_OBJ_TYPE_BLOB:
4601 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4602 NULL, NULL, diff_context, ignore_whitespace,
4603 force_text_diff, repo, stdout);
4605 case GOT_OBJ_TYPE_TREE:
4606 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4607 "", "", diff_context, ignore_whitespace, force_text_diff,
4610 case GOT_OBJ_TYPE_COMMIT:
4611 printf("diff %s %s\n", label1, label2);
4612 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4613 diff_context, ignore_whitespace, force_text_diff, repo,
4617 error = got_error(GOT_ERR_OBJ_TYPE);
4626 got_worktree_close(worktree);
4628 const struct got_error *close_err = got_repo_close(repo);
4632 got_ref_list_free(&refs);
4640 "usage: %s blame [-c commit] [-r repository-path] path\n",
4649 char datebuf[11]; /* YYYY-MM-DD + NUL */
4652 struct blame_cb_args {
4653 struct blame_line *lines;
4657 off_t *line_offsets;
4659 struct got_repository *repo;
4662 static const struct got_error *
4663 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4665 const struct got_error *err = NULL;
4666 struct blame_cb_args *a = arg;
4667 struct blame_line *bline;
4669 size_t linesize = 0;
4670 struct got_commit_object *commit = NULL;
4673 time_t committer_time;
4675 if (nlines != a->nlines ||
4676 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4677 return got_error(GOT_ERR_RANGE);
4679 if (sigint_received)
4680 return got_error(GOT_ERR_ITER_COMPLETED);
4683 return NULL; /* no change in this commit */
4685 /* Annotate this line. */
4686 bline = &a->lines[lineno - 1];
4687 if (bline->annotated)
4689 err = got_object_id_str(&bline->id_str, id);
4693 err = got_object_open_as_commit(&commit, a->repo, id);
4697 bline->committer = strdup(got_object_commit_get_committer(commit));
4698 if (bline->committer == NULL) {
4699 err = got_error_from_errno("strdup");
4703 committer_time = got_object_commit_get_committer_time(commit);
4704 if (gmtime_r(&committer_time, &tm) == NULL)
4705 return got_error_from_errno("gmtime_r");
4706 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4708 err = got_error(GOT_ERR_NO_SPACE);
4711 bline->annotated = 1;
4713 /* Print lines annotated so far. */
4714 bline = &a->lines[a->lineno_cur - 1];
4715 if (!bline->annotated)
4718 offset = a->line_offsets[a->lineno_cur - 1];
4719 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4720 err = got_error_from_errno("fseeko");
4724 while (bline->annotated) {
4725 char *smallerthan, *at, *nl, *committer;
4728 if (getline(&line, &linesize, a->f) == -1) {
4730 err = got_error_from_errno("getline");
4734 committer = bline->committer;
4735 smallerthan = strchr(committer, '<');
4736 if (smallerthan && smallerthan[1] != '\0')
4737 committer = smallerthan + 1;
4738 at = strchr(committer, '@');
4741 len = strlen(committer);
4743 committer[8] = '\0';
4745 nl = strchr(line, '\n');
4748 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4749 bline->id_str, bline->datebuf, committer, line);
4752 bline = &a->lines[a->lineno_cur - 1];
4756 got_object_commit_close(commit);
4761 static const struct got_error *
4762 cmd_blame(int argc, char *argv[])
4764 const struct got_error *error;
4765 struct got_repository *repo = NULL;
4766 struct got_worktree *worktree = NULL;
4767 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4768 char *link_target = NULL;
4769 struct got_object_id *obj_id = NULL;
4770 struct got_object_id *commit_id = NULL;
4771 struct got_blob_object *blob = NULL;
4772 char *commit_id_str = NULL;
4773 struct blame_cb_args bca;
4774 int ch, obj_type, i;
4777 memset(&bca, 0, sizeof(bca));
4780 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4785 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4788 commit_id_str = optarg;
4791 repo_path = realpath(optarg, NULL);
4792 if (repo_path == NULL)
4793 return got_error_from_errno2("realpath",
4795 got_path_strip_trailing_slashes(repo_path);
4811 cwd = getcwd(NULL, 0);
4813 error = got_error_from_errno("getcwd");
4816 if (repo_path == NULL) {
4817 error = got_worktree_open(&worktree, cwd);
4818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4824 strdup(got_worktree_get_repo_path(worktree));
4825 if (repo_path == NULL) {
4826 error = got_error_from_errno("strdup");
4831 repo_path = strdup(cwd);
4832 if (repo_path == NULL) {
4833 error = got_error_from_errno("strdup");
4839 error = got_repo_open(&repo, repo_path, NULL);
4844 const char *prefix = got_worktree_get_path_prefix(worktree);
4847 error = got_worktree_resolve_path(&p, worktree, path);
4850 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4851 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4853 error = got_error_from_errno("asprintf");
4858 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4860 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4863 error = got_repo_map_path(&in_repo_path, repo, path);
4868 if (commit_id_str == NULL) {
4869 struct got_reference *head_ref;
4870 error = got_ref_open(&head_ref, repo, worktree ?
4871 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4874 error = got_ref_resolve(&commit_id, repo, head_ref);
4875 got_ref_close(head_ref);
4879 struct got_reflist_head refs;
4881 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4885 error = got_repo_match_object_id(&commit_id, NULL,
4886 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4887 got_ref_list_free(&refs);
4892 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4897 error = got_object_id_by_path(&obj_id, repo, commit_id,
4898 link_target ? link_target : in_repo_path);
4902 error = got_object_get_type(&obj_type, repo, obj_id);
4906 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4907 error = got_error_path(link_target ? link_target : in_repo_path,
4912 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4915 bca.f = got_opentemp();
4916 if (bca.f == NULL) {
4917 error = got_error_from_errno("got_opentemp");
4920 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4921 &bca.line_offsets, bca.f, blob);
4922 if (error || bca.nlines == 0)
4925 /* Don't include \n at EOF in the blame line count. */
4926 if (bca.line_offsets[bca.nlines - 1] == filesize)
4929 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4930 if (bca.lines == NULL) {
4931 error = got_error_from_errno("calloc");
4935 bca.nlines_prec = 0;
4943 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4944 repo, blame_cb, &bca, check_cancelled, NULL);
4953 got_object_blob_close(blob);
4955 got_worktree_close(worktree);
4957 const struct got_error *close_err = got_repo_close(repo);
4962 for (i = 0; i < bca.nlines; i++) {
4963 struct blame_line *bline = &bca.lines[i];
4964 free(bline->id_str);
4965 free(bline->committer);
4969 free(bca.line_offsets);
4970 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4971 error = got_error_from_errno("fclose");
4979 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4984 static const struct got_error *
4985 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4986 const char *root_path, struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 int is_root_path = (strcmp(path, root_path) == 0);
4990 const char *modestr = "";
4991 mode_t mode = got_tree_entry_get_mode(te);
4992 char *link_target = NULL;
4994 path += strlen(root_path);
4995 while (path[0] == '/')
4998 if (got_object_tree_entry_is_submodule(te))
5000 else if (S_ISLNK(mode)) {
5003 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5006 for (i = 0; i < strlen(link_target); i++) {
5007 if (!isprint((unsigned char)link_target[i]))
5008 link_target[i] = '?';
5013 else if (S_ISDIR(mode))
5015 else if (mode & S_IXUSR)
5018 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5019 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5020 link_target ? " -> ": "", link_target ? link_target : "");
5026 static const struct got_error *
5027 print_tree(const char *path, struct got_object_id *commit_id,
5028 int show_ids, int recurse, const char *root_path,
5029 struct got_repository *repo)
5031 const struct got_error *err = NULL;
5032 struct got_object_id *tree_id = NULL;
5033 struct got_tree_object *tree = NULL;
5036 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5040 err = got_object_open_as_tree(&tree, repo, tree_id);
5043 nentries = got_object_tree_get_nentries(tree);
5044 for (i = 0; i < nentries; i++) {
5045 struct got_tree_entry *te;
5048 if (sigint_received || sigpipe_received)
5051 te = got_object_tree_get_entry(tree, i);
5054 err = got_object_id_str(&id_str,
5055 got_tree_entry_get_id(te));
5058 if (asprintf(&id, "%s ", id_str) == -1) {
5059 err = got_error_from_errno("asprintf");
5065 err = print_entry(te, id, path, root_path, repo);
5070 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5072 if (asprintf(&child_path, "%s%s%s", path,
5073 path[0] == '/' && path[1] == '\0' ? "" : "/",
5074 got_tree_entry_get_name(te)) == -1) {
5075 err = got_error_from_errno("asprintf");
5078 err = print_tree(child_path, commit_id, show_ids, 1,
5087 got_object_tree_close(tree);
5092 static const struct got_error *
5093 cmd_tree(int argc, char *argv[])
5095 const struct got_error *error;
5096 struct got_repository *repo = NULL;
5097 struct got_worktree *worktree = NULL;
5098 const char *path, *refname = NULL;
5099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5100 struct got_object_id *commit_id = NULL;
5101 char *commit_id_str = NULL;
5102 int show_ids = 0, recurse = 0;
5106 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5111 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5114 commit_id_str = optarg;
5117 repo_path = realpath(optarg, NULL);
5118 if (repo_path == NULL)
5119 return got_error_from_errno2("realpath",
5121 got_path_strip_trailing_slashes(repo_path);
5145 cwd = getcwd(NULL, 0);
5147 error = got_error_from_errno("getcwd");
5150 if (repo_path == NULL) {
5151 error = got_worktree_open(&worktree, cwd);
5152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5158 strdup(got_worktree_get_repo_path(worktree));
5159 if (repo_path == NULL)
5160 error = got_error_from_errno("strdup");
5164 repo_path = strdup(cwd);
5165 if (repo_path == NULL) {
5166 error = got_error_from_errno("strdup");
5172 error = got_repo_open(&repo, repo_path, NULL);
5177 const char *prefix = got_worktree_get_path_prefix(worktree);
5182 error = got_worktree_resolve_path(&p, worktree, path);
5185 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5186 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5188 error = got_error_from_errno("asprintf");
5193 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5197 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5202 error = got_repo_map_path(&in_repo_path, repo, path);
5207 if (commit_id_str == NULL) {
5208 struct got_reference *head_ref;
5210 refname = got_worktree_get_head_ref_name(worktree);
5212 refname = GOT_REF_HEAD;
5213 error = got_ref_open(&head_ref, repo, refname, 0);
5216 error = got_ref_resolve(&commit_id, repo, head_ref);
5217 got_ref_close(head_ref);
5221 struct got_reflist_head refs;
5223 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5227 error = got_repo_match_object_id(&commit_id, NULL,
5228 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5229 got_ref_list_free(&refs);
5234 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5235 in_repo_path, repo);
5242 got_worktree_close(worktree);
5244 const struct got_error *close_err = got_repo_close(repo);
5254 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5255 "[-S status-codes] [path ...]\n", getprogname());
5259 struct got_status_arg {
5264 static const struct got_error *
5265 print_status(void *arg, unsigned char status, unsigned char staged_status,
5266 const char *path, struct got_object_id *blob_id,
5267 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5268 int dirfd, const char *de_name)
5270 struct got_status_arg *st = arg;
5272 if (status == staged_status && (status == GOT_STATUS_DELETE))
5273 status = GOT_STATUS_NO_CHANGE;
5274 if (st != NULL && st->status_codes) {
5275 size_t ncodes = strlen(st->status_codes);
5278 for (i = 0; i < ncodes ; i++) {
5280 if (status == st->status_codes[i] ||
5281 staged_status == st->status_codes[i]) {
5286 if (status == st->status_codes[i] ||
5287 staged_status == st->status_codes[i])
5292 if (st->suppress && j == 0)
5299 printf("%c%c %s\n", status, staged_status, path);
5303 static const struct got_error *
5304 cmd_status(int argc, char *argv[])
5306 const struct got_error *error = NULL;
5307 struct got_repository *repo = NULL;
5308 struct got_worktree *worktree = NULL;
5309 struct got_status_arg st;
5311 struct got_pathlist_head paths;
5312 struct got_pathlist_entry *pe;
5313 int ch, i, no_ignores = 0;
5317 memset(&st, 0, sizeof(st));
5318 st.status_codes = NULL;
5321 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5327 for (i = 0; i < strlen(optarg); i++) {
5328 switch (optarg[i]) {
5329 case GOT_STATUS_MODIFY:
5330 case GOT_STATUS_ADD:
5331 case GOT_STATUS_DELETE:
5332 case GOT_STATUS_CONFLICT:
5333 case GOT_STATUS_MISSING:
5334 case GOT_STATUS_OBSTRUCTED:
5335 case GOT_STATUS_UNVERSIONED:
5336 case GOT_STATUS_MODE_CHANGE:
5337 case GOT_STATUS_NONEXISTENT:
5340 errx(1, "invalid status code '%c'",
5345 option_conflict('s', 'S');
5346 st.status_codes = optarg;
5349 if (st.status_codes != NULL && st.suppress == 0)
5350 option_conflict('S', 's');
5351 st.status_codes = optarg;
5364 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5368 cwd = getcwd(NULL, 0);
5370 error = got_error_from_errno("getcwd");
5374 error = got_worktree_open(&worktree, cwd);
5376 if (error->code == GOT_ERR_NOT_WORKTREE)
5377 error = wrap_not_worktree_error(error, "status", cwd);
5381 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5386 error = apply_unveil(got_repo_get_path(repo), 1,
5387 got_worktree_get_root_path(worktree));
5391 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5395 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5396 print_status, &st, check_cancelled, NULL);
5398 TAILQ_FOREACH(pe, &paths, entry)
5399 free((char *)pe->path);
5400 got_pathlist_free(&paths);
5409 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5415 static const struct got_error *
5416 list_refs(struct got_repository *repo, const char *refname)
5418 static const struct got_error *err = NULL;
5419 struct got_reflist_head refs;
5420 struct got_reflist_entry *re;
5423 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5427 TAILQ_FOREACH(re, &refs, entry) {
5429 refstr = got_ref_to_str(re->ref);
5431 return got_error_from_errno("got_ref_to_str");
5432 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5436 got_ref_list_free(&refs);
5440 static const struct got_error *
5441 delete_ref_by_name(struct got_repository *repo, const char *refname)
5443 const struct got_error *err;
5444 struct got_reference *ref;
5446 err = got_ref_open(&ref, repo, refname, 0);
5450 err = delete_ref(repo, ref);
5455 static const struct got_error *
5456 add_ref(struct got_repository *repo, const char *refname, const char *target)
5458 const struct got_error *err = NULL;
5459 struct got_object_id *id;
5460 struct got_reference *ref = NULL;
5463 * Don't let the user create a reference name with a leading '-'.
5464 * While technically a valid reference name, this case is usually
5465 * an unintended typo.
5467 if (refname[0] == '-')
5468 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5470 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5473 struct got_reference *target_ref;
5475 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5477 err = got_ref_open(&target_ref, repo, target, 0);
5480 err = got_ref_resolve(&id, repo, target_ref);
5481 got_ref_close(target_ref);
5486 err = got_ref_alloc(&ref, refname, id);
5490 err = got_ref_write(ref, repo);
5498 static const struct got_error *
5499 add_symref(struct got_repository *repo, const char *refname, const char *target)
5501 const struct got_error *err = NULL;
5502 struct got_reference *ref = NULL;
5503 struct got_reference *target_ref = NULL;
5506 * Don't let the user create a reference name with a leading '-'.
5507 * While technically a valid reference name, this case is usually
5508 * an unintended typo.
5510 if (refname[0] == '-')
5511 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5513 err = got_ref_open(&target_ref, repo, target, 0);
5517 err = got_ref_alloc_symref(&ref, refname, target_ref);
5521 err = got_ref_write(ref, repo);
5524 got_ref_close(target_ref);
5530 static const struct got_error *
5531 cmd_ref(int argc, char *argv[])
5533 const struct got_error *error = NULL;
5534 struct got_repository *repo = NULL;
5535 struct got_worktree *worktree = NULL;
5536 char *cwd = NULL, *repo_path = NULL;
5537 int ch, do_list = 0, do_delete = 0;
5538 const char *obj_arg = NULL, *symref_target= NULL;
5539 char *refname = NULL;
5541 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5550 repo_path = realpath(optarg, NULL);
5551 if (repo_path == NULL)
5552 return got_error_from_errno2("realpath",
5554 got_path_strip_trailing_slashes(repo_path);
5560 symref_target = optarg;
5568 if (obj_arg && do_list)
5569 option_conflict('c', 'l');
5570 if (obj_arg && do_delete)
5571 option_conflict('c', 'd');
5572 if (obj_arg && symref_target)
5573 option_conflict('c', 's');
5574 if (symref_target && do_delete)
5575 option_conflict('s', 'd');
5576 if (symref_target && do_list)
5577 option_conflict('s', 'l');
5578 if (do_delete && do_list)
5579 option_conflict('d', 'l');
5585 if (argc != 0 && argc != 1)
5588 refname = strdup(argv[0]);
5589 if (refname == NULL) {
5590 error = got_error_from_errno("strdup");
5597 refname = strdup(argv[0]);
5598 if (refname == NULL) {
5599 error = got_error_from_errno("strdup");
5605 got_path_strip_trailing_slashes(refname);
5609 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5613 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5614 "sendfd unveil", NULL) == -1)
5618 cwd = getcwd(NULL, 0);
5620 error = got_error_from_errno("getcwd");
5624 if (repo_path == NULL) {
5625 error = got_worktree_open(&worktree, cwd);
5626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5632 strdup(got_worktree_get_repo_path(worktree));
5633 if (repo_path == NULL)
5634 error = got_error_from_errno("strdup");
5638 repo_path = strdup(cwd);
5639 if (repo_path == NULL) {
5640 error = got_error_from_errno("strdup");
5646 error = got_repo_open(&repo, repo_path, NULL);
5650 error = apply_unveil(got_repo_get_path(repo), do_list,
5651 worktree ? got_worktree_get_root_path(worktree) : NULL);
5656 error = list_refs(repo, refname);
5658 error = delete_ref_by_name(repo, refname);
5659 else if (symref_target)
5660 error = add_symref(repo, refname, symref_target);
5662 if (obj_arg == NULL)
5664 error = add_ref(repo, refname, obj_arg);
5669 const struct got_error *close_err = got_repo_close(repo);
5674 got_worktree_close(worktree);
5684 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5685 "[name]\n", getprogname());
5689 static const struct got_error *
5690 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5691 struct got_reference *ref)
5693 const struct got_error *err = NULL;
5694 const char *refname, *marker = " ";
5697 refname = got_ref_get_name(ref);
5698 if (worktree && strcmp(refname,
5699 got_worktree_get_head_ref_name(worktree)) == 0) {
5700 struct got_object_id *id = NULL;
5702 err = got_ref_resolve(&id, repo, ref);
5705 if (got_object_id_cmp(id,
5706 got_worktree_get_base_commit_id(worktree)) == 0)
5713 if (strncmp(refname, "refs/heads/", 11) == 0)
5715 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5717 if (strncmp(refname, "refs/remotes/", 13) == 0)
5720 refstr = got_ref_to_str(ref);
5722 return got_error_from_errno("got_ref_to_str");
5724 printf("%s%s: %s\n", marker, refname, refstr);
5729 static const struct got_error *
5730 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5732 const char *refname;
5734 if (worktree == NULL)
5735 return got_error(GOT_ERR_NOT_WORKTREE);
5737 refname = got_worktree_get_head_ref_name(worktree);
5739 if (strncmp(refname, "refs/heads/", 11) == 0)
5741 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5744 printf("%s\n", refname);
5749 static const struct got_error *
5750 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5752 static const struct got_error *err = NULL;
5753 struct got_reflist_head refs;
5754 struct got_reflist_entry *re;
5755 struct got_reference *temp_ref = NULL;
5756 int rebase_in_progress, histedit_in_progress;
5761 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5766 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5771 if (rebase_in_progress || histedit_in_progress) {
5772 err = got_ref_open(&temp_ref, repo,
5773 got_worktree_get_head_ref_name(worktree), 0);
5776 list_branch(repo, worktree, temp_ref);
5777 got_ref_close(temp_ref);
5781 err = got_ref_list(&refs, repo, "refs/heads",
5782 got_ref_cmp_by_name, NULL);
5786 TAILQ_FOREACH(re, &refs, entry)
5787 list_branch(repo, worktree, re->ref);
5789 got_ref_list_free(&refs);
5791 err = got_ref_list(&refs, repo, "refs/remotes",
5792 got_ref_cmp_by_name, NULL);
5796 TAILQ_FOREACH(re, &refs, entry)
5797 list_branch(repo, worktree, re->ref);
5799 got_ref_list_free(&refs);
5804 static const struct got_error *
5805 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5806 const char *branch_name)
5808 const struct got_error *err = NULL;
5809 struct got_reference *ref = NULL;
5810 char *refname, *remote_refname = NULL;
5812 if (strncmp(branch_name, "refs/", 5) == 0)
5814 if (strncmp(branch_name, "heads/", 6) == 0)
5816 else if (strncmp(branch_name, "remotes/", 8) == 0)
5819 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5820 return got_error_from_errno("asprintf");
5822 if (asprintf(&remote_refname, "refs/remotes/%s",
5823 branch_name) == -1) {
5824 err = got_error_from_errno("asprintf");
5828 err = got_ref_open(&ref, repo, refname, 0);
5830 const struct got_error *err2;
5831 if (err->code != GOT_ERR_NOT_REF)
5834 * Keep 'err' intact such that if neither branch exists
5835 * we report "refs/heads" rather than "refs/remotes" in
5836 * our error message.
5838 err2 = got_ref_open(&ref, repo, remote_refname, 0);
5845 strcmp(got_worktree_get_head_ref_name(worktree),
5846 got_ref_get_name(ref)) == 0) {
5847 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5848 "will not delete this work tree's current branch");
5852 err = delete_ref(repo, ref);
5857 free(remote_refname);
5861 static const struct got_error *
5862 add_branch(struct got_repository *repo, const char *branch_name,
5863 struct got_object_id *base_commit_id)
5865 const struct got_error *err = NULL;
5866 struct got_reference *ref = NULL;
5867 char *base_refname = NULL, *refname = NULL;
5870 * Don't let the user create a branch name with a leading '-'.
5871 * While technically a valid reference name, this case is usually
5872 * an unintended typo.
5874 if (branch_name[0] == '-')
5875 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5877 if (strncmp(branch_name, "refs/heads/", 11) == 0)
5880 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5881 err = got_error_from_errno("asprintf");
5885 err = got_ref_open(&ref, repo, refname, 0);
5887 err = got_error(GOT_ERR_BRANCH_EXISTS);
5889 } else if (err->code != GOT_ERR_NOT_REF)
5892 err = got_ref_alloc(&ref, refname, base_commit_id);
5896 err = got_ref_write(ref, repo);
5905 static const struct got_error *
5906 cmd_branch(int argc, char *argv[])
5908 const struct got_error *error = NULL;
5909 struct got_repository *repo = NULL;
5910 struct got_worktree *worktree = NULL;
5911 char *cwd = NULL, *repo_path = NULL;
5912 int ch, do_list = 0, do_show = 0, do_update = 1;
5913 const char *delref = NULL, *commit_id_arg = NULL;
5914 struct got_reference *ref = NULL;
5915 struct got_pathlist_head paths;
5916 struct got_pathlist_entry *pe;
5917 struct got_object_id *commit_id = NULL;
5918 char *commit_id_str = NULL;
5922 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5925 commit_id_arg = optarg;
5931 repo_path = realpath(optarg, NULL);
5932 if (repo_path == NULL)
5933 return got_error_from_errno2("realpath",
5935 got_path_strip_trailing_slashes(repo_path);
5949 if (do_list && delref)
5950 option_conflict('l', 'd');
5955 if (!do_list && !delref && argc == 0)
5958 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5959 errx(1, "-c option can only be used when creating a branch");
5961 if (do_list || delref) {
5964 } else if (!do_show && argc != 1)
5968 if (do_list || do_show) {
5969 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5973 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5974 "sendfd unveil", NULL) == -1)
5978 cwd = getcwd(NULL, 0);
5980 error = got_error_from_errno("getcwd");
5984 if (repo_path == NULL) {
5985 error = got_worktree_open(&worktree, cwd);
5986 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5992 strdup(got_worktree_get_repo_path(worktree));
5993 if (repo_path == NULL)
5994 error = got_error_from_errno("strdup");
5998 repo_path = strdup(cwd);
5999 if (repo_path == NULL) {
6000 error = got_error_from_errno("strdup");
6006 error = got_repo_open(&repo, repo_path, NULL);
6010 error = apply_unveil(got_repo_get_path(repo), do_list,
6011 worktree ? got_worktree_get_root_path(worktree) : NULL);
6016 error = show_current_branch(repo, worktree);
6018 error = list_branches(repo, worktree);
6020 error = delete_branch(repo, worktree, delref);
6022 struct got_reflist_head refs;
6024 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6028 if (commit_id_arg == NULL)
6029 commit_id_arg = worktree ?
6030 got_worktree_get_head_ref_name(worktree) :
6032 error = got_repo_match_object_id(&commit_id, NULL,
6033 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6034 got_ref_list_free(&refs);
6037 error = add_branch(repo, argv[0], commit_id);
6040 if (worktree && do_update) {
6041 struct got_update_progress_arg upa;
6042 char *branch_refname = NULL;
6044 error = got_object_id_str(&commit_id_str, commit_id);
6047 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6051 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6053 error = got_error_from_errno("asprintf");
6056 error = got_ref_open(&ref, repo, branch_refname, 0);
6057 free(branch_refname);
6060 error = switch_head_ref(ref, commit_id, worktree,
6064 error = got_worktree_set_base_commit_id(worktree, repo,
6068 memset(&upa, 0, sizeof(upa));
6069 error = got_worktree_checkout_files(worktree, &paths,
6070 repo, update_progress, &upa, check_cancelled,
6074 if (upa.did_something)
6075 printf("Updated to commit %s\n", commit_id_str);
6076 print_update_progress_stats(&upa);
6083 const struct got_error *close_err = got_repo_close(repo);
6088 got_worktree_close(worktree);
6092 free(commit_id_str);
6093 TAILQ_FOREACH(pe, &paths, entry)
6094 free((char *)pe->path);
6095 got_pathlist_free(&paths);
6104 "usage: %s tag [-c commit] [-r repository] [-l] "
6105 "[-m message] name\n", getprogname());
6110 static const struct got_error *
6111 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6113 const struct got_error *err = NULL;
6114 struct got_reflist_entry *re, *se, *new;
6115 struct got_object_id *re_id, *se_id;
6116 struct got_tag_object *re_tag, *se_tag;
6117 time_t re_time, se_time;
6119 STAILQ_FOREACH(re, tags, entry) {
6120 se = STAILQ_FIRST(sorted);
6122 err = got_reflist_entry_dup(&new, re);
6125 STAILQ_INSERT_HEAD(sorted, new, entry);
6128 err = got_ref_resolve(&re_id, repo, re->ref);
6131 err = got_object_open_as_tag(&re_tag, repo, re_id);
6135 re_time = got_object_tag_get_tagger_time(re_tag);
6136 got_object_tag_close(re_tag);
6140 err = got_ref_resolve(&se_id, repo, re->ref);
6143 err = got_object_open_as_tag(&se_tag, repo, se_id);
6147 se_time = got_object_tag_get_tagger_time(se_tag);
6148 got_object_tag_close(se_tag);
6150 if (se_time > re_time) {
6151 err = got_reflist_entry_dup(&new, re);
6154 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6157 se = STAILQ_NEXT(se, entry);
6166 static const struct got_error *
6167 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6169 static const struct got_error *err = NULL;
6170 struct got_reflist_head refs;
6171 struct got_reflist_entry *re;
6175 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6179 TAILQ_FOREACH(re, &refs, entry) {
6180 const char *refname;
6181 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6185 struct got_object_id *id;
6186 struct got_tag_object *tag;
6187 struct got_commit_object *commit = NULL;
6189 refname = got_ref_get_name(re->ref);
6190 if (strncmp(refname, "refs/tags/", 10) != 0)
6193 refstr = got_ref_to_str(re->ref);
6194 if (refstr == NULL) {
6195 err = got_error_from_errno("got_ref_to_str");
6198 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6201 err = got_ref_resolve(&id, repo, re->ref);
6204 err = got_object_open_as_tag(&tag, repo, id);
6206 if (err->code != GOT_ERR_OBJ_TYPE) {
6210 /* "lightweight" tag */
6211 err = got_object_open_as_commit(&commit, repo, id);
6216 tagger = got_object_commit_get_committer(commit);
6218 got_object_commit_get_committer_time(commit);
6219 err = got_object_id_str(&id_str, id);
6225 tagger = got_object_tag_get_tagger(tag);
6226 tagger_time = got_object_tag_get_tagger_time(tag);
6227 err = got_object_id_str(&id_str,
6228 got_object_tag_get_object_id(tag));
6232 printf("from: %s\n", tagger);
6233 datestr = get_datestr(&tagger_time, datebuf);
6235 printf("date: %s UTC\n", datestr);
6237 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6239 switch (got_object_tag_get_object_type(tag)) {
6240 case GOT_OBJ_TYPE_BLOB:
6241 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6244 case GOT_OBJ_TYPE_TREE:
6245 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6248 case GOT_OBJ_TYPE_COMMIT:
6249 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6252 case GOT_OBJ_TYPE_TAG:
6253 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6262 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6265 got_object_commit_close(commit);
6267 tagmsg0 = strdup(got_object_tag_get_message(tag));
6268 got_object_tag_close(tag);
6269 if (tagmsg0 == NULL) {
6270 err = got_error_from_errno("strdup");
6277 line = strsep(&tagmsg, "\n");
6279 printf(" %s\n", line);
6284 got_ref_list_free(&refs);
6288 static const struct got_error *
6289 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6290 const char *tag_name, const char *repo_path)
6292 const struct got_error *err = NULL;
6293 char *template = NULL, *initial_content = NULL;
6294 char *editor = NULL;
6295 int initial_content_len;
6298 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6299 err = got_error_from_errno("asprintf");
6303 initial_content_len = asprintf(&initial_content,
6304 "\n# tagging commit %s as %s\n",
6305 commit_id_str, tag_name);
6306 if (initial_content_len == -1) {
6307 err = got_error_from_errno("asprintf");
6311 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6315 if (write(fd, initial_content, initial_content_len) == -1) {
6316 err = got_error_from_errno2("write", *tagmsg_path);
6320 err = get_editor(&editor);
6323 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6324 initial_content_len, 1);
6326 free(initial_content);
6330 if (fd != -1 && close(fd) == -1 && err == NULL)
6331 err = got_error_from_errno2("close", *tagmsg_path);
6333 /* Editor is done; we can now apply unveil(2) */
6335 err = apply_unveil(repo_path, 0, NULL);
6343 static const struct got_error *
6344 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6345 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6347 const struct got_error *err = NULL;
6348 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6349 char *label = NULL, *commit_id_str = NULL;
6350 struct got_reference *ref = NULL;
6351 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6352 char *tagmsg_path = NULL, *tag_id_str = NULL;
6353 int preserve_tagmsg = 0;
6354 struct got_reflist_head refs;
6359 * Don't let the user create a tag name with a leading '-'.
6360 * While technically a valid reference name, this case is usually
6361 * an unintended typo.
6363 if (tag_name[0] == '-')
6364 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6366 err = get_author(&tagger, repo, worktree);
6370 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6374 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6375 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6379 err = got_object_id_str(&commit_id_str, commit_id);
6383 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6384 refname = strdup(tag_name);
6385 if (refname == NULL) {
6386 err = got_error_from_errno("strdup");
6390 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6391 err = got_error_from_errno("asprintf");
6395 err = got_ref_open(&ref, repo, refname, 0);
6397 err = got_error(GOT_ERR_TAG_EXISTS);
6399 } else if (err->code != GOT_ERR_NOT_REF)
6402 if (tagmsg_arg == NULL) {
6403 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6404 tag_name, got_repo_get_path(repo));
6406 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6407 tagmsg_path != NULL)
6408 preserve_tagmsg = 1;
6413 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6414 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6417 preserve_tagmsg = 1;
6421 err = got_ref_alloc(&ref, refname, tag_id);
6424 preserve_tagmsg = 1;
6428 err = got_ref_write(ref, repo);
6431 preserve_tagmsg = 1;
6435 err = got_object_id_str(&tag_id_str, tag_id);
6438 preserve_tagmsg = 1;
6441 printf("Created tag %s\n", tag_id_str);
6443 if (preserve_tagmsg) {
6444 fprintf(stderr, "%s: tag message preserved in %s\n",
6445 getprogname(), tagmsg_path);
6446 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6447 err = got_error_from_errno2("unlink", tagmsg_path);
6452 free(commit_id_str);
6457 got_ref_list_free(&refs);
6461 static const struct got_error *
6462 cmd_tag(int argc, char *argv[])
6464 const struct got_error *error = NULL;
6465 struct got_repository *repo = NULL;
6466 struct got_worktree *worktree = NULL;
6467 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6468 char *gitconfig_path = NULL;
6469 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6470 int ch, do_list = 0;
6472 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6475 commit_id_arg = optarg;
6481 repo_path = realpath(optarg, NULL);
6482 if (repo_path == NULL)
6483 return got_error_from_errno2("realpath",
6485 got_path_strip_trailing_slashes(repo_path);
6500 if (commit_id_arg != NULL)
6502 "-c option can only be used when creating a tag");
6504 option_conflict('l', 'm');
6507 } else if (argc != 1)
6514 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6518 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6519 "sendfd unveil", NULL) == -1)
6523 cwd = getcwd(NULL, 0);
6525 error = got_error_from_errno("getcwd");
6529 if (repo_path == NULL) {
6530 error = got_worktree_open(&worktree, cwd);
6531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6537 strdup(got_worktree_get_repo_path(worktree));
6538 if (repo_path == NULL)
6539 error = got_error_from_errno("strdup");
6543 repo_path = strdup(cwd);
6544 if (repo_path == NULL) {
6545 error = got_error_from_errno("strdup");
6552 error = got_repo_open(&repo, repo_path, NULL);
6555 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6558 error = list_tags(repo, worktree);
6560 error = get_gitconfig_path(&gitconfig_path);
6563 error = got_repo_open(&repo, repo_path, gitconfig_path);
6568 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6573 if (commit_id_arg == NULL) {
6574 struct got_reference *head_ref;
6575 struct got_object_id *commit_id;
6576 error = got_ref_open(&head_ref, repo,
6577 worktree ? got_worktree_get_head_ref_name(worktree)
6581 error = got_ref_resolve(&commit_id, repo, head_ref);
6582 got_ref_close(head_ref);
6585 error = got_object_id_str(&commit_id_str, commit_id);
6591 error = add_tag(repo, worktree, tag_name,
6592 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6596 const struct got_error *close_err = got_repo_close(repo);
6601 got_worktree_close(worktree);
6604 free(gitconfig_path);
6605 free(commit_id_str);
6612 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6617 static const struct got_error *
6618 add_progress(void *arg, unsigned char status, const char *path)
6620 while (path[0] == '/')
6622 printf("%c %s\n", status, path);
6626 static const struct got_error *
6627 cmd_add(int argc, char *argv[])
6629 const struct got_error *error = NULL;
6630 struct got_repository *repo = NULL;
6631 struct got_worktree *worktree = NULL;
6633 struct got_pathlist_head paths;
6634 struct got_pathlist_entry *pe;
6635 int ch, can_recurse = 0, no_ignores = 0;
6639 while ((ch = getopt(argc, argv, "IR")) != -1) {
6657 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6664 cwd = getcwd(NULL, 0);
6666 error = got_error_from_errno("getcwd");
6670 error = got_worktree_open(&worktree, cwd);
6672 if (error->code == GOT_ERR_NOT_WORKTREE)
6673 error = wrap_not_worktree_error(error, "add", cwd);
6677 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6682 error = apply_unveil(got_repo_get_path(repo), 1,
6683 got_worktree_get_root_path(worktree));
6687 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6694 TAILQ_FOREACH(pe, &paths, entry) {
6695 if (asprintf(&ondisk_path, "%s/%s",
6696 got_worktree_get_root_path(worktree),
6698 error = got_error_from_errno("asprintf");
6701 if (lstat(ondisk_path, &sb) == -1) {
6702 if (errno == ENOENT) {
6706 error = got_error_from_errno2("lstat",
6712 if (S_ISDIR(sb.st_mode)) {
6713 error = got_error_msg(GOT_ERR_BAD_PATH,
6714 "adding directories requires -R option");
6720 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6721 NULL, repo, no_ignores);
6724 const struct got_error *close_err = got_repo_close(repo);
6729 got_worktree_close(worktree);
6730 TAILQ_FOREACH(pe, &paths, entry)
6731 free((char *)pe->path);
6732 got_pathlist_free(&paths);
6740 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6741 "path ...\n", getprogname());
6745 static const struct got_error *
6746 print_remove_status(void *arg, unsigned char status,
6747 unsigned char staged_status, const char *path)
6749 while (path[0] == '/')
6751 if (status == GOT_STATUS_NONEXISTENT)
6753 if (status == staged_status && (status == GOT_STATUS_DELETE))
6754 status = GOT_STATUS_NO_CHANGE;
6755 printf("%c%c %s\n", status, staged_status, path);
6759 static const struct got_error *
6760 cmd_remove(int argc, char *argv[])
6762 const struct got_error *error = NULL;
6763 struct got_worktree *worktree = NULL;
6764 struct got_repository *repo = NULL;
6765 const char *status_codes = NULL;
6767 struct got_pathlist_head paths;
6768 struct got_pathlist_entry *pe;
6769 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6773 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6776 delete_local_mods = 1;
6785 for (i = 0; i < strlen(optarg); i++) {
6786 switch (optarg[i]) {
6787 case GOT_STATUS_MODIFY:
6788 delete_local_mods = 1;
6790 case GOT_STATUS_MISSING:
6793 errx(1, "invalid status code '%c'",
6797 status_codes = optarg;
6809 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6816 cwd = getcwd(NULL, 0);
6818 error = got_error_from_errno("getcwd");
6821 error = got_worktree_open(&worktree, cwd);
6823 if (error->code == GOT_ERR_NOT_WORKTREE)
6824 error = wrap_not_worktree_error(error, "remove", cwd);
6828 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6833 error = apply_unveil(got_repo_get_path(repo), 1,
6834 got_worktree_get_root_path(worktree));
6838 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6845 TAILQ_FOREACH(pe, &paths, entry) {
6846 if (asprintf(&ondisk_path, "%s/%s",
6847 got_worktree_get_root_path(worktree),
6849 error = got_error_from_errno("asprintf");
6852 if (lstat(ondisk_path, &sb) == -1) {
6853 if (errno == ENOENT) {
6857 error = got_error_from_errno2("lstat",
6863 if (S_ISDIR(sb.st_mode)) {
6864 error = got_error_msg(GOT_ERR_BAD_PATH,
6865 "removing directories requires -R option");
6871 error = got_worktree_schedule_delete(worktree, &paths,
6872 delete_local_mods, status_codes, print_remove_status, NULL,
6873 repo, keep_on_disk);
6876 const struct got_error *close_err = got_repo_close(repo);
6881 got_worktree_close(worktree);
6882 TAILQ_FOREACH(pe, &paths, entry)
6883 free((char *)pe->path);
6884 got_pathlist_free(&paths);
6892 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6893 "path ...\n", getprogname());
6897 static const struct got_error *
6898 revert_progress(void *arg, unsigned char status, const char *path)
6900 if (status == GOT_STATUS_UNVERSIONED)
6903 while (path[0] == '/')
6905 printf("%c %s\n", status, path);
6909 struct choose_patch_arg {
6910 FILE *patch_script_file;
6914 static const struct got_error *
6915 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6916 int nchanges, const char *action)
6919 size_t linesize = 0;
6923 case GOT_STATUS_ADD:
6924 printf("A %s\n%s this addition? [y/n] ", path, action);
6926 case GOT_STATUS_DELETE:
6927 printf("D %s\n%s this deletion? [y/n] ", path, action);
6929 case GOT_STATUS_MODIFY:
6930 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6931 return got_error_from_errno("fseek");
6932 printf(GOT_COMMIT_SEP_STR);
6933 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6935 if (ferror(patch_file))
6936 return got_error_from_errno("getline");
6937 printf(GOT_COMMIT_SEP_STR);
6938 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6939 path, n, nchanges, action);
6942 return got_error_path(path, GOT_ERR_FILE_STATUS);
6948 static const struct got_error *
6949 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6950 FILE *patch_file, int n, int nchanges)
6952 const struct got_error *err = NULL;
6954 size_t linesize = 0;
6957 struct choose_patch_arg *a = arg;
6959 *choice = GOT_PATCH_CHOICE_NONE;
6961 if (a->patch_script_file) {
6963 err = show_change(status, path, patch_file, n, nchanges,
6967 linelen = getline(&line, &linesize, a->patch_script_file);
6968 if (linelen == -1) {
6969 if (ferror(a->patch_script_file))
6970 return got_error_from_errno("getline");
6973 nl = strchr(line, '\n');
6976 if (strcmp(line, "y") == 0) {
6977 *choice = GOT_PATCH_CHOICE_YES;
6979 } else if (strcmp(line, "n") == 0) {
6980 *choice = GOT_PATCH_CHOICE_NO;
6982 } else if (strcmp(line, "q") == 0 &&
6983 status == GOT_STATUS_MODIFY) {
6984 *choice = GOT_PATCH_CHOICE_QUIT;
6987 printf("invalid response '%s'\n", line);
6992 while (resp != 'y' && resp != 'n' && resp != 'q') {
6993 err = show_change(status, path, patch_file, n, nchanges,
7000 if (status == GOT_STATUS_MODIFY) {
7001 if (resp != 'y' && resp != 'n' && resp != 'q') {
7002 printf("invalid response '%c'\n", resp);
7005 } else if (resp != 'y' && resp != 'n') {
7006 printf("invalid response '%c'\n", resp);
7012 *choice = GOT_PATCH_CHOICE_YES;
7013 else if (resp == 'n')
7014 *choice = GOT_PATCH_CHOICE_NO;
7015 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7016 *choice = GOT_PATCH_CHOICE_QUIT;
7022 static const struct got_error *
7023 cmd_revert(int argc, char *argv[])
7025 const struct got_error *error = NULL;
7026 struct got_worktree *worktree = NULL;
7027 struct got_repository *repo = NULL;
7028 char *cwd = NULL, *path = NULL;
7029 struct got_pathlist_head paths;
7030 struct got_pathlist_entry *pe;
7031 int ch, can_recurse = 0, pflag = 0;
7032 FILE *patch_script_file = NULL;
7033 const char *patch_script_path = NULL;
7034 struct choose_patch_arg cpa;
7038 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7044 patch_script_path = optarg;
7059 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7060 "unveil", NULL) == -1)
7065 if (patch_script_path && !pflag)
7066 errx(1, "-F option can only be used together with -p option");
7068 cwd = getcwd(NULL, 0);
7070 error = got_error_from_errno("getcwd");
7073 error = got_worktree_open(&worktree, cwd);
7075 if (error->code == GOT_ERR_NOT_WORKTREE)
7076 error = wrap_not_worktree_error(error, "revert", cwd);
7080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7085 if (patch_script_path) {
7086 patch_script_file = fopen(patch_script_path, "r");
7087 if (patch_script_file == NULL) {
7088 error = got_error_from_errno2("fopen",
7093 error = apply_unveil(got_repo_get_path(repo), 1,
7094 got_worktree_get_root_path(worktree));
7098 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7105 TAILQ_FOREACH(pe, &paths, entry) {
7106 if (asprintf(&ondisk_path, "%s/%s",
7107 got_worktree_get_root_path(worktree),
7109 error = got_error_from_errno("asprintf");
7112 if (lstat(ondisk_path, &sb) == -1) {
7113 if (errno == ENOENT) {
7117 error = got_error_from_errno2("lstat",
7123 if (S_ISDIR(sb.st_mode)) {
7124 error = got_error_msg(GOT_ERR_BAD_PATH,
7125 "reverting directories requires -R option");
7131 cpa.patch_script_file = patch_script_file;
7132 cpa.action = "revert";
7133 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7134 pflag ? choose_patch : NULL, &cpa, repo);
7136 if (patch_script_file && fclose(patch_script_file) == EOF &&
7138 error = got_error_from_errno2("fclose", patch_script_path);
7140 const struct got_error *close_err = got_repo_close(repo);
7145 got_worktree_close(worktree);
7154 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7155 "[path ...]\n", getprogname());
7159 struct collect_commit_logmsg_arg {
7160 const char *cmdline_log;
7161 const char *prepared_log;
7162 int non_interactive;
7164 const char *worktree_path;
7165 const char *branch_name;
7166 const char *repo_path;
7171 static const struct got_error *
7172 read_prepared_logmsg(char **logmsg, const char *path)
7174 const struct got_error *err = NULL;
7180 memset(&sb, 0, sizeof(sb));
7182 f = fopen(path, "r");
7184 return got_error_from_errno2("fopen", path);
7186 if (fstat(fileno(f), &sb) == -1) {
7187 err = got_error_from_errno2("fstat", path);
7190 if (sb.st_size == 0) {
7191 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7195 *logmsg = malloc(sb.st_size + 1);
7196 if (*logmsg == NULL) {
7197 err = got_error_from_errno("malloc");
7201 r = fread(*logmsg, 1, sb.st_size, f);
7202 if (r != sb.st_size) {
7204 err = got_error_from_errno2("fread", path);
7206 err = got_error(GOT_ERR_IO);
7209 (*logmsg)[sb.st_size] = '\0';
7211 if (fclose(f) == EOF && err == NULL)
7212 err = got_error_from_errno2("fclose", path);
7221 static const struct got_error *
7222 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7225 char *initial_content = NULL;
7226 struct got_pathlist_entry *pe;
7227 const struct got_error *err = NULL;
7228 char *template = NULL;
7229 struct collect_commit_logmsg_arg *a = arg;
7230 int initial_content_len;
7234 /* if a message was specified on the command line, just use it */
7235 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7236 len = strlen(a->cmdline_log) + 1;
7237 *logmsg = malloc(len + 1);
7238 if (*logmsg == NULL)
7239 return got_error_from_errno("malloc");
7240 strlcpy(*logmsg, a->cmdline_log, len);
7242 } else if (a->prepared_log != NULL && a->non_interactive)
7243 return read_prepared_logmsg(logmsg, a->prepared_log);
7245 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7246 return got_error_from_errno("asprintf");
7248 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7252 if (a->prepared_log) {
7254 err = read_prepared_logmsg(&msg, a->prepared_log);
7257 if (write(fd, msg, strlen(msg)) == -1) {
7258 err = got_error_from_errno2("write", a->logmsg_path);
7265 initial_content_len = asprintf(&initial_content,
7266 "\n# changes to be committed on branch %s:\n",
7268 if (initial_content_len == -1) {
7269 err = got_error_from_errno("asprintf");
7273 if (write(fd, initial_content, initial_content_len) == -1) {
7274 err = got_error_from_errno2("write", a->logmsg_path);
7278 TAILQ_FOREACH(pe, commitable_paths, entry) {
7279 struct got_commitable *ct = pe->data;
7280 dprintf(fd, "# %c %s\n",
7281 got_commitable_get_status(ct),
7282 got_commitable_get_path(ct));
7285 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7286 initial_content_len, a->prepared_log ? 0 : 1);
7288 free(initial_content);
7291 if (fd != -1 && close(fd) == -1 && err == NULL)
7292 err = got_error_from_errno2("close", a->logmsg_path);
7294 /* Editor is done; we can now apply unveil(2) */
7296 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7304 static const struct got_error *
7305 cmd_commit(int argc, char *argv[])
7307 const struct got_error *error = NULL;
7308 struct got_worktree *worktree = NULL;
7309 struct got_repository *repo = NULL;
7310 char *cwd = NULL, *id_str = NULL;
7311 struct got_object_id *id = NULL;
7312 const char *logmsg = NULL;
7313 char *prepared_logmsg = NULL;
7314 struct collect_commit_logmsg_arg cl_arg;
7315 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7316 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7317 int allow_bad_symlinks = 0, non_interactive = 0;
7318 struct got_pathlist_head paths;
7321 cl_arg.logmsg_path = NULL;
7323 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7327 option_conflict('F', 'm');
7328 prepared_logmsg = realpath(optarg, NULL);
7329 if (prepared_logmsg == NULL)
7330 return got_error_from_errno2("realpath",
7334 if (prepared_logmsg)
7335 option_conflict('m', 'F');
7339 non_interactive = 1;
7342 allow_bad_symlinks = 1;
7354 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7355 "unveil", NULL) == -1)
7358 cwd = getcwd(NULL, 0);
7360 error = got_error_from_errno("getcwd");
7363 error = got_worktree_open(&worktree, cwd);
7365 if (error->code == GOT_ERR_NOT_WORKTREE)
7366 error = wrap_not_worktree_error(error, "commit", cwd);
7370 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7373 if (rebase_in_progress) {
7374 error = got_error(GOT_ERR_REBASING);
7378 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7383 error = get_gitconfig_path(&gitconfig_path);
7386 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7391 error = get_author(&author, repo, worktree);
7396 * unveil(2) traverses exec(2); if an editor is used we have
7397 * to apply unveil after the log message has been written.
7399 if (logmsg == NULL || strlen(logmsg) == 0)
7400 error = get_editor(&editor);
7402 error = apply_unveil(got_repo_get_path(repo), 0,
7403 got_worktree_get_root_path(worktree));
7407 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7411 cl_arg.editor = editor;
7412 cl_arg.cmdline_log = logmsg;
7413 cl_arg.prepared_log = prepared_logmsg;
7414 cl_arg.non_interactive = non_interactive;
7415 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7416 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7417 if (!histedit_in_progress) {
7418 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7419 error = got_error(GOT_ERR_COMMIT_BRANCH);
7422 cl_arg.branch_name += 11;
7424 cl_arg.repo_path = got_repo_get_path(repo);
7425 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7426 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7427 print_status, NULL, repo);
7429 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7430 cl_arg.logmsg_path != NULL)
7431 preserve_logmsg = 1;
7435 error = got_object_id_str(&id_str, id);
7438 printf("Created commit %s\n", id_str);
7440 if (preserve_logmsg) {
7441 fprintf(stderr, "%s: log message preserved in %s\n",
7442 getprogname(), cl_arg.logmsg_path);
7443 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7445 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7446 free(cl_arg.logmsg_path);
7448 const struct got_error *close_err = got_repo_close(repo);
7453 got_worktree_close(worktree);
7456 free(gitconfig_path);
7459 free(prepared_logmsg);
7466 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7467 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7468 "[remote-repository]\n", getprogname());
7472 struct got_send_progress_arg {
7473 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7476 int last_nobj_total;
7480 int printed_something;
7482 struct got_pathlist_head *delete_branches;
7485 static const struct got_error *
7486 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7487 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7490 struct got_send_progress_arg *a = arg;
7491 char scaled_packsize[FMT_SCALED_STRSIZE];
7492 char scaled_sent[FMT_SCALED_STRSIZE];
7493 int p_deltify = 0, p_written = 0, p_sent = 0;
7494 int print_searching = 0, print_total = 0;
7495 int print_deltify = 0, print_written = 0, print_sent = 0;
7497 if (a->verbosity < 0)
7501 const char *status = success ? "accepted" : "rejected";
7504 struct got_pathlist_entry *pe;
7505 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7506 const char *branchname = pe->path;
7507 if (got_path_cmp(branchname, refname,
7508 strlen(branchname), strlen(refname)) == 0) {
7510 a->sent_something = 1;
7516 if (a->printed_something)
7518 printf("Server has %s %s", status, refname);
7519 a->printed_something = 1;
7523 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7524 return got_error_from_errno("fmt_scaled");
7525 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7526 return got_error_from_errno("fmt_scaled");
7528 if (a->last_ncommits != ncommits) {
7529 print_searching = 1;
7530 a->last_ncommits = ncommits;
7533 if (a->last_nobj_total != nobj_total) {
7534 print_searching = 1;
7536 a->last_nobj_total = nobj_total;
7539 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7540 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7541 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7542 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7543 return got_error(GOT_ERR_NO_SPACE);
7546 if (nobj_deltify > 0 || nobj_written > 0) {
7547 if (nobj_deltify > 0) {
7548 p_deltify = (nobj_deltify * 100) / nobj_total;
7549 if (p_deltify != a->last_p_deltify) {
7550 a->last_p_deltify = p_deltify;
7551 print_searching = 1;
7556 if (nobj_written > 0) {
7557 p_written = (nobj_written * 100) / nobj_total;
7558 if (p_written != a->last_p_written) {
7559 a->last_p_written = p_written;
7560 print_searching = 1;
7568 if (bytes_sent > 0) {
7569 p_sent = (bytes_sent * 100) / packfile_size;
7570 if (p_sent != a->last_p_sent) {
7571 a->last_p_sent = p_sent;
7572 print_searching = 1;
7578 a->sent_something = 1;
7581 if (print_searching || print_total || print_deltify || print_written ||
7584 if (print_searching)
7585 printf("packing %d reference%s", ncommits,
7586 ncommits == 1 ? "" : "s");
7588 printf("; %d object%s", nobj_total,
7589 nobj_total == 1 ? "" : "s");
7591 printf("; deltify: %d%%", p_deltify);
7593 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7594 scaled_packsize, p_sent);
7595 else if (print_written)
7596 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7597 scaled_packsize, p_written);
7598 if (print_searching || print_total || print_deltify ||
7599 print_written || print_sent) {
7600 a->printed_something = 1;
7606 static const struct got_error *
7607 cmd_send(int argc, char *argv[])
7609 const struct got_error *error = NULL;
7610 char *cwd = NULL, *repo_path = NULL;
7611 const char *remote_name;
7612 char *proto = NULL, *host = NULL, *port = NULL;
7613 char *repo_name = NULL, *server_path = NULL;
7614 const struct got_remote_repo *remotes, *remote = NULL;
7615 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7616 struct got_repository *repo = NULL;
7617 struct got_worktree *worktree = NULL;
7618 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7619 struct got_pathlist_head branches;
7620 struct got_pathlist_head tags;
7621 struct got_reflist_head all_branches;
7622 struct got_reflist_head all_tags;
7623 struct got_pathlist_head delete_args;
7624 struct got_pathlist_head delete_branches;
7625 struct got_reflist_entry *re;
7626 struct got_pathlist_entry *pe;
7627 int i, ch, sendfd = -1, sendstatus;
7629 struct got_send_progress_arg spa;
7630 int verbosity = 0, overwrite_refs = 0;
7631 int send_all_branches = 0, send_all_tags = 0;
7632 struct got_reference *ref = NULL;
7634 TAILQ_INIT(&branches);
7636 TAILQ_INIT(&all_branches);
7637 TAILQ_INIT(&all_tags);
7638 TAILQ_INIT(&delete_args);
7639 TAILQ_INIT(&delete_branches);
7641 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7644 send_all_branches = 1;
7647 error = got_pathlist_append(&branches, optarg, NULL);
7653 error = got_pathlist_append(&delete_args, optarg, NULL);
7661 repo_path = realpath(optarg, NULL);
7662 if (repo_path == NULL)
7663 return got_error_from_errno2("realpath",
7665 got_path_strip_trailing_slashes(repo_path);
7668 error = got_pathlist_append(&tags, optarg, NULL);
7679 else if (verbosity < 3)
7693 if (send_all_branches && !TAILQ_EMPTY(&branches))
7694 option_conflict('a', 'b');
7695 if (send_all_tags && !TAILQ_EMPTY(&tags))
7696 option_conflict('T', 't');
7700 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7702 remote_name = argv[0];
7706 cwd = getcwd(NULL, 0);
7708 error = got_error_from_errno("getcwd");
7712 if (repo_path == NULL) {
7713 error = got_worktree_open(&worktree, cwd);
7714 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7720 strdup(got_worktree_get_repo_path(worktree));
7721 if (repo_path == NULL)
7722 error = got_error_from_errno("strdup");
7726 repo_path = strdup(cwd);
7727 if (repo_path == NULL) {
7728 error = got_error_from_errno("strdup");
7734 error = got_repo_open(&repo, repo_path, NULL);
7739 worktree_conf = got_worktree_get_gotconfig(worktree);
7740 if (worktree_conf) {
7741 got_gotconfig_get_remotes(&nremotes, &remotes,
7743 for (i = 0; i < nremotes; i++) {
7744 if (strcmp(remotes[i].name, remote_name) == 0) {
7745 remote = &remotes[i];
7751 if (remote == NULL) {
7752 repo_conf = got_repo_get_gotconfig(repo);
7754 got_gotconfig_get_remotes(&nremotes, &remotes,
7756 for (i = 0; i < nremotes; i++) {
7757 if (strcmp(remotes[i].name, remote_name) == 0) {
7758 remote = &remotes[i];
7764 if (remote == NULL) {
7765 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7766 for (i = 0; i < nremotes; i++) {
7767 if (strcmp(remotes[i].name, remote_name) == 0) {
7768 remote = &remotes[i];
7773 if (remote == NULL) {
7774 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7778 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7779 &repo_name, remote->send_url);
7783 if (strcmp(proto, "git") == 0) {
7785 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7786 "sendfd dns inet unveil", NULL) == -1)
7789 } else if (strcmp(proto, "git+ssh") == 0 ||
7790 strcmp(proto, "ssh") == 0) {
7792 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7793 "sendfd unveil", NULL) == -1)
7796 } else if (strcmp(proto, "http") == 0 ||
7797 strcmp(proto, "git+http") == 0) {
7798 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7801 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7805 error = got_dial_apply_unveil(proto);
7809 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7813 if (send_all_branches) {
7814 error = got_ref_list(&all_branches, repo, "refs/heads",
7815 got_ref_cmp_by_name, NULL);
7818 TAILQ_FOREACH(re, &all_branches, entry) {
7819 const char *branchname = got_ref_get_name(re->ref);
7820 error = got_pathlist_append(&branches,
7826 } else if (nbranches == 0) {
7827 for (i = 0; i < remote->nsend_branches; i++) {
7828 got_pathlist_append(&branches,
7829 remote->send_branches[i], NULL);
7833 if (send_all_tags) {
7834 error = got_ref_list(&all_tags, repo, "refs/tags",
7835 got_ref_cmp_by_name, NULL);
7838 TAILQ_FOREACH(re, &all_tags, entry) {
7839 const char *tagname = got_ref_get_name(re->ref);
7840 error = got_pathlist_append(&tags,
7849 * To prevent accidents only branches in refs/heads/ can be deleted
7850 * with 'got send -d'.
7851 * Deleting anything else requires local repository access or Git.
7853 TAILQ_FOREACH(pe, &delete_args, entry) {
7854 const char *branchname = pe->path;
7856 struct got_pathlist_entry *new;
7857 if (strncmp(branchname, "refs/heads/", 11) == 0) {
7858 s = strdup(branchname);
7860 error = got_error_from_errno("strdup");
7864 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7865 error = got_error_from_errno("asprintf");
7869 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7870 if (error || new == NULL /* duplicate */)
7877 if (nbranches == 0 && ndelete_branches == 0) {
7878 struct got_reference *head_ref;
7880 error = got_ref_open(&head_ref, repo,
7881 got_worktree_get_head_ref_name(worktree), 0);
7883 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7886 if (got_ref_is_symbolic(head_ref)) {
7887 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7888 got_ref_close(head_ref);
7893 error = got_pathlist_append(&branches, got_ref_get_name(ref),
7901 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7902 port ? ":" : "", port ? port : "");
7904 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7905 server_path, verbosity);
7909 memset(&spa, 0, sizeof(spa));
7910 spa.last_scaled_packsize[0] = '\0';
7911 spa.last_p_deltify = -1;
7912 spa.last_p_written = -1;
7913 spa.verbosity = verbosity;
7914 spa.delete_branches = &delete_branches;
7915 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7916 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7917 check_cancelled, NULL);
7918 if (spa.printed_something)
7922 if (!spa.sent_something && verbosity >= 0)
7923 printf("Already up-to-date\n");
7926 if (kill(sendpid, SIGTERM) == -1)
7927 error = got_error_from_errno("kill");
7928 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7929 error = got_error_from_errno("waitpid");
7931 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7932 error = got_error_from_errno("close");
7934 const struct got_error *close_err = got_repo_close(repo);
7939 got_worktree_close(worktree);
7942 got_pathlist_free(&branches);
7943 got_pathlist_free(&tags);
7944 got_ref_list_free(&all_branches);
7945 got_ref_list_free(&all_tags);
7946 got_pathlist_free(&delete_args);
7947 TAILQ_FOREACH(pe, &delete_branches, entry)
7948 free((char *)pe->path);
7949 got_pathlist_free(&delete_branches);
7961 usage_cherrypick(void)
7963 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7967 static const struct got_error *
7968 cmd_cherrypick(int argc, char *argv[])
7970 const struct got_error *error = NULL;
7971 struct got_worktree *worktree = NULL;
7972 struct got_repository *repo = NULL;
7973 char *cwd = NULL, *commit_id_str = NULL;
7974 struct got_object_id *commit_id = NULL;
7975 struct got_commit_object *commit = NULL;
7976 struct got_object_qid *pid;
7978 struct got_update_progress_arg upa;
7980 while ((ch = getopt(argc, argv, "")) != -1) {
7992 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7993 "unveil", NULL) == -1)
7999 cwd = getcwd(NULL, 0);
8001 error = got_error_from_errno("getcwd");
8004 error = got_worktree_open(&worktree, cwd);
8006 if (error->code == GOT_ERR_NOT_WORKTREE)
8007 error = wrap_not_worktree_error(error, "cherrypick",
8012 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8017 error = apply_unveil(got_repo_get_path(repo), 0,
8018 got_worktree_get_root_path(worktree));
8022 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8023 GOT_OBJ_TYPE_COMMIT, repo);
8024 if (error != NULL) {
8025 struct got_reference *ref;
8026 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8028 error = got_ref_open(&ref, repo, argv[0], 0);
8031 error = got_ref_resolve(&commit_id, repo, ref);
8036 error = got_object_id_str(&commit_id_str, commit_id);
8040 error = got_object_open_as_commit(&commit, repo, commit_id);
8043 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8044 memset(&upa, 0, sizeof(upa));
8045 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8046 commit_id, repo, update_progress, &upa, check_cancelled,
8051 if (upa.did_something)
8052 printf("Merged commit %s\n", commit_id_str);
8053 print_update_progress_stats(&upa);
8056 got_object_commit_close(commit);
8057 free(commit_id_str);
8059 got_worktree_close(worktree);
8061 const struct got_error *close_err = got_repo_close(repo);
8071 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8075 static const struct got_error *
8076 cmd_backout(int argc, char *argv[])
8078 const struct got_error *error = NULL;
8079 struct got_worktree *worktree = NULL;
8080 struct got_repository *repo = NULL;
8081 char *cwd = NULL, *commit_id_str = NULL;
8082 struct got_object_id *commit_id = NULL;
8083 struct got_commit_object *commit = NULL;
8084 struct got_object_qid *pid;
8086 struct got_update_progress_arg upa;
8088 while ((ch = getopt(argc, argv, "")) != -1) {
8100 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8101 "unveil", NULL) == -1)
8107 cwd = getcwd(NULL, 0);
8109 error = got_error_from_errno("getcwd");
8112 error = got_worktree_open(&worktree, cwd);
8114 if (error->code == GOT_ERR_NOT_WORKTREE)
8115 error = wrap_not_worktree_error(error, "backout", cwd);
8119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8124 error = apply_unveil(got_repo_get_path(repo), 0,
8125 got_worktree_get_root_path(worktree));
8129 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8130 GOT_OBJ_TYPE_COMMIT, repo);
8131 if (error != NULL) {
8132 struct got_reference *ref;
8133 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8135 error = got_ref_open(&ref, repo, argv[0], 0);
8138 error = got_ref_resolve(&commit_id, repo, ref);
8143 error = got_object_id_str(&commit_id_str, commit_id);
8147 error = got_object_open_as_commit(&commit, repo, commit_id);
8150 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8152 error = got_error(GOT_ERR_ROOT_COMMIT);
8156 memset(&upa, 0, sizeof(upa));
8157 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8158 update_progress, &upa, check_cancelled, NULL);
8162 if (upa.did_something)
8163 printf("Backed out commit %s\n", commit_id_str);
8164 print_update_progress_stats(&upa);
8167 got_object_commit_close(commit);
8168 free(commit_id_str);
8170 got_worktree_close(worktree);
8172 const struct got_error *close_err = got_repo_close(repo);
8182 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8188 trim_logmsg(char *logmsg, int limit)
8193 len = strlen(logmsg);
8197 nl = strchr(logmsg, '\n');
8202 static const struct got_error *
8203 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8205 const struct got_error *err;
8206 char *logmsg0 = NULL;
8209 err = got_object_commit_get_logmsg(&logmsg0, commit);
8214 while (isspace((unsigned char)s[0]))
8217 *logmsg = strdup(s);
8218 if (*logmsg == NULL) {
8219 err = got_error_from_errno("strdup");
8223 trim_logmsg(*logmsg, limit);
8229 static const struct got_error *
8230 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8232 const struct got_error *err;
8233 struct got_commit_object *commit = NULL;
8234 char *id_str = NULL, *logmsg = NULL;
8236 err = got_object_open_as_commit(&commit, repo, id);
8240 err = got_object_id_str(&id_str, id);
8246 err = get_short_logmsg(&logmsg, 42, commit);
8250 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8253 got_object_commit_close(commit);
8258 static const struct got_error *
8259 show_rebase_progress(struct got_commit_object *commit,
8260 struct got_object_id *old_id, struct got_object_id *new_id)
8262 const struct got_error *err;
8263 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8265 err = got_object_id_str(&old_id_str, old_id);
8270 err = got_object_id_str(&new_id_str, new_id);
8275 old_id_str[12] = '\0';
8277 new_id_str[12] = '\0';
8279 err = get_short_logmsg(&logmsg, 42, commit);
8283 printf("%s -> %s: %s\n", old_id_str,
8284 new_id_str ? new_id_str : "no-op change", logmsg);
8292 static const struct got_error *
8293 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8294 struct got_reference *branch, struct got_reference *new_base_branch,
8295 struct got_reference *tmp_branch, struct got_repository *repo,
8298 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8299 return got_worktree_rebase_complete(worktree, fileindex,
8300 new_base_branch, tmp_branch, branch, repo, create_backup);
8303 static const struct got_error *
8304 rebase_commit(struct got_pathlist_head *merged_paths,
8305 struct got_worktree *worktree, struct got_fileindex *fileindex,
8306 struct got_reference *tmp_branch,
8307 struct got_object_id *commit_id, struct got_repository *repo)
8309 const struct got_error *error;
8310 struct got_commit_object *commit;
8311 struct got_object_id *new_commit_id;
8313 error = got_object_open_as_commit(&commit, repo, commit_id);
8317 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8318 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8320 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8322 error = show_rebase_progress(commit, commit_id, NULL);
8324 error = show_rebase_progress(commit, commit_id, new_commit_id);
8325 free(new_commit_id);
8328 got_object_commit_close(commit);
8332 struct check_path_prefix_arg {
8333 const char *path_prefix;
8338 static const struct got_error *
8339 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8340 struct got_blob_object *blob2, struct got_object_id *id1,
8341 struct got_object_id *id2, const char *path1, const char *path2,
8342 mode_t mode1, mode_t mode2, struct got_repository *repo)
8344 struct check_path_prefix_arg *a = arg;
8346 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8347 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8348 return got_error(a->errcode);
8353 static const struct got_error *
8354 check_path_prefix(struct got_object_id *parent_id,
8355 struct got_object_id *commit_id, const char *path_prefix,
8356 int errcode, struct got_repository *repo)
8358 const struct got_error *err;
8359 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8360 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8361 struct check_path_prefix_arg cpp_arg;
8363 if (got_path_is_root_dir(path_prefix))
8366 err = got_object_open_as_commit(&commit, repo, commit_id);
8370 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8374 err = got_object_open_as_tree(&tree1, repo,
8375 got_object_commit_get_tree_id(parent_commit));
8379 err = got_object_open_as_tree(&tree2, repo,
8380 got_object_commit_get_tree_id(commit));
8384 cpp_arg.path_prefix = path_prefix;
8385 while (cpp_arg.path_prefix[0] == '/')
8386 cpp_arg.path_prefix++;
8387 cpp_arg.len = strlen(cpp_arg.path_prefix);
8388 cpp_arg.errcode = errcode;
8389 err = got_diff_tree(tree1, tree2, "", "", repo,
8390 check_path_prefix_in_diff, &cpp_arg, 0);
8393 got_object_tree_close(tree1);
8395 got_object_tree_close(tree2);
8397 got_object_commit_close(commit);
8399 got_object_commit_close(parent_commit);
8403 static const struct got_error *
8404 collect_commits(struct got_object_id_queue *commits,
8405 struct got_object_id *initial_commit_id,
8406 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8407 const char *path_prefix, int path_prefix_errcode,
8408 struct got_repository *repo)
8410 const struct got_error *err = NULL;
8411 struct got_commit_graph *graph = NULL;
8412 struct got_object_id *parent_id = NULL;
8413 struct got_object_qid *qid;
8414 struct got_object_id *commit_id = initial_commit_id;
8416 err = got_commit_graph_open(&graph, "/", 1);
8420 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8421 check_cancelled, NULL);
8424 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8425 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8426 check_cancelled, NULL);
8428 if (err->code == GOT_ERR_ITER_COMPLETED) {
8429 err = got_error_msg(GOT_ERR_ANCESTRY,
8430 "ran out of commits to rebase before "
8431 "youngest common ancestor commit has "
8436 err = check_path_prefix(parent_id, commit_id,
8437 path_prefix, path_prefix_errcode, repo);
8441 err = got_object_qid_alloc(&qid, commit_id);
8444 STAILQ_INSERT_HEAD(commits, qid, entry);
8445 commit_id = parent_id;
8449 got_commit_graph_close(graph);
8453 static const struct got_error *
8454 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8456 const struct got_error *err = NULL;
8457 time_t committer_time;
8459 char datebuf[11]; /* YYYY-MM-DD + NUL */
8460 char *author0 = NULL, *author, *smallerthan;
8461 char *logmsg0 = NULL, *logmsg, *newline;
8463 committer_time = got_object_commit_get_committer_time(commit);
8464 if (gmtime_r(&committer_time, &tm) == NULL)
8465 return got_error_from_errno("gmtime_r");
8466 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8467 return got_error(GOT_ERR_NO_SPACE);
8469 author0 = strdup(got_object_commit_get_author(commit));
8470 if (author0 == NULL)
8471 return got_error_from_errno("strdup");
8473 smallerthan = strchr(author, '<');
8474 if (smallerthan && smallerthan[1] != '\0')
8475 author = smallerthan + 1;
8476 author[strcspn(author, "@>")] = '\0';
8478 err = got_object_commit_get_logmsg(&logmsg0, commit);
8482 while (*logmsg == '\n')
8484 newline = strchr(logmsg, '\n');
8488 if (asprintf(brief_str, "%s %s %s",
8489 datebuf, author, logmsg) == -1)
8490 err = got_error_from_errno("asprintf");
8497 static const struct got_error *
8498 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8499 struct got_repository *repo)
8501 const struct got_error *err;
8504 err = got_object_id_str(&id_str, id);
8508 err = got_ref_delete(ref, repo);
8512 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8518 static const struct got_error *
8519 print_backup_ref(const char *branch_name, const char *new_id_str,
8520 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8521 struct got_reflist_object_id_map *refs_idmap,
8522 struct got_repository *repo)
8524 const struct got_error *err = NULL;
8525 struct got_reflist_head *refs;
8526 char *refs_str = NULL;
8527 struct got_object_id *new_commit_id = NULL;
8528 struct got_commit_object *new_commit = NULL;
8529 char *new_commit_brief_str = NULL;
8530 struct got_object_id *yca_id = NULL;
8531 struct got_commit_object *yca_commit = NULL;
8532 char *yca_id_str = NULL, *yca_brief_str = NULL;
8533 char *custom_refs_str;
8535 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8536 return got_error_from_errno("asprintf");
8538 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8539 0, 0, refs_idmap, custom_refs_str);
8543 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8547 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8549 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8554 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8558 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8562 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8563 old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8567 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8568 refs_str ? " (" : "", refs_str ? refs_str : "",
8569 refs_str ? ")" : "", new_commit_brief_str);
8570 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8571 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8575 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8579 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8583 err = got_object_id_str(&yca_id_str, yca_id);
8587 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8589 err = build_refs_str(&refs_str, refs, yca_id, repo);
8593 printf("history forked at %s%s%s%s\n %s\n",
8595 refs_str ? " (" : "", refs_str ? refs_str : "",
8596 refs_str ? ")" : "", yca_brief_str);
8599 free(custom_refs_str);
8600 free(new_commit_id);
8604 free(yca_brief_str);
8606 got_object_commit_close(new_commit);
8608 got_object_commit_close(yca_commit);
8613 static const struct got_error *
8614 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8615 int delete, struct got_repository *repo)
8617 const struct got_error *err;
8618 struct got_reflist_head refs, backup_refs;
8619 struct got_reflist_entry *re;
8620 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8621 struct got_object_id *old_commit_id = NULL;
8622 char *branch_name = NULL;
8623 struct got_commit_object *old_commit = NULL;
8624 struct got_reflist_object_id_map *refs_idmap = NULL;
8625 int wanted_branch_found = 0;
8628 TAILQ_INIT(&backup_refs);
8630 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8634 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8638 if (wanted_branch_name) {
8639 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8640 wanted_branch_name += 11;
8643 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8644 got_ref_cmp_by_commit_timestamp_descending, repo);
8648 TAILQ_FOREACH(re, &backup_refs, entry) {
8649 const char *refname = got_ref_get_name(re->ref);
8652 err = check_cancelled(NULL);
8656 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8660 err = got_object_open_as_commit(&old_commit, repo,
8665 if (strncmp(backup_ref_prefix, refname,
8666 backup_ref_prefix_len) == 0)
8667 refname += backup_ref_prefix_len;
8669 while (refname[0] == '/')
8672 branch_name = strdup(refname);
8673 if (branch_name == NULL) {
8674 err = got_error_from_errno("strdup");
8677 slash = strrchr(branch_name, '/');
8680 refname += strlen(branch_name) + 1;
8683 if (wanted_branch_name == NULL ||
8684 strcmp(wanted_branch_name, branch_name) == 0) {
8685 wanted_branch_found = 1;
8687 err = delete_backup_ref(re->ref,
8688 old_commit_id, repo);
8690 err = print_backup_ref(branch_name, refname,
8691 old_commit_id, old_commit, refs_idmap,
8698 free(old_commit_id);
8699 old_commit_id = NULL;
8702 got_object_commit_close(old_commit);
8706 if (wanted_branch_name && !wanted_branch_found) {
8707 err = got_error_fmt(GOT_ERR_NOT_REF,
8708 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8712 got_reflist_object_id_map_free(refs_idmap);
8713 got_ref_list_free(&refs);
8714 got_ref_list_free(&backup_refs);
8715 free(old_commit_id);
8718 got_object_commit_close(old_commit);
8722 static const struct got_error *
8723 cmd_rebase(int argc, char *argv[])
8725 const struct got_error *error = NULL;
8726 struct got_worktree *worktree = NULL;
8727 struct got_repository *repo = NULL;
8728 struct got_fileindex *fileindex = NULL;
8730 struct got_reference *branch = NULL;
8731 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8732 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8733 struct got_object_id *resume_commit_id = NULL;
8734 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8735 struct got_commit_object *commit = NULL;
8736 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8737 int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8738 int delete_backups = 0;
8739 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8740 struct got_object_id_queue commits;
8741 struct got_pathlist_head merged_paths;
8742 const struct got_object_id_queue *parent_ids;
8743 struct got_object_qid *qid, *pid;
8745 STAILQ_INIT(&commits);
8746 TAILQ_INIT(&merged_paths);
8748 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8754 continue_rebase = 1;
8772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8773 "unveil", NULL) == -1)
8778 option_conflict('l', 'a');
8779 if (continue_rebase)
8780 option_conflict('l', 'c');
8782 option_conflict('l', 'X');
8783 if (argc != 0 && argc != 1)
8785 } else if (delete_backups) {
8787 option_conflict('X', 'a');
8788 if (continue_rebase)
8789 option_conflict('X', 'c');
8791 option_conflict('l', 'X');
8792 if (argc != 0 && argc != 1)
8795 if (abort_rebase && continue_rebase)
8797 else if (abort_rebase || continue_rebase) {
8800 } else if (argc != 1)
8804 cwd = getcwd(NULL, 0);
8806 error = got_error_from_errno("getcwd");
8809 error = got_worktree_open(&worktree, cwd);
8811 if (list_backups || delete_backups) {
8812 if (error->code != GOT_ERR_NOT_WORKTREE)
8815 if (error->code == GOT_ERR_NOT_WORKTREE)
8816 error = wrap_not_worktree_error(error,
8822 error = got_repo_open(&repo,
8823 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8827 error = apply_unveil(got_repo_get_path(repo), 0,
8828 worktree ? got_worktree_get_root_path(worktree) : NULL);
8832 if (list_backups || delete_backups) {
8833 error = process_backup_refs(
8834 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8835 argc == 1 ? argv[0] : NULL, delete_backups, repo);
8836 goto done; /* nothing else to do */
8839 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8843 if (histedit_in_progress) {
8844 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8848 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8853 struct got_update_progress_arg upa;
8854 if (!rebase_in_progress) {
8855 error = got_error(GOT_ERR_NOT_REBASING);
8858 error = got_worktree_rebase_continue(&resume_commit_id,
8859 &new_base_branch, &tmp_branch, &branch, &fileindex,
8863 printf("Switching work tree to %s\n",
8864 got_ref_get_symref_target(new_base_branch));
8865 memset(&upa, 0, sizeof(upa));
8866 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8867 new_base_branch, update_progress, &upa);
8870 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8871 print_update_progress_stats(&upa);
8872 goto done; /* nothing else to do */
8875 if (continue_rebase) {
8876 if (!rebase_in_progress) {
8877 error = got_error(GOT_ERR_NOT_REBASING);
8880 error = got_worktree_rebase_continue(&resume_commit_id,
8881 &new_base_branch, &tmp_branch, &branch, &fileindex,
8886 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8887 resume_commit_id, repo);
8891 yca_id = got_object_id_dup(resume_commit_id);
8892 if (yca_id == NULL) {
8893 error = got_error_from_errno("got_object_id_dup");
8897 error = got_ref_open(&branch, repo, argv[0], 0);
8902 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8906 if (!continue_rebase) {
8907 struct got_object_id *base_commit_id;
8909 base_commit_id = got_worktree_get_base_commit_id(worktree);
8910 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8911 base_commit_id, branch_head_commit_id, repo,
8912 check_cancelled, NULL);
8915 if (yca_id == NULL) {
8916 error = got_error_msg(GOT_ERR_ANCESTRY,
8917 "specified branch shares no common ancestry "
8918 "with work tree's branch");
8922 error = check_same_branch(base_commit_id, branch, yca_id, repo);
8924 if (error->code != GOT_ERR_ANCESTRY)
8928 static char msg[128];
8929 snprintf(msg, sizeof(msg),
8930 "%s is already based on %s",
8931 got_ref_get_name(branch),
8932 got_worktree_get_head_ref_name(worktree));
8933 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8936 error = got_worktree_rebase_prepare(&new_base_branch,
8937 &tmp_branch, &fileindex, worktree, branch, repo);
8942 commit_id = branch_head_commit_id;
8943 error = got_object_open_as_commit(&commit, repo, commit_id);
8947 parent_ids = got_object_commit_get_parent_ids(commit);
8948 pid = STAILQ_FIRST(parent_ids);
8950 if (!continue_rebase) {
8951 struct got_update_progress_arg upa;
8952 memset(&upa, 0, sizeof(upa));
8953 error = got_worktree_rebase_abort(worktree, fileindex,
8954 repo, new_base_branch, update_progress, &upa);
8957 printf("Rebase of %s aborted\n",
8958 got_ref_get_name(branch));
8959 print_update_progress_stats(&upa);
8962 error = got_error(GOT_ERR_EMPTY_REBASE);
8965 error = collect_commits(&commits, commit_id, pid->id,
8966 yca_id, got_worktree_get_path_prefix(worktree),
8967 GOT_ERR_REBASE_PATH, repo);
8968 got_object_commit_close(commit);
8973 if (STAILQ_EMPTY(&commits)) {
8974 if (continue_rebase) {
8975 error = rebase_complete(worktree, fileindex,
8976 branch, new_base_branch, tmp_branch, repo,
8980 /* Fast-forward the reference of the branch. */
8981 struct got_object_id *new_head_commit_id;
8983 error = got_ref_resolve(&new_head_commit_id, repo,
8987 error = got_object_id_str(&id_str, new_head_commit_id);
8988 printf("Forwarding %s to commit %s\n",
8989 got_ref_get_name(branch), id_str);
8991 error = got_ref_change_ref(branch,
8992 new_head_commit_id);
8995 /* No backup needed since objects did not change. */
9001 STAILQ_FOREACH(qid, &commits, entry) {
9002 struct got_update_progress_arg upa;
9004 commit_id = qid->id;
9005 parent_id = pid ? pid->id : yca_id;
9008 memset(&upa, 0, sizeof(upa));
9009 error = got_worktree_rebase_merge_files(&merged_paths,
9010 worktree, fileindex, parent_id, commit_id, repo,
9011 update_progress, &upa, check_cancelled, NULL);
9015 print_update_progress_stats(&upa);
9016 if (upa.conflicts > 0)
9017 rebase_status = GOT_STATUS_CONFLICT;
9019 if (rebase_status == GOT_STATUS_CONFLICT) {
9020 error = show_rebase_merge_conflict(qid->id, repo);
9023 got_worktree_rebase_pathlist_free(&merged_paths);
9027 error = rebase_commit(&merged_paths, worktree, fileindex,
9028 tmp_branch, commit_id, repo);
9029 got_worktree_rebase_pathlist_free(&merged_paths);
9034 if (rebase_status == GOT_STATUS_CONFLICT) {
9035 error = got_worktree_rebase_postpone(worktree, fileindex);
9038 error = got_error_msg(GOT_ERR_CONFLICTS,
9039 "conflicts must be resolved before rebasing can continue");
9041 error = rebase_complete(worktree, fileindex, branch,
9042 new_base_branch, tmp_branch, repo, create_backup);
9044 got_object_id_queue_free(&commits);
9045 free(branch_head_commit_id);
9046 free(resume_commit_id);
9049 got_object_commit_close(commit);
9051 got_ref_close(branch);
9052 if (new_base_branch)
9053 got_ref_close(new_base_branch);
9055 got_ref_close(tmp_branch);
9057 got_worktree_close(worktree);
9059 const struct got_error *close_err = got_repo_close(repo);
9067 usage_histedit(void)
9069 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9070 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9075 #define GOT_HISTEDIT_PICK 'p'
9076 #define GOT_HISTEDIT_EDIT 'e'
9077 #define GOT_HISTEDIT_FOLD 'f'
9078 #define GOT_HISTEDIT_DROP 'd'
9079 #define GOT_HISTEDIT_MESG 'm'
9081 static struct got_histedit_cmd {
9085 } got_histedit_cmds[] = {
9086 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9087 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9088 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9090 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9091 { GOT_HISTEDIT_MESG, "mesg",
9092 "single-line log message for commit above (open editor if empty)" },
9095 struct got_histedit_list_entry {
9096 TAILQ_ENTRY(got_histedit_list_entry) entry;
9097 struct got_object_id *commit_id;
9098 const struct got_histedit_cmd *cmd;
9101 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9103 static const struct got_error *
9104 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9105 FILE *f, struct got_repository *repo)
9107 const struct got_error *err = NULL;
9108 char *logmsg = NULL, *id_str = NULL;
9109 struct got_commit_object *commit = NULL;
9112 err = got_object_open_as_commit(&commit, repo, commit_id);
9116 err = get_short_logmsg(&logmsg, 34, commit);
9120 err = got_object_id_str(&id_str, commit_id);
9124 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9126 err = got_ferror(f, GOT_ERR_IO);
9129 got_object_commit_close(commit);
9135 static const struct got_error *
9136 histedit_write_commit_list(struct got_object_id_queue *commits,
9137 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9139 const struct got_error *err = NULL;
9140 struct got_object_qid *qid;
9141 const char *histedit_cmd = NULL;
9143 if (STAILQ_EMPTY(commits))
9144 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9146 STAILQ_FOREACH(qid, commits, entry) {
9147 histedit_cmd = got_histedit_cmds[0].name;
9148 if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9149 histedit_cmd = "fold";
9150 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9153 if (edit_logmsg_only) {
9154 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9156 err = got_ferror(f, GOT_ERR_IO);
9165 static const struct got_error *
9166 write_cmd_list(FILE *f, const char *branch_name,
9167 struct got_object_id_queue *commits)
9169 const struct got_error *err = NULL;
9173 struct got_object_qid *qid;
9175 qid = STAILQ_FIRST(commits);
9176 err = got_object_id_str(&id_str, qid->id);
9181 "# Editing the history of branch '%s' starting at\n"
9183 "# Commits will be processed in order from top to "
9184 "bottom of this file.\n", branch_name, id_str);
9186 err = got_ferror(f, GOT_ERR_IO);
9190 n = fprintf(f, "# Available histedit commands:\n");
9192 err = got_ferror(f, GOT_ERR_IO);
9196 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9197 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9198 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9201 err = got_ferror(f, GOT_ERR_IO);
9210 static const struct got_error *
9211 histedit_syntax_error(int lineno)
9213 static char msg[42];
9216 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9218 if (ret == -1 || ret >= sizeof(msg))
9219 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9221 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9224 static const struct got_error *
9225 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9226 char *logmsg, struct got_repository *repo)
9228 const struct got_error *err;
9229 struct got_commit_object *folded_commit = NULL;
9230 char *id_str, *folded_logmsg = NULL;
9232 err = got_object_id_str(&id_str, hle->commit_id);
9236 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9240 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9243 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9244 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9245 folded_logmsg) == -1) {
9246 err = got_error_from_errno("asprintf");
9250 got_object_commit_close(folded_commit);
9252 free(folded_logmsg);
9256 static struct got_histedit_list_entry *
9257 get_folded_commits(struct got_histedit_list_entry *hle)
9259 struct got_histedit_list_entry *prev, *folded = NULL;
9261 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9262 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9263 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9264 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9266 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9272 static const struct got_error *
9273 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9274 struct got_repository *repo)
9276 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9277 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9278 const struct got_error *err = NULL;
9279 struct got_commit_object *commit = NULL;
9282 struct got_histedit_list_entry *folded = NULL;
9284 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9288 folded = get_folded_commits(hle);
9290 while (folded != hle) {
9291 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9292 folded = TAILQ_NEXT(folded, entry);
9295 err = append_folded_commit_msg(&new_msg, folded,
9301 folded = TAILQ_NEXT(folded, entry);
9305 err = got_object_id_str(&id_str, hle->commit_id);
9308 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9311 logmsg_len = asprintf(&new_msg,
9312 "%s\n# original log message of commit %s: %s",
9313 logmsg ? logmsg : "", id_str, orig_logmsg);
9314 if (logmsg_len == -1) {
9315 err = got_error_from_errno("asprintf");
9321 err = got_object_id_str(&id_str, hle->commit_id);
9325 err = got_opentemp_named_fd(&logmsg_path, &fd,
9326 GOT_TMPDIR_STR "/got-logmsg");
9330 write(fd, logmsg, logmsg_len);
9333 err = get_editor(&editor);
9337 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9340 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9343 hle->logmsg = strdup(new_msg);
9344 if (hle->logmsg == NULL)
9345 err = got_error_from_errno("strdup");
9348 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9349 err = got_error_from_errno2("unlink", logmsg_path);
9355 got_object_commit_close(commit);
9359 static const struct got_error *
9360 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9361 FILE *f, struct got_repository *repo)
9363 const struct got_error *err = NULL;
9364 char *line = NULL, *p, *end;
9368 const struct got_histedit_cmd *cmd;
9369 struct got_object_id *commit_id = NULL;
9370 struct got_histedit_list_entry *hle = NULL;
9373 len = getline(&line, &size, f);
9375 const struct got_error *getline_err;
9378 getline_err = got_error_from_errno("getline");
9379 err = got_ferror(f, getline_err->code);
9384 while (isspace((unsigned char)p[0]))
9386 if (p[0] == '#' || p[0] == '\0') {
9392 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9393 cmd = &got_histedit_cmds[i];
9394 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9395 isspace((unsigned char)p[strlen(cmd->name)])) {
9396 p += strlen(cmd->name);
9399 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9404 if (i == nitems(got_histedit_cmds)) {
9405 err = histedit_syntax_error(lineno);
9408 while (isspace((unsigned char)p[0]))
9410 if (cmd->code == GOT_HISTEDIT_MESG) {
9411 if (hle == NULL || hle->logmsg != NULL) {
9412 err = got_error(GOT_ERR_HISTEDIT_CMD);
9416 err = histedit_edit_logmsg(hle, repo);
9420 hle->logmsg = strdup(p);
9421 if (hle->logmsg == NULL) {
9422 err = got_error_from_errno("strdup");
9431 while (end[0] && !isspace((unsigned char)end[0]))
9435 err = got_object_resolve_id_str(&commit_id, repo, p);
9437 /* override error code */
9438 err = histedit_syntax_error(lineno);
9442 hle = malloc(sizeof(*hle));
9444 err = got_error_from_errno("malloc");
9448 hle->commit_id = commit_id;
9453 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9461 static const struct got_error *
9462 histedit_check_script(struct got_histedit_list *histedit_cmds,
9463 struct got_object_id_queue *commits, struct got_repository *repo)
9465 const struct got_error *err = NULL;
9466 struct got_object_qid *qid;
9467 struct got_histedit_list_entry *hle;
9468 static char msg[92];
9471 if (TAILQ_EMPTY(histedit_cmds))
9472 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9473 "histedit script contains no commands");
9474 if (STAILQ_EMPTY(commits))
9475 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9477 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9478 struct got_histedit_list_entry *hle2;
9479 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9482 if (got_object_id_cmp(hle->commit_id,
9483 hle2->commit_id) != 0)
9485 err = got_object_id_str(&id_str, hle->commit_id);
9488 snprintf(msg, sizeof(msg), "commit %s is listed "
9489 "more than once in histedit script", id_str);
9491 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9495 STAILQ_FOREACH(qid, commits, entry) {
9496 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9497 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9501 err = got_object_id_str(&id_str, qid->id);
9504 snprintf(msg, sizeof(msg),
9505 "commit %s missing from histedit script", id_str);
9507 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9511 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9512 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9513 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9514 "last commit in histedit script cannot be folded");
9519 static const struct got_error *
9520 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9521 const char *path, struct got_object_id_queue *commits,
9522 struct got_repository *repo)
9524 const struct got_error *err = NULL;
9528 err = get_editor(&editor);
9532 if (spawn_editor(editor, path) == -1) {
9533 err = got_error_from_errno("failed spawning editor");
9537 f = fopen(path, "r");
9539 err = got_error_from_errno("fopen");
9542 err = histedit_parse_list(histedit_cmds, f, repo);
9546 err = histedit_check_script(histedit_cmds, commits, repo);
9548 if (f && fclose(f) == EOF && err == NULL)
9549 err = got_error_from_errno("fclose");
9554 static const struct got_error *
9555 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9556 struct got_object_id_queue *, const char *, const char *,
9557 struct got_repository *);
9559 static const struct got_error *
9560 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9561 struct got_object_id_queue *commits, const char *branch_name,
9562 int edit_logmsg_only, int fold_only, struct got_repository *repo)
9564 const struct got_error *err;
9568 err = got_opentemp_named(&path, &f, "got-histedit");
9572 err = write_cmd_list(f, branch_name, commits);
9576 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9581 if (edit_logmsg_only || fold_only) {
9583 err = histedit_parse_list(histedit_cmds, f, repo);
9585 if (fclose(f) == EOF) {
9586 err = got_error_from_errno("fclose");
9590 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9592 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9593 err->code != GOT_ERR_HISTEDIT_CMD)
9595 err = histedit_edit_list_retry(histedit_cmds, err,
9596 commits, path, branch_name, repo);
9600 if (f && fclose(f) == EOF && err == NULL)
9601 err = got_error_from_errno("fclose");
9602 if (path && unlink(path) != 0 && err == NULL)
9603 err = got_error_from_errno2("unlink", path);
9608 static const struct got_error *
9609 histedit_save_list(struct got_histedit_list *histedit_cmds,
9610 struct got_worktree *worktree, struct got_repository *repo)
9612 const struct got_error *err = NULL;
9615 struct got_histedit_list_entry *hle;
9616 struct got_commit_object *commit = NULL;
9618 err = got_worktree_get_histedit_script_path(&path, worktree);
9622 f = fopen(path, "w");
9624 err = got_error_from_errno2("fopen", path);
9627 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9628 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9634 int n = fprintf(f, "%c %s\n",
9635 GOT_HISTEDIT_MESG, hle->logmsg);
9637 err = got_ferror(f, GOT_ERR_IO);
9643 if (f && fclose(f) == EOF && err == NULL)
9644 err = got_error_from_errno("fclose");
9647 got_object_commit_close(commit);
9652 histedit_free_list(struct got_histedit_list *histedit_cmds)
9654 struct got_histedit_list_entry *hle;
9656 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9657 TAILQ_REMOVE(histedit_cmds, hle, entry);
9662 static const struct got_error *
9663 histedit_load_list(struct got_histedit_list *histedit_cmds,
9664 const char *path, struct got_repository *repo)
9666 const struct got_error *err = NULL;
9669 f = fopen(path, "r");
9671 err = got_error_from_errno2("fopen", path);
9675 err = histedit_parse_list(histedit_cmds, f, repo);
9677 if (f && fclose(f) == EOF && err == NULL)
9678 err = got_error_from_errno("fclose");
9682 static const struct got_error *
9683 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9684 const struct got_error *edit_err, struct got_object_id_queue *commits,
9685 const char *path, const char *branch_name, struct got_repository *repo)
9687 const struct got_error *err = NULL, *prev_err = edit_err;
9690 while (resp != 'c' && resp != 'r' && resp != 'a') {
9691 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9692 "or (a)bort: ", getprogname(), prev_err->msg);
9697 histedit_free_list(histedit_cmds);
9698 err = histedit_run_editor(histedit_cmds, path, commits,
9701 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9702 err->code != GOT_ERR_HISTEDIT_CMD)
9709 } else if (resp == 'r') {
9710 histedit_free_list(histedit_cmds);
9711 err = histedit_edit_script(histedit_cmds,
9712 commits, branch_name, 0, 0, repo);
9714 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9715 err->code != GOT_ERR_HISTEDIT_CMD)
9722 } else if (resp == 'a') {
9723 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9726 printf("invalid response '%c'\n", resp);
9732 static const struct got_error *
9733 histedit_complete(struct got_worktree *worktree,
9734 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9735 struct got_reference *branch, struct got_repository *repo)
9737 printf("Switching work tree to %s\n",
9738 got_ref_get_symref_target(branch));
9739 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9743 static const struct got_error *
9744 show_histedit_progress(struct got_commit_object *commit,
9745 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9747 const struct got_error *err;
9748 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9750 err = got_object_id_str(&old_id_str, hle->commit_id);
9755 err = got_object_id_str(&new_id_str, new_id);
9760 old_id_str[12] = '\0';
9762 new_id_str[12] = '\0';
9765 logmsg = strdup(hle->logmsg);
9766 if (logmsg == NULL) {
9767 err = got_error_from_errno("strdup");
9770 trim_logmsg(logmsg, 42);
9772 err = get_short_logmsg(&logmsg, 42, commit);
9777 switch (hle->cmd->code) {
9778 case GOT_HISTEDIT_PICK:
9779 case GOT_HISTEDIT_EDIT:
9780 printf("%s -> %s: %s\n", old_id_str,
9781 new_id_str ? new_id_str : "no-op change", logmsg);
9783 case GOT_HISTEDIT_DROP:
9784 case GOT_HISTEDIT_FOLD:
9785 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9797 static const struct got_error *
9798 histedit_commit(struct got_pathlist_head *merged_paths,
9799 struct got_worktree *worktree, struct got_fileindex *fileindex,
9800 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9801 struct got_repository *repo)
9803 const struct got_error *err;
9804 struct got_commit_object *commit;
9805 struct got_object_id *new_commit_id;
9807 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9808 && hle->logmsg == NULL) {
9809 err = histedit_edit_logmsg(hle, repo);
9814 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9818 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9819 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9822 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9824 err = show_histedit_progress(commit, hle, NULL);
9826 err = show_histedit_progress(commit, hle, new_commit_id);
9827 free(new_commit_id);
9830 got_object_commit_close(commit);
9834 static const struct got_error *
9835 histedit_skip_commit(struct got_histedit_list_entry *hle,
9836 struct got_worktree *worktree, struct got_repository *repo)
9838 const struct got_error *error;
9839 struct got_commit_object *commit;
9841 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9846 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9850 error = show_histedit_progress(commit, hle, NULL);
9851 got_object_commit_close(commit);
9855 static const struct got_error *
9856 check_local_changes(void *arg, unsigned char status,
9857 unsigned char staged_status, const char *path,
9858 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9859 struct got_object_id *commit_id, int dirfd, const char *de_name)
9861 int *have_local_changes = arg;
9864 case GOT_STATUS_ADD:
9865 case GOT_STATUS_DELETE:
9866 case GOT_STATUS_MODIFY:
9867 case GOT_STATUS_CONFLICT:
9868 *have_local_changes = 1;
9869 return got_error(GOT_ERR_CANCELLED);
9874 switch (staged_status) {
9875 case GOT_STATUS_ADD:
9876 case GOT_STATUS_DELETE:
9877 case GOT_STATUS_MODIFY:
9878 *have_local_changes = 1;
9879 return got_error(GOT_ERR_CANCELLED);
9887 static const struct got_error *
9888 cmd_histedit(int argc, char *argv[])
9890 const struct got_error *error = NULL;
9891 struct got_worktree *worktree = NULL;
9892 struct got_fileindex *fileindex = NULL;
9893 struct got_repository *repo = NULL;
9895 struct got_reference *branch = NULL;
9896 struct got_reference *tmp_branch = NULL;
9897 struct got_object_id *resume_commit_id = NULL;
9898 struct got_object_id *base_commit_id = NULL;
9899 struct got_object_id *head_commit_id = NULL;
9900 struct got_commit_object *commit = NULL;
9901 int ch, rebase_in_progress = 0;
9902 struct got_update_progress_arg upa;
9903 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9904 int edit_logmsg_only = 0, fold_only = 0;
9905 int list_backups = 0, delete_backups = 0;
9906 const char *edit_script_path = NULL;
9907 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9908 struct got_object_id_queue commits;
9909 struct got_pathlist_head merged_paths;
9910 const struct got_object_id_queue *parent_ids;
9911 struct got_object_qid *pid;
9912 struct got_histedit_list histedit_cmds;
9913 struct got_histedit_list_entry *hle;
9915 STAILQ_INIT(&commits);
9916 TAILQ_INIT(&histedit_cmds);
9917 TAILQ_INIT(&merged_paths);
9918 memset(&upa, 0, sizeof(upa));
9920 while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9932 edit_script_path = optarg;
9935 edit_logmsg_only = 1;
9953 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9954 "unveil", NULL) == -1)
9957 if (abort_edit && continue_edit)
9958 option_conflict('a', 'c');
9959 if (edit_script_path && edit_logmsg_only)
9960 option_conflict('F', 'm');
9961 if (abort_edit && edit_logmsg_only)
9962 option_conflict('a', 'm');
9963 if (continue_edit && edit_logmsg_only)
9964 option_conflict('c', 'm');
9965 if (abort_edit && fold_only)
9966 option_conflict('a', 'f');
9967 if (continue_edit && fold_only)
9968 option_conflict('c', 'f');
9969 if (fold_only && edit_logmsg_only)
9970 option_conflict('f', 'm');
9971 if (edit_script_path && fold_only)
9972 option_conflict('F', 'f');
9975 option_conflict('l', 'a');
9977 option_conflict('l', 'c');
9978 if (edit_script_path)
9979 option_conflict('l', 'F');
9980 if (edit_logmsg_only)
9981 option_conflict('l', 'm');
9983 option_conflict('l', 'f');
9985 option_conflict('l', 'X');
9986 if (argc != 0 && argc != 1)
9988 } else if (delete_backups) {
9990 option_conflict('X', 'a');
9992 option_conflict('X', 'c');
9993 if (edit_script_path)
9994 option_conflict('X', 'F');
9995 if (edit_logmsg_only)
9996 option_conflict('X', 'm');
9998 option_conflict('X', 'f');
10000 option_conflict('X', 'l');
10001 if (argc != 0 && argc != 1)
10003 } else if (argc != 0)
10007 * This command cannot apply unveil(2) in all cases because the
10008 * user may choose to run an editor to edit the histedit script
10009 * and to edit individual commit log messages.
10010 * unveil(2) traverses exec(2); if an editor is used we have to
10011 * apply unveil after edit script and log messages have been written.
10012 * XXX TODO: Make use of unveil(2) where possible.
10015 cwd = getcwd(NULL, 0);
10017 error = got_error_from_errno("getcwd");
10020 error = got_worktree_open(&worktree, cwd);
10022 if (list_backups || delete_backups) {
10023 if (error->code != GOT_ERR_NOT_WORKTREE)
10026 if (error->code == GOT_ERR_NOT_WORKTREE)
10027 error = wrap_not_worktree_error(error,
10033 if (list_backups || delete_backups) {
10034 error = got_repo_open(&repo,
10035 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10039 error = apply_unveil(got_repo_get_path(repo), 0,
10040 worktree ? got_worktree_get_root_path(worktree) : NULL);
10043 error = process_backup_refs(
10044 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10045 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10046 goto done; /* nothing else to do */
10049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10054 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10057 if (rebase_in_progress) {
10058 error = got_error(GOT_ERR_REBASING);
10062 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10066 if (edit_in_progress && edit_logmsg_only) {
10067 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10068 "histedit operation is in progress in this "
10069 "work tree and must be continued or aborted "
10070 "before the -m option can be used");
10073 if (edit_in_progress && fold_only) {
10074 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10075 "histedit operation is in progress in this "
10076 "work tree and must be continued or aborted "
10077 "before the -f option can be used");
10081 if (edit_in_progress && abort_edit) {
10082 error = got_worktree_histedit_continue(&resume_commit_id,
10083 &tmp_branch, &branch, &base_commit_id, &fileindex,
10087 printf("Switching work tree to %s\n",
10088 got_ref_get_symref_target(branch));
10089 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10090 branch, base_commit_id, update_progress, &upa);
10093 printf("Histedit of %s aborted\n",
10094 got_ref_get_symref_target(branch));
10095 print_update_progress_stats(&upa);
10096 goto done; /* nothing else to do */
10097 } else if (abort_edit) {
10098 error = got_error(GOT_ERR_NOT_HISTEDIT);
10102 if (continue_edit) {
10105 if (!edit_in_progress) {
10106 error = got_error(GOT_ERR_NOT_HISTEDIT);
10110 error = got_worktree_get_histedit_script_path(&path, worktree);
10114 error = histedit_load_list(&histedit_cmds, path, repo);
10119 error = got_worktree_histedit_continue(&resume_commit_id,
10120 &tmp_branch, &branch, &base_commit_id, &fileindex,
10125 error = got_ref_resolve(&head_commit_id, repo, branch);
10129 error = got_object_open_as_commit(&commit, repo,
10133 parent_ids = got_object_commit_get_parent_ids(commit);
10134 pid = STAILQ_FIRST(parent_ids);
10136 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10139 error = collect_commits(&commits, head_commit_id, pid->id,
10140 base_commit_id, got_worktree_get_path_prefix(worktree),
10141 GOT_ERR_HISTEDIT_PATH, repo);
10142 got_object_commit_close(commit);
10147 if (edit_in_progress) {
10148 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10152 error = got_ref_open(&branch, repo,
10153 got_worktree_get_head_ref_name(worktree), 0);
10157 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10158 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10159 "will not edit commit history of a branch outside "
10160 "the \"refs/heads/\" reference namespace");
10164 error = got_ref_resolve(&head_commit_id, repo, branch);
10165 got_ref_close(branch);
10170 error = got_object_open_as_commit(&commit, repo,
10174 parent_ids = got_object_commit_get_parent_ids(commit);
10175 pid = STAILQ_FIRST(parent_ids);
10177 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10180 error = collect_commits(&commits, head_commit_id, pid->id,
10181 got_worktree_get_base_commit_id(worktree),
10182 got_worktree_get_path_prefix(worktree),
10183 GOT_ERR_HISTEDIT_PATH, repo);
10184 got_object_commit_close(commit);
10189 if (STAILQ_EMPTY(&commits)) {
10190 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10194 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10195 &base_commit_id, &fileindex, worktree, repo);
10199 if (edit_script_path) {
10200 error = histedit_load_list(&histedit_cmds,
10201 edit_script_path, repo);
10203 got_worktree_histedit_abort(worktree, fileindex,
10204 repo, branch, base_commit_id,
10205 update_progress, &upa);
10206 print_update_progress_stats(&upa);
10210 const char *branch_name;
10211 branch_name = got_ref_get_symref_target(branch);
10212 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10214 error = histedit_edit_script(&histedit_cmds, &commits,
10215 branch_name, edit_logmsg_only, fold_only, repo);
10217 got_worktree_histedit_abort(worktree, fileindex,
10218 repo, branch, base_commit_id,
10219 update_progress, &upa);
10220 print_update_progress_stats(&upa);
10226 error = histedit_save_list(&histedit_cmds, worktree,
10229 got_worktree_histedit_abort(worktree, fileindex,
10230 repo, branch, base_commit_id,
10231 update_progress, &upa);
10232 print_update_progress_stats(&upa);
10238 error = histedit_check_script(&histedit_cmds, &commits, repo);
10242 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10243 if (resume_commit_id) {
10244 if (got_object_id_cmp(hle->commit_id,
10245 resume_commit_id) != 0)
10248 resume_commit_id = NULL;
10249 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10250 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10251 error = histedit_skip_commit(hle, worktree,
10256 struct got_pathlist_head paths;
10257 int have_changes = 0;
10259 TAILQ_INIT(&paths);
10260 error = got_pathlist_append(&paths, "", NULL);
10263 error = got_worktree_status(worktree, &paths,
10264 repo, 0, check_local_changes, &have_changes,
10265 check_cancelled, NULL);
10266 got_pathlist_free(&paths);
10268 if (error->code != GOT_ERR_CANCELLED)
10270 if (sigint_received || sigpipe_received)
10273 if (have_changes) {
10274 error = histedit_commit(NULL, worktree,
10275 fileindex, tmp_branch, hle, repo);
10279 error = got_object_open_as_commit(
10280 &commit, repo, hle->commit_id);
10283 error = show_histedit_progress(commit,
10285 got_object_commit_close(commit);
10294 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10295 error = histedit_skip_commit(hle, worktree, repo);
10301 error = got_object_open_as_commit(&commit, repo,
10305 parent_ids = got_object_commit_get_parent_ids(commit);
10306 pid = STAILQ_FIRST(parent_ids);
10308 error = got_worktree_histedit_merge_files(&merged_paths,
10309 worktree, fileindex, pid->id, hle->commit_id, repo,
10310 update_progress, &upa, check_cancelled, NULL);
10313 got_object_commit_close(commit);
10316 print_update_progress_stats(&upa);
10317 if (upa.conflicts > 0)
10318 rebase_status = GOT_STATUS_CONFLICT;
10320 if (rebase_status == GOT_STATUS_CONFLICT) {
10321 error = show_rebase_merge_conflict(hle->commit_id,
10325 got_worktree_rebase_pathlist_free(&merged_paths);
10329 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10331 error = got_object_id_str(&id_str, hle->commit_id);
10334 printf("Stopping histedit for amending commit %s\n",
10337 got_worktree_rebase_pathlist_free(&merged_paths);
10338 error = got_worktree_histedit_postpone(worktree,
10343 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10344 error = histedit_skip_commit(hle, worktree, repo);
10350 error = histedit_commit(&merged_paths, worktree, fileindex,
10351 tmp_branch, hle, repo);
10352 got_worktree_rebase_pathlist_free(&merged_paths);
10357 if (rebase_status == GOT_STATUS_CONFLICT) {
10358 error = got_worktree_histedit_postpone(worktree, fileindex);
10361 error = got_error_msg(GOT_ERR_CONFLICTS,
10362 "conflicts must be resolved before histedit can continue");
10364 error = histedit_complete(worktree, fileindex, tmp_branch,
10367 got_object_id_queue_free(&commits);
10368 histedit_free_list(&histedit_cmds);
10369 free(head_commit_id);
10370 free(base_commit_id);
10371 free(resume_commit_id);
10373 got_object_commit_close(commit);
10375 got_ref_close(branch);
10377 got_ref_close(tmp_branch);
10379 got_worktree_close(worktree);
10381 const struct got_error *close_err = got_repo_close(repo);
10389 usage_integrate(void)
10391 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10395 static const struct got_error *
10396 cmd_integrate(int argc, char *argv[])
10398 const struct got_error *error = NULL;
10399 struct got_repository *repo = NULL;
10400 struct got_worktree *worktree = NULL;
10401 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10402 const char *branch_arg = NULL;
10403 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10404 struct got_fileindex *fileindex = NULL;
10405 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10407 struct got_update_progress_arg upa;
10409 while ((ch = getopt(argc, argv, "")) != -1) {
10422 branch_arg = argv[0];
10424 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10425 "unveil", NULL) == -1)
10428 cwd = getcwd(NULL, 0);
10430 error = got_error_from_errno("getcwd");
10434 error = got_worktree_open(&worktree, cwd);
10436 if (error->code == GOT_ERR_NOT_WORKTREE)
10437 error = wrap_not_worktree_error(error, "integrate",
10442 error = check_rebase_or_histedit_in_progress(worktree);
10446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10451 error = apply_unveil(got_repo_get_path(repo), 0,
10452 got_worktree_get_root_path(worktree));
10456 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10457 error = got_error_from_errno("asprintf");
10461 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10462 &base_branch_ref, worktree, refname, repo);
10466 refname = strdup(got_ref_get_name(branch_ref));
10467 if (refname == NULL) {
10468 error = got_error_from_errno("strdup");
10469 got_worktree_integrate_abort(worktree, fileindex, repo,
10470 branch_ref, base_branch_ref);
10473 base_refname = strdup(got_ref_get_name(base_branch_ref));
10474 if (base_refname == NULL) {
10475 error = got_error_from_errno("strdup");
10476 got_worktree_integrate_abort(worktree, fileindex, repo,
10477 branch_ref, base_branch_ref);
10481 error = got_ref_resolve(&commit_id, repo, branch_ref);
10485 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10489 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10490 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10491 "specified branch has already been integrated");
10492 got_worktree_integrate_abort(worktree, fileindex, repo,
10493 branch_ref, base_branch_ref);
10497 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10499 if (error->code == GOT_ERR_ANCESTRY)
10500 error = got_error(GOT_ERR_REBASE_REQUIRED);
10501 got_worktree_integrate_abort(worktree, fileindex, repo,
10502 branch_ref, base_branch_ref);
10506 memset(&upa, 0, sizeof(upa));
10507 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10508 branch_ref, base_branch_ref, update_progress, &upa,
10509 check_cancelled, NULL);
10513 printf("Integrated %s into %s\n", refname, base_refname);
10514 print_update_progress_stats(&upa);
10517 const struct got_error *close_err = got_repo_close(repo);
10522 got_worktree_close(worktree);
10524 free(base_commit_id);
10527 free(base_refname);
10534 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10535 "[-S] [file-path ...]\n",
10540 static const struct got_error *
10541 print_stage(void *arg, unsigned char status, unsigned char staged_status,
10542 const char *path, struct got_object_id *blob_id,
10543 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10544 int dirfd, const char *de_name)
10546 const struct got_error *err = NULL;
10547 char *id_str = NULL;
10549 if (staged_status != GOT_STATUS_ADD &&
10550 staged_status != GOT_STATUS_MODIFY &&
10551 staged_status != GOT_STATUS_DELETE)
10554 if (staged_status == GOT_STATUS_ADD ||
10555 staged_status == GOT_STATUS_MODIFY)
10556 err = got_object_id_str(&id_str, staged_blob_id);
10558 err = got_object_id_str(&id_str, blob_id);
10562 printf("%s %c %s\n", id_str, staged_status, path);
10567 static const struct got_error *
10568 cmd_stage(int argc, char *argv[])
10570 const struct got_error *error = NULL;
10571 struct got_repository *repo = NULL;
10572 struct got_worktree *worktree = NULL;
10574 struct got_pathlist_head paths;
10575 struct got_pathlist_entry *pe;
10576 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10577 FILE *patch_script_file = NULL;
10578 const char *patch_script_path = NULL;
10579 struct choose_patch_arg cpa;
10581 TAILQ_INIT(&paths);
10583 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10592 patch_script_path = optarg;
10595 allow_bad_symlinks = 1;
10607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10608 "unveil", NULL) == -1)
10611 if (list_stage && (pflag || patch_script_path))
10612 errx(1, "-l option cannot be used with other options");
10613 if (patch_script_path && !pflag)
10614 errx(1, "-F option can only be used together with -p option");
10616 cwd = getcwd(NULL, 0);
10618 error = got_error_from_errno("getcwd");
10622 error = got_worktree_open(&worktree, cwd);
10624 if (error->code == GOT_ERR_NOT_WORKTREE)
10625 error = wrap_not_worktree_error(error, "stage", cwd);
10629 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10634 if (patch_script_path) {
10635 patch_script_file = fopen(patch_script_path, "r");
10636 if (patch_script_file == NULL) {
10637 error = got_error_from_errno2("fopen",
10638 patch_script_path);
10642 error = apply_unveil(got_repo_get_path(repo), 0,
10643 got_worktree_get_root_path(worktree));
10647 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10652 error = got_worktree_status(worktree, &paths, repo, 0,
10653 print_stage, NULL, check_cancelled, NULL);
10655 cpa.patch_script_file = patch_script_file;
10656 cpa.action = "stage";
10657 error = got_worktree_stage(worktree, &paths,
10658 pflag ? NULL : print_status, NULL,
10659 pflag ? choose_patch : NULL, &cpa,
10660 allow_bad_symlinks, repo);
10663 if (patch_script_file && fclose(patch_script_file) == EOF &&
10665 error = got_error_from_errno2("fclose", patch_script_path);
10667 const struct got_error *close_err = got_repo_close(repo);
10672 got_worktree_close(worktree);
10673 TAILQ_FOREACH(pe, &paths, entry)
10674 free((char *)pe->path);
10675 got_pathlist_free(&paths);
10681 usage_unstage(void)
10683 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10684 "[file-path ...]\n",
10690 static const struct got_error *
10691 cmd_unstage(int argc, char *argv[])
10693 const struct got_error *error = NULL;
10694 struct got_repository *repo = NULL;
10695 struct got_worktree *worktree = NULL;
10697 struct got_pathlist_head paths;
10698 struct got_pathlist_entry *pe;
10700 struct got_update_progress_arg upa;
10701 FILE *patch_script_file = NULL;
10702 const char *patch_script_path = NULL;
10703 struct choose_patch_arg cpa;
10705 TAILQ_INIT(&paths);
10707 while ((ch = getopt(argc, argv, "pF:")) != -1) {
10713 patch_script_path = optarg;
10725 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10726 "unveil", NULL) == -1)
10729 if (patch_script_path && !pflag)
10730 errx(1, "-F option can only be used together with -p option");
10732 cwd = getcwd(NULL, 0);
10734 error = got_error_from_errno("getcwd");
10738 error = got_worktree_open(&worktree, cwd);
10740 if (error->code == GOT_ERR_NOT_WORKTREE)
10741 error = wrap_not_worktree_error(error, "unstage", cwd);
10745 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10750 if (patch_script_path) {
10751 patch_script_file = fopen(patch_script_path, "r");
10752 if (patch_script_file == NULL) {
10753 error = got_error_from_errno2("fopen",
10754 patch_script_path);
10759 error = apply_unveil(got_repo_get_path(repo), 0,
10760 got_worktree_get_root_path(worktree));
10764 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10768 cpa.patch_script_file = patch_script_file;
10769 cpa.action = "unstage";
10770 memset(&upa, 0, sizeof(upa));
10771 error = got_worktree_unstage(worktree, &paths, update_progress,
10772 &upa, pflag ? choose_patch : NULL, &cpa, repo);
10774 print_update_progress_stats(&upa);
10776 if (patch_script_file && fclose(patch_script_file) == EOF &&
10778 error = got_error_from_errno2("fclose", patch_script_path);
10780 const struct got_error *close_err = got_repo_close(repo);
10785 got_worktree_close(worktree);
10786 TAILQ_FOREACH(pe, &paths, entry)
10787 free((char *)pe->path);
10788 got_pathlist_free(&paths);
10796 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10797 "arg1 [arg2 ...]\n", getprogname());
10801 static const struct got_error *
10802 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10804 const struct got_error *err;
10805 struct got_blob_object *blob;
10807 err = got_object_open_as_blob(&blob, repo, id, 8192);
10811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10812 got_object_blob_close(blob);
10816 static const struct got_error *
10817 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10819 const struct got_error *err;
10820 struct got_tree_object *tree;
10823 err = got_object_open_as_tree(&tree, repo, id);
10827 nentries = got_object_tree_get_nentries(tree);
10828 for (i = 0; i < nentries; i++) {
10829 struct got_tree_entry *te;
10831 if (sigint_received || sigpipe_received)
10833 te = got_object_tree_get_entry(tree, i);
10834 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10837 fprintf(outfile, "%s %.7o %s\n", id_str,
10838 got_tree_entry_get_mode(te),
10839 got_tree_entry_get_name(te));
10843 got_object_tree_close(tree);
10847 static const struct got_error *
10848 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10850 const struct got_error *err;
10851 struct got_commit_object *commit;
10852 const struct got_object_id_queue *parent_ids;
10853 struct got_object_qid *pid;
10854 char *id_str = NULL;
10855 const char *logmsg = NULL;
10857 err = got_object_open_as_commit(&commit, repo, id);
10861 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10865 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10866 parent_ids = got_object_commit_get_parent_ids(commit);
10867 fprintf(outfile, "numparents %d\n",
10868 got_object_commit_get_nparents(commit));
10869 STAILQ_FOREACH(pid, parent_ids, entry) {
10871 err = got_object_id_str(&pid_str, pid->id);
10874 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10877 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10878 got_object_commit_get_author(commit),
10879 (long long)got_object_commit_get_author_time(commit));
10881 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10882 got_object_commit_get_author(commit),
10883 (long long)got_object_commit_get_committer_time(commit));
10885 logmsg = got_object_commit_get_logmsg_raw(commit);
10886 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10887 fprintf(outfile, "%s", logmsg);
10890 got_object_commit_close(commit);
10894 static const struct got_error *
10895 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10897 const struct got_error *err;
10898 struct got_tag_object *tag;
10899 char *id_str = NULL;
10900 const char *tagmsg = NULL;
10902 err = got_object_open_as_tag(&tag, repo, id);
10906 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10910 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10912 switch (got_object_tag_get_object_type(tag)) {
10913 case GOT_OBJ_TYPE_BLOB:
10914 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10915 GOT_OBJ_LABEL_BLOB);
10917 case GOT_OBJ_TYPE_TREE:
10918 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10919 GOT_OBJ_LABEL_TREE);
10921 case GOT_OBJ_TYPE_COMMIT:
10922 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10923 GOT_OBJ_LABEL_COMMIT);
10925 case GOT_OBJ_TYPE_TAG:
10926 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10927 GOT_OBJ_LABEL_TAG);
10933 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10934 got_object_tag_get_name(tag));
10936 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10937 got_object_tag_get_tagger(tag),
10938 (long long)got_object_tag_get_tagger_time(tag));
10940 tagmsg = got_object_tag_get_message(tag);
10941 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10942 fprintf(outfile, "%s", tagmsg);
10945 got_object_tag_close(tag);
10949 static const struct got_error *
10950 cmd_cat(int argc, char *argv[])
10952 const struct got_error *error;
10953 struct got_repository *repo = NULL;
10954 struct got_worktree *worktree = NULL;
10955 char *cwd = NULL, *repo_path = NULL, *label = NULL;
10956 const char *commit_id_str = NULL;
10957 struct got_object_id *id = NULL, *commit_id = NULL;
10958 int ch, obj_type, i, force_path = 0;
10959 struct got_reflist_head refs;
10964 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10969 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10972 commit_id_str = optarg;
10975 repo_path = realpath(optarg, NULL);
10976 if (repo_path == NULL)
10977 return got_error_from_errno2("realpath",
10979 got_path_strip_trailing_slashes(repo_path);
10993 cwd = getcwd(NULL, 0);
10995 error = got_error_from_errno("getcwd");
10998 error = got_worktree_open(&worktree, cwd);
10999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11002 if (repo_path == NULL) {
11003 repo_path = strdup(
11004 got_worktree_get_repo_path(worktree));
11005 if (repo_path == NULL) {
11006 error = got_error_from_errno("strdup");
11012 if (repo_path == NULL) {
11013 repo_path = getcwd(NULL, 0);
11014 if (repo_path == NULL)
11015 return got_error_from_errno("getcwd");
11018 error = got_repo_open(&repo, repo_path, NULL);
11023 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11027 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11031 if (commit_id_str == NULL)
11032 commit_id_str = GOT_REF_HEAD;
11033 error = got_repo_match_object_id(&commit_id, NULL,
11034 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11038 for (i = 0; i < argc; i++) {
11040 error = got_object_id_by_path(&id, repo, commit_id,
11045 error = got_repo_match_object_id(&id, &label, argv[i],
11046 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11049 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11050 error->code != GOT_ERR_NOT_REF)
11052 error = got_object_id_by_path(&id, repo,
11053 commit_id, argv[i]);
11059 error = got_object_get_type(&obj_type, repo, id);
11063 switch (obj_type) {
11064 case GOT_OBJ_TYPE_BLOB:
11065 error = cat_blob(id, repo, stdout);
11067 case GOT_OBJ_TYPE_TREE:
11068 error = cat_tree(id, repo, stdout);
11070 case GOT_OBJ_TYPE_COMMIT:
11071 error = cat_commit(id, repo, stdout);
11073 case GOT_OBJ_TYPE_TAG:
11074 error = cat_tag(id, repo, stdout);
11077 error = got_error(GOT_ERR_OBJ_TYPE);
11092 got_worktree_close(worktree);
11094 const struct got_error *close_err = got_repo_close(repo);
11098 got_ref_list_free(&refs);
11105 fprintf(stderr, "usage: %s info [path ...]\n",
11110 static const struct got_error *
11111 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11112 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11113 struct got_object_id *commit_id)
11115 const struct got_error *err = NULL;
11116 char *id_str = NULL;
11118 struct tm mytm, *tm;
11119 struct got_pathlist_head *paths = arg;
11120 struct got_pathlist_entry *pe;
11123 * Clear error indication from any of the path arguments which
11124 * would cause this file index entry to be displayed.
11126 TAILQ_FOREACH(pe, paths, entry) {
11127 if (got_path_cmp(path, pe->path, strlen(path),
11128 pe->path_len) == 0 ||
11129 got_path_is_child(path, pe->path, pe->path_len))
11130 pe->data = NULL; /* no error */
11133 printf(GOT_COMMIT_SEP_STR);
11135 printf("symlink: %s\n", path);
11136 else if (S_ISREG(mode)) {
11137 printf("file: %s\n", path);
11138 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11139 } else if (S_ISDIR(mode))
11140 printf("directory: %s\n", path);
11142 printf("something: %s\n", path);
11144 tm = localtime_r(&mtime, &mytm);
11147 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11148 return got_error(GOT_ERR_NO_SPACE);
11149 printf("timestamp: %s\n", datebuf);
11152 err = got_object_id_str(&id_str, blob_id);
11155 printf("based on blob: %s\n", id_str);
11159 if (staged_blob_id) {
11160 err = got_object_id_str(&id_str, staged_blob_id);
11163 printf("based on staged blob: %s\n", id_str);
11168 err = got_object_id_str(&id_str, commit_id);
11171 printf("based on commit: %s\n", id_str);
11178 static const struct got_error *
11179 cmd_info(int argc, char *argv[])
11181 const struct got_error *error = NULL;
11182 struct got_worktree *worktree = NULL;
11183 char *cwd = NULL, *id_str = NULL;
11184 struct got_pathlist_head paths;
11185 struct got_pathlist_entry *pe;
11186 char *uuidstr = NULL;
11187 int ch, show_files = 0;
11189 TAILQ_INIT(&paths);
11191 while ((ch = getopt(argc, argv, "")) != -1) {
11203 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11207 cwd = getcwd(NULL, 0);
11209 error = got_error_from_errno("getcwd");
11213 error = got_worktree_open(&worktree, cwd);
11215 if (error->code == GOT_ERR_NOT_WORKTREE)
11216 error = wrap_not_worktree_error(error, "info", cwd);
11220 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11225 error = get_worktree_paths_from_argv(&paths, argc, argv,
11232 error = got_object_id_str(&id_str,
11233 got_worktree_get_base_commit_id(worktree));
11237 error = got_worktree_get_uuid(&uuidstr, worktree);
11241 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11242 printf("work tree base commit: %s\n", id_str);
11243 printf("work tree path prefix: %s\n",
11244 got_worktree_get_path_prefix(worktree));
11245 printf("work tree branch reference: %s\n",
11246 got_worktree_get_head_ref_name(worktree));
11247 printf("work tree UUID: %s\n", uuidstr);
11248 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11251 struct got_pathlist_entry *pe;
11252 TAILQ_FOREACH(pe, &paths, entry) {
11253 if (pe->path_len == 0)
11256 * Assume this path will fail. This will be corrected
11257 * in print_path_info() in case the path does suceeed.
11259 pe->data = (void *)got_error_path(pe->path,
11262 error = got_worktree_path_info(worktree, &paths,
11263 print_path_info, &paths, check_cancelled, NULL);
11266 TAILQ_FOREACH(pe, &paths, entry) {
11267 if (pe->data != NULL) {
11268 error = pe->data; /* bad path */
11274 TAILQ_FOREACH(pe, &paths, entry)
11275 free((char *)pe->path);
11276 got_pathlist_free(&paths);