2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
63 catch_sigint(int signo)
69 catch_sigpipe(int signo)
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
174 main(int argc, char *argv[])
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
206 got_version_print_str();
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
265 editor = getenv("VISUAL");
267 editor = getenv("EDITOR");
270 err = got_path_find_prog(abspath, editor);
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
301 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
303 err = got_privsep_unveil_exec_helpers();
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
327 while ((ch = getopt(argc, argv, "")) != -1) {
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
356 error = apply_unveil(repo_path, 0, NULL);
360 error = got_repo_init(repo_path);
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
375 spawn_editor(const char *editor, const char *file)
378 sig_t sighup, sigint, sigquit;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
389 execl(editor, editor, file, (char *)NULL);
393 while (waitpid(pid, &st, 0) == -1)
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
418 int content_changed = 0;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
438 return got_error_from_errno("malloc");
442 fp = fopen(logmsg_path, "r");
444 err = got_error_from_errno("fopen");
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd,
486 GOT_TMPDIR_STR "/got-importmsg");
490 dprintf(fd, initial_content);
493 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
495 free(initial_content);
499 static const struct got_error *
500 import_progress(void *arg, const char *path)
502 printf("A %s\n", path);
506 static const struct got_error *
507 get_author(char **author, struct got_repository *repo)
509 const struct got_error *err = NULL;
510 const char *got_author, *name, *email;
514 name = got_repo_get_gitconfig_author_name(repo);
515 email = got_repo_get_gitconfig_author_email(repo);
517 if (asprintf(author, "%s <%s>", name, email) == -1)
518 return got_error_from_errno("asprintf");
522 got_author = getenv("GOT_AUTHOR");
523 if (got_author == NULL) {
524 name = got_repo_get_global_gitconfig_author_name(repo);
525 email = got_repo_get_global_gitconfig_author_email(repo);
527 if (asprintf(author, "%s <%s>", name, email) == -1)
528 return got_error_from_errno("asprintf");
531 /* TODO: Look up user in password database? */
532 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
535 *author = strdup(got_author);
537 return got_error_from_errno("strdup");
540 * Really dumb email address check; we're only doing this to
541 * avoid git's object parser breaking on commits we create.
543 while (*got_author && *got_author != '<')
545 if (*got_author != '<') {
546 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 while (*got_author && *got_author != '@')
551 if (*got_author != '@') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 while (*got_author && *got_author != '>')
557 if (*got_author != '>')
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
567 static const struct got_error *
568 get_gitconfig_path(char **gitconfig_path)
570 const char *homedir = getenv("HOME");
572 *gitconfig_path = NULL;
574 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
575 return got_error_from_errno("asprintf");
581 static const struct got_error *
582 cmd_import(int argc, char *argv[])
584 const struct got_error *error = NULL;
585 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
586 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
587 const char *branch_name = "main";
588 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
589 struct got_repository *repo = NULL;
590 struct got_reference *branch_ref = NULL, *head_ref = NULL;
591 struct got_object_id *new_commit_id = NULL;
593 struct got_pathlist_head ignores;
594 struct got_pathlist_entry *pe;
595 int preserve_logmsg = 0;
597 TAILQ_INIT(&ignores);
599 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
602 branch_name = optarg;
605 logmsg = strdup(optarg);
606 if (logmsg == NULL) {
607 error = got_error_from_errno("strdup");
612 repo_path = realpath(optarg, NULL);
613 if (repo_path == NULL) {
614 error = got_error_from_errno2("realpath",
620 if (optarg[0] == '\0')
622 error = got_pathlist_insert(&pe, &ignores, optarg,
637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
645 if (repo_path == NULL) {
646 repo_path = getcwd(NULL, 0);
647 if (repo_path == NULL)
648 return got_error_from_errno("getcwd");
650 got_path_strip_trailing_slashes(repo_path);
651 error = get_gitconfig_path(&gitconfig_path);
654 error = got_repo_open(&repo, repo_path, gitconfig_path);
658 error = get_author(&author, repo);
663 * Don't let the user create a branch name with a leading '-'.
664 * While technically a valid reference name, this case is usually
665 * an unintended typo.
667 if (branch_name[0] == '-')
668 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
670 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
671 error = got_error_from_errno("asprintf");
675 error = got_ref_open(&branch_ref, repo, refname, 0);
677 if (error->code != GOT_ERR_NOT_REF)
680 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
681 "import target branch already exists");
685 path_dir = realpath(argv[0], NULL);
686 if (path_dir == NULL) {
687 error = got_error_from_errno2("realpath", argv[0]);
690 got_path_strip_trailing_slashes(path_dir);
693 * unveil(2) traverses exec(2); if an editor is used we have
694 * to apply unveil after the log message has been written.
696 if (logmsg == NULL || strlen(logmsg) == 0) {
697 error = get_editor(&editor);
701 error = collect_import_msg(&logmsg, &logmsg_path, editor,
704 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 if (unveil(path_dir, "r") != 0) {
712 error = got_error_from_errno2("unveil", path_dir);
718 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 error = got_repo_import(&new_commit_id, path_dir, logmsg,
726 author, &ignores, repo, import_progress, NULL);
733 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 error = got_ref_write(branch_ref, repo);
747 error = got_object_id_str(&id_str, new_commit_id);
754 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
756 if (error->code != GOT_ERR_NOT_REF) {
762 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
770 error = got_ref_write(head_ref, repo);
778 printf("Created branch %s with commit %s\n",
779 got_ref_get_name(branch_ref), id_str);
781 if (preserve_logmsg) {
782 fprintf(stderr, "%s: log message preserved in %s\n",
783 getprogname(), logmsg_path);
784 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
785 error = got_error_from_errno2("unlink", logmsg_path);
794 free(gitconfig_path);
796 got_ref_close(branch_ref);
798 got_ref_close(head_ref);
805 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
806 "[-p prefix] repository-path [worktree-path]\n", getprogname());
811 show_worktree_base_ref_warning(void)
813 fprintf(stderr, "%s: warning: could not create a reference "
814 "to the work tree's base commit; the commit could be "
815 "garbage-collected by Git; making the repository "
816 "writable and running 'got update' will prevent this\n",
820 struct got_checkout_progress_arg {
821 const char *worktree_path;
822 int had_base_commit_ref_error;
825 static const struct got_error *
826 checkout_progress(void *arg, unsigned char status, const char *path)
828 struct got_checkout_progress_arg *a = arg;
830 /* Base commit bump happens silently. */
831 if (status == GOT_STATUS_BUMP_BASE)
834 if (status == GOT_STATUS_BASE_REF_ERR) {
835 a->had_base_commit_ref_error = 1;
839 while (path[0] == '/')
842 printf("%c %s/%s\n", status, a->worktree_path, path);
846 static const struct got_error *
847 check_cancelled(void *arg)
849 if (sigint_received || sigpipe_received)
850 return got_error(GOT_ERR_CANCELLED);
854 static const struct got_error *
855 check_linear_ancestry(struct got_object_id *commit_id,
856 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
857 struct got_repository *repo)
859 const struct got_error *err = NULL;
860 struct got_object_id *yca_id;
862 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
863 commit_id, base_commit_id, repo, check_cancelled, NULL);
868 return got_error(GOT_ERR_ANCESTRY);
871 * Require a straight line of history between the target commit
872 * and the work tree's base commit.
874 * Non-linear situations such as this require a rebase:
876 * (commit) D F (base_commit)
884 * 'got update' only handles linear cases:
885 * Update forwards in time: A (base/yca) - B - C - D (commit)
886 * Update backwards in time: D (base) - C - B - A (commit/yca)
888 if (allow_forwards_in_time_only) {
889 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
891 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
892 got_object_id_cmp(base_commit_id, yca_id) != 0)
893 return got_error(GOT_ERR_ANCESTRY);
899 static const struct got_error *
900 check_same_branch(struct got_object_id *commit_id,
901 struct got_reference *head_ref, struct got_object_id *yca_id,
902 struct got_repository *repo)
904 const struct got_error *err = NULL;
905 struct got_commit_graph *graph = NULL;
906 struct got_object_id *head_commit_id = NULL;
907 int is_same_branch = 0;
909 err = got_ref_resolve(&head_commit_id, repo, head_ref);
913 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
917 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
922 err = got_commit_graph_open(&graph, "/", 1);
926 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
927 check_cancelled, NULL);
932 struct got_object_id *id;
933 err = got_commit_graph_iter_next(&id, graph, repo,
934 check_cancelled, NULL);
936 if (err->code == GOT_ERR_ITER_COMPLETED)
942 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
944 if (got_object_id_cmp(id, commit_id) == 0) {
952 got_commit_graph_close(graph);
953 free(head_commit_id);
954 if (!err && !is_same_branch)
955 err = got_error(GOT_ERR_ANCESTRY);
959 static const struct got_error *
960 cmd_checkout(int argc, char *argv[])
962 const struct got_error *error = NULL;
963 struct got_repository *repo = NULL;
964 struct got_reference *head_ref = NULL;
965 struct got_worktree *worktree = NULL;
966 char *repo_path = NULL;
967 char *worktree_path = NULL;
968 const char *path_prefix = "";
969 const char *branch_name = GOT_REF_HEAD;
970 char *commit_id_str = NULL;
971 int ch, same_path_prefix, allow_nonempty = 0;
972 struct got_pathlist_head paths;
973 struct got_checkout_progress_arg cpa;
977 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
980 branch_name = optarg;
983 commit_id_str = strdup(optarg);
984 if (commit_id_str == NULL)
985 return got_error_from_errno("strdup");
991 path_prefix = optarg;
1003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1004 "unveil", NULL) == -1)
1008 char *cwd, *base, *dotgit;
1009 repo_path = realpath(argv[0], NULL);
1010 if (repo_path == NULL)
1011 return got_error_from_errno2("realpath", argv[0]);
1012 cwd = getcwd(NULL, 0);
1014 error = got_error_from_errno("getcwd");
1017 if (path_prefix[0]) {
1018 base = basename(path_prefix);
1020 error = got_error_from_errno2("basename",
1025 base = basename(repo_path);
1027 error = got_error_from_errno2("basename",
1032 dotgit = strstr(base, ".git");
1035 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1036 error = got_error_from_errno("asprintf");
1041 } else if (argc == 2) {
1042 repo_path = realpath(argv[0], NULL);
1043 if (repo_path == NULL) {
1044 error = got_error_from_errno2("realpath", argv[0]);
1047 worktree_path = realpath(argv[1], NULL);
1048 if (worktree_path == NULL) {
1049 if (errno != ENOENT) {
1050 error = got_error_from_errno2("realpath",
1054 worktree_path = strdup(argv[1]);
1055 if (worktree_path == NULL) {
1056 error = got_error_from_errno("strdup");
1063 got_path_strip_trailing_slashes(repo_path);
1064 got_path_strip_trailing_slashes(worktree_path);
1066 error = got_repo_open(&repo, repo_path, NULL);
1070 /* Pre-create work tree path for unveil(2) */
1071 error = got_path_mkdir(worktree_path);
1073 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1074 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1076 if (!allow_nonempty &&
1077 !got_path_dir_is_empty(worktree_path)) {
1078 error = got_error_path(worktree_path,
1079 GOT_ERR_DIR_NOT_EMPTY);
1084 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1088 error = got_ref_open(&head_ref, repo, branch_name, 0);
1092 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1096 error = got_worktree_open(&worktree, worktree_path);
1100 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1104 if (!same_path_prefix) {
1105 error = got_error(GOT_ERR_PATH_PREFIX);
1109 if (commit_id_str) {
1110 struct got_object_id *commit_id;
1111 error = got_repo_match_object_id(&commit_id, NULL,
1112 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1115 error = check_linear_ancestry(commit_id,
1116 got_worktree_get_base_commit_id(worktree), 0, repo);
1117 if (error != NULL) {
1121 error = check_same_branch(commit_id, head_ref, NULL, repo);
1124 error = got_worktree_set_base_commit_id(worktree, repo,
1131 error = got_pathlist_append(&paths, "", NULL);
1134 cpa.worktree_path = worktree_path;
1135 cpa.had_base_commit_ref_error = 0;
1136 error = got_worktree_checkout_files(worktree, &paths, repo,
1137 checkout_progress, &cpa, check_cancelled, NULL);
1141 printf("Now shut up and hack\n");
1142 if (cpa.had_base_commit_ref_error)
1143 show_worktree_base_ref_warning();
1145 got_pathlist_free(&paths);
1146 free(commit_id_str);
1148 free(worktree_path);
1155 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1160 static const struct got_error *
1161 update_progress(void *arg, unsigned char status, const char *path)
1163 int *did_something = arg;
1165 if (status == GOT_STATUS_EXISTS ||
1166 status == GOT_STATUS_BASE_REF_ERR)
1171 /* Base commit bump happens silently. */
1172 if (status == GOT_STATUS_BUMP_BASE)
1175 while (path[0] == '/')
1177 printf("%c %s\n", status, path);
1181 static const struct got_error *
1182 switch_head_ref(struct got_reference *head_ref,
1183 struct got_object_id *commit_id, struct got_worktree *worktree,
1184 struct got_repository *repo)
1186 const struct got_error *err = NULL;
1188 int ref_has_moved = 0;
1190 /* Trivial case: switching between two different references. */
1191 if (strcmp(got_ref_get_name(head_ref),
1192 got_worktree_get_head_ref_name(worktree)) != 0) {
1193 printf("Switching work tree from %s to %s\n",
1194 got_worktree_get_head_ref_name(worktree),
1195 got_ref_get_name(head_ref));
1196 return got_worktree_set_head_ref(worktree, head_ref);
1199 err = check_linear_ancestry(commit_id,
1200 got_worktree_get_base_commit_id(worktree), 0, repo);
1202 if (err->code != GOT_ERR_ANCESTRY)
1209 /* Switching to a rebased branch with the same reference name. */
1210 err = got_object_id_str(&base_id_str,
1211 got_worktree_get_base_commit_id(worktree));
1214 printf("Reference %s now points at a different branch\n",
1215 got_worktree_get_head_ref_name(worktree));
1216 printf("Switching work tree from %s to %s\n", base_id_str,
1217 got_worktree_get_head_ref_name(worktree));
1221 static const struct got_error *
1222 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1224 const struct got_error *err;
1227 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1231 return got_error(GOT_ERR_REBASING);
1233 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1237 return got_error(GOT_ERR_HISTEDIT_BUSY);
1242 static const struct got_error *
1243 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1244 char *argv[], struct got_worktree *worktree)
1246 const struct got_error *err = NULL;
1253 return got_error_from_errno("strdup");
1254 return got_pathlist_append(paths, path, NULL);
1257 for (i = 0; i < argc; i++) {
1258 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1261 err = got_pathlist_append(paths, path, NULL);
1271 static const struct got_error *
1272 cmd_update(int argc, char *argv[])
1274 const struct got_error *error = NULL;
1275 struct got_repository *repo = NULL;
1276 struct got_worktree *worktree = NULL;
1277 char *worktree_path = NULL;
1278 struct got_object_id *commit_id = NULL;
1279 char *commit_id_str = NULL;
1280 const char *branch_name = NULL;
1281 struct got_reference *head_ref = NULL;
1282 struct got_pathlist_head paths;
1283 struct got_pathlist_entry *pe;
1284 int ch, did_something = 0;
1288 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1291 branch_name = optarg;
1294 commit_id_str = strdup(optarg);
1295 if (commit_id_str == NULL)
1296 return got_error_from_errno("strdup");
1308 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1309 "unveil", NULL) == -1)
1312 worktree_path = getcwd(NULL, 0);
1313 if (worktree_path == NULL) {
1314 error = got_error_from_errno("getcwd");
1317 error = got_worktree_open(&worktree, worktree_path);
1321 error = check_rebase_or_histedit_in_progress(worktree);
1325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 error = apply_unveil(got_repo_get_path(repo), 0,
1331 got_worktree_get_root_path(worktree));
1335 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1339 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1340 got_worktree_get_head_ref_name(worktree), 0);
1343 if (commit_id_str == NULL) {
1344 error = got_ref_resolve(&commit_id, repo, head_ref);
1347 error = got_object_id_str(&commit_id_str, commit_id);
1351 error = got_repo_match_object_id(&commit_id, NULL,
1352 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1353 free(commit_id_str);
1354 commit_id_str = NULL;
1357 error = got_object_id_str(&commit_id_str, commit_id);
1363 struct got_object_id *head_commit_id;
1364 TAILQ_FOREACH(pe, &paths, entry) {
1365 if (pe->path_len == 0)
1367 error = got_error_msg(GOT_ERR_BAD_PATH,
1368 "switching between branches requires that "
1369 "the entire work tree gets updated");
1372 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1375 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1377 free(head_commit_id);
1380 error = check_same_branch(commit_id, head_ref, NULL, repo);
1383 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1387 error = check_linear_ancestry(commit_id,
1388 got_worktree_get_base_commit_id(worktree), 0, repo);
1389 if (error != NULL) {
1390 if (error->code == GOT_ERR_ANCESTRY)
1391 error = got_error(GOT_ERR_BRANCH_MOVED);
1394 error = check_same_branch(commit_id, head_ref, NULL, repo);
1399 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1401 error = got_worktree_set_base_commit_id(worktree, repo,
1407 error = got_worktree_checkout_files(worktree, &paths, repo,
1408 update_progress, &did_something, check_cancelled, NULL);
1413 printf("Updated to commit %s\n", commit_id_str);
1415 printf("Already up-to-date\n");
1417 free(worktree_path);
1418 TAILQ_FOREACH(pe, &paths, entry)
1419 free((char *)pe->path);
1420 got_pathlist_free(&paths);
1422 free(commit_id_str);
1426 static const struct got_error *
1427 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1428 const char *path, int diff_context, int ignore_whitespace,
1429 struct got_repository *repo)
1431 const struct got_error *err = NULL;
1432 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1435 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1440 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 while (path[0] == '/')
1446 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1447 ignore_whitespace, stdout);
1450 got_object_blob_close(blob1);
1451 got_object_blob_close(blob2);
1455 static const struct got_error *
1456 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1457 const char *path, int diff_context, int ignore_whitespace,
1458 struct got_repository *repo)
1460 const struct got_error *err = NULL;
1461 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1462 struct got_diff_blob_output_unidiff_arg arg;
1465 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1470 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 arg.diff_context = diff_context;
1475 arg.ignore_whitespace = ignore_whitespace;
1476 arg.outfile = stdout;
1477 while (path[0] == '/')
1479 err = got_diff_tree(tree1, tree2, path, path, repo,
1480 got_diff_blob_output_unidiff, &arg, 1);
1483 got_object_tree_close(tree1);
1485 got_object_tree_close(tree2);
1489 static const struct got_error *
1490 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1491 const char *path, int diff_context, struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 struct got_commit_object *pcommit = NULL;
1495 char *id_str1 = NULL, *id_str2 = NULL;
1496 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1497 struct got_object_qid *qid;
1499 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1501 err = got_object_open_as_commit(&pcommit, repo,
1507 if (path && path[0] != '\0') {
1509 err = got_object_id_by_path(&obj_id2, repo, id, path);
1512 err = got_object_id_str(&id_str2, obj_id2);
1518 err = got_object_id_by_path(&obj_id1, repo,
1524 err = got_object_id_str(&id_str1, obj_id1);
1530 err = got_object_get_type(&obj_type, repo, obj_id2);
1535 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1537 case GOT_OBJ_TYPE_BLOB:
1538 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1541 case GOT_OBJ_TYPE_TREE:
1542 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1546 err = got_error(GOT_ERR_OBJ_TYPE);
1552 obj_id2 = got_object_commit_get_tree_id(commit);
1553 err = got_object_id_str(&id_str2, obj_id2);
1556 obj_id1 = got_object_commit_get_tree_id(pcommit);
1557 err = got_object_id_str(&id_str1, obj_id1);
1560 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1567 got_object_commit_close(pcommit);
1572 get_datestr(time_t *time, char *datebuf)
1574 struct tm mytm, *tm;
1577 tm = gmtime_r(time, &mytm);
1580 s = asctime_r(tm, datebuf);
1583 p = strchr(s, '\n');
1589 static const struct got_error *
1590 match_logmsg(int *have_match, struct got_object_id *id,
1591 struct got_commit_object *commit, regex_t *regex)
1593 const struct got_error *err = NULL;
1594 regmatch_t regmatch;
1595 char *id_str = NULL, *logmsg = NULL;
1599 err = got_object_id_str(&id_str, id);
1603 err = got_object_commit_get_logmsg(&logmsg, commit);
1607 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1615 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1617 static const struct got_error *
1618 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1619 struct got_repository *repo, const char *path, int show_patch,
1620 int diff_context, struct got_reflist_head *refs)
1622 const struct got_error *err = NULL;
1623 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1625 time_t committer_time;
1626 const char *author, *committer;
1627 char *refs_str = NULL;
1628 struct got_reflist_entry *re;
1630 SIMPLEQ_FOREACH(re, refs, entry) {
1633 struct got_tag_object *tag = NULL;
1636 name = got_ref_get_name(re->ref);
1637 if (strcmp(name, GOT_REF_HEAD) == 0)
1639 if (strncmp(name, "refs/", 5) == 0)
1641 if (strncmp(name, "got/", 4) == 0)
1643 if (strncmp(name, "heads/", 6) == 0)
1645 if (strncmp(name, "remotes/", 8) == 0)
1647 if (strncmp(name, "tags/", 5) == 0) {
1648 err = got_object_open_as_tag(&tag, repo, re->id);
1650 if (err->code != GOT_ERR_OBJ_TYPE)
1652 /* Ref points at something other than a tag. */
1657 cmp = got_object_id_cmp(tag ?
1658 got_object_tag_get_object_id(tag) : re->id, id);
1660 got_object_tag_close(tag);
1664 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1666 err = got_error_from_errno("asprintf");
1672 err = got_object_id_str(&id_str, id);
1676 printf(GOT_COMMIT_SEP_STR);
1677 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1678 refs_str ? refs_str : "", refs_str ? ")" : "");
1683 printf("from: %s\n", got_object_commit_get_author(commit));
1684 committer_time = got_object_commit_get_committer_time(commit);
1685 datestr = get_datestr(&committer_time, datebuf);
1687 printf("date: %s UTC\n", datestr);
1688 author = got_object_commit_get_author(commit);
1689 committer = got_object_commit_get_committer(commit);
1690 if (strcmp(author, committer) != 0)
1691 printf("via: %s\n", committer);
1692 if (got_object_commit_get_nparents(commit) > 1) {
1693 const struct got_object_id_queue *parent_ids;
1694 struct got_object_qid *qid;
1696 parent_ids = got_object_commit_get_parent_ids(commit);
1697 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1698 err = got_object_id_str(&id_str, qid->id);
1701 printf("parent %d: %s\n", n++, id_str);
1706 err = got_object_commit_get_logmsg(&logmsg0, commit);
1712 line = strsep(&logmsg, "\n");
1714 printf(" %s\n", line);
1719 err = print_patch(commit, id, path, diff_context, repo);
1724 if (fflush(stdout) != 0 && err == NULL)
1725 err = got_error_from_errno("fflush");
1729 static const struct got_error *
1730 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1731 const char *path, int show_patch, const char *search_pattern,
1732 int diff_context, int limit, int log_branches,
1733 struct got_reflist_head *refs)
1735 const struct got_error *err;
1736 struct got_commit_graph *graph;
1740 if (search_pattern &&
1741 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1742 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1744 err = got_commit_graph_open(&graph, path, !log_branches);
1747 err = got_commit_graph_iter_start(graph, root_id, repo,
1748 check_cancelled, NULL);
1752 struct got_commit_object *commit;
1753 struct got_object_id *id;
1755 if (sigint_received || sigpipe_received)
1758 err = got_commit_graph_iter_next(&id, graph, repo,
1759 check_cancelled, NULL);
1761 if (err->code == GOT_ERR_ITER_COMPLETED)
1768 err = got_object_open_as_commit(&commit, repo, id);
1772 if (search_pattern) {
1773 err = match_logmsg(&have_match, id, commit, ®ex);
1775 got_object_commit_close(commit);
1778 if (have_match == 0) {
1779 got_object_commit_close(commit);
1784 err = print_commit(commit, id, repo, path, show_patch,
1785 diff_context, refs);
1786 got_object_commit_close(commit);
1787 if (err || (limit && --limit == 0))
1793 got_commit_graph_close(graph);
1800 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1801 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1806 get_default_log_limit(void)
1808 const char *got_default_log_limit;
1812 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1813 if (got_default_log_limit == NULL)
1815 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1821 static const struct got_error *
1822 cmd_log(int argc, char *argv[])
1824 const struct got_error *error;
1825 struct got_repository *repo = NULL;
1826 struct got_worktree *worktree = NULL;
1827 struct got_commit_object *commit = NULL;
1828 struct got_object_id *id = NULL;
1829 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1830 const char *start_commit = NULL, *search_pattern = NULL;
1831 int diff_context = -1, ch;
1832 int show_patch = 0, limit = 0, log_branches = 0;
1834 struct got_reflist_head refs;
1836 SIMPLEQ_INIT(&refs);
1839 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1845 limit = get_default_log_limit();
1847 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1853 start_commit = optarg;
1856 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1859 err(1, "-C option %s", errstr);
1862 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1864 err(1, "-l option %s", errstr);
1870 repo_path = realpath(optarg, NULL);
1871 if (repo_path == NULL)
1872 return got_error_from_errno2("realpath",
1874 got_path_strip_trailing_slashes(repo_path);
1877 search_pattern = optarg;
1888 if (diff_context == -1)
1890 else if (!show_patch)
1891 errx(1, "-C reguires -p");
1893 cwd = getcwd(NULL, 0);
1895 error = got_error_from_errno("getcwd");
1899 error = got_worktree_open(&worktree, cwd);
1900 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1907 error = got_error_from_errno("strdup");
1910 } else if (argc == 1) {
1912 error = got_worktree_resolve_path(&path, worktree,
1917 path = strdup(argv[0]);
1919 error = got_error_from_errno("strdup");
1926 if (repo_path == NULL) {
1927 repo_path = worktree ?
1928 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1930 if (repo_path == NULL) {
1931 error = got_error_from_errno("strdup");
1935 error = got_repo_open(&repo, repo_path, NULL);
1939 error = apply_unveil(got_repo_get_path(repo), 1,
1940 worktree ? got_worktree_get_root_path(worktree) : NULL);
1944 if (start_commit == NULL) {
1945 struct got_reference *head_ref;
1946 error = got_ref_open(&head_ref, repo,
1947 worktree ? got_worktree_get_head_ref_name(worktree)
1951 error = got_ref_resolve(&id, repo, head_ref);
1952 got_ref_close(head_ref);
1955 error = got_object_open_as_commit(&commit, repo, id);
1957 struct got_reference *ref;
1958 error = got_ref_open(&ref, repo, start_commit, 0);
1959 if (error == NULL) {
1961 error = got_ref_resolve(&id, repo, ref);
1965 error = got_object_get_type(&obj_type, repo, id);
1968 if (obj_type == GOT_OBJ_TYPE_TAG) {
1969 struct got_tag_object *tag;
1970 error = got_object_open_as_tag(&tag, repo, id);
1973 if (got_object_tag_get_object_type(tag) !=
1974 GOT_OBJ_TYPE_COMMIT) {
1975 got_object_tag_close(tag);
1976 error = got_error(GOT_ERR_OBJ_TYPE);
1980 id = got_object_id_dup(
1981 got_object_tag_get_object_id(tag));
1983 error = got_error_from_errno(
1984 "got_object_id_dup");
1985 got_object_tag_close(tag);
1988 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1989 error = got_error(GOT_ERR_OBJ_TYPE);
1992 error = got_object_open_as_commit(&commit, repo, id);
1996 if (commit == NULL) {
1997 error = got_repo_match_object_id_prefix(&id,
1998 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2007 const char *prefix = got_worktree_get_path_prefix(worktree);
2009 if (asprintf(&p, "%s%s%s", prefix,
2010 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2011 error = got_error_from_errno("asprintf");
2014 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2017 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2022 path = in_repo_path;
2025 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2029 error = print_commits(id, repo, path, show_patch, search_pattern,
2030 diff_context, limit, log_branches, &refs);
2037 got_worktree_close(worktree);
2039 const struct got_error *repo_error;
2040 repo_error = got_repo_close(repo);
2044 got_ref_list_free(&refs);
2051 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2052 "[-w] [object1 object2 | path]\n", getprogname());
2056 struct print_diff_arg {
2057 struct got_repository *repo;
2058 struct got_worktree *worktree;
2063 int ignore_whitespace;
2066 static const struct got_error *
2067 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2068 const char *path, struct got_object_id *blob_id,
2069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2070 int dirfd, const char *de_name)
2072 struct print_diff_arg *a = arg;
2073 const struct got_error *err = NULL;
2074 struct got_blob_object *blob1 = NULL;
2077 char *abspath = NULL, *label1 = NULL;
2080 if (a->diff_staged) {
2081 if (staged_status != GOT_STATUS_MODIFY &&
2082 staged_status != GOT_STATUS_ADD &&
2083 staged_status != GOT_STATUS_DELETE)
2086 if (staged_status == GOT_STATUS_DELETE)
2088 if (status == GOT_STATUS_NONEXISTENT)
2089 return got_error_set_errno(ENOENT, path);
2090 if (status != GOT_STATUS_MODIFY &&
2091 status != GOT_STATUS_ADD &&
2092 status != GOT_STATUS_DELETE &&
2093 status != GOT_STATUS_CONFLICT)
2097 if (!a->header_shown) {
2098 printf("diff %s %s%s\n", a->id_str,
2099 got_worktree_get_root_path(a->worktree),
2100 a->diff_staged ? " (staged changes)" : "");
2101 a->header_shown = 1;
2104 if (a->diff_staged) {
2105 const char *label1 = NULL, *label2 = NULL;
2106 switch (staged_status) {
2107 case GOT_STATUS_MODIFY:
2111 case GOT_STATUS_ADD:
2114 case GOT_STATUS_DELETE:
2118 return got_error(GOT_ERR_FILE_STATUS);
2120 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2121 label1, label2, a->diff_context, a->ignore_whitespace,
2125 if (staged_status == GOT_STATUS_ADD ||
2126 staged_status == GOT_STATUS_MODIFY) {
2128 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2132 err = got_object_id_str(&id_str, staged_blob_id);
2135 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2136 err = got_error_from_errno("asprintf");
2141 } else if (status != GOT_STATUS_ADD) {
2142 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2147 if (status != GOT_STATUS_DELETE) {
2148 if (asprintf(&abspath, "%s/%s",
2149 got_worktree_get_root_path(a->worktree), path) == -1) {
2150 err = got_error_from_errno("asprintf");
2155 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2157 err = got_error_from_errno2("openat", abspath);
2161 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2163 err = got_error_from_errno2("open", abspath);
2167 if (fstat(fd, &sb) == -1) {
2168 err = got_error_from_errno2("fstat", abspath);
2171 f2 = fdopen(fd, "r");
2173 err = got_error_from_errno2("fdopen", abspath);
2180 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2181 a->diff_context, a->ignore_whitespace, stdout);
2184 got_object_blob_close(blob1);
2185 if (f2 && fclose(f2) == EOF && err == NULL)
2186 err = got_error_from_errno("fclose");
2187 if (fd != -1 && close(fd) == -1 && err == NULL)
2188 err = got_error_from_errno("close");
2193 static const struct got_error *
2194 cmd_diff(int argc, char *argv[])
2196 const struct got_error *error;
2197 struct got_repository *repo = NULL;
2198 struct got_worktree *worktree = NULL;
2199 char *cwd = NULL, *repo_path = NULL;
2200 struct got_object_id *id1 = NULL, *id2 = NULL;
2201 const char *id_str1 = NULL, *id_str2 = NULL;
2202 char *label1 = NULL, *label2 = NULL;
2204 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2209 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2214 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2217 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2220 err(1, "-C option %s", errstr);
2223 repo_path = realpath(optarg, NULL);
2224 if (repo_path == NULL)
2225 return got_error_from_errno2("realpath",
2227 got_path_strip_trailing_slashes(repo_path);
2233 ignore_whitespace = 1;
2244 cwd = getcwd(NULL, 0);
2246 error = got_error_from_errno("getcwd");
2249 error = got_worktree_open(&worktree, cwd);
2250 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2253 if (worktree == NULL) {
2254 error = got_error(GOT_ERR_NOT_WORKTREE);
2259 "-r option can't be used when diffing a work tree");
2260 repo_path = strdup(got_worktree_get_repo_path(worktree));
2261 if (repo_path == NULL) {
2262 error = got_error_from_errno("strdup");
2266 error = got_worktree_resolve_path(&path, worktree,
2273 error = got_error_from_errno("strdup");
2277 } else if (argc == 2) {
2279 errx(1, "-s option can't be used when diffing "
2280 "objects in repository");
2283 if (worktree && repo_path == NULL) {
2285 strdup(got_worktree_get_repo_path(worktree));
2286 if (repo_path == NULL) {
2287 error = got_error_from_errno("strdup");
2294 if (repo_path == NULL) {
2295 repo_path = getcwd(NULL, 0);
2296 if (repo_path == NULL)
2297 return got_error_from_errno("getcwd");
2300 error = got_repo_open(&repo, repo_path, NULL);
2305 error = apply_unveil(got_repo_get_path(repo), 1,
2306 worktree ? got_worktree_get_root_path(worktree) : NULL);
2311 struct print_diff_arg arg;
2312 struct got_pathlist_head paths;
2317 error = got_object_id_str(&id_str,
2318 got_worktree_get_base_commit_id(worktree));
2322 arg.worktree = worktree;
2323 arg.diff_context = diff_context;
2324 arg.id_str = id_str;
2325 arg.header_shown = 0;
2326 arg.diff_staged = diff_staged;
2327 arg.ignore_whitespace = ignore_whitespace;
2329 error = got_pathlist_append(&paths, path, NULL);
2333 error = got_worktree_status(worktree, &paths, repo, print_diff,
2334 &arg, check_cancelled, NULL);
2336 got_pathlist_free(&paths);
2340 error = got_repo_match_object_id(&id1, &label1, id_str1,
2341 GOT_OBJ_TYPE_ANY, 1, repo);
2345 error = got_repo_match_object_id(&id2, &label2, id_str2,
2346 GOT_OBJ_TYPE_ANY, 1, repo);
2350 error = got_object_get_type(&type1, repo, id1);
2354 error = got_object_get_type(&type2, repo, id2);
2358 if (type1 != type2) {
2359 error = got_error(GOT_ERR_OBJ_TYPE);
2364 case GOT_OBJ_TYPE_BLOB:
2365 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2366 diff_context, ignore_whitespace, repo, stdout);
2368 case GOT_OBJ_TYPE_TREE:
2369 error = got_diff_objects_as_trees(id1, id2, "", "",
2370 diff_context, ignore_whitespace, repo, stdout);
2372 case GOT_OBJ_TYPE_COMMIT:
2373 printf("diff %s %s\n", label1, label2);
2374 error = got_diff_objects_as_commits(id1, id2, diff_context,
2375 ignore_whitespace, repo, stdout);
2378 error = got_error(GOT_ERR_OBJ_TYPE);
2387 got_worktree_close(worktree);
2389 const struct got_error *repo_error;
2390 repo_error = got_repo_close(repo);
2401 "usage: %s blame [-c commit] [-r repository-path] path\n",
2410 char datebuf[11]; /* YYYY-MM-DD + NUL */
2413 struct blame_cb_args {
2414 struct blame_line *lines;
2418 off_t *line_offsets;
2420 struct got_repository *repo;
2423 static const struct got_error *
2424 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2426 const struct got_error *err = NULL;
2427 struct blame_cb_args *a = arg;
2428 struct blame_line *bline;
2430 size_t linesize = 0;
2431 struct got_commit_object *commit = NULL;
2434 time_t committer_time;
2436 if (nlines != a->nlines ||
2437 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2438 return got_error(GOT_ERR_RANGE);
2440 if (sigint_received)
2441 return got_error(GOT_ERR_ITER_COMPLETED);
2444 return NULL; /* no change in this commit */
2446 /* Annotate this line. */
2447 bline = &a->lines[lineno - 1];
2448 if (bline->annotated)
2450 err = got_object_id_str(&bline->id_str, id);
2454 err = got_object_open_as_commit(&commit, a->repo, id);
2458 bline->committer = strdup(got_object_commit_get_committer(commit));
2459 if (bline->committer == NULL) {
2460 err = got_error_from_errno("strdup");
2464 committer_time = got_object_commit_get_committer_time(commit);
2465 if (localtime_r(&committer_time, &tm) == NULL)
2466 return got_error_from_errno("localtime_r");
2467 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2468 &tm) >= sizeof(bline->datebuf)) {
2469 err = got_error(GOT_ERR_NO_SPACE);
2472 bline->annotated = 1;
2474 /* Print lines annotated so far. */
2475 bline = &a->lines[a->lineno_cur - 1];
2476 if (!bline->annotated)
2479 offset = a->line_offsets[a->lineno_cur - 1];
2480 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2481 err = got_error_from_errno("fseeko");
2485 while (bline->annotated) {
2486 char *smallerthan, *at, *nl, *committer;
2489 if (getline(&line, &linesize, a->f) == -1) {
2491 err = got_error_from_errno("getline");
2495 committer = bline->committer;
2496 smallerthan = strchr(committer, '<');
2497 if (smallerthan && smallerthan[1] != '\0')
2498 committer = smallerthan + 1;
2499 at = strchr(committer, '@');
2502 len = strlen(committer);
2504 committer[8] = '\0';
2506 nl = strchr(line, '\n');
2509 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2510 bline->id_str, bline->datebuf, committer, line);
2513 bline = &a->lines[a->lineno_cur - 1];
2517 got_object_commit_close(commit);
2522 static const struct got_error *
2523 cmd_blame(int argc, char *argv[])
2525 const struct got_error *error;
2526 struct got_repository *repo = NULL;
2527 struct got_worktree *worktree = NULL;
2528 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2529 struct got_object_id *obj_id = NULL;
2530 struct got_object_id *commit_id = NULL;
2531 struct got_blob_object *blob = NULL;
2532 char *commit_id_str = NULL;
2533 struct blame_cb_args bca;
2534 int ch, obj_type, i;
2537 memset(&bca, 0, sizeof(bca));
2540 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2545 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2548 commit_id_str = optarg;
2551 repo_path = realpath(optarg, NULL);
2552 if (repo_path == NULL)
2553 return got_error_from_errno2("realpath",
2555 got_path_strip_trailing_slashes(repo_path);
2571 cwd = getcwd(NULL, 0);
2573 error = got_error_from_errno("getcwd");
2576 if (repo_path == NULL) {
2577 error = got_worktree_open(&worktree, cwd);
2578 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2584 strdup(got_worktree_get_repo_path(worktree));
2585 if (repo_path == NULL)
2586 error = got_error_from_errno("strdup");
2590 repo_path = strdup(cwd);
2591 if (repo_path == NULL) {
2592 error = got_error_from_errno("strdup");
2598 error = got_repo_open(&repo, repo_path, NULL);
2602 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2607 const char *prefix = got_worktree_get_path_prefix(worktree);
2608 char *p, *worktree_subdir = cwd +
2609 strlen(got_worktree_get_root_path(worktree));
2610 if (asprintf(&p, "%s%s%s%s%s",
2611 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2612 worktree_subdir, worktree_subdir[0] ? "/" : "",
2614 error = got_error_from_errno("asprintf");
2617 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2620 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2625 if (commit_id_str == NULL) {
2626 struct got_reference *head_ref;
2627 error = got_ref_open(&head_ref, repo, worktree ?
2628 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2631 error = got_ref_resolve(&commit_id, repo, head_ref);
2632 got_ref_close(head_ref);
2636 error = got_repo_match_object_id(&commit_id, NULL,
2637 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2642 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2646 error = got_object_get_type(&obj_type, repo, obj_id);
2650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2651 error = got_error(GOT_ERR_OBJ_TYPE);
2655 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2658 bca.f = got_opentemp();
2659 if (bca.f == NULL) {
2660 error = got_error_from_errno("got_opentemp");
2663 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2664 &bca.line_offsets, bca.f, blob);
2665 if (error || bca.nlines == 0)
2668 /* Don't include \n at EOF in the blame line count. */
2669 if (bca.line_offsets[bca.nlines - 1] == filesize)
2672 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2673 if (bca.lines == NULL) {
2674 error = got_error_from_errno("calloc");
2678 bca.nlines_prec = 0;
2686 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2687 check_cancelled, NULL);
2695 got_object_blob_close(blob);
2697 got_worktree_close(worktree);
2699 const struct got_error *repo_error;
2700 repo_error = got_repo_close(repo);
2705 for (i = 0; i < bca.nlines; i++) {
2706 struct blame_line *bline = &bca.lines[i];
2707 free(bline->id_str);
2708 free(bline->committer);
2712 free(bca.line_offsets);
2713 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2714 error = got_error_from_errno("fclose");
2722 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2728 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2729 const char *root_path)
2731 int is_root_path = (strcmp(path, root_path) == 0);
2732 const char *modestr = "";
2733 mode_t mode = got_tree_entry_get_mode(te);
2735 path += strlen(root_path);
2736 while (path[0] == '/')
2739 if (got_object_tree_entry_is_submodule(te))
2741 else if (S_ISLNK(mode))
2743 else if (S_ISDIR(mode))
2745 else if (mode & S_IXUSR)
2748 printf("%s%s%s%s%s\n", id ? id : "", path,
2749 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2752 static const struct got_error *
2753 print_tree(const char *path, struct got_object_id *commit_id,
2754 int show_ids, int recurse, const char *root_path,
2755 struct got_repository *repo)
2757 const struct got_error *err = NULL;
2758 struct got_object_id *tree_id = NULL;
2759 struct got_tree_object *tree = NULL;
2762 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2766 err = got_object_open_as_tree(&tree, repo, tree_id);
2769 nentries = got_object_tree_get_nentries(tree);
2770 for (i = 0; i < nentries; i++) {
2771 struct got_tree_entry *te;
2774 if (sigint_received || sigpipe_received)
2777 te = got_object_tree_get_entry(tree, i);
2780 err = got_object_id_str(&id_str,
2781 got_tree_entry_get_id(te));
2784 if (asprintf(&id, "%s ", id_str) == -1) {
2785 err = got_error_from_errno("asprintf");
2791 print_entry(te, id, path, root_path);
2794 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2796 if (asprintf(&child_path, "%s%s%s", path,
2797 path[0] == '/' && path[1] == '\0' ? "" : "/",
2798 got_tree_entry_get_name(te)) == -1) {
2799 err = got_error_from_errno("asprintf");
2802 err = print_tree(child_path, commit_id, show_ids, 1,
2811 got_object_tree_close(tree);
2816 static const struct got_error *
2817 cmd_tree(int argc, char *argv[])
2819 const struct got_error *error;
2820 struct got_repository *repo = NULL;
2821 struct got_worktree *worktree = NULL;
2823 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2824 struct got_object_id *commit_id = NULL;
2825 char *commit_id_str = NULL;
2826 int show_ids = 0, recurse = 0;
2830 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2835 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2838 commit_id_str = optarg;
2841 repo_path = realpath(optarg, NULL);
2842 if (repo_path == NULL)
2843 return got_error_from_errno2("realpath",
2845 got_path_strip_trailing_slashes(repo_path);
2869 cwd = getcwd(NULL, 0);
2871 error = got_error_from_errno("getcwd");
2874 if (repo_path == NULL) {
2875 error = got_worktree_open(&worktree, cwd);
2876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2882 strdup(got_worktree_get_repo_path(worktree));
2883 if (repo_path == NULL)
2884 error = got_error_from_errno("strdup");
2888 repo_path = strdup(cwd);
2889 if (repo_path == NULL) {
2890 error = got_error_from_errno("strdup");
2896 error = got_repo_open(&repo, repo_path, NULL);
2900 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2906 char *p, *worktree_subdir = cwd +
2907 strlen(got_worktree_get_root_path(worktree));
2908 if (asprintf(&p, "%s/%s",
2909 got_worktree_get_path_prefix(worktree),
2910 worktree_subdir) == -1) {
2911 error = got_error_from_errno("asprintf");
2914 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2921 if (in_repo_path == NULL) {
2922 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2927 if (commit_id_str == NULL) {
2928 struct got_reference *head_ref;
2929 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2932 error = got_ref_resolve(&commit_id, repo, head_ref);
2933 got_ref_close(head_ref);
2937 error = got_repo_match_object_id(&commit_id, NULL,
2938 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2943 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 in_repo_path, repo);
2951 got_worktree_close(worktree);
2953 const struct got_error *repo_error;
2954 repo_error = got_repo_close(repo);
2964 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2968 static const struct got_error *
2969 print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 const char *path, struct got_object_id *blob_id,
2971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2972 int dirfd, const char *de_name)
2974 if (status == staged_status && (status == GOT_STATUS_DELETE))
2975 status = GOT_STATUS_NO_CHANGE;
2976 printf("%c%c %s\n", status, staged_status, path);
2980 static const struct got_error *
2981 cmd_status(int argc, char *argv[])
2983 const struct got_error *error = NULL;
2984 struct got_repository *repo = NULL;
2985 struct got_worktree *worktree = NULL;
2987 struct got_pathlist_head paths;
2988 struct got_pathlist_entry *pe;
2993 while ((ch = getopt(argc, argv, "")) != -1) {
3005 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3009 cwd = getcwd(NULL, 0);
3011 error = got_error_from_errno("getcwd");
3015 error = got_worktree_open(&worktree, cwd);
3019 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3024 error = apply_unveil(got_repo_get_path(repo), 1,
3025 got_worktree_get_root_path(worktree));
3029 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3033 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3034 check_cancelled, NULL);
3036 TAILQ_FOREACH(pe, &paths, entry)
3037 free((char *)pe->path);
3038 got_pathlist_free(&paths);
3047 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3052 static const struct got_error *
3053 list_refs(struct got_repository *repo)
3055 static const struct got_error *err = NULL;
3056 struct got_reflist_head refs;
3057 struct got_reflist_entry *re;
3059 SIMPLEQ_INIT(&refs);
3060 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3064 SIMPLEQ_FOREACH(re, &refs, entry) {
3066 refstr = got_ref_to_str(re->ref);
3068 return got_error_from_errno("got_ref_to_str");
3069 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3073 got_ref_list_free(&refs);
3077 static const struct got_error *
3078 delete_ref(struct got_repository *repo, const char *refname)
3080 const struct got_error *err = NULL;
3081 struct got_reference *ref;
3083 err = got_ref_open(&ref, repo, refname, 0);
3087 err = got_ref_delete(ref, repo);
3092 static const struct got_error *
3093 add_ref(struct got_repository *repo, const char *refname, const char *target)
3095 const struct got_error *err = NULL;
3096 struct got_object_id *id;
3097 struct got_reference *ref = NULL;
3100 * Don't let the user create a reference name with a leading '-'.
3101 * While technically a valid reference name, this case is usually
3102 * an unintended typo.
3104 if (refname[0] == '-')
3105 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3107 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3110 struct got_reference *target_ref;
3112 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3114 err = got_ref_open(&target_ref, repo, target, 0);
3117 err = got_ref_resolve(&id, repo, target_ref);
3118 got_ref_close(target_ref);
3123 err = got_ref_alloc(&ref, refname, id);
3127 err = got_ref_write(ref, repo);
3135 static const struct got_error *
3136 add_symref(struct got_repository *repo, const char *refname, const char *target)
3138 const struct got_error *err = NULL;
3139 struct got_reference *ref = NULL;
3140 struct got_reference *target_ref = NULL;
3143 * Don't let the user create a reference name with a leading '-'.
3144 * While technically a valid reference name, this case is usually
3145 * an unintended typo.
3147 if (refname[0] == '-')
3148 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3150 err = got_ref_open(&target_ref, repo, target, 0);
3154 err = got_ref_alloc_symref(&ref, refname, target_ref);
3158 err = got_ref_write(ref, repo);
3161 got_ref_close(target_ref);
3167 static const struct got_error *
3168 cmd_ref(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_repository *repo = NULL;
3172 struct got_worktree *worktree = NULL;
3173 char *cwd = NULL, *repo_path = NULL;
3174 int ch, do_list = 0, create_symref = 0;
3175 const char *delref = NULL;
3177 /* TODO: Add -s option for adding symbolic references. */
3178 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3184 repo_path = realpath(optarg, NULL);
3185 if (repo_path == NULL)
3186 return got_error_from_errno2("realpath",
3188 got_path_strip_trailing_slashes(repo_path);
3202 if (do_list && delref)
3203 errx(1, "-l and -d options are mutually exclusive\n");
3208 if (do_list || delref) {
3210 errx(1, "-s option cannot be used together with the "
3211 "-l or -d options");
3214 } else if (argc != 2)
3219 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3223 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3224 "sendfd unveil", NULL) == -1)
3228 cwd = getcwd(NULL, 0);
3230 error = got_error_from_errno("getcwd");
3234 if (repo_path == NULL) {
3235 error = got_worktree_open(&worktree, cwd);
3236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3242 strdup(got_worktree_get_repo_path(worktree));
3243 if (repo_path == NULL)
3244 error = got_error_from_errno("strdup");
3248 repo_path = strdup(cwd);
3249 if (repo_path == NULL) {
3250 error = got_error_from_errno("strdup");
3256 error = got_repo_open(&repo, repo_path, NULL);
3260 error = apply_unveil(got_repo_get_path(repo), do_list,
3261 worktree ? got_worktree_get_root_path(worktree) : NULL);
3266 error = list_refs(repo);
3268 error = delete_ref(repo, delref);
3269 else if (create_symref)
3270 error = add_symref(repo, argv[0], argv[1]);
3272 error = add_ref(repo, argv[0], argv[1]);
3275 got_repo_close(repo);
3277 got_worktree_close(worktree);
3287 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3288 "[name]\n", getprogname());
3292 static const struct got_error *
3293 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3294 struct got_reference *ref)
3296 const struct got_error *err = NULL;
3297 const char *refname, *marker = " ";
3300 refname = got_ref_get_name(ref);
3301 if (worktree && strcmp(refname,
3302 got_worktree_get_head_ref_name(worktree)) == 0) {
3303 struct got_object_id *id = NULL;
3305 err = got_ref_resolve(&id, repo, ref);
3308 if (got_object_id_cmp(id,
3309 got_worktree_get_base_commit_id(worktree)) == 0)
3316 if (strncmp(refname, "refs/heads/", 11) == 0)
3318 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3321 refstr = got_ref_to_str(ref);
3323 return got_error_from_errno("got_ref_to_str");
3325 printf("%s%s: %s\n", marker, refname, refstr);
3330 static const struct got_error *
3331 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3333 const char *refname;
3335 if (worktree == NULL)
3336 return got_error(GOT_ERR_NOT_WORKTREE);
3338 refname = got_worktree_get_head_ref_name(worktree);
3340 if (strncmp(refname, "refs/heads/", 11) == 0)
3342 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3345 printf("%s\n", refname);
3350 static const struct got_error *
3351 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3353 static const struct got_error *err = NULL;
3354 struct got_reflist_head refs;
3355 struct got_reflist_entry *re;
3356 struct got_reference *temp_ref = NULL;
3357 int rebase_in_progress, histedit_in_progress;
3359 SIMPLEQ_INIT(&refs);
3362 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3367 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3372 if (rebase_in_progress || histedit_in_progress) {
3373 err = got_ref_open(&temp_ref, repo,
3374 got_worktree_get_head_ref_name(worktree), 0);
3377 list_branch(repo, worktree, temp_ref);
3378 got_ref_close(temp_ref);
3382 err = got_ref_list(&refs, repo, "refs/heads",
3383 got_ref_cmp_by_name, NULL);
3387 SIMPLEQ_FOREACH(re, &refs, entry)
3388 list_branch(repo, worktree, re->ref);
3390 got_ref_list_free(&refs);
3394 static const struct got_error *
3395 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3396 const char *branch_name)
3398 const struct got_error *err = NULL;
3399 struct got_reference *ref = NULL;
3402 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3403 return got_error_from_errno("asprintf");
3405 err = got_ref_open(&ref, repo, refname, 0);
3410 strcmp(got_worktree_get_head_ref_name(worktree),
3411 got_ref_get_name(ref)) == 0) {
3412 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3413 "will not delete this work tree's current branch");
3417 err = got_ref_delete(ref, repo);
3425 static const struct got_error *
3426 add_branch(struct got_repository *repo, const char *branch_name,
3427 struct got_object_id *base_commit_id)
3429 const struct got_error *err = NULL;
3430 struct got_reference *ref = NULL;
3431 char *base_refname = NULL, *refname = NULL;
3434 * Don't let the user create a branch name with a leading '-'.
3435 * While technically a valid reference name, this case is usually
3436 * an unintended typo.
3438 if (branch_name[0] == '-')
3439 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3441 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3442 err = got_error_from_errno("asprintf");
3446 err = got_ref_open(&ref, repo, refname, 0);
3448 err = got_error(GOT_ERR_BRANCH_EXISTS);
3450 } else if (err->code != GOT_ERR_NOT_REF)
3453 err = got_ref_alloc(&ref, refname, base_commit_id);
3457 err = got_ref_write(ref, repo);
3466 static const struct got_error *
3467 cmd_branch(int argc, char *argv[])
3469 const struct got_error *error = NULL;
3470 struct got_repository *repo = NULL;
3471 struct got_worktree *worktree = NULL;
3472 char *cwd = NULL, *repo_path = NULL;
3473 int ch, do_list = 0, do_show = 0;
3474 const char *delref = NULL, *commit_id_arg = NULL;
3476 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3479 commit_id_arg = optarg;
3485 repo_path = realpath(optarg, NULL);
3486 if (repo_path == NULL)
3487 return got_error_from_errno2("realpath",
3489 got_path_strip_trailing_slashes(repo_path);
3500 if (do_list && delref)
3501 errx(1, "-l and -d options are mutually exclusive\n");
3506 if (!do_list && !delref && argc == 0)
3509 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3510 errx(1, "-c option can only be used when creating a branch");
3512 if (do_list || delref) {
3515 } else if (!do_show && argc != 1)
3519 if (do_list || do_show) {
3520 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3524 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3525 "sendfd unveil", NULL) == -1)
3529 cwd = getcwd(NULL, 0);
3531 error = got_error_from_errno("getcwd");
3535 if (repo_path == NULL) {
3536 error = got_worktree_open(&worktree, cwd);
3537 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3543 strdup(got_worktree_get_repo_path(worktree));
3544 if (repo_path == NULL)
3545 error = got_error_from_errno("strdup");
3549 repo_path = strdup(cwd);
3550 if (repo_path == NULL) {
3551 error = got_error_from_errno("strdup");
3557 error = got_repo_open(&repo, repo_path, NULL);
3561 error = apply_unveil(got_repo_get_path(repo), do_list,
3562 worktree ? got_worktree_get_root_path(worktree) : NULL);
3567 error = show_current_branch(repo, worktree);
3569 error = list_branches(repo, worktree);
3571 error = delete_branch(repo, worktree, delref);
3573 struct got_object_id *commit_id;
3574 if (commit_id_arg == NULL)
3575 commit_id_arg = worktree ?
3576 got_worktree_get_head_ref_name(worktree) :
3578 error = got_repo_match_object_id(&commit_id, NULL,
3579 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3582 error = add_branch(repo, argv[0], commit_id);
3587 got_repo_close(repo);
3589 got_worktree_close(worktree);
3600 "usage: %s tag [-r repository] | -l | "
3601 "[-m message] name [commit]\n", getprogname());
3606 static const struct got_error *
3607 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3609 const struct got_error *err = NULL;
3610 struct got_reflist_entry *re, *se, *new;
3611 struct got_object_id *re_id, *se_id;
3612 struct got_tag_object *re_tag, *se_tag;
3613 time_t re_time, se_time;
3615 SIMPLEQ_FOREACH(re, tags, entry) {
3616 se = SIMPLEQ_FIRST(sorted);
3618 err = got_reflist_entry_dup(&new, re);
3621 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3624 err = got_ref_resolve(&re_id, repo, re->ref);
3627 err = got_object_open_as_tag(&re_tag, repo, re_id);
3631 re_time = got_object_tag_get_tagger_time(re_tag);
3632 got_object_tag_close(re_tag);
3636 err = got_ref_resolve(&se_id, repo, re->ref);
3639 err = got_object_open_as_tag(&se_tag, repo, se_id);
3643 se_time = got_object_tag_get_tagger_time(se_tag);
3644 got_object_tag_close(se_tag);
3646 if (se_time > re_time) {
3647 err = got_reflist_entry_dup(&new, re);
3650 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3653 se = SIMPLEQ_NEXT(se, entry);
3662 static const struct got_error *
3663 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3665 static const struct got_error *err = NULL;
3666 struct got_reflist_head refs;
3667 struct got_reflist_entry *re;
3669 SIMPLEQ_INIT(&refs);
3671 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3675 SIMPLEQ_FOREACH(re, &refs, entry) {
3676 const char *refname;
3677 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3681 struct got_object_id *id;
3682 struct got_tag_object *tag;
3683 struct got_commit_object *commit = NULL;
3685 refname = got_ref_get_name(re->ref);
3686 if (strncmp(refname, "refs/tags/", 10) != 0)
3689 refstr = got_ref_to_str(re->ref);
3690 if (refstr == NULL) {
3691 err = got_error_from_errno("got_ref_to_str");
3694 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3697 err = got_ref_resolve(&id, repo, re->ref);
3700 err = got_object_open_as_tag(&tag, repo, id);
3702 if (err->code != GOT_ERR_OBJ_TYPE) {
3706 /* "lightweight" tag */
3707 err = got_object_open_as_commit(&commit, repo, id);
3712 tagger = got_object_commit_get_committer(commit);
3714 got_object_commit_get_committer_time(commit);
3715 err = got_object_id_str(&id_str, id);
3721 tagger = got_object_tag_get_tagger(tag);
3722 tagger_time = got_object_tag_get_tagger_time(tag);
3723 err = got_object_id_str(&id_str,
3724 got_object_tag_get_object_id(tag));
3728 printf("from: %s\n", tagger);
3729 datestr = get_datestr(&tagger_time, datebuf);
3731 printf("date: %s UTC\n", datestr);
3733 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3735 switch (got_object_tag_get_object_type(tag)) {
3736 case GOT_OBJ_TYPE_BLOB:
3737 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3740 case GOT_OBJ_TYPE_TREE:
3741 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3744 case GOT_OBJ_TYPE_COMMIT:
3745 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3748 case GOT_OBJ_TYPE_TAG:
3749 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3758 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3761 got_object_commit_close(commit);
3763 tagmsg0 = strdup(got_object_tag_get_message(tag));
3764 got_object_tag_close(tag);
3765 if (tagmsg0 == NULL) {
3766 err = got_error_from_errno("strdup");
3773 line = strsep(&tagmsg, "\n");
3775 printf(" %s\n", line);
3780 got_ref_list_free(&refs);
3784 static const struct got_error *
3785 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3786 const char *tag_name, const char *repo_path)
3788 const struct got_error *err = NULL;
3789 char *template = NULL, *initial_content = NULL;
3790 char *editor = NULL;
3793 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3794 err = got_error_from_errno("asprintf");
3798 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3799 commit_id_str, tag_name) == -1) {
3800 err = got_error_from_errno("asprintf");
3804 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3808 dprintf(fd, initial_content);
3811 err = get_editor(&editor);
3814 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3816 free(initial_content);
3820 /* Editor is done; we can now apply unveil(2) */
3822 err = apply_unveil(repo_path, 0, NULL);
3831 static const struct got_error *
3832 add_tag(struct got_repository *repo, const char *tag_name,
3833 const char *commit_arg, const char *tagmsg_arg)
3835 const struct got_error *err = NULL;
3836 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3837 char *label = NULL, *commit_id_str = NULL;
3838 struct got_reference *ref = NULL;
3839 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3840 char *tagmsg_path = NULL, *tag_id_str = NULL;
3841 int preserve_tagmsg = 0;
3844 * Don't let the user create a tag name with a leading '-'.
3845 * While technically a valid reference name, this case is usually
3846 * an unintended typo.
3848 if (tag_name[0] == '-')
3849 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3851 err = get_author(&tagger, repo);
3855 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3856 GOT_OBJ_TYPE_COMMIT, 1, repo);
3860 err = got_object_id_str(&commit_id_str, commit_id);
3864 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3865 refname = strdup(tag_name);
3866 if (refname == NULL) {
3867 err = got_error_from_errno("strdup");
3871 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3872 err = got_error_from_errno("asprintf");
3876 err = got_ref_open(&ref, repo, refname, 0);
3878 err = got_error(GOT_ERR_TAG_EXISTS);
3880 } else if (err->code != GOT_ERR_NOT_REF)
3883 if (tagmsg_arg == NULL) {
3884 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3885 tag_name, got_repo_get_path(repo));
3887 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3888 tagmsg_path != NULL)
3889 preserve_tagmsg = 1;
3894 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3895 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3898 preserve_tagmsg = 1;
3902 err = got_ref_alloc(&ref, refname, tag_id);
3905 preserve_tagmsg = 1;
3909 err = got_ref_write(ref, repo);
3912 preserve_tagmsg = 1;
3916 err = got_object_id_str(&tag_id_str, tag_id);
3919 preserve_tagmsg = 1;
3922 printf("Created tag %s\n", tag_id_str);
3924 if (preserve_tagmsg) {
3925 fprintf(stderr, "%s: tag message preserved in %s\n",
3926 getprogname(), tagmsg_path);
3927 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3928 err = got_error_from_errno2("unlink", tagmsg_path);
3933 free(commit_id_str);
3941 static const struct got_error *
3942 cmd_tag(int argc, char *argv[])
3944 const struct got_error *error = NULL;
3945 struct got_repository *repo = NULL;
3946 struct got_worktree *worktree = NULL;
3947 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3948 char *gitconfig_path = NULL;
3949 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3950 int ch, do_list = 0;
3952 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3958 repo_path = realpath(optarg, NULL);
3959 if (repo_path == NULL)
3960 return got_error_from_errno2("realpath",
3962 got_path_strip_trailing_slashes(repo_path);
3978 errx(1, "-l and -m options are mutually exclusive\n");
3981 } else if (argc < 1 || argc > 2)
3984 commit_id_arg = argv[1];
3989 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3993 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3994 "sendfd unveil", NULL) == -1)
3998 cwd = getcwd(NULL, 0);
4000 error = got_error_from_errno("getcwd");
4004 if (repo_path == NULL) {
4005 error = got_worktree_open(&worktree, cwd);
4006 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4012 strdup(got_worktree_get_repo_path(worktree));
4013 if (repo_path == NULL)
4014 error = got_error_from_errno("strdup");
4018 repo_path = strdup(cwd);
4019 if (repo_path == NULL) {
4020 error = got_error_from_errno("strdup");
4027 error = got_repo_open(&repo, repo_path, NULL);
4030 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4033 error = list_tags(repo, worktree);
4035 error = get_gitconfig_path(&gitconfig_path);
4038 error = got_repo_open(&repo, repo_path, gitconfig_path);
4043 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4048 if (commit_id_arg == NULL) {
4049 struct got_reference *head_ref;
4050 struct got_object_id *commit_id;
4051 error = got_ref_open(&head_ref, repo,
4052 worktree ? got_worktree_get_head_ref_name(worktree)
4056 error = got_ref_resolve(&commit_id, repo, head_ref);
4057 got_ref_close(head_ref);
4060 error = got_object_id_str(&commit_id_str, commit_id);
4066 error = add_tag(repo, tag_name,
4067 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4071 got_repo_close(repo);
4073 got_worktree_close(worktree);
4076 free(gitconfig_path);
4077 free(commit_id_str);
4084 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4089 static const struct got_error *
4090 add_progress(void *arg, unsigned char status, const char *path)
4092 while (path[0] == '/')
4094 printf("%c %s\n", status, path);
4098 static const struct got_error *
4099 cmd_add(int argc, char *argv[])
4101 const struct got_error *error = NULL;
4102 struct got_repository *repo = NULL;
4103 struct got_worktree *worktree = NULL;
4105 struct got_pathlist_head paths;
4106 struct got_pathlist_entry *pe;
4107 int ch, can_recurse = 0, no_ignores = 0;
4111 while ((ch = getopt(argc, argv, "IR")) != -1) {
4129 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4136 cwd = getcwd(NULL, 0);
4138 error = got_error_from_errno("getcwd");
4142 error = got_worktree_open(&worktree, cwd);
4146 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4151 error = apply_unveil(got_repo_get_path(repo), 1,
4152 got_worktree_get_root_path(worktree));
4156 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4160 if (!can_recurse && no_ignores) {
4161 error = got_error_msg(GOT_ERR_BAD_PATH,
4162 "disregarding ignores requires -R option");
4170 TAILQ_FOREACH(pe, &paths, entry) {
4171 if (asprintf(&ondisk_path, "%s/%s",
4172 got_worktree_get_root_path(worktree),
4174 error = got_error_from_errno("asprintf");
4177 if (lstat(ondisk_path, &sb) == -1) {
4178 if (errno == ENOENT) {
4182 error = got_error_from_errno2("lstat",
4188 if (S_ISDIR(sb.st_mode)) {
4189 error = got_error_msg(GOT_ERR_BAD_PATH,
4190 "adding directories requires -R option");
4196 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4197 NULL, repo, no_ignores);
4200 got_repo_close(repo);
4202 got_worktree_close(worktree);
4203 TAILQ_FOREACH(pe, &paths, entry)
4204 free((char *)pe->path);
4205 got_pathlist_free(&paths);
4213 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4218 static const struct got_error *
4219 print_remove_status(void *arg, unsigned char status,
4220 unsigned char staged_status, const char *path)
4222 while (path[0] == '/')
4224 if (status == GOT_STATUS_NONEXISTENT)
4226 if (status == staged_status && (status == GOT_STATUS_DELETE))
4227 status = GOT_STATUS_NO_CHANGE;
4228 printf("%c%c %s\n", status, staged_status, path);
4232 static const struct got_error *
4233 cmd_remove(int argc, char *argv[])
4235 const struct got_error *error = NULL;
4236 struct got_worktree *worktree = NULL;
4237 struct got_repository *repo = NULL;
4239 struct got_pathlist_head paths;
4240 struct got_pathlist_entry *pe;
4241 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4245 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4248 delete_local_mods = 1;
4266 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4273 cwd = getcwd(NULL, 0);
4275 error = got_error_from_errno("getcwd");
4278 error = got_worktree_open(&worktree, cwd);
4282 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4287 error = apply_unveil(got_repo_get_path(repo), 1,
4288 got_worktree_get_root_path(worktree));
4292 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4299 TAILQ_FOREACH(pe, &paths, entry) {
4300 if (asprintf(&ondisk_path, "%s/%s",
4301 got_worktree_get_root_path(worktree),
4303 error = got_error_from_errno("asprintf");
4306 if (lstat(ondisk_path, &sb) == -1) {
4307 if (errno == ENOENT) {
4311 error = got_error_from_errno2("lstat",
4317 if (S_ISDIR(sb.st_mode)) {
4318 error = got_error_msg(GOT_ERR_BAD_PATH,
4319 "removing directories requires -R option");
4325 error = got_worktree_schedule_delete(worktree, &paths,
4326 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4329 got_repo_close(repo);
4331 got_worktree_close(worktree);
4332 TAILQ_FOREACH(pe, &paths, entry)
4333 free((char *)pe->path);
4334 got_pathlist_free(&paths);
4342 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4343 "path ...\n", getprogname());
4347 static const struct got_error *
4348 revert_progress(void *arg, unsigned char status, const char *path)
4350 if (status == GOT_STATUS_UNVERSIONED)
4353 while (path[0] == '/')
4355 printf("%c %s\n", status, path);
4359 struct choose_patch_arg {
4360 FILE *patch_script_file;
4364 static const struct got_error *
4365 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4366 int nchanges, const char *action)
4369 size_t linesize = 0;
4373 case GOT_STATUS_ADD:
4374 printf("A %s\n%s this addition? [y/n] ", path, action);
4376 case GOT_STATUS_DELETE:
4377 printf("D %s\n%s this deletion? [y/n] ", path, action);
4379 case GOT_STATUS_MODIFY:
4380 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4381 return got_error_from_errno("fseek");
4382 printf(GOT_COMMIT_SEP_STR);
4383 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4385 if (ferror(patch_file))
4386 return got_error_from_errno("getline");
4387 printf(GOT_COMMIT_SEP_STR);
4388 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4389 path, n, nchanges, action);
4392 return got_error_path(path, GOT_ERR_FILE_STATUS);
4398 static const struct got_error *
4399 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4400 FILE *patch_file, int n, int nchanges)
4402 const struct got_error *err = NULL;
4404 size_t linesize = 0;
4407 struct choose_patch_arg *a = arg;
4409 *choice = GOT_PATCH_CHOICE_NONE;
4411 if (a->patch_script_file) {
4413 err = show_change(status, path, patch_file, n, nchanges,
4417 linelen = getline(&line, &linesize, a->patch_script_file);
4418 if (linelen == -1) {
4419 if (ferror(a->patch_script_file))
4420 return got_error_from_errno("getline");
4423 nl = strchr(line, '\n');
4426 if (strcmp(line, "y") == 0) {
4427 *choice = GOT_PATCH_CHOICE_YES;
4429 } else if (strcmp(line, "n") == 0) {
4430 *choice = GOT_PATCH_CHOICE_NO;
4432 } else if (strcmp(line, "q") == 0 &&
4433 status == GOT_STATUS_MODIFY) {
4434 *choice = GOT_PATCH_CHOICE_QUIT;
4437 printf("invalid response '%s'\n", line);
4442 while (resp != 'y' && resp != 'n' && resp != 'q') {
4443 err = show_change(status, path, patch_file, n, nchanges,
4450 if (status == GOT_STATUS_MODIFY) {
4451 if (resp != 'y' && resp != 'n' && resp != 'q') {
4452 printf("invalid response '%c'\n", resp);
4455 } else if (resp != 'y' && resp != 'n') {
4456 printf("invalid response '%c'\n", resp);
4462 *choice = GOT_PATCH_CHOICE_YES;
4463 else if (resp == 'n')
4464 *choice = GOT_PATCH_CHOICE_NO;
4465 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4466 *choice = GOT_PATCH_CHOICE_QUIT;
4472 static const struct got_error *
4473 cmd_revert(int argc, char *argv[])
4475 const struct got_error *error = NULL;
4476 struct got_worktree *worktree = NULL;
4477 struct got_repository *repo = NULL;
4478 char *cwd = NULL, *path = NULL;
4479 struct got_pathlist_head paths;
4480 struct got_pathlist_entry *pe;
4481 int ch, can_recurse = 0, pflag = 0;
4482 FILE *patch_script_file = NULL;
4483 const char *patch_script_path = NULL;
4484 struct choose_patch_arg cpa;
4488 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4494 patch_script_path = optarg;
4509 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4510 "unveil", NULL) == -1)
4515 if (patch_script_path && !pflag)
4516 errx(1, "-F option can only be used together with -p option");
4518 cwd = getcwd(NULL, 0);
4520 error = got_error_from_errno("getcwd");
4523 error = got_worktree_open(&worktree, cwd);
4527 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4532 if (patch_script_path) {
4533 patch_script_file = fopen(patch_script_path, "r");
4534 if (patch_script_file == NULL) {
4535 error = got_error_from_errno2("fopen",
4540 error = apply_unveil(got_repo_get_path(repo), 1,
4541 got_worktree_get_root_path(worktree));
4545 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4552 TAILQ_FOREACH(pe, &paths, entry) {
4553 if (asprintf(&ondisk_path, "%s/%s",
4554 got_worktree_get_root_path(worktree),
4556 error = got_error_from_errno("asprintf");
4559 if (lstat(ondisk_path, &sb) == -1) {
4560 if (errno == ENOENT) {
4564 error = got_error_from_errno2("lstat",
4570 if (S_ISDIR(sb.st_mode)) {
4571 error = got_error_msg(GOT_ERR_BAD_PATH,
4572 "reverting directories requires -R option");
4578 cpa.patch_script_file = patch_script_file;
4579 cpa.action = "revert";
4580 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4581 pflag ? choose_patch : NULL, &cpa, repo);
4583 if (patch_script_file && fclose(patch_script_file) == EOF &&
4585 error = got_error_from_errno2("fclose", patch_script_path);
4587 got_repo_close(repo);
4589 got_worktree_close(worktree);
4598 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4603 struct collect_commit_logmsg_arg {
4604 const char *cmdline_log;
4606 const char *worktree_path;
4607 const char *branch_name;
4608 const char *repo_path;
4613 static const struct got_error *
4614 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4617 char *initial_content = NULL;
4618 struct got_pathlist_entry *pe;
4619 const struct got_error *err = NULL;
4620 char *template = NULL;
4621 struct collect_commit_logmsg_arg *a = arg;
4625 /* if a message was specified on the command line, just use it */
4626 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4627 len = strlen(a->cmdline_log) + 1;
4628 *logmsg = malloc(len + 1);
4629 if (*logmsg == NULL)
4630 return got_error_from_errno("malloc");
4631 strlcpy(*logmsg, a->cmdline_log, len);
4635 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4636 return got_error_from_errno("asprintf");
4638 if (asprintf(&initial_content,
4639 "\n# changes to be committed on branch %s:\n",
4640 a->branch_name) == -1)
4641 return got_error_from_errno("asprintf");
4643 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4647 dprintf(fd, initial_content);
4649 TAILQ_FOREACH(pe, commitable_paths, entry) {
4650 struct got_commitable *ct = pe->data;
4651 dprintf(fd, "# %c %s\n",
4652 got_commitable_get_status(ct),
4653 got_commitable_get_path(ct));
4657 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4659 free(initial_content);
4662 /* Editor is done; we can now apply unveil(2) */
4664 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4673 static const struct got_error *
4674 cmd_commit(int argc, char *argv[])
4676 const struct got_error *error = NULL;
4677 struct got_worktree *worktree = NULL;
4678 struct got_repository *repo = NULL;
4679 char *cwd = NULL, *id_str = NULL;
4680 struct got_object_id *id = NULL;
4681 const char *logmsg = NULL;
4682 struct collect_commit_logmsg_arg cl_arg;
4683 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4684 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4685 struct got_pathlist_head paths;
4688 cl_arg.logmsg_path = NULL;
4690 while ((ch = getopt(argc, argv, "m:")) != -1) {
4705 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4706 "unveil", NULL) == -1)
4709 cwd = getcwd(NULL, 0);
4711 error = got_error_from_errno("getcwd");
4714 error = got_worktree_open(&worktree, cwd);
4718 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4721 if (rebase_in_progress) {
4722 error = got_error(GOT_ERR_REBASING);
4726 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4731 error = get_gitconfig_path(&gitconfig_path);
4734 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4739 error = get_author(&author, repo);
4744 * unveil(2) traverses exec(2); if an editor is used we have
4745 * to apply unveil after the log message has been written.
4747 if (logmsg == NULL || strlen(logmsg) == 0)
4748 error = get_editor(&editor);
4750 error = apply_unveil(got_repo_get_path(repo), 0,
4751 got_worktree_get_root_path(worktree));
4755 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4759 cl_arg.editor = editor;
4760 cl_arg.cmdline_log = logmsg;
4761 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4762 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4763 if (!histedit_in_progress) {
4764 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4765 error = got_error(GOT_ERR_COMMIT_BRANCH);
4768 cl_arg.branch_name += 11;
4770 cl_arg.repo_path = got_repo_get_path(repo);
4771 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4772 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4774 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4775 cl_arg.logmsg_path != NULL)
4776 preserve_logmsg = 1;
4780 error = got_object_id_str(&id_str, id);
4783 printf("Created commit %s\n", id_str);
4785 if (preserve_logmsg) {
4786 fprintf(stderr, "%s: log message preserved in %s\n",
4787 getprogname(), cl_arg.logmsg_path);
4788 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4790 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4791 free(cl_arg.logmsg_path);
4793 got_repo_close(repo);
4795 got_worktree_close(worktree);
4798 free(gitconfig_path);
4805 usage_cherrypick(void)
4807 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4811 static const struct got_error *
4812 cmd_cherrypick(int argc, char *argv[])
4814 const struct got_error *error = NULL;
4815 struct got_worktree *worktree = NULL;
4816 struct got_repository *repo = NULL;
4817 char *cwd = NULL, *commit_id_str = NULL;
4818 struct got_object_id *commit_id = NULL;
4819 struct got_commit_object *commit = NULL;
4820 struct got_object_qid *pid;
4821 struct got_reference *head_ref = NULL;
4822 int ch, did_something = 0;
4824 while ((ch = getopt(argc, argv, "")) != -1) {
4836 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4837 "unveil", NULL) == -1)
4843 cwd = getcwd(NULL, 0);
4845 error = got_error_from_errno("getcwd");
4848 error = got_worktree_open(&worktree, cwd);
4852 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4857 error = apply_unveil(got_repo_get_path(repo), 0,
4858 got_worktree_get_root_path(worktree));
4862 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4863 GOT_OBJ_TYPE_COMMIT, repo);
4864 if (error != NULL) {
4865 struct got_reference *ref;
4866 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4868 error = got_ref_open(&ref, repo, argv[0], 0);
4871 error = got_ref_resolve(&commit_id, repo, ref);
4876 error = got_object_id_str(&commit_id_str, commit_id);
4880 error = got_ref_open(&head_ref, repo,
4881 got_worktree_get_head_ref_name(worktree), 0);
4885 error = check_same_branch(commit_id, head_ref, NULL, repo);
4887 if (error->code != GOT_ERR_ANCESTRY)
4891 error = got_error(GOT_ERR_SAME_BRANCH);
4895 error = got_object_open_as_commit(&commit, repo, commit_id);
4898 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4899 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4900 commit_id, repo, update_progress, &did_something, check_cancelled,
4906 printf("Merged commit %s\n", commit_id_str);
4909 got_object_commit_close(commit);
4910 free(commit_id_str);
4912 got_ref_close(head_ref);
4914 got_worktree_close(worktree);
4916 got_repo_close(repo);
4923 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4927 static const struct got_error *
4928 cmd_backout(int argc, char *argv[])
4930 const struct got_error *error = NULL;
4931 struct got_worktree *worktree = NULL;
4932 struct got_repository *repo = NULL;
4933 char *cwd = NULL, *commit_id_str = NULL;
4934 struct got_object_id *commit_id = NULL;
4935 struct got_commit_object *commit = NULL;
4936 struct got_object_qid *pid;
4937 struct got_reference *head_ref = NULL;
4938 int ch, did_something = 0;
4940 while ((ch = getopt(argc, argv, "")) != -1) {
4952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4953 "unveil", NULL) == -1)
4959 cwd = getcwd(NULL, 0);
4961 error = got_error_from_errno("getcwd");
4964 error = got_worktree_open(&worktree, cwd);
4968 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4973 error = apply_unveil(got_repo_get_path(repo), 0,
4974 got_worktree_get_root_path(worktree));
4978 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4979 GOT_OBJ_TYPE_COMMIT, repo);
4980 if (error != NULL) {
4981 struct got_reference *ref;
4982 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4984 error = got_ref_open(&ref, repo, argv[0], 0);
4987 error = got_ref_resolve(&commit_id, repo, ref);
4992 error = got_object_id_str(&commit_id_str, commit_id);
4996 error = got_ref_open(&head_ref, repo,
4997 got_worktree_get_head_ref_name(worktree), 0);
5001 error = check_same_branch(commit_id, head_ref, NULL, repo);
5005 error = got_object_open_as_commit(&commit, repo, commit_id);
5008 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5010 error = got_error(GOT_ERR_ROOT_COMMIT);
5014 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5015 update_progress, &did_something, check_cancelled, NULL);
5020 printf("Backed out commit %s\n", commit_id_str);
5023 got_object_commit_close(commit);
5024 free(commit_id_str);
5026 got_ref_close(head_ref);
5028 got_worktree_close(worktree);
5030 got_repo_close(repo);
5037 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5043 trim_logmsg(char *logmsg, int limit)
5048 len = strlen(logmsg);
5052 nl = strchr(logmsg, '\n');
5057 static const struct got_error *
5058 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5060 const struct got_error *err;
5061 char *logmsg0 = NULL;
5064 err = got_object_commit_get_logmsg(&logmsg0, commit);
5069 while (isspace((unsigned char)s[0]))
5072 *logmsg = strdup(s);
5073 if (*logmsg == NULL) {
5074 err = got_error_from_errno("strdup");
5078 trim_logmsg(*logmsg, limit);
5084 static const struct got_error *
5085 show_rebase_progress(struct got_commit_object *commit,
5086 struct got_object_id *old_id, struct got_object_id *new_id)
5088 const struct got_error *err;
5089 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5091 err = got_object_id_str(&old_id_str, old_id);
5096 err = got_object_id_str(&new_id_str, new_id);
5101 old_id_str[12] = '\0';
5103 new_id_str[12] = '\0';
5105 err = get_short_logmsg(&logmsg, 42, commit);
5109 printf("%s -> %s: %s\n", old_id_str,
5110 new_id_str ? new_id_str : "no-op change", logmsg);
5117 static const struct got_error *
5118 rebase_progress(void *arg, unsigned char status, const char *path)
5120 unsigned char *rebase_status = arg;
5122 while (path[0] == '/')
5124 printf("%c %s\n", status, path);
5126 if (*rebase_status == GOT_STATUS_CONFLICT)
5128 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5129 *rebase_status = status;
5133 static const struct got_error *
5134 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5135 struct got_reference *branch, struct got_reference *new_base_branch,
5136 struct got_reference *tmp_branch, struct got_repository *repo)
5138 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5139 return got_worktree_rebase_complete(worktree, fileindex,
5140 new_base_branch, tmp_branch, branch, repo);
5143 static const struct got_error *
5144 rebase_commit(struct got_pathlist_head *merged_paths,
5145 struct got_worktree *worktree, struct got_fileindex *fileindex,
5146 struct got_reference *tmp_branch,
5147 struct got_object_id *commit_id, struct got_repository *repo)
5149 const struct got_error *error;
5150 struct got_commit_object *commit;
5151 struct got_object_id *new_commit_id;
5153 error = got_object_open_as_commit(&commit, repo, commit_id);
5157 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5158 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5160 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5162 error = show_rebase_progress(commit, commit_id, NULL);
5164 error = show_rebase_progress(commit, commit_id, new_commit_id);
5165 free(new_commit_id);
5168 got_object_commit_close(commit);
5172 struct check_path_prefix_arg {
5173 const char *path_prefix;
5178 static const struct got_error *
5179 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5180 struct got_blob_object *blob2, struct got_object_id *id1,
5181 struct got_object_id *id2, const char *path1, const char *path2,
5182 mode_t mode1, mode_t mode2, struct got_repository *repo)
5184 struct check_path_prefix_arg *a = arg;
5186 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5187 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5188 return got_error(a->errcode);
5193 static const struct got_error *
5194 check_path_prefix(struct got_object_id *parent_id,
5195 struct got_object_id *commit_id, const char *path_prefix,
5196 int errcode, struct got_repository *repo)
5198 const struct got_error *err;
5199 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5200 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5201 struct check_path_prefix_arg cpp_arg;
5203 if (got_path_is_root_dir(path_prefix))
5206 err = got_object_open_as_commit(&commit, repo, commit_id);
5210 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5214 err = got_object_open_as_tree(&tree1, repo,
5215 got_object_commit_get_tree_id(parent_commit));
5219 err = got_object_open_as_tree(&tree2, repo,
5220 got_object_commit_get_tree_id(commit));
5224 cpp_arg.path_prefix = path_prefix;
5225 while (cpp_arg.path_prefix[0] == '/')
5226 cpp_arg.path_prefix++;
5227 cpp_arg.len = strlen(cpp_arg.path_prefix);
5228 cpp_arg.errcode = errcode;
5229 err = got_diff_tree(tree1, tree2, "", "", repo,
5230 check_path_prefix_in_diff, &cpp_arg, 0);
5233 got_object_tree_close(tree1);
5235 got_object_tree_close(tree2);
5237 got_object_commit_close(commit);
5239 got_object_commit_close(parent_commit);
5243 static const struct got_error *
5244 collect_commits(struct got_object_id_queue *commits,
5245 struct got_object_id *initial_commit_id,
5246 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5247 const char *path_prefix, int path_prefix_errcode,
5248 struct got_repository *repo)
5250 const struct got_error *err = NULL;
5251 struct got_commit_graph *graph = NULL;
5252 struct got_object_id *parent_id = NULL;
5253 struct got_object_qid *qid;
5254 struct got_object_id *commit_id = initial_commit_id;
5256 err = got_commit_graph_open(&graph, "/", 1);
5260 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5261 check_cancelled, NULL);
5264 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5265 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5266 check_cancelled, NULL);
5268 if (err->code == GOT_ERR_ITER_COMPLETED) {
5269 err = got_error_msg(GOT_ERR_ANCESTRY,
5270 "ran out of commits to rebase before "
5271 "youngest common ancestor commit has "
5276 err = check_path_prefix(parent_id, commit_id,
5277 path_prefix, path_prefix_errcode, repo);
5281 err = got_object_qid_alloc(&qid, commit_id);
5284 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5285 commit_id = parent_id;
5289 got_commit_graph_close(graph);
5293 static const struct got_error *
5294 cmd_rebase(int argc, char *argv[])
5296 const struct got_error *error = NULL;
5297 struct got_worktree *worktree = NULL;
5298 struct got_repository *repo = NULL;
5299 struct got_fileindex *fileindex = NULL;
5301 struct got_reference *branch = NULL;
5302 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5303 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5304 struct got_object_id *resume_commit_id = NULL;
5305 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5306 struct got_commit_object *commit = NULL;
5307 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5308 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5309 struct got_object_id_queue commits;
5310 struct got_pathlist_head merged_paths;
5311 const struct got_object_id_queue *parent_ids;
5312 struct got_object_qid *qid, *pid;
5314 SIMPLEQ_INIT(&commits);
5315 TAILQ_INIT(&merged_paths);
5317 while ((ch = getopt(argc, argv, "ac")) != -1) {
5323 continue_rebase = 1;
5335 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5336 "unveil", NULL) == -1)
5339 if (abort_rebase && continue_rebase)
5341 else if (abort_rebase || continue_rebase) {
5344 } else if (argc != 1)
5347 cwd = getcwd(NULL, 0);
5349 error = got_error_from_errno("getcwd");
5352 error = got_worktree_open(&worktree, cwd);
5356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5361 error = apply_unveil(got_repo_get_path(repo), 0,
5362 got_worktree_get_root_path(worktree));
5366 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5372 if (!rebase_in_progress) {
5373 error = got_error(GOT_ERR_NOT_REBASING);
5376 error = got_worktree_rebase_continue(&resume_commit_id,
5377 &new_base_branch, &tmp_branch, &branch, &fileindex,
5381 printf("Switching work tree to %s\n",
5382 got_ref_get_symref_target(new_base_branch));
5383 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5384 new_base_branch, update_progress, &did_something);
5387 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5388 goto done; /* nothing else to do */
5391 if (continue_rebase) {
5392 if (!rebase_in_progress) {
5393 error = got_error(GOT_ERR_NOT_REBASING);
5396 error = got_worktree_rebase_continue(&resume_commit_id,
5397 &new_base_branch, &tmp_branch, &branch, &fileindex,
5402 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5403 resume_commit_id, repo);
5407 yca_id = got_object_id_dup(resume_commit_id);
5408 if (yca_id == NULL) {
5409 error = got_error_from_errno("got_object_id_dup");
5413 error = got_ref_open(&branch, repo, argv[0], 0);
5418 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5422 if (!continue_rebase) {
5423 struct got_object_id *base_commit_id;
5425 base_commit_id = got_worktree_get_base_commit_id(worktree);
5426 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5427 base_commit_id, branch_head_commit_id, repo,
5428 check_cancelled, NULL);
5431 if (yca_id == NULL) {
5432 error = got_error_msg(GOT_ERR_ANCESTRY,
5433 "specified branch shares no common ancestry "
5434 "with work tree's branch");
5438 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5440 if (error->code != GOT_ERR_ANCESTRY)
5444 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5445 "specified branch resolves to a commit which "
5446 "is already contained in work tree's branch");
5449 error = got_worktree_rebase_prepare(&new_base_branch,
5450 &tmp_branch, &fileindex, worktree, branch, repo);
5455 commit_id = branch_head_commit_id;
5456 error = got_object_open_as_commit(&commit, repo, commit_id);
5460 parent_ids = got_object_commit_get_parent_ids(commit);
5461 pid = SIMPLEQ_FIRST(parent_ids);
5463 if (!continue_rebase) {
5465 error = got_worktree_rebase_abort(worktree, fileindex,
5466 repo, new_base_branch, update_progress,
5470 printf("Rebase of %s aborted\n",
5471 got_ref_get_name(branch));
5473 error = got_error(GOT_ERR_EMPTY_REBASE);
5476 error = collect_commits(&commits, commit_id, pid->id,
5477 yca_id, got_worktree_get_path_prefix(worktree),
5478 GOT_ERR_REBASE_PATH, repo);
5479 got_object_commit_close(commit);
5484 if (SIMPLEQ_EMPTY(&commits)) {
5485 if (continue_rebase) {
5486 error = rebase_complete(worktree, fileindex,
5487 branch, new_base_branch, tmp_branch, repo);
5490 /* Fast-forward the reference of the branch. */
5491 struct got_object_id *new_head_commit_id;
5493 error = got_ref_resolve(&new_head_commit_id, repo,
5497 error = got_object_id_str(&id_str, new_head_commit_id);
5498 printf("Forwarding %s to commit %s\n",
5499 got_ref_get_name(branch), id_str);
5501 error = got_ref_change_ref(branch,
5502 new_head_commit_id);
5509 SIMPLEQ_FOREACH(qid, &commits, entry) {
5510 commit_id = qid->id;
5511 parent_id = pid ? pid->id : yca_id;
5514 error = got_worktree_rebase_merge_files(&merged_paths,
5515 worktree, fileindex, parent_id, commit_id, repo,
5516 rebase_progress, &rebase_status, check_cancelled, NULL);
5520 if (rebase_status == GOT_STATUS_CONFLICT) {
5521 got_worktree_rebase_pathlist_free(&merged_paths);
5525 error = rebase_commit(&merged_paths, worktree, fileindex,
5526 tmp_branch, commit_id, repo);
5527 got_worktree_rebase_pathlist_free(&merged_paths);
5532 if (rebase_status == GOT_STATUS_CONFLICT) {
5533 error = got_worktree_rebase_postpone(worktree, fileindex);
5536 error = got_error_msg(GOT_ERR_CONFLICTS,
5537 "conflicts must be resolved before rebasing can continue");
5539 error = rebase_complete(worktree, fileindex, branch,
5540 new_base_branch, tmp_branch, repo);
5542 got_object_id_queue_free(&commits);
5543 free(branch_head_commit_id);
5544 free(resume_commit_id);
5547 got_object_commit_close(commit);
5549 got_ref_close(branch);
5550 if (new_base_branch)
5551 got_ref_close(new_base_branch);
5553 got_ref_close(tmp_branch);
5555 got_worktree_close(worktree);
5557 got_repo_close(repo);
5562 usage_histedit(void)
5564 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5569 #define GOT_HISTEDIT_PICK 'p'
5570 #define GOT_HISTEDIT_EDIT 'e'
5571 #define GOT_HISTEDIT_FOLD 'f'
5572 #define GOT_HISTEDIT_DROP 'd'
5573 #define GOT_HISTEDIT_MESG 'm'
5575 static struct got_histedit_cmd {
5579 } got_histedit_cmds[] = {
5580 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5581 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5582 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5584 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5585 { GOT_HISTEDIT_MESG, "mesg",
5586 "single-line log message for commit above (open editor if empty)" },
5589 struct got_histedit_list_entry {
5590 TAILQ_ENTRY(got_histedit_list_entry) entry;
5591 struct got_object_id *commit_id;
5592 const struct got_histedit_cmd *cmd;
5595 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5597 static const struct got_error *
5598 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5599 FILE *f, struct got_repository *repo)
5601 const struct got_error *err = NULL;
5602 char *logmsg = NULL, *id_str = NULL;
5603 struct got_commit_object *commit = NULL;
5606 err = got_object_open_as_commit(&commit, repo, commit_id);
5610 err = get_short_logmsg(&logmsg, 34, commit);
5614 err = got_object_id_str(&id_str, commit_id);
5618 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5620 err = got_ferror(f, GOT_ERR_IO);
5623 got_object_commit_close(commit);
5629 static const struct got_error *
5630 histedit_write_commit_list(struct got_object_id_queue *commits,
5631 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5633 const struct got_error *err = NULL;
5634 struct got_object_qid *qid;
5636 if (SIMPLEQ_EMPTY(commits))
5637 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5639 SIMPLEQ_FOREACH(qid, commits, entry) {
5640 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5644 if (edit_logmsg_only) {
5645 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5647 err = got_ferror(f, GOT_ERR_IO);
5656 static const struct got_error *
5657 write_cmd_list(FILE *f, const char *branch_name,
5658 struct got_object_id_queue *commits)
5660 const struct got_error *err = NULL;
5663 struct got_object_qid *qid;
5665 qid = SIMPLEQ_FIRST(commits);
5666 err = got_object_id_str(&id_str, qid->id);
5671 "# Editing the history of branch '%s' starting at\n"
5673 "# Commits will be processed in order from top to "
5674 "bottom of this file.\n", branch_name, id_str);
5676 err = got_ferror(f, GOT_ERR_IO);
5680 n = fprintf(f, "# Available histedit commands:\n");
5682 err = got_ferror(f, GOT_ERR_IO);
5686 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5687 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5688 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5691 err = got_ferror(f, GOT_ERR_IO);
5700 static const struct got_error *
5701 histedit_syntax_error(int lineno)
5703 static char msg[42];
5706 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5708 if (ret == -1 || ret >= sizeof(msg))
5709 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5711 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5714 static const struct got_error *
5715 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5716 char *logmsg, struct got_repository *repo)
5718 const struct got_error *err;
5719 struct got_commit_object *folded_commit = NULL;
5720 char *id_str, *folded_logmsg = NULL;
5722 err = got_object_id_str(&id_str, hle->commit_id);
5726 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5730 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5733 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5734 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5735 folded_logmsg) == -1) {
5736 err = got_error_from_errno("asprintf");
5740 got_object_commit_close(folded_commit);
5742 free(folded_logmsg);
5746 static struct got_histedit_list_entry *
5747 get_folded_commits(struct got_histedit_list_entry *hle)
5749 struct got_histedit_list_entry *prev, *folded = NULL;
5751 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5752 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5753 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5754 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5756 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5762 static const struct got_error *
5763 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5764 struct got_repository *repo)
5766 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5767 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5768 const struct got_error *err = NULL;
5769 struct got_commit_object *commit = NULL;
5771 struct got_histedit_list_entry *folded = NULL;
5773 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5777 folded = get_folded_commits(hle);
5779 while (folded != hle) {
5780 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5781 folded = TAILQ_NEXT(folded, entry);
5784 err = append_folded_commit_msg(&new_msg, folded,
5790 folded = TAILQ_NEXT(folded, entry);
5794 err = got_object_id_str(&id_str, hle->commit_id);
5797 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5800 if (asprintf(&new_msg,
5801 "%s\n# original log message of commit %s: %s",
5802 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5803 err = got_error_from_errno("asprintf");
5809 err = got_object_id_str(&id_str, hle->commit_id);
5813 err = got_opentemp_named_fd(&logmsg_path, &fd,
5814 GOT_TMPDIR_STR "/got-logmsg");
5818 dprintf(fd, logmsg);
5821 err = get_editor(&editor);
5825 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5827 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5829 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5832 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5833 err = got_error_from_errno2("unlink", logmsg_path);
5839 got_object_commit_close(commit);
5843 static const struct got_error *
5844 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5845 FILE *f, struct got_repository *repo)
5847 const struct got_error *err = NULL;
5848 char *line = NULL, *p, *end;
5852 const struct got_histedit_cmd *cmd;
5853 struct got_object_id *commit_id = NULL;
5854 struct got_histedit_list_entry *hle = NULL;
5857 len = getline(&line, &size, f);
5859 const struct got_error *getline_err;
5862 getline_err = got_error_from_errno("getline");
5863 err = got_ferror(f, getline_err->code);
5868 while (isspace((unsigned char)p[0]))
5870 if (p[0] == '#' || p[0] == '\0') {
5876 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5877 cmd = &got_histedit_cmds[i];
5878 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5879 isspace((unsigned char)p[strlen(cmd->name)])) {
5880 p += strlen(cmd->name);
5883 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5888 if (i == nitems(got_histedit_cmds)) {
5889 err = histedit_syntax_error(lineno);
5892 while (isspace((unsigned char)p[0]))
5894 if (cmd->code == GOT_HISTEDIT_MESG) {
5895 if (hle == NULL || hle->logmsg != NULL) {
5896 err = got_error(GOT_ERR_HISTEDIT_CMD);
5900 err = histedit_edit_logmsg(hle, repo);
5904 hle->logmsg = strdup(p);
5905 if (hle->logmsg == NULL) {
5906 err = got_error_from_errno("strdup");
5915 while (end[0] && !isspace((unsigned char)end[0]))
5919 err = got_object_resolve_id_str(&commit_id, repo, p);
5921 /* override error code */
5922 err = histedit_syntax_error(lineno);
5926 hle = malloc(sizeof(*hle));
5928 err = got_error_from_errno("malloc");
5932 hle->commit_id = commit_id;
5937 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5945 static const struct got_error *
5946 histedit_check_script(struct got_histedit_list *histedit_cmds,
5947 struct got_object_id_queue *commits, struct got_repository *repo)
5949 const struct got_error *err = NULL;
5950 struct got_object_qid *qid;
5951 struct got_histedit_list_entry *hle;
5952 static char msg[80];
5955 if (TAILQ_EMPTY(histedit_cmds))
5956 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5957 "histedit script contains no commands");
5958 if (SIMPLEQ_EMPTY(commits))
5959 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5961 SIMPLEQ_FOREACH(qid, commits, entry) {
5962 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5963 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5967 err = got_object_id_str(&id_str, qid->id);
5970 snprintf(msg, sizeof(msg),
5971 "commit %s missing from histedit script", id_str);
5973 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5977 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5978 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5979 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5980 "last commit in histedit script cannot be folded");
5985 static const struct got_error *
5986 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5987 const char *path, struct got_object_id_queue *commits,
5988 struct got_repository *repo)
5990 const struct got_error *err = NULL;
5994 err = get_editor(&editor);
5998 if (spawn_editor(editor, path) == -1) {
5999 err = got_error_from_errno("failed spawning editor");
6003 f = fopen(path, "r");
6005 err = got_error_from_errno("fopen");
6008 err = histedit_parse_list(histedit_cmds, f, repo);
6012 err = histedit_check_script(histedit_cmds, commits, repo);
6014 if (f && fclose(f) != 0 && err == NULL)
6015 err = got_error_from_errno("fclose");
6020 static const struct got_error *
6021 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6022 struct got_object_id_queue *, const char *, const char *,
6023 struct got_repository *);
6025 static const struct got_error *
6026 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6027 struct got_object_id_queue *commits, const char *branch_name,
6028 int edit_logmsg_only, struct got_repository *repo)
6030 const struct got_error *err;
6034 err = got_opentemp_named(&path, &f, "got-histedit");
6038 err = write_cmd_list(f, branch_name, commits);
6042 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6046 if (edit_logmsg_only) {
6048 err = histedit_parse_list(histedit_cmds, f, repo);
6050 if (fclose(f) != 0) {
6051 err = got_error_from_errno("fclose");
6055 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6057 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6058 err->code != GOT_ERR_HISTEDIT_CMD)
6060 err = histedit_edit_list_retry(histedit_cmds, err,
6061 commits, path, branch_name, repo);
6065 if (f && fclose(f) != 0 && err == NULL)
6066 err = got_error_from_errno("fclose");
6067 if (path && unlink(path) != 0 && err == NULL)
6068 err = got_error_from_errno2("unlink", path);
6073 static const struct got_error *
6074 histedit_save_list(struct got_histedit_list *histedit_cmds,
6075 struct got_worktree *worktree, struct got_repository *repo)
6077 const struct got_error *err = NULL;
6080 struct got_histedit_list_entry *hle;
6081 struct got_commit_object *commit = NULL;
6083 err = got_worktree_get_histedit_script_path(&path, worktree);
6087 f = fopen(path, "w");
6089 err = got_error_from_errno2("fopen", path);
6092 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6093 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6099 int n = fprintf(f, "%c %s\n",
6100 GOT_HISTEDIT_MESG, hle->logmsg);
6102 err = got_ferror(f, GOT_ERR_IO);
6108 if (f && fclose(f) != 0 && err == NULL)
6109 err = got_error_from_errno("fclose");
6112 got_object_commit_close(commit);
6117 histedit_free_list(struct got_histedit_list *histedit_cmds)
6119 struct got_histedit_list_entry *hle;
6121 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6122 TAILQ_REMOVE(histedit_cmds, hle, entry);
6127 static const struct got_error *
6128 histedit_load_list(struct got_histedit_list *histedit_cmds,
6129 const char *path, struct got_repository *repo)
6131 const struct got_error *err = NULL;
6134 f = fopen(path, "r");
6136 err = got_error_from_errno2("fopen", path);
6140 err = histedit_parse_list(histedit_cmds, f, repo);
6142 if (f && fclose(f) != 0 && err == NULL)
6143 err = got_error_from_errno("fclose");
6147 static const struct got_error *
6148 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6149 const struct got_error *edit_err, struct got_object_id_queue *commits,
6150 const char *path, const char *branch_name, struct got_repository *repo)
6152 const struct got_error *err = NULL, *prev_err = edit_err;
6155 while (resp != 'c' && resp != 'r' && resp != 'a') {
6156 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6157 "or (a)bort: ", getprogname(), prev_err->msg);
6162 histedit_free_list(histedit_cmds);
6163 err = histedit_run_editor(histedit_cmds, path, commits,
6166 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6167 err->code != GOT_ERR_HISTEDIT_CMD)
6174 } else if (resp == 'r') {
6175 histedit_free_list(histedit_cmds);
6176 err = histedit_edit_script(histedit_cmds,
6177 commits, branch_name, 0, repo);
6179 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6180 err->code != GOT_ERR_HISTEDIT_CMD)
6187 } else if (resp == 'a') {
6188 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6191 printf("invalid response '%c'\n", resp);
6197 static const struct got_error *
6198 histedit_complete(struct got_worktree *worktree,
6199 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6200 struct got_reference *branch, struct got_repository *repo)
6202 printf("Switching work tree to %s\n",
6203 got_ref_get_symref_target(branch));
6204 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6208 static const struct got_error *
6209 show_histedit_progress(struct got_commit_object *commit,
6210 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6212 const struct got_error *err;
6213 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6215 err = got_object_id_str(&old_id_str, hle->commit_id);
6220 err = got_object_id_str(&new_id_str, new_id);
6225 old_id_str[12] = '\0';
6227 new_id_str[12] = '\0';
6230 logmsg = strdup(hle->logmsg);
6231 if (logmsg == NULL) {
6232 err = got_error_from_errno("strdup");
6235 trim_logmsg(logmsg, 42);
6237 err = get_short_logmsg(&logmsg, 42, commit);
6242 switch (hle->cmd->code) {
6243 case GOT_HISTEDIT_PICK:
6244 case GOT_HISTEDIT_EDIT:
6245 printf("%s -> %s: %s\n", old_id_str,
6246 new_id_str ? new_id_str : "no-op change", logmsg);
6248 case GOT_HISTEDIT_DROP:
6249 case GOT_HISTEDIT_FOLD:
6250 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6262 static const struct got_error *
6263 histedit_commit(struct got_pathlist_head *merged_paths,
6264 struct got_worktree *worktree, struct got_fileindex *fileindex,
6265 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6266 struct got_repository *repo)
6268 const struct got_error *err;
6269 struct got_commit_object *commit;
6270 struct got_object_id *new_commit_id;
6272 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6273 && hle->logmsg == NULL) {
6274 err = histedit_edit_logmsg(hle, repo);
6279 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6283 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6284 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6287 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6289 err = show_histedit_progress(commit, hle, NULL);
6291 err = show_histedit_progress(commit, hle, new_commit_id);
6292 free(new_commit_id);
6295 got_object_commit_close(commit);
6299 static const struct got_error *
6300 histedit_skip_commit(struct got_histedit_list_entry *hle,
6301 struct got_worktree *worktree, struct got_repository *repo)
6303 const struct got_error *error;
6304 struct got_commit_object *commit;
6306 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6311 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6315 error = show_histedit_progress(commit, hle, NULL);
6316 got_object_commit_close(commit);
6320 static const struct got_error *
6321 check_local_changes(void *arg, unsigned char status,
6322 unsigned char staged_status, const char *path,
6323 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6324 struct got_object_id *commit_id, int dirfd, const char *de_name)
6326 int *have_local_changes = arg;
6329 case GOT_STATUS_ADD:
6330 case GOT_STATUS_DELETE:
6331 case GOT_STATUS_MODIFY:
6332 case GOT_STATUS_CONFLICT:
6333 *have_local_changes = 1;
6334 return got_error(GOT_ERR_CANCELLED);
6339 switch (staged_status) {
6340 case GOT_STATUS_ADD:
6341 case GOT_STATUS_DELETE:
6342 case GOT_STATUS_MODIFY:
6343 *have_local_changes = 1;
6344 return got_error(GOT_ERR_CANCELLED);
6352 static const struct got_error *
6353 cmd_histedit(int argc, char *argv[])
6355 const struct got_error *error = NULL;
6356 struct got_worktree *worktree = NULL;
6357 struct got_fileindex *fileindex = NULL;
6358 struct got_repository *repo = NULL;
6360 struct got_reference *branch = NULL;
6361 struct got_reference *tmp_branch = NULL;
6362 struct got_object_id *resume_commit_id = NULL;
6363 struct got_object_id *base_commit_id = NULL;
6364 struct got_object_id *head_commit_id = NULL;
6365 struct got_commit_object *commit = NULL;
6366 int ch, rebase_in_progress = 0, did_something;
6367 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6368 int edit_logmsg_only = 0;
6369 const char *edit_script_path = NULL;
6370 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6371 struct got_object_id_queue commits;
6372 struct got_pathlist_head merged_paths;
6373 const struct got_object_id_queue *parent_ids;
6374 struct got_object_qid *pid;
6375 struct got_histedit_list histedit_cmds;
6376 struct got_histedit_list_entry *hle;
6378 SIMPLEQ_INIT(&commits);
6379 TAILQ_INIT(&histedit_cmds);
6380 TAILQ_INIT(&merged_paths);
6382 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6391 edit_script_path = optarg;
6394 edit_logmsg_only = 1;
6406 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6407 "unveil", NULL) == -1)
6410 if (abort_edit && continue_edit)
6411 errx(1, "histedit's -a and -c options are mutually exclusive");
6412 if (edit_script_path && edit_logmsg_only)
6413 errx(1, "histedit's -F and -m options are mutually exclusive");
6414 if (abort_edit && edit_logmsg_only)
6415 errx(1, "histedit's -a and -m options are mutually exclusive");
6416 if (continue_edit && edit_logmsg_only)
6417 errx(1, "histedit's -c and -m options are mutually exclusive");
6422 * This command cannot apply unveil(2) in all cases because the
6423 * user may choose to run an editor to edit the histedit script
6424 * and to edit individual commit log messages.
6425 * unveil(2) traverses exec(2); if an editor is used we have to
6426 * apply unveil after edit script and log messages have been written.
6427 * XXX TODO: Make use of unveil(2) where possible.
6430 cwd = getcwd(NULL, 0);
6432 error = got_error_from_errno("getcwd");
6435 error = got_worktree_open(&worktree, cwd);
6439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6444 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6447 if (rebase_in_progress) {
6448 error = got_error(GOT_ERR_REBASING);
6452 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6456 if (edit_in_progress && edit_logmsg_only) {
6457 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6458 "histedit operation is in progress in this "
6459 "work tree and must be continued or aborted "
6460 "before the -m option can be used");
6464 if (edit_in_progress && abort_edit) {
6465 error = got_worktree_histedit_continue(&resume_commit_id,
6466 &tmp_branch, &branch, &base_commit_id, &fileindex,
6470 printf("Switching work tree to %s\n",
6471 got_ref_get_symref_target(branch));
6472 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6473 branch, base_commit_id, update_progress, &did_something);
6476 printf("Histedit of %s aborted\n",
6477 got_ref_get_symref_target(branch));
6478 goto done; /* nothing else to do */
6479 } else if (abort_edit) {
6480 error = got_error(GOT_ERR_NOT_HISTEDIT);
6484 if (continue_edit) {
6487 if (!edit_in_progress) {
6488 error = got_error(GOT_ERR_NOT_HISTEDIT);
6492 error = got_worktree_get_histedit_script_path(&path, worktree);
6496 error = histedit_load_list(&histedit_cmds, path, repo);
6501 error = got_worktree_histedit_continue(&resume_commit_id,
6502 &tmp_branch, &branch, &base_commit_id, &fileindex,
6507 error = got_ref_resolve(&head_commit_id, repo, branch);
6511 error = got_object_open_as_commit(&commit, repo,
6515 parent_ids = got_object_commit_get_parent_ids(commit);
6516 pid = SIMPLEQ_FIRST(parent_ids);
6518 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6521 error = collect_commits(&commits, head_commit_id, pid->id,
6522 base_commit_id, got_worktree_get_path_prefix(worktree),
6523 GOT_ERR_HISTEDIT_PATH, repo);
6524 got_object_commit_close(commit);
6529 if (edit_in_progress) {
6530 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6534 error = got_ref_open(&branch, repo,
6535 got_worktree_get_head_ref_name(worktree), 0);
6539 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6540 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6541 "will not edit commit history of a branch outside "
6542 "the \"refs/heads/\" reference namespace");
6546 error = got_ref_resolve(&head_commit_id, repo, branch);
6547 got_ref_close(branch);
6552 error = got_object_open_as_commit(&commit, repo,
6556 parent_ids = got_object_commit_get_parent_ids(commit);
6557 pid = SIMPLEQ_FIRST(parent_ids);
6559 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6562 error = collect_commits(&commits, head_commit_id, pid->id,
6563 got_worktree_get_base_commit_id(worktree),
6564 got_worktree_get_path_prefix(worktree),
6565 GOT_ERR_HISTEDIT_PATH, repo);
6566 got_object_commit_close(commit);
6571 if (SIMPLEQ_EMPTY(&commits)) {
6572 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6576 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6577 &base_commit_id, &fileindex, worktree, repo);
6581 if (edit_script_path) {
6582 error = histedit_load_list(&histedit_cmds,
6583 edit_script_path, repo);
6585 got_worktree_histedit_abort(worktree, fileindex,
6586 repo, branch, base_commit_id,
6587 update_progress, &did_something);
6591 const char *branch_name;
6592 branch_name = got_ref_get_symref_target(branch);
6593 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6595 error = histedit_edit_script(&histedit_cmds, &commits,
6596 branch_name, edit_logmsg_only, repo);
6598 got_worktree_histedit_abort(worktree, fileindex,
6599 repo, branch, base_commit_id,
6600 update_progress, &did_something);
6606 error = histedit_save_list(&histedit_cmds, worktree,
6609 got_worktree_histedit_abort(worktree, fileindex,
6610 repo, branch, base_commit_id,
6611 update_progress, &did_something);
6617 error = histedit_check_script(&histedit_cmds, &commits, repo);
6621 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6622 if (resume_commit_id) {
6623 if (got_object_id_cmp(hle->commit_id,
6624 resume_commit_id) != 0)
6627 resume_commit_id = NULL;
6628 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6629 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6630 error = histedit_skip_commit(hle, worktree,
6635 struct got_pathlist_head paths;
6636 int have_changes = 0;
6639 error = got_pathlist_append(&paths, "", NULL);
6642 error = got_worktree_status(worktree, &paths,
6643 repo, check_local_changes, &have_changes,
6644 check_cancelled, NULL);
6645 got_pathlist_free(&paths);
6647 if (error->code != GOT_ERR_CANCELLED)
6649 if (sigint_received || sigpipe_received)
6653 error = histedit_commit(NULL, worktree,
6654 fileindex, tmp_branch, hle, repo);
6658 error = got_object_open_as_commit(
6659 &commit, repo, hle->commit_id);
6662 error = show_histedit_progress(commit,
6664 got_object_commit_close(commit);
6673 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6674 error = histedit_skip_commit(hle, worktree, repo);
6680 error = got_object_open_as_commit(&commit, repo,
6684 parent_ids = got_object_commit_get_parent_ids(commit);
6685 pid = SIMPLEQ_FIRST(parent_ids);
6687 error = got_worktree_histedit_merge_files(&merged_paths,
6688 worktree, fileindex, pid->id, hle->commit_id, repo,
6689 rebase_progress, &rebase_status, check_cancelled, NULL);
6692 got_object_commit_close(commit);
6695 if (rebase_status == GOT_STATUS_CONFLICT) {
6696 got_worktree_rebase_pathlist_free(&merged_paths);
6700 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6702 error = got_object_id_str(&id_str, hle->commit_id);
6705 printf("Stopping histedit for amending commit %s\n",
6708 got_worktree_rebase_pathlist_free(&merged_paths);
6709 error = got_worktree_histedit_postpone(worktree,
6714 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6715 error = histedit_skip_commit(hle, worktree, repo);
6721 error = histedit_commit(&merged_paths, worktree, fileindex,
6722 tmp_branch, hle, repo);
6723 got_worktree_rebase_pathlist_free(&merged_paths);
6728 if (rebase_status == GOT_STATUS_CONFLICT) {
6729 error = got_worktree_histedit_postpone(worktree, fileindex);
6732 error = got_error_msg(GOT_ERR_CONFLICTS,
6733 "conflicts must be resolved before rebasing can continue");
6735 error = histedit_complete(worktree, fileindex, tmp_branch,
6738 got_object_id_queue_free(&commits);
6739 histedit_free_list(&histedit_cmds);
6740 free(head_commit_id);
6741 free(base_commit_id);
6742 free(resume_commit_id);
6744 got_object_commit_close(commit);
6746 got_ref_close(branch);
6748 got_ref_close(tmp_branch);
6750 got_worktree_close(worktree);
6752 got_repo_close(repo);
6757 usage_integrate(void)
6759 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6763 static const struct got_error *
6764 cmd_integrate(int argc, char *argv[])
6766 const struct got_error *error = NULL;
6767 struct got_repository *repo = NULL;
6768 struct got_worktree *worktree = NULL;
6769 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6770 const char *branch_arg = NULL;
6771 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6772 struct got_fileindex *fileindex = NULL;
6773 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6774 int ch, did_something = 0;
6776 while ((ch = getopt(argc, argv, "")) != -1) {
6789 branch_arg = argv[0];
6791 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6792 "unveil", NULL) == -1)
6795 cwd = getcwd(NULL, 0);
6797 error = got_error_from_errno("getcwd");
6801 error = got_worktree_open(&worktree, cwd);
6805 error = check_rebase_or_histedit_in_progress(worktree);
6809 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6814 error = apply_unveil(got_repo_get_path(repo), 0,
6815 got_worktree_get_root_path(worktree));
6819 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6820 error = got_error_from_errno("asprintf");
6824 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6825 &base_branch_ref, worktree, refname, repo);
6829 refname = strdup(got_ref_get_name(branch_ref));
6830 if (refname == NULL) {
6831 error = got_error_from_errno("strdup");
6832 got_worktree_integrate_abort(worktree, fileindex, repo,
6833 branch_ref, base_branch_ref);
6836 base_refname = strdup(got_ref_get_name(base_branch_ref));
6837 if (base_refname == NULL) {
6838 error = got_error_from_errno("strdup");
6839 got_worktree_integrate_abort(worktree, fileindex, repo,
6840 branch_ref, base_branch_ref);
6844 error = got_ref_resolve(&commit_id, repo, branch_ref);
6848 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6852 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6853 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6854 "specified branch has already been integrated");
6855 got_worktree_integrate_abort(worktree, fileindex, repo,
6856 branch_ref, base_branch_ref);
6860 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6862 if (error->code == GOT_ERR_ANCESTRY)
6863 error = got_error(GOT_ERR_REBASE_REQUIRED);
6864 got_worktree_integrate_abort(worktree, fileindex, repo,
6865 branch_ref, base_branch_ref);
6869 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6870 branch_ref, base_branch_ref, update_progress, &did_something,
6871 check_cancelled, NULL);
6875 printf("Integrated %s into %s\n", refname, base_refname);
6878 got_repo_close(repo);
6880 got_worktree_close(worktree);
6882 free(base_commit_id);
6892 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6893 "[file-path ...]\n",
6898 static const struct got_error *
6899 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6900 const char *path, struct got_object_id *blob_id,
6901 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6902 int dirfd, const char *de_name)
6904 const struct got_error *err = NULL;
6905 char *id_str = NULL;
6907 if (staged_status != GOT_STATUS_ADD &&
6908 staged_status != GOT_STATUS_MODIFY &&
6909 staged_status != GOT_STATUS_DELETE)
6912 if (staged_status == GOT_STATUS_ADD ||
6913 staged_status == GOT_STATUS_MODIFY)
6914 err = got_object_id_str(&id_str, staged_blob_id);
6916 err = got_object_id_str(&id_str, blob_id);
6920 printf("%s %c %s\n", id_str, staged_status, path);
6925 static const struct got_error *
6926 cmd_stage(int argc, char *argv[])
6928 const struct got_error *error = NULL;
6929 struct got_repository *repo = NULL;
6930 struct got_worktree *worktree = NULL;
6932 struct got_pathlist_head paths;
6933 struct got_pathlist_entry *pe;
6934 int ch, list_stage = 0, pflag = 0;
6935 FILE *patch_script_file = NULL;
6936 const char *patch_script_path = NULL;
6937 struct choose_patch_arg cpa;
6941 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6950 patch_script_path = optarg;
6962 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6963 "unveil", NULL) == -1)
6966 if (list_stage && (pflag || patch_script_path))
6967 errx(1, "-l option cannot be used with other options");
6968 if (patch_script_path && !pflag)
6969 errx(1, "-F option can only be used together with -p option");
6971 cwd = getcwd(NULL, 0);
6973 error = got_error_from_errno("getcwd");
6977 error = got_worktree_open(&worktree, cwd);
6981 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6986 if (patch_script_path) {
6987 patch_script_file = fopen(patch_script_path, "r");
6988 if (patch_script_file == NULL) {
6989 error = got_error_from_errno2("fopen",
6994 error = apply_unveil(got_repo_get_path(repo), 0,
6995 got_worktree_get_root_path(worktree));
6999 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7004 error = got_worktree_status(worktree, &paths, repo,
7005 print_stage, NULL, check_cancelled, NULL);
7007 cpa.patch_script_file = patch_script_file;
7008 cpa.action = "stage";
7009 error = got_worktree_stage(worktree, &paths,
7010 pflag ? NULL : print_status, NULL,
7011 pflag ? choose_patch : NULL, &cpa, repo);
7014 if (patch_script_file && fclose(patch_script_file) == EOF &&
7016 error = got_error_from_errno2("fclose", patch_script_path);
7018 got_repo_close(repo);
7020 got_worktree_close(worktree);
7021 TAILQ_FOREACH(pe, &paths, entry)
7022 free((char *)pe->path);
7023 got_pathlist_free(&paths);
7031 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7032 "[file-path ...]\n",
7038 static const struct got_error *
7039 cmd_unstage(int argc, char *argv[])
7041 const struct got_error *error = NULL;
7042 struct got_repository *repo = NULL;
7043 struct got_worktree *worktree = NULL;
7045 struct got_pathlist_head paths;
7046 struct got_pathlist_entry *pe;
7047 int ch, did_something = 0, pflag = 0;
7048 FILE *patch_script_file = NULL;
7049 const char *patch_script_path = NULL;
7050 struct choose_patch_arg cpa;
7054 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7060 patch_script_path = optarg;
7072 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7073 "unveil", NULL) == -1)
7076 if (patch_script_path && !pflag)
7077 errx(1, "-F option can only be used together with -p option");
7079 cwd = getcwd(NULL, 0);
7081 error = got_error_from_errno("getcwd");
7085 error = got_worktree_open(&worktree, cwd);
7089 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7094 if (patch_script_path) {
7095 patch_script_file = fopen(patch_script_path, "r");
7096 if (patch_script_file == NULL) {
7097 error = got_error_from_errno2("fopen",
7103 error = apply_unveil(got_repo_get_path(repo), 0,
7104 got_worktree_get_root_path(worktree));
7108 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7112 cpa.patch_script_file = patch_script_file;
7113 cpa.action = "unstage";
7114 error = got_worktree_unstage(worktree, &paths, update_progress,
7115 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7117 if (patch_script_file && fclose(patch_script_file) == EOF &&
7119 error = got_error_from_errno2("fclose", patch_script_path);
7121 got_repo_close(repo);
7123 got_worktree_close(worktree);
7124 TAILQ_FOREACH(pe, &paths, entry)
7125 free((char *)pe->path);
7126 got_pathlist_free(&paths);
7134 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7135 "arg1 [arg2 ...]\n", getprogname());
7139 static const struct got_error *
7140 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7142 const struct got_error *err;
7143 struct got_blob_object *blob;
7145 err = got_object_open_as_blob(&blob, repo, id, 8192);
7149 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7150 got_object_blob_close(blob);
7154 static const struct got_error *
7155 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7157 const struct got_error *err;
7158 struct got_tree_object *tree;
7161 err = got_object_open_as_tree(&tree, repo, id);
7165 nentries = got_object_tree_get_nentries(tree);
7166 for (i = 0; i < nentries; i++) {
7167 struct got_tree_entry *te;
7169 if (sigint_received || sigpipe_received)
7171 te = got_object_tree_get_entry(tree, i);
7172 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7175 fprintf(outfile, "%s %.7o %s\n", id_str,
7176 got_tree_entry_get_mode(te),
7177 got_tree_entry_get_name(te));
7181 got_object_tree_close(tree);
7185 static const struct got_error *
7186 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7188 const struct got_error *err;
7189 struct got_commit_object *commit;
7190 const struct got_object_id_queue *parent_ids;
7191 struct got_object_qid *pid;
7192 char *id_str = NULL;
7193 const char *logmsg = NULL;
7195 err = got_object_open_as_commit(&commit, repo, id);
7199 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7203 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7204 parent_ids = got_object_commit_get_parent_ids(commit);
7205 fprintf(outfile, "numparents %d\n",
7206 got_object_commit_get_nparents(commit));
7207 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7209 err = got_object_id_str(&pid_str, pid->id);
7212 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7215 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7216 got_object_commit_get_author(commit),
7217 got_object_commit_get_author_time(commit));
7219 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7220 got_object_commit_get_author(commit),
7221 got_object_commit_get_committer_time(commit));
7223 logmsg = got_object_commit_get_logmsg_raw(commit);
7224 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7225 fprintf(outfile, "%s", logmsg);
7228 got_object_commit_close(commit);
7232 static const struct got_error *
7233 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7235 const struct got_error *err;
7236 struct got_tag_object *tag;
7237 char *id_str = NULL;
7238 const char *tagmsg = NULL;
7240 err = got_object_open_as_tag(&tag, repo, id);
7244 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7248 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7250 switch (got_object_tag_get_object_type(tag)) {
7251 case GOT_OBJ_TYPE_BLOB:
7252 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7253 GOT_OBJ_LABEL_BLOB);
7255 case GOT_OBJ_TYPE_TREE:
7256 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7257 GOT_OBJ_LABEL_TREE);
7259 case GOT_OBJ_TYPE_COMMIT:
7260 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7261 GOT_OBJ_LABEL_COMMIT);
7263 case GOT_OBJ_TYPE_TAG:
7264 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7271 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7272 got_object_tag_get_name(tag));
7274 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7275 got_object_tag_get_tagger(tag),
7276 got_object_tag_get_tagger_time(tag));
7278 tagmsg = got_object_tag_get_message(tag);
7279 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7280 fprintf(outfile, "%s", tagmsg);
7283 got_object_tag_close(tag);
7287 static const struct got_error *
7288 cmd_cat(int argc, char *argv[])
7290 const struct got_error *error;
7291 struct got_repository *repo = NULL;
7292 struct got_worktree *worktree = NULL;
7293 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7294 const char *commit_id_str = NULL;
7295 struct got_object_id *id = NULL, *commit_id = NULL;
7296 int ch, obj_type, i, force_path = 0;
7299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7304 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7307 commit_id_str = optarg;
7310 repo_path = realpath(optarg, NULL);
7311 if (repo_path == NULL)
7312 return got_error_from_errno2("realpath",
7314 got_path_strip_trailing_slashes(repo_path);
7328 cwd = getcwd(NULL, 0);
7330 error = got_error_from_errno("getcwd");
7333 error = got_worktree_open(&worktree, cwd);
7334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7337 if (repo_path == NULL) {
7339 got_worktree_get_repo_path(worktree));
7340 if (repo_path == NULL) {
7341 error = got_error_from_errno("strdup");
7347 if (repo_path == NULL) {
7348 repo_path = getcwd(NULL, 0);
7349 if (repo_path == NULL)
7350 return got_error_from_errno("getcwd");
7353 error = got_repo_open(&repo, repo_path, NULL);
7358 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7362 if (commit_id_str == NULL)
7363 commit_id_str = GOT_REF_HEAD;
7364 error = got_repo_match_object_id(&commit_id, NULL,
7365 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7369 for (i = 0; i < argc; i++) {
7371 error = got_object_id_by_path(&id, repo, commit_id,
7376 error = got_repo_match_object_id(&id, &label, argv[i],
7377 GOT_OBJ_TYPE_ANY, 0, repo);
7379 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7380 error->code != GOT_ERR_NOT_REF)
7382 error = got_object_id_by_path(&id, repo,
7383 commit_id, argv[i]);
7389 error = got_object_get_type(&obj_type, repo, id);
7394 case GOT_OBJ_TYPE_BLOB:
7395 error = cat_blob(id, repo, stdout);
7397 case GOT_OBJ_TYPE_TREE:
7398 error = cat_tree(id, repo, stdout);
7400 case GOT_OBJ_TYPE_COMMIT:
7401 error = cat_commit(id, repo, stdout);
7403 case GOT_OBJ_TYPE_TAG:
7404 error = cat_tag(id, repo, stdout);
7407 error = got_error(GOT_ERR_OBJ_TYPE);
7422 got_worktree_close(worktree);
7424 const struct got_error *repo_error;
7425 repo_error = got_repo_close(repo);