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("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
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);
372 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
373 "[-r repository-path] [-I pattern] path\n", getprogname());
378 spawn_editor(const char *editor, const char *file)
381 sig_t sighup, sigint, sigquit;
384 sighup = signal(SIGHUP, SIG_IGN);
385 sigint = signal(SIGINT, SIG_IGN);
386 sigquit = signal(SIGQUIT, SIG_IGN);
388 switch (pid = fork()) {
392 execl(editor, editor, file, (char *)NULL);
396 while (waitpid(pid, &st, 0) == -1)
401 (void)signal(SIGHUP, sighup);
402 (void)signal(SIGINT, sigint);
403 (void)signal(SIGQUIT, sigquit);
405 if (!WIFEXITED(st)) {
410 return WEXITSTATUS(st);
413 static const struct got_error *
414 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 const char *initial_content)
417 const struct got_error *err = NULL;
421 int content_changed = 0;
426 if (stat(logmsg_path, &st) == -1)
427 return got_error_from_errno2("stat", logmsg_path);
429 if (spawn_editor(editor, logmsg_path) == -1)
430 return got_error_from_errno("failed spawning editor");
432 if (stat(logmsg_path, &st2) == -1)
433 return got_error_from_errno("stat");
435 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
436 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "no changes made to commit message, aborting");
439 *logmsg = malloc(st2.st_size + 1);
441 return got_error_from_errno("malloc");
445 fp = fopen(logmsg_path, "r");
447 err = got_error_from_errno("fopen");
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 if (!content_changed && strcmp(buf, initial_content) != 0)
453 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
454 continue; /* remove comments and leading empty lines */
455 len = strlcat(*logmsg, buf, st2.st_size);
459 while (len > 0 && (*logmsg)[len - 1] == '\n') {
460 (*logmsg)[len - 1] = '\0';
464 if (len == 0 || !content_changed)
465 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
466 "commit message cannot be empty, aborting");
475 static const struct got_error *
476 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
477 const char *path_dir, const char *branch_name)
479 char *initial_content = NULL;
480 const struct got_error *err = NULL;
483 if (asprintf(&initial_content,
484 "\n# %s to be imported to branch %s\n", path_dir,
486 return got_error_from_errno("asprintf");
488 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
492 dprintf(fd, initial_content);
495 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
497 free(initial_content);
501 static const struct got_error *
502 import_progress(void *arg, const char *path)
504 printf("A %s\n", path);
508 static const struct got_error *
509 get_author(char **author, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 const char *got_author, *name, *email;
516 name = got_repo_get_gitconfig_author_name(repo);
517 email = got_repo_get_gitconfig_author_email(repo);
519 if (asprintf(author, "%s <%s>", name, email) == -1)
520 return got_error_from_errno("asprintf");
524 got_author = getenv("GOT_AUTHOR");
525 if (got_author == NULL) {
526 name = got_repo_get_global_gitconfig_author_name(repo);
527 email = got_repo_get_global_gitconfig_author_email(repo);
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
533 /* TODO: Look up user in password database? */
534 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 *author = strdup(got_author);
539 return got_error_from_errno("strdup");
542 * Really dumb email address check; we're only doing this to
543 * avoid git's object parser breaking on commits we create.
545 while (*got_author && *got_author != '<')
547 if (*got_author != '<') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
551 while (*got_author && *got_author != '@')
553 if (*got_author != '@') {
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
557 while (*got_author && *got_author != '>')
559 if (*got_author != '>')
560 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
569 static const struct got_error *
570 get_gitconfig_path(char **gitconfig_path)
572 const char *homedir = getenv("HOME");
574 *gitconfig_path = NULL;
576 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
577 return got_error_from_errno("asprintf");
583 static const struct got_error *
584 cmd_import(int argc, char *argv[])
586 const struct got_error *error = NULL;
587 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
588 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
589 const char *branch_name = "main";
590 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
591 struct got_repository *repo = NULL;
592 struct got_reference *branch_ref = NULL, *head_ref = NULL;
593 struct got_object_id *new_commit_id = NULL;
595 struct got_pathlist_head ignores;
596 struct got_pathlist_entry *pe;
597 int preserve_logmsg = 0;
599 TAILQ_INIT(&ignores);
601 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
604 branch_name = optarg;
607 logmsg = strdup(optarg);
608 if (logmsg == NULL) {
609 error = got_error_from_errno("strdup");
614 repo_path = realpath(optarg, NULL);
615 if (repo_path == NULL) {
616 error = got_error_from_errno2("realpath",
622 if (optarg[0] == '\0')
624 error = got_pathlist_insert(&pe, &ignores, optarg,
639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 if (repo_path == NULL) {
648 repo_path = getcwd(NULL, 0);
649 if (repo_path == NULL)
650 return got_error_from_errno("getcwd");
652 got_path_strip_trailing_slashes(repo_path);
653 error = get_gitconfig_path(&gitconfig_path);
656 error = got_repo_open(&repo, repo_path, gitconfig_path);
660 error = get_author(&author, repo);
665 * Don't let the user create a branch name with a leading '-'.
666 * While technically a valid reference name, this case is usually
667 * an unintended typo.
669 if (branch_name[0] == '-')
670 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
672 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
673 error = got_error_from_errno("asprintf");
677 error = got_ref_open(&branch_ref, repo, refname, 0);
679 if (error->code != GOT_ERR_NOT_REF)
682 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
683 "import target branch already exists");
687 path_dir = realpath(argv[0], NULL);
688 if (path_dir == NULL) {
689 error = got_error_from_errno2("realpath", argv[0]);
692 got_path_strip_trailing_slashes(path_dir);
695 * unveil(2) traverses exec(2); if an editor is used we have
696 * to apply unveil after the log message has been written.
698 if (logmsg == NULL || strlen(logmsg) == 0) {
699 error = get_editor(&editor);
703 error = collect_import_msg(&logmsg, &logmsg_path, editor,
706 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
713 if (unveil(path_dir, "r") != 0) {
714 error = got_error_from_errno2("unveil", path_dir);
720 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
727 error = got_repo_import(&new_commit_id, path_dir, logmsg,
728 author, &ignores, repo, import_progress, NULL);
735 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
742 error = got_ref_write(branch_ref, repo);
749 error = got_object_id_str(&id_str, new_commit_id);
756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
758 if (error->code != GOT_ERR_NOT_REF) {
764 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 error = got_ref_write(head_ref, repo);
780 printf("Created branch %s with commit %s\n",
781 got_ref_get_name(branch_ref), id_str);
783 if (preserve_logmsg) {
784 fprintf(stderr, "%s: log message preserved in %s\n",
785 getprogname(), logmsg_path);
786 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
787 error = got_error_from_errno2("unlink", logmsg_path);
796 free(gitconfig_path);
798 got_ref_close(branch_ref);
800 got_ref_close(head_ref);
807 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
808 "[-p prefix] repository-path [worktree-path]\n", getprogname());
813 show_worktree_base_ref_warning(void)
815 fprintf(stderr, "%s: warning: could not create a reference "
816 "to the work tree's base commit; the commit could be "
817 "garbage-collected by Git; making the repository "
818 "writable and running 'got update' will prevent this\n",
822 struct got_checkout_progress_arg {
823 const char *worktree_path;
824 int had_base_commit_ref_error;
827 static const struct got_error *
828 checkout_progress(void *arg, unsigned char status, const char *path)
830 struct got_checkout_progress_arg *a = arg;
832 /* Base commit bump happens silently. */
833 if (status == GOT_STATUS_BUMP_BASE)
836 if (status == GOT_STATUS_BASE_REF_ERR) {
837 a->had_base_commit_ref_error = 1;
841 while (path[0] == '/')
844 printf("%c %s/%s\n", status, a->worktree_path, path);
848 static const struct got_error *
849 check_cancelled(void *arg)
851 if (sigint_received || sigpipe_received)
852 return got_error(GOT_ERR_CANCELLED);
856 static const struct got_error *
857 check_linear_ancestry(struct got_object_id *commit_id,
858 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
859 struct got_repository *repo)
861 const struct got_error *err = NULL;
862 struct got_object_id *yca_id;
864 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
865 commit_id, base_commit_id, repo, check_cancelled, NULL);
870 return got_error(GOT_ERR_ANCESTRY);
873 * Require a straight line of history between the target commit
874 * and the work tree's base commit.
876 * Non-linear situations such as this require a rebase:
878 * (commit) D F (base_commit)
886 * 'got update' only handles linear cases:
887 * Update forwards in time: A (base/yca) - B - C - D (commit)
888 * Update backwards in time: D (base) - C - B - A (commit/yca)
890 if (allow_forwards_in_time_only) {
891 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
893 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
894 got_object_id_cmp(base_commit_id, yca_id) != 0)
895 return got_error(GOT_ERR_ANCESTRY);
901 static const struct got_error *
902 check_same_branch(struct got_object_id *commit_id,
903 struct got_reference *head_ref, struct got_object_id *yca_id,
904 struct got_repository *repo)
906 const struct got_error *err = NULL;
907 struct got_commit_graph *graph = NULL;
908 struct got_object_id *head_commit_id = NULL;
909 int is_same_branch = 0;
911 err = got_ref_resolve(&head_commit_id, repo, head_ref);
915 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
919 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
924 err = got_commit_graph_open(&graph, "/", 1);
928 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
929 check_cancelled, NULL);
934 struct got_object_id *id;
935 err = got_commit_graph_iter_next(&id, graph, repo,
936 check_cancelled, NULL);
938 if (err->code == GOT_ERR_ITER_COMPLETED)
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
946 if (got_object_id_cmp(id, commit_id) == 0) {
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
978 } else if (err->code != GOT_ERR_NO_OBJ)
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
983 err = got_ref_resolve(commit_id, repo, ref);
986 if (err->code != GOT_ERR_NOT_REF)
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix, allow_nonempty = 0;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1012 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1015 branch_name = optarg;
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1026 path_prefix = optarg;
1038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1039 "unveil", NULL) == -1)
1043 char *cwd, *base, *dotgit;
1044 repo_path = realpath(argv[0], NULL);
1045 if (repo_path == NULL)
1046 return got_error_from_errno2("realpath", argv[0]);
1047 cwd = getcwd(NULL, 0);
1049 error = got_error_from_errno("getcwd");
1052 if (path_prefix[0]) {
1053 base = basename(path_prefix);
1055 error = got_error_from_errno2("basename",
1060 base = basename(repo_path);
1062 error = got_error_from_errno2("basename",
1067 dotgit = strstr(base, ".git");
1070 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1071 error = got_error_from_errno("asprintf");
1076 } else if (argc == 2) {
1077 repo_path = realpath(argv[0], NULL);
1078 if (repo_path == NULL) {
1079 error = got_error_from_errno2("realpath", argv[0]);
1082 worktree_path = realpath(argv[1], NULL);
1083 if (worktree_path == NULL) {
1084 if (errno != ENOENT) {
1085 error = got_error_from_errno2("realpath",
1089 worktree_path = strdup(argv[1]);
1090 if (worktree_path == NULL) {
1091 error = got_error_from_errno("strdup");
1098 got_path_strip_trailing_slashes(repo_path);
1099 got_path_strip_trailing_slashes(worktree_path);
1101 error = got_repo_open(&repo, repo_path, NULL);
1105 /* Pre-create work tree path for unveil(2) */
1106 error = got_path_mkdir(worktree_path);
1108 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1109 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1111 if (!allow_nonempty &&
1112 !got_path_dir_is_empty(worktree_path)) {
1113 error = got_error_path(worktree_path,
1114 GOT_ERR_DIR_NOT_EMPTY);
1119 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1123 error = got_ref_open(&head_ref, repo, branch_name, 0);
1127 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1128 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1131 error = got_worktree_open(&worktree, worktree_path);
1135 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1139 if (!same_path_prefix) {
1140 error = got_error(GOT_ERR_PATH_PREFIX);
1144 if (commit_id_str) {
1145 struct got_object_id *commit_id;
1146 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1149 error = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (error != NULL) {
1155 error = check_same_branch(commit_id, head_ref, NULL, repo);
1158 error = got_worktree_set_base_commit_id(worktree, repo,
1165 error = got_pathlist_append(&paths, "", NULL);
1168 cpa.worktree_path = worktree_path;
1169 cpa.had_base_commit_ref_error = 0;
1170 error = got_worktree_checkout_files(worktree, &paths, repo,
1171 checkout_progress, &cpa, check_cancelled, NULL);
1175 printf("Now shut up and hack\n");
1176 if (cpa.had_base_commit_ref_error)
1177 show_worktree_base_ref_warning();
1180 got_pathlist_free(&paths);
1181 free(commit_id_str);
1183 free(worktree_path);
1190 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1195 static const struct got_error *
1196 update_progress(void *arg, unsigned char status, const char *path)
1198 int *did_something = arg;
1200 if (status == GOT_STATUS_EXISTS ||
1201 status == GOT_STATUS_BASE_REF_ERR)
1206 /* Base commit bump happens silently. */
1207 if (status == GOT_STATUS_BUMP_BASE)
1210 while (path[0] == '/')
1212 printf("%c %s\n", status, path);
1216 static const struct got_error *
1217 switch_head_ref(struct got_reference *head_ref,
1218 struct got_object_id *commit_id, struct got_worktree *worktree,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1223 int ref_has_moved = 0;
1225 /* Trivial case: switching between two different references. */
1226 if (strcmp(got_ref_get_name(head_ref),
1227 got_worktree_get_head_ref_name(worktree)) != 0) {
1228 printf("Switching work tree from %s to %s\n",
1229 got_worktree_get_head_ref_name(worktree),
1230 got_ref_get_name(head_ref));
1231 return got_worktree_set_head_ref(worktree, head_ref);
1234 err = check_linear_ancestry(commit_id,
1235 got_worktree_get_base_commit_id(worktree), 0, repo);
1237 if (err->code != GOT_ERR_ANCESTRY)
1244 /* Switching to a rebased branch with the same reference name. */
1245 err = got_object_id_str(&base_id_str,
1246 got_worktree_get_base_commit_id(worktree));
1249 printf("Reference %s now points at a different branch\n",
1250 got_worktree_get_head_ref_name(worktree));
1251 printf("Switching work tree from %s to %s\n", base_id_str,
1252 got_worktree_get_head_ref_name(worktree));
1256 static const struct got_error *
1257 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1259 const struct got_error *err;
1262 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1266 return got_error(GOT_ERR_REBASING);
1268 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1272 return got_error(GOT_ERR_HISTEDIT_BUSY);
1277 static const struct got_error *
1278 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1279 char *argv[], struct got_worktree *worktree)
1281 const struct got_error *err = NULL;
1288 return got_error_from_errno("strdup");
1289 return got_pathlist_append(paths, path, NULL);
1292 for (i = 0; i < argc; i++) {
1293 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1296 err = got_pathlist_append(paths, path, NULL);
1306 static const struct got_error *
1307 cmd_update(int argc, char *argv[])
1309 const struct got_error *error = NULL;
1310 struct got_repository *repo = NULL;
1311 struct got_worktree *worktree = NULL;
1312 char *worktree_path = NULL;
1313 struct got_object_id *commit_id = NULL;
1314 char *commit_id_str = NULL;
1315 const char *branch_name = NULL;
1316 struct got_reference *head_ref = NULL;
1317 struct got_pathlist_head paths;
1318 struct got_pathlist_entry *pe;
1319 int ch, did_something = 0;
1323 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1326 branch_name = optarg;
1329 commit_id_str = strdup(optarg);
1330 if (commit_id_str == NULL)
1331 return got_error_from_errno("strdup");
1343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1344 "unveil", NULL) == -1)
1347 worktree_path = getcwd(NULL, 0);
1348 if (worktree_path == NULL) {
1349 error = got_error_from_errno("getcwd");
1352 error = got_worktree_open(&worktree, worktree_path);
1356 error = check_rebase_or_histedit_in_progress(worktree);
1360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1365 error = apply_unveil(got_repo_get_path(repo), 0,
1366 got_worktree_get_root_path(worktree));
1370 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1374 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1375 got_worktree_get_head_ref_name(worktree), 0);
1378 if (commit_id_str == NULL) {
1379 error = got_ref_resolve(&commit_id, repo, head_ref);
1382 error = got_object_id_str(&commit_id_str, commit_id);
1386 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1387 free(commit_id_str);
1388 commit_id_str = NULL;
1391 error = got_object_id_str(&commit_id_str, commit_id);
1397 struct got_object_id *head_commit_id;
1398 TAILQ_FOREACH(pe, &paths, entry) {
1399 if (pe->path_len == 0)
1401 error = got_error_msg(GOT_ERR_BAD_PATH,
1402 "switching between branches requires that "
1403 "the entire work tree gets updated");
1406 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1409 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1411 free(head_commit_id);
1414 error = check_same_branch(commit_id, head_ref, NULL, repo);
1417 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1421 error = check_linear_ancestry(commit_id,
1422 got_worktree_get_base_commit_id(worktree), 0, repo);
1423 if (error != NULL) {
1424 if (error->code == GOT_ERR_ANCESTRY)
1425 error = got_error(GOT_ERR_BRANCH_MOVED);
1428 error = check_same_branch(commit_id, head_ref, NULL, repo);
1433 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1435 error = got_worktree_set_base_commit_id(worktree, repo,
1441 error = got_worktree_checkout_files(worktree, &paths, repo,
1442 update_progress, &did_something, check_cancelled, NULL);
1447 printf("Updated to commit %s\n", commit_id_str);
1449 printf("Already up-to-date\n");
1451 free(worktree_path);
1452 TAILQ_FOREACH(pe, &paths, entry)
1453 free((char *)pe->path);
1454 got_pathlist_free(&paths);
1456 free(commit_id_str);
1460 static const struct got_error *
1461 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1462 const char *path, int diff_context, int ignore_whitespace,
1463 struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1469 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1474 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1478 while (path[0] == '/')
1480 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1481 ignore_whitespace, stdout);
1484 got_object_blob_close(blob1);
1485 got_object_blob_close(blob2);
1489 static const struct got_error *
1490 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1491 const char *path, int diff_context, int ignore_whitespace,
1492 struct got_repository *repo)
1494 const struct got_error *err = NULL;
1495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1496 struct got_diff_blob_output_unidiff_arg arg;
1499 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1508 arg.diff_context = diff_context;
1509 arg.ignore_whitespace = ignore_whitespace;
1510 arg.outfile = stdout;
1511 while (path[0] == '/')
1513 err = got_diff_tree(tree1, tree2, path, path, repo,
1514 got_diff_blob_output_unidiff, &arg, 1);
1517 got_object_tree_close(tree1);
1519 got_object_tree_close(tree2);
1523 static const struct got_error *
1524 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1525 const char *path, int diff_context, struct got_repository *repo)
1527 const struct got_error *err = NULL;
1528 struct got_commit_object *pcommit = NULL;
1529 char *id_str1 = NULL, *id_str2 = NULL;
1530 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1531 struct got_object_qid *qid;
1533 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1535 err = got_object_open_as_commit(&pcommit, repo,
1541 if (path && path[0] != '\0') {
1543 err = got_object_id_by_path(&obj_id2, repo, id, path);
1546 err = got_object_id_str(&id_str2, obj_id2);
1552 err = got_object_id_by_path(&obj_id1, repo,
1558 err = got_object_id_str(&id_str1, obj_id1);
1564 err = got_object_get_type(&obj_type, repo, obj_id2);
1569 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1575 case GOT_OBJ_TYPE_TREE:
1576 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1580 err = got_error(GOT_ERR_OBJ_TYPE);
1586 obj_id2 = got_object_commit_get_tree_id(commit);
1587 err = got_object_id_str(&id_str2, obj_id2);
1590 obj_id1 = got_object_commit_get_tree_id(pcommit);
1591 err = got_object_id_str(&id_str1, obj_id1);
1594 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1595 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1602 got_object_commit_close(pcommit);
1607 get_datestr(time_t *time, char *datebuf)
1609 struct tm mytm, *tm;
1612 tm = gmtime_r(time, &mytm);
1615 s = asctime_r(tm, datebuf);
1618 p = strchr(s, '\n');
1624 static const struct got_error *
1625 match_logmsg(int *have_match, struct got_object_id *id,
1626 struct got_commit_object *commit, regex_t *regex)
1628 const struct got_error *err = NULL;
1629 regmatch_t regmatch;
1630 char *id_str = NULL, *logmsg = NULL;
1634 err = got_object_id_str(&id_str, id);
1638 err = got_object_commit_get_logmsg(&logmsg, commit);
1642 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1650 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1652 static const struct got_error *
1653 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1654 struct got_repository *repo, const char *path, int show_patch,
1655 int diff_context, struct got_reflist_head *refs)
1657 const struct got_error *err = NULL;
1658 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1660 time_t committer_time;
1661 const char *author, *committer;
1662 char *refs_str = NULL;
1663 struct got_reflist_entry *re;
1665 SIMPLEQ_FOREACH(re, refs, entry) {
1668 struct got_tag_object *tag = NULL;
1671 name = got_ref_get_name(re->ref);
1672 if (strcmp(name, GOT_REF_HEAD) == 0)
1674 if (strncmp(name, "refs/", 5) == 0)
1676 if (strncmp(name, "got/", 4) == 0)
1678 if (strncmp(name, "heads/", 6) == 0)
1680 if (strncmp(name, "remotes/", 8) == 0)
1682 if (strncmp(name, "tags/", 5) == 0) {
1683 err = got_object_open_as_tag(&tag, repo, re->id);
1685 if (err->code != GOT_ERR_OBJ_TYPE)
1687 /* Ref points at something other than a tag. */
1692 cmp = got_object_id_cmp(tag ?
1693 got_object_tag_get_object_id(tag) : re->id, id);
1695 got_object_tag_close(tag);
1699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1701 err = got_error_from_errno("asprintf");
1707 err = got_object_id_str(&id_str, id);
1711 printf(GOT_COMMIT_SEP_STR);
1712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1713 refs_str ? refs_str : "", refs_str ? ")" : "");
1718 printf("from: %s\n", got_object_commit_get_author(commit));
1719 committer_time = got_object_commit_get_committer_time(commit);
1720 datestr = get_datestr(&committer_time, datebuf);
1722 printf("date: %s UTC\n", datestr);
1723 author = got_object_commit_get_author(commit);
1724 committer = got_object_commit_get_committer(commit);
1725 if (strcmp(author, committer) != 0)
1726 printf("via: %s\n", committer);
1727 if (got_object_commit_get_nparents(commit) > 1) {
1728 const struct got_object_id_queue *parent_ids;
1729 struct got_object_qid *qid;
1731 parent_ids = got_object_commit_get_parent_ids(commit);
1732 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1733 err = got_object_id_str(&id_str, qid->id);
1736 printf("parent %d: %s\n", n++, id_str);
1741 err = got_object_commit_get_logmsg(&logmsg0, commit);
1747 line = strsep(&logmsg, "\n");
1749 printf(" %s\n", line);
1754 err = print_patch(commit, id, path, diff_context, repo);
1759 if (fflush(stdout) != 0 && err == NULL)
1760 err = got_error_from_errno("fflush");
1764 static const struct got_error *
1765 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1766 const char *path, int show_patch, const char *search_pattern,
1767 int diff_context, int limit, int first_parent_traversal,
1768 struct got_reflist_head *refs)
1770 const struct got_error *err;
1771 struct got_commit_graph *graph;
1775 if (search_pattern &&
1776 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1777 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1779 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1782 err = got_commit_graph_iter_start(graph, root_id, repo,
1783 check_cancelled, NULL);
1787 struct got_commit_object *commit;
1788 struct got_object_id *id;
1790 if (sigint_received || sigpipe_received)
1793 err = got_commit_graph_iter_next(&id, graph, repo,
1794 check_cancelled, NULL);
1796 if (err->code == GOT_ERR_ITER_COMPLETED)
1803 err = got_object_open_as_commit(&commit, repo, id);
1807 if (search_pattern) {
1808 err = match_logmsg(&have_match, id, commit, ®ex);
1810 got_object_commit_close(commit);
1813 if (have_match == 0) {
1814 got_object_commit_close(commit);
1819 err = print_commit(commit, id, repo, path, show_patch,
1820 diff_context, refs);
1821 got_object_commit_close(commit);
1822 if (err || (limit && --limit == 0))
1828 got_commit_graph_close(graph);
1835 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1836 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1841 get_default_log_limit(void)
1843 const char *got_default_log_limit;
1847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1848 if (got_default_log_limit == NULL)
1850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1856 static const struct got_error *
1857 cmd_log(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 struct got_commit_object *commit = NULL;
1863 struct got_object_id *id = NULL;
1864 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1865 const char *start_commit = NULL, *search_pattern = NULL;
1866 int diff_context = -1, ch;
1867 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1869 struct got_reflist_head refs;
1871 SIMPLEQ_INIT(&refs);
1874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1880 limit = get_default_log_limit();
1882 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1888 start_commit = optarg;
1891 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1894 err(1, "-C option %s", errstr);
1897 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1899 err(1, "-l option %s", errstr);
1902 first_parent_traversal = 1;
1905 repo_path = realpath(optarg, NULL);
1906 if (repo_path == NULL)
1907 return got_error_from_errno2("realpath",
1909 got_path_strip_trailing_slashes(repo_path);
1912 search_pattern = optarg;
1923 if (diff_context == -1)
1925 else if (!show_patch)
1926 errx(1, "-C reguires -p");
1928 cwd = getcwd(NULL, 0);
1930 error = got_error_from_errno("getcwd");
1934 error = got_worktree_open(&worktree, cwd);
1935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1942 error = got_error_from_errno("strdup");
1945 } else if (argc == 1) {
1947 error = got_worktree_resolve_path(&path, worktree,
1952 path = strdup(argv[0]);
1954 error = got_error_from_errno("strdup");
1961 if (repo_path == NULL) {
1962 repo_path = worktree ?
1963 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1965 if (repo_path == NULL) {
1966 error = got_error_from_errno("strdup");
1970 error = got_repo_open(&repo, repo_path, NULL);
1974 error = apply_unveil(got_repo_get_path(repo), 1,
1975 worktree ? got_worktree_get_root_path(worktree) : NULL);
1979 if (start_commit == NULL) {
1980 struct got_reference *head_ref;
1981 error = got_ref_open(&head_ref, repo,
1982 worktree ? got_worktree_get_head_ref_name(worktree)
1986 error = got_ref_resolve(&id, repo, head_ref);
1987 got_ref_close(head_ref);
1990 error = got_object_open_as_commit(&commit, repo, id);
1992 struct got_reference *ref;
1993 error = got_ref_open(&ref, repo, start_commit, 0);
1994 if (error == NULL) {
1996 error = got_ref_resolve(&id, repo, ref);
2000 error = got_object_get_type(&obj_type, repo, id);
2003 if (obj_type == GOT_OBJ_TYPE_TAG) {
2004 struct got_tag_object *tag;
2005 error = got_object_open_as_tag(&tag, repo, id);
2008 if (got_object_tag_get_object_type(tag) !=
2009 GOT_OBJ_TYPE_COMMIT) {
2010 got_object_tag_close(tag);
2011 error = got_error(GOT_ERR_OBJ_TYPE);
2015 id = got_object_id_dup(
2016 got_object_tag_get_object_id(tag));
2018 error = got_error_from_errno(
2019 "got_object_id_dup");
2020 got_object_tag_close(tag);
2023 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2024 error = got_error(GOT_ERR_OBJ_TYPE);
2027 error = got_object_open_as_commit(&commit, repo, id);
2031 if (commit == NULL) {
2032 error = got_repo_match_object_id_prefix(&id,
2033 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2042 const char *prefix = got_worktree_get_path_prefix(worktree);
2044 if (asprintf(&p, "%s%s%s", prefix,
2045 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2046 error = got_error_from_errno("asprintf");
2049 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2052 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2057 path = in_repo_path;
2060 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2064 error = print_commits(id, repo, path, show_patch, search_pattern,
2065 diff_context, limit, first_parent_traversal, &refs);
2072 got_worktree_close(worktree);
2074 const struct got_error *repo_error;
2075 repo_error = got_repo_close(repo);
2079 got_ref_list_free(&refs);
2086 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2087 "[-w] [object1 object2 | path]\n", getprogname());
2091 struct print_diff_arg {
2092 struct got_repository *repo;
2093 struct got_worktree *worktree;
2098 int ignore_whitespace;
2101 static const struct got_error *
2102 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2103 const char *path, struct got_object_id *blob_id,
2104 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2105 int dirfd, const char *de_name)
2107 struct print_diff_arg *a = arg;
2108 const struct got_error *err = NULL;
2109 struct got_blob_object *blob1 = NULL;
2112 char *abspath = NULL, *label1 = NULL;
2115 if (a->diff_staged) {
2116 if (staged_status != GOT_STATUS_MODIFY &&
2117 staged_status != GOT_STATUS_ADD &&
2118 staged_status != GOT_STATUS_DELETE)
2121 if (staged_status == GOT_STATUS_DELETE)
2123 if (status == GOT_STATUS_NONEXISTENT)
2124 return got_error_set_errno(ENOENT, path);
2125 if (status != GOT_STATUS_MODIFY &&
2126 status != GOT_STATUS_ADD &&
2127 status != GOT_STATUS_DELETE &&
2128 status != GOT_STATUS_CONFLICT)
2132 if (!a->header_shown) {
2133 printf("diff %s %s%s\n", a->id_str,
2134 got_worktree_get_root_path(a->worktree),
2135 a->diff_staged ? " (staged changes)" : "");
2136 a->header_shown = 1;
2139 if (a->diff_staged) {
2140 const char *label1 = NULL, *label2 = NULL;
2141 switch (staged_status) {
2142 case GOT_STATUS_MODIFY:
2146 case GOT_STATUS_ADD:
2149 case GOT_STATUS_DELETE:
2153 return got_error(GOT_ERR_FILE_STATUS);
2155 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2156 label1, label2, a->diff_context, a->ignore_whitespace,
2160 if (staged_status == GOT_STATUS_ADD ||
2161 staged_status == GOT_STATUS_MODIFY) {
2163 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2167 err = got_object_id_str(&id_str, staged_blob_id);
2170 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2171 err = got_error_from_errno("asprintf");
2176 } else if (status != GOT_STATUS_ADD) {
2177 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2182 if (status != GOT_STATUS_DELETE) {
2183 if (asprintf(&abspath, "%s/%s",
2184 got_worktree_get_root_path(a->worktree), path) == -1) {
2185 err = got_error_from_errno("asprintf");
2190 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2192 err = got_error_from_errno2("openat", abspath);
2196 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2198 err = got_error_from_errno2("open", abspath);
2202 if (fstat(fd, &sb) == -1) {
2203 err = got_error_from_errno2("fstat", abspath);
2206 f2 = fdopen(fd, "r");
2208 err = got_error_from_errno2("fdopen", abspath);
2215 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2216 a->diff_context, a->ignore_whitespace, stdout);
2219 got_object_blob_close(blob1);
2220 if (f2 && fclose(f2) == EOF && err == NULL)
2221 err = got_error_from_errno("fclose");
2222 if (fd != -1 && close(fd) == -1 && err == NULL)
2223 err = got_error_from_errno("close");
2228 static const struct got_error *
2229 match_object_id(struct got_object_id **id, char **label,
2230 const char *id_str, int obj_type, int resolve_tags,
2231 struct got_repository *repo)
2233 const struct got_error *err;
2234 struct got_tag_object *tag;
2235 struct got_reference *ref = NULL;
2241 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2244 *id = got_object_id_dup(
2245 got_object_tag_get_object_id(tag));
2247 err = got_error_from_errno("got_object_id_dup");
2248 else if (asprintf(label, "refs/tags/%s",
2249 got_object_tag_get_name(tag)) == -1) {
2250 err = got_error_from_errno("asprintf");
2254 got_object_tag_close(tag);
2256 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2257 err->code != GOT_ERR_NO_OBJ)
2261 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2263 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2265 err = got_ref_open(&ref, repo, id_str, 0);
2268 *label = strdup(got_ref_get_name(ref));
2269 if (*label == NULL) {
2270 err = got_error_from_errno("strdup");
2273 err = got_ref_resolve(id, repo, ref);
2275 err = got_object_id_str(label, *id);
2276 if (*label == NULL) {
2277 err = got_error_from_errno("strdup");
2288 static const struct got_error *
2289 cmd_diff(int argc, char *argv[])
2291 const struct got_error *error;
2292 struct got_repository *repo = NULL;
2293 struct got_worktree *worktree = NULL;
2294 char *cwd = NULL, *repo_path = NULL;
2295 struct got_object_id *id1 = NULL, *id2 = NULL;
2296 const char *id_str1 = NULL, *id_str2 = NULL;
2297 char *label1 = NULL, *label2 = NULL;
2299 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2304 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2309 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2312 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2315 err(1, "-C option %s", errstr);
2318 repo_path = realpath(optarg, NULL);
2319 if (repo_path == NULL)
2320 return got_error_from_errno2("realpath",
2322 got_path_strip_trailing_slashes(repo_path);
2328 ignore_whitespace = 1;
2339 cwd = getcwd(NULL, 0);
2341 error = got_error_from_errno("getcwd");
2344 error = got_worktree_open(&worktree, cwd);
2345 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2348 if (worktree == NULL) {
2349 error = got_error(GOT_ERR_NOT_WORKTREE);
2354 "-r option can't be used when diffing a work tree");
2355 repo_path = strdup(got_worktree_get_repo_path(worktree));
2356 if (repo_path == NULL) {
2357 error = got_error_from_errno("strdup");
2361 error = got_worktree_resolve_path(&path, worktree,
2368 error = got_error_from_errno("strdup");
2372 } else if (argc == 2) {
2374 errx(1, "-s option can't be used when diffing "
2375 "objects in repository");
2378 if (worktree && repo_path == NULL) {
2380 strdup(got_worktree_get_repo_path(worktree));
2381 if (repo_path == NULL) {
2382 error = got_error_from_errno("strdup");
2389 if (repo_path == NULL) {
2390 repo_path = getcwd(NULL, 0);
2391 if (repo_path == NULL)
2392 return got_error_from_errno("getcwd");
2395 error = got_repo_open(&repo, repo_path, NULL);
2400 error = apply_unveil(got_repo_get_path(repo), 1,
2401 worktree ? got_worktree_get_root_path(worktree) : NULL);
2406 struct print_diff_arg arg;
2407 struct got_pathlist_head paths;
2412 error = got_object_id_str(&id_str,
2413 got_worktree_get_base_commit_id(worktree));
2417 arg.worktree = worktree;
2418 arg.diff_context = diff_context;
2419 arg.id_str = id_str;
2420 arg.header_shown = 0;
2421 arg.diff_staged = diff_staged;
2422 arg.ignore_whitespace = ignore_whitespace;
2424 error = got_pathlist_append(&paths, path, NULL);
2428 error = got_worktree_status(worktree, &paths, repo, print_diff,
2429 &arg, check_cancelled, NULL);
2431 got_pathlist_free(&paths);
2435 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2440 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2445 error = got_object_get_type(&type1, repo, id1);
2449 error = got_object_get_type(&type2, repo, id2);
2453 if (type1 != type2) {
2454 error = got_error(GOT_ERR_OBJ_TYPE);
2459 case GOT_OBJ_TYPE_BLOB:
2460 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2461 diff_context, ignore_whitespace, repo, stdout);
2463 case GOT_OBJ_TYPE_TREE:
2464 error = got_diff_objects_as_trees(id1, id2, "", "",
2465 diff_context, ignore_whitespace, repo, stdout);
2467 case GOT_OBJ_TYPE_COMMIT:
2468 printf("diff %s %s\n", label1, label2);
2469 error = got_diff_objects_as_commits(id1, id2, diff_context,
2470 ignore_whitespace, repo, stdout);
2473 error = got_error(GOT_ERR_OBJ_TYPE);
2483 got_worktree_close(worktree);
2485 const struct got_error *repo_error;
2486 repo_error = got_repo_close(repo);
2497 "usage: %s blame [-c commit] [-r repository-path] path\n",
2506 char datebuf[11]; /* YYYY-MM-DD + NUL */
2509 struct blame_cb_args {
2510 struct blame_line *lines;
2514 off_t *line_offsets;
2516 struct got_repository *repo;
2519 static const struct got_error *
2520 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2522 const struct got_error *err = NULL;
2523 struct blame_cb_args *a = arg;
2524 struct blame_line *bline;
2526 size_t linesize = 0;
2527 struct got_commit_object *commit = NULL;
2530 time_t committer_time;
2532 if (nlines != a->nlines ||
2533 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2534 return got_error(GOT_ERR_RANGE);
2536 if (sigint_received)
2537 return got_error(GOT_ERR_ITER_COMPLETED);
2540 return NULL; /* no change in this commit */
2542 /* Annotate this line. */
2543 bline = &a->lines[lineno - 1];
2544 if (bline->annotated)
2546 err = got_object_id_str(&bline->id_str, id);
2550 err = got_object_open_as_commit(&commit, a->repo, id);
2554 bline->committer = strdup(got_object_commit_get_committer(commit));
2555 if (bline->committer == NULL) {
2556 err = got_error_from_errno("strdup");
2560 committer_time = got_object_commit_get_committer_time(commit);
2561 if (localtime_r(&committer_time, &tm) == NULL)
2562 return got_error_from_errno("localtime_r");
2563 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2564 &tm) >= sizeof(bline->datebuf)) {
2565 err = got_error(GOT_ERR_NO_SPACE);
2568 bline->annotated = 1;
2570 /* Print lines annotated so far. */
2571 bline = &a->lines[a->lineno_cur - 1];
2572 if (!bline->annotated)
2575 offset = a->line_offsets[a->lineno_cur - 1];
2576 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2577 err = got_error_from_errno("fseeko");
2581 while (bline->annotated) {
2582 char *smallerthan, *at, *nl, *committer;
2585 if (getline(&line, &linesize, a->f) == -1) {
2587 err = got_error_from_errno("getline");
2591 committer = bline->committer;
2592 smallerthan = strchr(committer, '<');
2593 if (smallerthan && smallerthan[1] != '\0')
2594 committer = smallerthan + 1;
2595 at = strchr(committer, '@');
2598 len = strlen(committer);
2600 committer[8] = '\0';
2602 nl = strchr(line, '\n');
2605 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2606 bline->id_str, bline->datebuf, committer, line);
2609 bline = &a->lines[a->lineno_cur - 1];
2613 got_object_commit_close(commit);
2618 static const struct got_error *
2619 cmd_blame(int argc, char *argv[])
2621 const struct got_error *error;
2622 struct got_repository *repo = NULL;
2623 struct got_worktree *worktree = NULL;
2624 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2625 struct got_object_id *obj_id = NULL;
2626 struct got_object_id *commit_id = NULL;
2627 struct got_blob_object *blob = NULL;
2628 char *commit_id_str = NULL;
2629 struct blame_cb_args bca;
2630 int ch, obj_type, i;
2633 memset(&bca, 0, sizeof(bca));
2636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2644 commit_id_str = optarg;
2647 repo_path = realpath(optarg, NULL);
2648 if (repo_path == NULL)
2649 return got_error_from_errno2("realpath",
2651 got_path_strip_trailing_slashes(repo_path);
2667 cwd = getcwd(NULL, 0);
2669 error = got_error_from_errno("getcwd");
2672 if (repo_path == NULL) {
2673 error = got_worktree_open(&worktree, cwd);
2674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2680 strdup(got_worktree_get_repo_path(worktree));
2681 if (repo_path == NULL)
2682 error = got_error_from_errno("strdup");
2686 repo_path = strdup(cwd);
2687 if (repo_path == NULL) {
2688 error = got_error_from_errno("strdup");
2694 error = got_repo_open(&repo, repo_path, NULL);
2698 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2703 const char *prefix = got_worktree_get_path_prefix(worktree);
2704 char *p, *worktree_subdir = cwd +
2705 strlen(got_worktree_get_root_path(worktree));
2706 if (asprintf(&p, "%s%s%s%s%s",
2707 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2708 worktree_subdir, worktree_subdir[0] ? "/" : "",
2710 error = got_error_from_errno("asprintf");
2713 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2716 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2721 if (commit_id_str == NULL) {
2722 struct got_reference *head_ref;
2723 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2726 error = got_ref_resolve(&commit_id, repo, head_ref);
2727 got_ref_close(head_ref);
2731 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2736 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2739 if (obj_id == NULL) {
2740 error = got_error(GOT_ERR_NO_OBJ);
2744 error = got_object_get_type(&obj_type, repo, obj_id);
2748 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2749 error = got_error(GOT_ERR_OBJ_TYPE);
2753 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2756 bca.f = got_opentemp();
2757 if (bca.f == NULL) {
2758 error = got_error_from_errno("got_opentemp");
2761 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2762 &bca.line_offsets, bca.f, blob);
2763 if (error || bca.nlines == 0)
2766 /* Don't include \n at EOF in the blame line count. */
2767 if (bca.line_offsets[bca.nlines - 1] == filesize)
2770 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2771 if (bca.lines == NULL) {
2772 error = got_error_from_errno("calloc");
2776 bca.nlines_prec = 0;
2784 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2785 check_cancelled, NULL);
2795 got_object_blob_close(blob);
2797 got_worktree_close(worktree);
2799 const struct got_error *repo_error;
2800 repo_error = got_repo_close(repo);
2805 for (i = 0; i < bca.nlines; i++) {
2806 struct blame_line *bline = &bca.lines[i];
2807 free(bline->id_str);
2808 free(bline->committer);
2812 free(bca.line_offsets);
2813 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2814 error = got_error_from_errno("fclose");
2822 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2828 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2829 const char *root_path)
2831 int is_root_path = (strcmp(path, root_path) == 0);
2832 const char *modestr = "";
2833 mode_t mode = got_tree_entry_get_mode(te);
2835 path += strlen(root_path);
2836 while (path[0] == '/')
2839 if (got_object_tree_entry_is_submodule(te))
2841 else if (S_ISLNK(mode))
2843 else if (S_ISDIR(mode))
2845 else if (mode & S_IXUSR)
2848 printf("%s%s%s%s%s\n", id ? id : "", path,
2849 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2852 static const struct got_error *
2853 print_tree(const char *path, struct got_object_id *commit_id,
2854 int show_ids, int recurse, const char *root_path,
2855 struct got_repository *repo)
2857 const struct got_error *err = NULL;
2858 struct got_object_id *tree_id = NULL;
2859 struct got_tree_object *tree = NULL;
2862 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2866 err = got_object_open_as_tree(&tree, repo, tree_id);
2869 nentries = got_object_tree_get_nentries(tree);
2870 for (i = 0; i < nentries; i++) {
2871 struct got_tree_entry *te;
2874 if (sigint_received || sigpipe_received)
2877 te = got_object_tree_get_entry(tree, i);
2880 err = got_object_id_str(&id_str,
2881 got_tree_entry_get_id(te));
2884 if (asprintf(&id, "%s ", id_str) == -1) {
2885 err = got_error_from_errno("asprintf");
2891 print_entry(te, id, path, root_path);
2894 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2896 if (asprintf(&child_path, "%s%s%s", path,
2897 path[0] == '/' && path[1] == '\0' ? "" : "/",
2898 got_tree_entry_get_name(te)) == -1) {
2899 err = got_error_from_errno("asprintf");
2902 err = print_tree(child_path, commit_id, show_ids, 1,
2911 got_object_tree_close(tree);
2916 static const struct got_error *
2917 cmd_tree(int argc, char *argv[])
2919 const struct got_error *error;
2920 struct got_repository *repo = NULL;
2921 struct got_worktree *worktree = NULL;
2923 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2924 struct got_object_id *commit_id = NULL;
2925 char *commit_id_str = NULL;
2926 int show_ids = 0, recurse = 0;
2930 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2935 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2938 commit_id_str = optarg;
2941 repo_path = realpath(optarg, NULL);
2942 if (repo_path == NULL)
2943 return got_error_from_errno2("realpath",
2945 got_path_strip_trailing_slashes(repo_path);
2969 cwd = getcwd(NULL, 0);
2971 error = got_error_from_errno("getcwd");
2974 if (repo_path == NULL) {
2975 error = got_worktree_open(&worktree, cwd);
2976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2982 strdup(got_worktree_get_repo_path(worktree));
2983 if (repo_path == NULL)
2984 error = got_error_from_errno("strdup");
2988 repo_path = strdup(cwd);
2989 if (repo_path == NULL) {
2990 error = got_error_from_errno("strdup");
2996 error = got_repo_open(&repo, repo_path, NULL);
3000 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3006 char *p, *worktree_subdir = cwd +
3007 strlen(got_worktree_get_root_path(worktree));
3008 if (asprintf(&p, "%s/%s",
3009 got_worktree_get_path_prefix(worktree),
3010 worktree_subdir) == -1) {
3011 error = got_error_from_errno("asprintf");
3014 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3021 if (in_repo_path == NULL) {
3022 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3027 if (commit_id_str == NULL) {
3028 struct got_reference *head_ref;
3029 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3032 error = got_ref_resolve(&commit_id, repo, head_ref);
3033 got_ref_close(head_ref);
3037 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3042 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3043 in_repo_path, repo);
3050 got_worktree_close(worktree);
3052 const struct got_error *repo_error;
3053 repo_error = got_repo_close(repo);
3063 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3067 static const struct got_error *
3068 print_status(void *arg, unsigned char status, unsigned char staged_status,
3069 const char *path, struct got_object_id *blob_id,
3070 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3071 int dirfd, const char *de_name)
3073 if (status == staged_status && (status == GOT_STATUS_DELETE))
3074 status = GOT_STATUS_NO_CHANGE;
3075 printf("%c%c %s\n", status, staged_status, path);
3079 static const struct got_error *
3080 cmd_status(int argc, char *argv[])
3082 const struct got_error *error = NULL;
3083 struct got_repository *repo = NULL;
3084 struct got_worktree *worktree = NULL;
3086 struct got_pathlist_head paths;
3087 struct got_pathlist_entry *pe;
3092 while ((ch = getopt(argc, argv, "")) != -1) {
3104 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3108 cwd = getcwd(NULL, 0);
3110 error = got_error_from_errno("getcwd");
3114 error = got_worktree_open(&worktree, cwd);
3118 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3123 error = apply_unveil(got_repo_get_path(repo), 1,
3124 got_worktree_get_root_path(worktree));
3128 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3132 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3133 check_cancelled, NULL);
3135 TAILQ_FOREACH(pe, &paths, entry)
3136 free((char *)pe->path);
3137 got_pathlist_free(&paths);
3146 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3151 static const struct got_error *
3152 list_refs(struct got_repository *repo)
3154 static const struct got_error *err = NULL;
3155 struct got_reflist_head refs;
3156 struct got_reflist_entry *re;
3158 SIMPLEQ_INIT(&refs);
3159 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3163 SIMPLEQ_FOREACH(re, &refs, entry) {
3165 refstr = got_ref_to_str(re->ref);
3167 return got_error_from_errno("got_ref_to_str");
3168 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3172 got_ref_list_free(&refs);
3176 static const struct got_error *
3177 delete_ref(struct got_repository *repo, const char *refname)
3179 const struct got_error *err = NULL;
3180 struct got_reference *ref;
3182 err = got_ref_open(&ref, repo, refname, 0);
3186 err = got_ref_delete(ref, repo);
3191 static const struct got_error *
3192 add_ref(struct got_repository *repo, const char *refname, const char *target)
3194 const struct got_error *err = NULL;
3195 struct got_object_id *id;
3196 struct got_reference *ref = NULL;
3199 * Don't let the user create a reference name with a leading '-'.
3200 * While technically a valid reference name, this case is usually
3201 * an unintended typo.
3203 if (refname[0] == '-')
3204 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3206 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3209 struct got_reference *target_ref;
3211 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3213 err = got_ref_open(&target_ref, repo, target, 0);
3216 err = got_ref_resolve(&id, repo, target_ref);
3217 got_ref_close(target_ref);
3222 err = got_ref_alloc(&ref, refname, id);
3226 err = got_ref_write(ref, repo);
3234 static const struct got_error *
3235 add_symref(struct got_repository *repo, const char *refname, const char *target)
3237 const struct got_error *err = NULL;
3238 struct got_reference *ref = NULL;
3239 struct got_reference *target_ref = NULL;
3242 * Don't let the user create a reference name with a leading '-'.
3243 * While technically a valid reference name, this case is usually
3244 * an unintended typo.
3246 if (refname[0] == '-')
3247 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3249 err = got_ref_open(&target_ref, repo, target, 0);
3253 err = got_ref_alloc_symref(&ref, refname, target_ref);
3257 err = got_ref_write(ref, repo);
3260 got_ref_close(target_ref);
3266 static const struct got_error *
3267 cmd_ref(int argc, char *argv[])
3269 const struct got_error *error = NULL;
3270 struct got_repository *repo = NULL;
3271 struct got_worktree *worktree = NULL;
3272 char *cwd = NULL, *repo_path = NULL;
3273 int ch, do_list = 0, create_symref = 0;
3274 const char *delref = NULL;
3276 /* TODO: Add -s option for adding symbolic references. */
3277 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3283 repo_path = realpath(optarg, NULL);
3284 if (repo_path == NULL)
3285 return got_error_from_errno2("realpath",
3287 got_path_strip_trailing_slashes(repo_path);
3301 if (do_list && delref)
3302 errx(1, "-l and -d options are mutually exclusive\n");
3307 if (do_list || delref) {
3309 errx(1, "-s option cannot be used together with the "
3310 "-l or -d options");
3313 } else if (argc != 2)
3318 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3322 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3323 "sendfd unveil", NULL) == -1)
3327 cwd = getcwd(NULL, 0);
3329 error = got_error_from_errno("getcwd");
3333 if (repo_path == NULL) {
3334 error = got_worktree_open(&worktree, cwd);
3335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3341 strdup(got_worktree_get_repo_path(worktree));
3342 if (repo_path == NULL)
3343 error = got_error_from_errno("strdup");
3347 repo_path = strdup(cwd);
3348 if (repo_path == NULL) {
3349 error = got_error_from_errno("strdup");
3355 error = got_repo_open(&repo, repo_path, NULL);
3359 error = apply_unveil(got_repo_get_path(repo), do_list,
3360 worktree ? got_worktree_get_root_path(worktree) : NULL);
3365 error = list_refs(repo);
3367 error = delete_ref(repo, delref);
3368 else if (create_symref)
3369 error = add_symref(repo, argv[0], argv[1]);
3371 error = add_ref(repo, argv[0], argv[1]);
3374 got_repo_close(repo);
3376 got_worktree_close(worktree);
3386 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3387 "[name]\n", getprogname());
3391 static const struct got_error *
3392 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3393 struct got_reference *ref)
3395 const struct got_error *err = NULL;
3396 const char *refname, *marker = " ";
3399 refname = got_ref_get_name(ref);
3400 if (worktree && strcmp(refname,
3401 got_worktree_get_head_ref_name(worktree)) == 0) {
3402 struct got_object_id *id = NULL;
3404 err = got_ref_resolve(&id, repo, ref);
3407 if (got_object_id_cmp(id,
3408 got_worktree_get_base_commit_id(worktree)) == 0)
3415 if (strncmp(refname, "refs/heads/", 11) == 0)
3417 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3420 refstr = got_ref_to_str(ref);
3422 return got_error_from_errno("got_ref_to_str");
3424 printf("%s%s: %s\n", marker, refname, refstr);
3429 static const struct got_error *
3430 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3432 const char *refname;
3434 if (worktree == NULL)
3435 return got_error(GOT_ERR_NOT_WORKTREE);
3437 refname = got_worktree_get_head_ref_name(worktree);
3439 if (strncmp(refname, "refs/heads/", 11) == 0)
3441 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3444 printf("%s\n", refname);
3449 static const struct got_error *
3450 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3452 static const struct got_error *err = NULL;
3453 struct got_reflist_head refs;
3454 struct got_reflist_entry *re;
3455 struct got_reference *temp_ref = NULL;
3456 int rebase_in_progress, histedit_in_progress;
3458 SIMPLEQ_INIT(&refs);
3461 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3466 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3471 if (rebase_in_progress || histedit_in_progress) {
3472 err = got_ref_open(&temp_ref, repo,
3473 got_worktree_get_head_ref_name(worktree), 0);
3476 list_branch(repo, worktree, temp_ref);
3477 got_ref_close(temp_ref);
3481 err = got_ref_list(&refs, repo, "refs/heads",
3482 got_ref_cmp_by_name, NULL);
3486 SIMPLEQ_FOREACH(re, &refs, entry)
3487 list_branch(repo, worktree, re->ref);
3489 got_ref_list_free(&refs);
3493 static const struct got_error *
3494 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 const char *branch_name)
3497 const struct got_error *err = NULL;
3498 struct got_reference *ref = NULL;
3501 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3502 return got_error_from_errno("asprintf");
3504 err = got_ref_open(&ref, repo, refname, 0);
3509 strcmp(got_worktree_get_head_ref_name(worktree),
3510 got_ref_get_name(ref)) == 0) {
3511 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3512 "will not delete this work tree's current branch");
3516 err = got_ref_delete(ref, repo);
3524 static const struct got_error *
3525 add_branch(struct got_repository *repo, const char *branch_name,
3526 struct got_object_id *base_commit_id)
3528 const struct got_error *err = NULL;
3529 struct got_reference *ref = NULL;
3530 char *base_refname = NULL, *refname = NULL;
3533 * Don't let the user create a branch name with a leading '-'.
3534 * While technically a valid reference name, this case is usually
3535 * an unintended typo.
3537 if (branch_name[0] == '-')
3538 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3540 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3541 err = got_error_from_errno("asprintf");
3545 err = got_ref_open(&ref, repo, refname, 0);
3547 err = got_error(GOT_ERR_BRANCH_EXISTS);
3549 } else if (err->code != GOT_ERR_NOT_REF)
3552 err = got_ref_alloc(&ref, refname, base_commit_id);
3556 err = got_ref_write(ref, repo);
3565 static const struct got_error *
3566 cmd_branch(int argc, char *argv[])
3568 const struct got_error *error = NULL;
3569 struct got_repository *repo = NULL;
3570 struct got_worktree *worktree = NULL;
3571 char *cwd = NULL, *repo_path = NULL;
3572 int ch, do_list = 0, do_show = 0;
3573 const char *delref = NULL, *commit_id_arg = NULL;
3575 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3578 commit_id_arg = optarg;
3584 repo_path = realpath(optarg, NULL);
3585 if (repo_path == NULL)
3586 return got_error_from_errno2("realpath",
3588 got_path_strip_trailing_slashes(repo_path);
3599 if (do_list && delref)
3600 errx(1, "-l and -d options are mutually exclusive\n");
3605 if (!do_list && !delref && argc == 0)
3608 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3609 errx(1, "-c option can only be used when creating a branch");
3611 if (do_list || delref) {
3614 } else if (!do_show && argc != 1)
3618 if (do_list || do_show) {
3619 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3623 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3624 "sendfd unveil", NULL) == -1)
3628 cwd = getcwd(NULL, 0);
3630 error = got_error_from_errno("getcwd");
3634 if (repo_path == NULL) {
3635 error = got_worktree_open(&worktree, cwd);
3636 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3642 strdup(got_worktree_get_repo_path(worktree));
3643 if (repo_path == NULL)
3644 error = got_error_from_errno("strdup");
3648 repo_path = strdup(cwd);
3649 if (repo_path == NULL) {
3650 error = got_error_from_errno("strdup");
3656 error = got_repo_open(&repo, repo_path, NULL);
3660 error = apply_unveil(got_repo_get_path(repo), do_list,
3661 worktree ? got_worktree_get_root_path(worktree) : NULL);
3666 error = show_current_branch(repo, worktree);
3668 error = list_branches(repo, worktree);
3670 error = delete_branch(repo, worktree, delref);
3672 struct got_object_id *commit_id;
3673 if (commit_id_arg == NULL)
3674 commit_id_arg = worktree ?
3675 got_worktree_get_head_ref_name(worktree) :
3677 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3680 error = add_branch(repo, argv[0], commit_id);
3685 got_repo_close(repo);
3687 got_worktree_close(worktree);
3698 "usage: %s tag [-r repository] | -l | "
3699 "[-m message] name [commit]\n", getprogname());
3704 static const struct got_error *
3705 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3707 const struct got_error *err = NULL;
3708 struct got_reflist_entry *re, *se, *new;
3709 struct got_object_id *re_id, *se_id;
3710 struct got_tag_object *re_tag, *se_tag;
3711 time_t re_time, se_time;
3713 SIMPLEQ_FOREACH(re, tags, entry) {
3714 se = SIMPLEQ_FIRST(sorted);
3716 err = got_reflist_entry_dup(&new, re);
3719 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3722 err = got_ref_resolve(&re_id, repo, re->ref);
3725 err = got_object_open_as_tag(&re_tag, repo, re_id);
3729 re_time = got_object_tag_get_tagger_time(re_tag);
3730 got_object_tag_close(re_tag);
3734 err = got_ref_resolve(&se_id, repo, re->ref);
3737 err = got_object_open_as_tag(&se_tag, repo, se_id);
3741 se_time = got_object_tag_get_tagger_time(se_tag);
3742 got_object_tag_close(se_tag);
3744 if (se_time > re_time) {
3745 err = got_reflist_entry_dup(&new, re);
3748 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3751 se = SIMPLEQ_NEXT(se, entry);
3760 static const struct got_error *
3761 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3762 struct got_reference *ref2)
3764 const struct got_error *err = NULL;
3765 struct got_repository *repo = arg;
3766 struct got_object_id *id1, *id2 = NULL;
3767 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3768 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3769 time_t time1, time2;
3773 err = got_ref_resolve(&id1, repo, ref1);
3776 err = got_object_open_as_tag(&tag1, repo, id1);
3778 if (err->code != GOT_ERR_OBJ_TYPE)
3780 /* "lightweight" tag */
3781 err = got_object_open_as_commit(&commit1, repo, id1);
3784 time1 = got_object_commit_get_committer_time(commit1);
3786 time1 = got_object_tag_get_tagger_time(tag1);
3788 err = got_ref_resolve(&id2, repo, ref2);
3791 err = got_object_open_as_tag(&tag2, repo, id2);
3793 if (err->code != GOT_ERR_OBJ_TYPE)
3795 /* "lightweight" tag */
3796 err = got_object_open_as_commit(&commit2, repo, id2);
3799 time2 = got_object_commit_get_committer_time(commit2);
3801 time2 = got_object_tag_get_tagger_time(tag2);
3803 /* Put latest tags first. */
3806 else if (time1 > time2)
3809 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3814 got_object_tag_close(tag1);
3816 got_object_tag_close(tag2);
3818 got_object_commit_close(commit1);
3820 got_object_commit_close(commit2);
3824 static const struct got_error *
3825 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3827 static const struct got_error *err = NULL;
3828 struct got_reflist_head refs;
3829 struct got_reflist_entry *re;
3831 SIMPLEQ_INIT(&refs);
3833 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3837 SIMPLEQ_FOREACH(re, &refs, entry) {
3838 const char *refname;
3839 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3843 struct got_object_id *id;
3844 struct got_tag_object *tag;
3845 struct got_commit_object *commit = NULL;
3847 refname = got_ref_get_name(re->ref);
3848 if (strncmp(refname, "refs/tags/", 10) != 0)
3851 refstr = got_ref_to_str(re->ref);
3852 if (refstr == NULL) {
3853 err = got_error_from_errno("got_ref_to_str");
3856 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3859 err = got_ref_resolve(&id, repo, re->ref);
3862 err = got_object_open_as_tag(&tag, repo, id);
3864 if (err->code != GOT_ERR_OBJ_TYPE) {
3868 /* "lightweight" tag */
3869 err = got_object_open_as_commit(&commit, repo, id);
3874 tagger = got_object_commit_get_committer(commit);
3876 got_object_commit_get_committer_time(commit);
3877 err = got_object_id_str(&id_str, id);
3883 tagger = got_object_tag_get_tagger(tag);
3884 tagger_time = got_object_tag_get_tagger_time(tag);
3885 err = got_object_id_str(&id_str,
3886 got_object_tag_get_object_id(tag));
3890 printf("from: %s\n", tagger);
3891 datestr = get_datestr(&tagger_time, datebuf);
3893 printf("date: %s UTC\n", datestr);
3895 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3897 switch (got_object_tag_get_object_type(tag)) {
3898 case GOT_OBJ_TYPE_BLOB:
3899 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3902 case GOT_OBJ_TYPE_TREE:
3903 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3906 case GOT_OBJ_TYPE_COMMIT:
3907 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3910 case GOT_OBJ_TYPE_TAG:
3911 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3920 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3923 got_object_commit_close(commit);
3925 tagmsg0 = strdup(got_object_tag_get_message(tag));
3926 got_object_tag_close(tag);
3927 if (tagmsg0 == NULL) {
3928 err = got_error_from_errno("strdup");
3935 line = strsep(&tagmsg, "\n");
3937 printf(" %s\n", line);
3942 got_ref_list_free(&refs);
3946 static const struct got_error *
3947 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3948 const char *tag_name, const char *repo_path)
3950 const struct got_error *err = NULL;
3951 char *template = NULL, *initial_content = NULL;
3952 char *editor = NULL;
3955 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3956 err = got_error_from_errno("asprintf");
3960 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3961 commit_id_str, tag_name) == -1) {
3962 err = got_error_from_errno("asprintf");
3966 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3970 dprintf(fd, initial_content);
3973 err = get_editor(&editor);
3976 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3978 free(initial_content);
3982 /* Editor is done; we can now apply unveil(2) */
3984 err = apply_unveil(repo_path, 0, NULL);
3993 static const struct got_error *
3994 add_tag(struct got_repository *repo, const char *tag_name,
3995 const char *commit_arg, const char *tagmsg_arg)
3997 const struct got_error *err = NULL;
3998 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3999 char *label = NULL, *commit_id_str = NULL;
4000 struct got_reference *ref = NULL;
4001 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4002 char *tagmsg_path = NULL, *tag_id_str = NULL;
4003 int preserve_tagmsg = 0;
4006 * Don't let the user create a tag name with a leading '-'.
4007 * While technically a valid reference name, this case is usually
4008 * an unintended typo.
4010 if (tag_name[0] == '-')
4011 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4013 err = get_author(&tagger, repo);
4017 err = match_object_id(&commit_id, &label, commit_arg,
4018 GOT_OBJ_TYPE_COMMIT, 1, repo);
4022 err = got_object_id_str(&commit_id_str, commit_id);
4026 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4027 refname = strdup(tag_name);
4028 if (refname == NULL) {
4029 err = got_error_from_errno("strdup");
4033 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4034 err = got_error_from_errno("asprintf");
4038 err = got_ref_open(&ref, repo, refname, 0);
4040 err = got_error(GOT_ERR_TAG_EXISTS);
4042 } else if (err->code != GOT_ERR_NOT_REF)
4045 if (tagmsg_arg == NULL) {
4046 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4047 tag_name, got_repo_get_path(repo));
4049 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4050 tagmsg_path != NULL)
4051 preserve_tagmsg = 1;
4056 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4057 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4060 preserve_tagmsg = 1;
4064 err = got_ref_alloc(&ref, refname, tag_id);
4067 preserve_tagmsg = 1;
4071 err = got_ref_write(ref, repo);
4074 preserve_tagmsg = 1;
4078 err = got_object_id_str(&tag_id_str, tag_id);
4081 preserve_tagmsg = 1;
4084 printf("Created tag %s\n", tag_id_str);
4086 if (preserve_tagmsg) {
4087 fprintf(stderr, "%s: tag message preserved in %s\n",
4088 getprogname(), tagmsg_path);
4089 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4090 err = got_error_from_errno2("unlink", tagmsg_path);
4095 free(commit_id_str);
4103 static const struct got_error *
4104 cmd_tag(int argc, char *argv[])
4106 const struct got_error *error = NULL;
4107 struct got_repository *repo = NULL;
4108 struct got_worktree *worktree = NULL;
4109 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4110 char *gitconfig_path = NULL;
4111 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4112 int ch, do_list = 0;
4114 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4120 repo_path = realpath(optarg, NULL);
4121 if (repo_path == NULL)
4122 return got_error_from_errno2("realpath",
4124 got_path_strip_trailing_slashes(repo_path);
4140 errx(1, "-l and -m options are mutually exclusive\n");
4143 } else if (argc < 1 || argc > 2)
4146 commit_id_arg = argv[1];
4151 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4155 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4156 "sendfd unveil", NULL) == -1)
4160 cwd = getcwd(NULL, 0);
4162 error = got_error_from_errno("getcwd");
4166 if (repo_path == NULL) {
4167 error = got_worktree_open(&worktree, cwd);
4168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4174 strdup(got_worktree_get_repo_path(worktree));
4175 if (repo_path == NULL)
4176 error = got_error_from_errno("strdup");
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4189 error = got_repo_open(&repo, repo_path, NULL);
4192 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4195 error = list_tags(repo, worktree);
4197 error = get_gitconfig_path(&gitconfig_path);
4200 error = got_repo_open(&repo, repo_path, gitconfig_path);
4205 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4210 if (commit_id_arg == NULL) {
4211 struct got_reference *head_ref;
4212 struct got_object_id *commit_id;
4213 error = got_ref_open(&head_ref, repo,
4214 worktree ? got_worktree_get_head_ref_name(worktree)
4218 error = got_ref_resolve(&commit_id, repo, head_ref);
4219 got_ref_close(head_ref);
4222 error = got_object_id_str(&commit_id_str, commit_id);
4228 error = add_tag(repo, tag_name,
4229 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4233 got_repo_close(repo);
4235 got_worktree_close(worktree);
4238 free(gitconfig_path);
4239 free(commit_id_str);
4246 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4251 static const struct got_error *
4252 add_progress(void *arg, unsigned char status, const char *path)
4254 while (path[0] == '/')
4256 printf("%c %s\n", status, path);
4260 static const struct got_error *
4261 cmd_add(int argc, char *argv[])
4263 const struct got_error *error = NULL;
4264 struct got_repository *repo = NULL;
4265 struct got_worktree *worktree = NULL;
4267 struct got_pathlist_head paths;
4268 struct got_pathlist_entry *pe;
4269 int ch, can_recurse = 0, no_ignores = 0;
4273 while ((ch = getopt(argc, argv, "IR")) != -1) {
4291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4298 cwd = getcwd(NULL, 0);
4300 error = got_error_from_errno("getcwd");
4304 error = got_worktree_open(&worktree, cwd);
4308 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4313 error = apply_unveil(got_repo_get_path(repo), 1,
4314 got_worktree_get_root_path(worktree));
4318 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4322 if (!can_recurse && no_ignores) {
4323 error = got_error_msg(GOT_ERR_BAD_PATH,
4324 "disregarding ignores requires -R option");
4332 TAILQ_FOREACH(pe, &paths, entry) {
4333 if (asprintf(&ondisk_path, "%s/%s",
4334 got_worktree_get_root_path(worktree),
4336 error = got_error_from_errno("asprintf");
4339 if (lstat(ondisk_path, &sb) == -1) {
4340 if (errno == ENOENT) {
4344 error = got_error_from_errno2("lstat",
4350 if (S_ISDIR(sb.st_mode)) {
4351 error = got_error_msg(GOT_ERR_BAD_PATH,
4352 "adding directories requires -R option");
4358 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4359 NULL, repo, no_ignores);
4362 got_repo_close(repo);
4364 got_worktree_close(worktree);
4365 TAILQ_FOREACH(pe, &paths, entry)
4366 free((char *)pe->path);
4367 got_pathlist_free(&paths);
4375 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4380 static const struct got_error *
4381 print_remove_status(void *arg, unsigned char status,
4382 unsigned char staged_status, const char *path)
4384 while (path[0] == '/')
4386 if (status == GOT_STATUS_NONEXISTENT)
4388 if (status == staged_status && (status == GOT_STATUS_DELETE))
4389 status = GOT_STATUS_NO_CHANGE;
4390 printf("%c%c %s\n", status, staged_status, path);
4394 static const struct got_error *
4395 cmd_remove(int argc, char *argv[])
4397 const struct got_error *error = NULL;
4398 struct got_worktree *worktree = NULL;
4399 struct got_repository *repo = NULL;
4401 struct got_pathlist_head paths;
4402 struct got_pathlist_entry *pe;
4403 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4407 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4410 delete_local_mods = 1;
4428 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4435 cwd = getcwd(NULL, 0);
4437 error = got_error_from_errno("getcwd");
4440 error = got_worktree_open(&worktree, cwd);
4444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4449 error = apply_unveil(got_repo_get_path(repo), 1,
4450 got_worktree_get_root_path(worktree));
4454 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4461 TAILQ_FOREACH(pe, &paths, entry) {
4462 if (asprintf(&ondisk_path, "%s/%s",
4463 got_worktree_get_root_path(worktree),
4465 error = got_error_from_errno("asprintf");
4468 if (lstat(ondisk_path, &sb) == -1) {
4469 if (errno == ENOENT) {
4473 error = got_error_from_errno2("lstat",
4479 if (S_ISDIR(sb.st_mode)) {
4480 error = got_error_msg(GOT_ERR_BAD_PATH,
4481 "removing directories requires -R option");
4487 error = got_worktree_schedule_delete(worktree, &paths,
4488 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4493 got_repo_close(repo);
4495 got_worktree_close(worktree);
4496 TAILQ_FOREACH(pe, &paths, entry)
4497 free((char *)pe->path);
4498 got_pathlist_free(&paths);
4506 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4507 "path ...\n", getprogname());
4511 static const struct got_error *
4512 revert_progress(void *arg, unsigned char status, const char *path)
4514 while (path[0] == '/')
4516 printf("%c %s\n", status, path);
4520 struct choose_patch_arg {
4521 FILE *patch_script_file;
4525 static const struct got_error *
4526 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4527 int nchanges, const char *action)
4530 size_t linesize = 0;
4534 case GOT_STATUS_ADD:
4535 printf("A %s\n%s this addition? [y/n] ", path, action);
4537 case GOT_STATUS_DELETE:
4538 printf("D %s\n%s this deletion? [y/n] ", path, action);
4540 case GOT_STATUS_MODIFY:
4541 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4542 return got_error_from_errno("fseek");
4543 printf(GOT_COMMIT_SEP_STR);
4544 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4546 if (ferror(patch_file))
4547 return got_error_from_errno("getline");
4548 printf(GOT_COMMIT_SEP_STR);
4549 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4550 path, n, nchanges, action);
4553 return got_error_path(path, GOT_ERR_FILE_STATUS);
4559 static const struct got_error *
4560 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4561 FILE *patch_file, int n, int nchanges)
4563 const struct got_error *err = NULL;
4565 size_t linesize = 0;
4568 struct choose_patch_arg *a = arg;
4570 *choice = GOT_PATCH_CHOICE_NONE;
4572 if (a->patch_script_file) {
4574 err = show_change(status, path, patch_file, n, nchanges,
4578 linelen = getline(&line, &linesize, a->patch_script_file);
4579 if (linelen == -1) {
4580 if (ferror(a->patch_script_file))
4581 return got_error_from_errno("getline");
4584 nl = strchr(line, '\n');
4587 if (strcmp(line, "y") == 0) {
4588 *choice = GOT_PATCH_CHOICE_YES;
4590 } else if (strcmp(line, "n") == 0) {
4591 *choice = GOT_PATCH_CHOICE_NO;
4593 } else if (strcmp(line, "q") == 0 &&
4594 status == GOT_STATUS_MODIFY) {
4595 *choice = GOT_PATCH_CHOICE_QUIT;
4598 printf("invalid response '%s'\n", line);
4603 while (resp != 'y' && resp != 'n' && resp != 'q') {
4604 err = show_change(status, path, patch_file, n, nchanges,
4611 if (status == GOT_STATUS_MODIFY) {
4612 if (resp != 'y' && resp != 'n' && resp != 'q') {
4613 printf("invalid response '%c'\n", resp);
4616 } else if (resp != 'y' && resp != 'n') {
4617 printf("invalid response '%c'\n", resp);
4623 *choice = GOT_PATCH_CHOICE_YES;
4624 else if (resp == 'n')
4625 *choice = GOT_PATCH_CHOICE_NO;
4626 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4627 *choice = GOT_PATCH_CHOICE_QUIT;
4633 static const struct got_error *
4634 cmd_revert(int argc, char *argv[])
4636 const struct got_error *error = NULL;
4637 struct got_worktree *worktree = NULL;
4638 struct got_repository *repo = NULL;
4639 char *cwd = NULL, *path = NULL;
4640 struct got_pathlist_head paths;
4641 struct got_pathlist_entry *pe;
4642 int ch, can_recurse = 0, pflag = 0;
4643 FILE *patch_script_file = NULL;
4644 const char *patch_script_path = NULL;
4645 struct choose_patch_arg cpa;
4649 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4655 patch_script_path = optarg;
4670 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4671 "unveil", NULL) == -1)
4676 if (patch_script_path && !pflag)
4677 errx(1, "-F option can only be used together with -p option");
4679 cwd = getcwd(NULL, 0);
4681 error = got_error_from_errno("getcwd");
4684 error = got_worktree_open(&worktree, cwd);
4688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4693 if (patch_script_path) {
4694 patch_script_file = fopen(patch_script_path, "r");
4695 if (patch_script_file == NULL) {
4696 error = got_error_from_errno2("fopen",
4701 error = apply_unveil(got_repo_get_path(repo), 1,
4702 got_worktree_get_root_path(worktree));
4706 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4713 TAILQ_FOREACH(pe, &paths, entry) {
4714 if (asprintf(&ondisk_path, "%s/%s",
4715 got_worktree_get_root_path(worktree),
4717 error = got_error_from_errno("asprintf");
4720 if (lstat(ondisk_path, &sb) == -1) {
4721 if (errno == ENOENT) {
4725 error = got_error_from_errno2("lstat",
4731 if (S_ISDIR(sb.st_mode)) {
4732 error = got_error_msg(GOT_ERR_BAD_PATH,
4733 "reverting directories requires -R option");
4739 cpa.patch_script_file = patch_script_file;
4740 cpa.action = "revert";
4741 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4742 pflag ? choose_patch : NULL, &cpa, repo);
4746 if (patch_script_file && fclose(patch_script_file) == EOF &&
4748 error = got_error_from_errno2("fclose", patch_script_path);
4750 got_repo_close(repo);
4752 got_worktree_close(worktree);
4761 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4766 struct collect_commit_logmsg_arg {
4767 const char *cmdline_log;
4769 const char *worktree_path;
4770 const char *branch_name;
4771 const char *repo_path;
4776 static const struct got_error *
4777 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4780 char *initial_content = NULL;
4781 struct got_pathlist_entry *pe;
4782 const struct got_error *err = NULL;
4783 char *template = NULL;
4784 struct collect_commit_logmsg_arg *a = arg;
4788 /* if a message was specified on the command line, just use it */
4789 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4790 len = strlen(a->cmdline_log) + 1;
4791 *logmsg = malloc(len + 1);
4792 if (*logmsg == NULL)
4793 return got_error_from_errno("malloc");
4794 strlcpy(*logmsg, a->cmdline_log, len);
4798 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4799 return got_error_from_errno("asprintf");
4801 if (asprintf(&initial_content,
4802 "\n# changes to be committed on branch %s:\n",
4803 a->branch_name) == -1)
4804 return got_error_from_errno("asprintf");
4806 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4810 dprintf(fd, initial_content);
4812 TAILQ_FOREACH(pe, commitable_paths, entry) {
4813 struct got_commitable *ct = pe->data;
4814 dprintf(fd, "# %c %s\n",
4815 got_commitable_get_status(ct),
4816 got_commitable_get_path(ct));
4820 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4822 free(initial_content);
4825 /* Editor is done; we can now apply unveil(2) */
4827 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4836 static const struct got_error *
4837 cmd_commit(int argc, char *argv[])
4839 const struct got_error *error = NULL;
4840 struct got_worktree *worktree = NULL;
4841 struct got_repository *repo = NULL;
4842 char *cwd = NULL, *id_str = NULL;
4843 struct got_object_id *id = NULL;
4844 const char *logmsg = NULL;
4845 struct collect_commit_logmsg_arg cl_arg;
4846 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4847 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4848 struct got_pathlist_head paths;
4851 cl_arg.logmsg_path = NULL;
4853 while ((ch = getopt(argc, argv, "m:")) != -1) {
4868 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4869 "unveil", NULL) == -1)
4872 cwd = getcwd(NULL, 0);
4874 error = got_error_from_errno("getcwd");
4877 error = got_worktree_open(&worktree, cwd);
4881 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4884 if (rebase_in_progress) {
4885 error = got_error(GOT_ERR_REBASING);
4889 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4894 error = get_gitconfig_path(&gitconfig_path);
4897 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4902 error = get_author(&author, repo);
4907 * unveil(2) traverses exec(2); if an editor is used we have
4908 * to apply unveil after the log message has been written.
4910 if (logmsg == NULL || strlen(logmsg) == 0)
4911 error = get_editor(&editor);
4913 error = apply_unveil(got_repo_get_path(repo), 0,
4914 got_worktree_get_root_path(worktree));
4918 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4922 cl_arg.editor = editor;
4923 cl_arg.cmdline_log = logmsg;
4924 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4925 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4926 if (!histedit_in_progress) {
4927 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4928 error = got_error(GOT_ERR_COMMIT_BRANCH);
4931 cl_arg.branch_name += 11;
4933 cl_arg.repo_path = got_repo_get_path(repo);
4934 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4935 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4937 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4938 cl_arg.logmsg_path != NULL)
4939 preserve_logmsg = 1;
4943 error = got_object_id_str(&id_str, id);
4946 printf("Created commit %s\n", id_str);
4948 if (preserve_logmsg) {
4949 fprintf(stderr, "%s: log message preserved in %s\n",
4950 getprogname(), cl_arg.logmsg_path);
4951 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4953 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4954 free(cl_arg.logmsg_path);
4956 got_repo_close(repo);
4958 got_worktree_close(worktree);
4961 free(gitconfig_path);
4968 usage_cherrypick(void)
4970 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4974 static const struct got_error *
4975 cmd_cherrypick(int argc, char *argv[])
4977 const struct got_error *error = NULL;
4978 struct got_worktree *worktree = NULL;
4979 struct got_repository *repo = NULL;
4980 char *cwd = NULL, *commit_id_str = NULL;
4981 struct got_object_id *commit_id = NULL;
4982 struct got_commit_object *commit = NULL;
4983 struct got_object_qid *pid;
4984 struct got_reference *head_ref = NULL;
4985 int ch, did_something = 0;
4987 while ((ch = getopt(argc, argv, "")) != -1) {
4999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5000 "unveil", NULL) == -1)
5006 cwd = getcwd(NULL, 0);
5008 error = got_error_from_errno("getcwd");
5011 error = got_worktree_open(&worktree, cwd);
5015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5020 error = apply_unveil(got_repo_get_path(repo), 0,
5021 got_worktree_get_root_path(worktree));
5025 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5026 GOT_OBJ_TYPE_COMMIT, repo);
5027 if (error != NULL) {
5028 struct got_reference *ref;
5029 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5031 error = got_ref_open(&ref, repo, argv[0], 0);
5034 error = got_ref_resolve(&commit_id, repo, ref);
5039 error = got_object_id_str(&commit_id_str, commit_id);
5043 error = got_ref_open(&head_ref, repo,
5044 got_worktree_get_head_ref_name(worktree), 0);
5048 error = check_same_branch(commit_id, head_ref, NULL, repo);
5050 if (error->code != GOT_ERR_ANCESTRY)
5054 error = got_error(GOT_ERR_SAME_BRANCH);
5058 error = got_object_open_as_commit(&commit, repo, commit_id);
5061 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5062 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5063 commit_id, repo, update_progress, &did_something, check_cancelled,
5069 printf("Merged commit %s\n", commit_id_str);
5072 got_object_commit_close(commit);
5073 free(commit_id_str);
5075 got_ref_close(head_ref);
5077 got_worktree_close(worktree);
5079 got_repo_close(repo);
5086 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5090 static const struct got_error *
5091 cmd_backout(int argc, char *argv[])
5093 const struct got_error *error = NULL;
5094 struct got_worktree *worktree = NULL;
5095 struct got_repository *repo = NULL;
5096 char *cwd = NULL, *commit_id_str = NULL;
5097 struct got_object_id *commit_id = NULL;
5098 struct got_commit_object *commit = NULL;
5099 struct got_object_qid *pid;
5100 struct got_reference *head_ref = NULL;
5101 int ch, did_something = 0;
5103 while ((ch = getopt(argc, argv, "")) != -1) {
5115 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5116 "unveil", NULL) == -1)
5122 cwd = getcwd(NULL, 0);
5124 error = got_error_from_errno("getcwd");
5127 error = got_worktree_open(&worktree, cwd);
5131 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5136 error = apply_unveil(got_repo_get_path(repo), 0,
5137 got_worktree_get_root_path(worktree));
5141 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5142 GOT_OBJ_TYPE_COMMIT, repo);
5143 if (error != NULL) {
5144 struct got_reference *ref;
5145 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5147 error = got_ref_open(&ref, repo, argv[0], 0);
5150 error = got_ref_resolve(&commit_id, repo, ref);
5155 error = got_object_id_str(&commit_id_str, commit_id);
5159 error = got_ref_open(&head_ref, repo,
5160 got_worktree_get_head_ref_name(worktree), 0);
5164 error = check_same_branch(commit_id, head_ref, NULL, repo);
5168 error = got_object_open_as_commit(&commit, repo, commit_id);
5171 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5173 error = got_error(GOT_ERR_ROOT_COMMIT);
5177 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5178 update_progress, &did_something, check_cancelled, NULL);
5183 printf("Backed out commit %s\n", commit_id_str);
5186 got_object_commit_close(commit);
5187 free(commit_id_str);
5189 got_ref_close(head_ref);
5191 got_worktree_close(worktree);
5193 got_repo_close(repo);
5200 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5206 trim_logmsg(char *logmsg, int limit)
5211 len = strlen(logmsg);
5215 nl = strchr(logmsg, '\n');
5220 static const struct got_error *
5221 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5223 const struct got_error *err;
5224 char *logmsg0 = NULL;
5227 err = got_object_commit_get_logmsg(&logmsg0, commit);
5232 while (isspace((unsigned char)s[0]))
5235 *logmsg = strdup(s);
5236 if (*logmsg == NULL) {
5237 err = got_error_from_errno("strdup");
5241 trim_logmsg(*logmsg, limit);
5247 static const struct got_error *
5248 show_rebase_progress(struct got_commit_object *commit,
5249 struct got_object_id *old_id, struct got_object_id *new_id)
5251 const struct got_error *err;
5252 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5254 err = got_object_id_str(&old_id_str, old_id);
5259 err = got_object_id_str(&new_id_str, new_id);
5264 old_id_str[12] = '\0';
5266 new_id_str[12] = '\0';
5268 err = get_short_logmsg(&logmsg, 42, commit);
5272 printf("%s -> %s: %s\n", old_id_str,
5273 new_id_str ? new_id_str : "no-op change", logmsg);
5280 static const struct got_error *
5281 rebase_progress(void *arg, unsigned char status, const char *path)
5283 unsigned char *rebase_status = arg;
5285 while (path[0] == '/')
5287 printf("%c %s\n", status, path);
5289 if (*rebase_status == GOT_STATUS_CONFLICT)
5291 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5292 *rebase_status = status;
5296 static const struct got_error *
5297 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5298 struct got_reference *branch, struct got_reference *new_base_branch,
5299 struct got_reference *tmp_branch, struct got_repository *repo)
5301 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5302 return got_worktree_rebase_complete(worktree, fileindex,
5303 new_base_branch, tmp_branch, branch, repo);
5306 static const struct got_error *
5307 rebase_commit(struct got_pathlist_head *merged_paths,
5308 struct got_worktree *worktree, struct got_fileindex *fileindex,
5309 struct got_reference *tmp_branch,
5310 struct got_object_id *commit_id, struct got_repository *repo)
5312 const struct got_error *error;
5313 struct got_commit_object *commit;
5314 struct got_object_id *new_commit_id;
5316 error = got_object_open_as_commit(&commit, repo, commit_id);
5320 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5321 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5323 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5325 error = show_rebase_progress(commit, commit_id, NULL);
5327 error = show_rebase_progress(commit, commit_id, new_commit_id);
5328 free(new_commit_id);
5331 got_object_commit_close(commit);
5335 struct check_path_prefix_arg {
5336 const char *path_prefix;
5341 static const struct got_error *
5342 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5343 struct got_blob_object *blob2, struct got_object_id *id1,
5344 struct got_object_id *id2, const char *path1, const char *path2,
5345 mode_t mode1, mode_t mode2, struct got_repository *repo)
5347 struct check_path_prefix_arg *a = arg;
5349 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5350 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5351 return got_error(a->errcode);
5356 static const struct got_error *
5357 check_path_prefix(struct got_object_id *parent_id,
5358 struct got_object_id *commit_id, const char *path_prefix,
5359 int errcode, struct got_repository *repo)
5361 const struct got_error *err;
5362 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5363 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5364 struct check_path_prefix_arg cpp_arg;
5366 if (got_path_is_root_dir(path_prefix))
5369 err = got_object_open_as_commit(&commit, repo, commit_id);
5373 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5377 err = got_object_open_as_tree(&tree1, repo,
5378 got_object_commit_get_tree_id(parent_commit));
5382 err = got_object_open_as_tree(&tree2, repo,
5383 got_object_commit_get_tree_id(commit));
5387 cpp_arg.path_prefix = path_prefix;
5388 while (cpp_arg.path_prefix[0] == '/')
5389 cpp_arg.path_prefix++;
5390 cpp_arg.len = strlen(cpp_arg.path_prefix);
5391 cpp_arg.errcode = errcode;
5392 err = got_diff_tree(tree1, tree2, "", "", repo,
5393 check_path_prefix_in_diff, &cpp_arg, 0);
5396 got_object_tree_close(tree1);
5398 got_object_tree_close(tree2);
5400 got_object_commit_close(commit);
5402 got_object_commit_close(parent_commit);
5406 static const struct got_error *
5407 collect_commits(struct got_object_id_queue *commits,
5408 struct got_object_id *initial_commit_id,
5409 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5410 const char *path_prefix, int path_prefix_errcode,
5411 struct got_repository *repo)
5413 const struct got_error *err = NULL;
5414 struct got_commit_graph *graph = NULL;
5415 struct got_object_id *parent_id = NULL;
5416 struct got_object_qid *qid;
5417 struct got_object_id *commit_id = initial_commit_id;
5419 err = got_commit_graph_open(&graph, "/", 1);
5423 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5424 check_cancelled, NULL);
5427 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5428 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5429 check_cancelled, NULL);
5431 if (err->code == GOT_ERR_ITER_COMPLETED) {
5432 err = got_error_msg(GOT_ERR_ANCESTRY,
5433 "ran out of commits to rebase before "
5434 "youngest common ancestor commit has "
5439 err = check_path_prefix(parent_id, commit_id,
5440 path_prefix, path_prefix_errcode, repo);
5444 err = got_object_qid_alloc(&qid, commit_id);
5447 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5448 commit_id = parent_id;
5452 got_commit_graph_close(graph);
5456 static const struct got_error *
5457 cmd_rebase(int argc, char *argv[])
5459 const struct got_error *error = NULL;
5460 struct got_worktree *worktree = NULL;
5461 struct got_repository *repo = NULL;
5462 struct got_fileindex *fileindex = NULL;
5464 struct got_reference *branch = NULL;
5465 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5466 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5467 struct got_object_id *resume_commit_id = NULL;
5468 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5469 struct got_commit_object *commit = NULL;
5470 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5471 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5472 struct got_object_id_queue commits;
5473 struct got_pathlist_head merged_paths;
5474 const struct got_object_id_queue *parent_ids;
5475 struct got_object_qid *qid, *pid;
5477 SIMPLEQ_INIT(&commits);
5478 TAILQ_INIT(&merged_paths);
5480 while ((ch = getopt(argc, argv, "ac")) != -1) {
5486 continue_rebase = 1;
5498 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5499 "unveil", NULL) == -1)
5502 if (abort_rebase && continue_rebase)
5504 else if (abort_rebase || continue_rebase) {
5507 } else if (argc != 1)
5510 cwd = getcwd(NULL, 0);
5512 error = got_error_from_errno("getcwd");
5515 error = got_worktree_open(&worktree, cwd);
5519 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5524 error = apply_unveil(got_repo_get_path(repo), 0,
5525 got_worktree_get_root_path(worktree));
5529 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5535 if (!rebase_in_progress) {
5536 error = got_error(GOT_ERR_NOT_REBASING);
5539 error = got_worktree_rebase_continue(&resume_commit_id,
5540 &new_base_branch, &tmp_branch, &branch, &fileindex,
5544 printf("Switching work tree to %s\n",
5545 got_ref_get_symref_target(new_base_branch));
5546 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5547 new_base_branch, update_progress, &did_something);
5550 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5551 goto done; /* nothing else to do */
5554 if (continue_rebase) {
5555 if (!rebase_in_progress) {
5556 error = got_error(GOT_ERR_NOT_REBASING);
5559 error = got_worktree_rebase_continue(&resume_commit_id,
5560 &new_base_branch, &tmp_branch, &branch, &fileindex,
5565 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5566 resume_commit_id, repo);
5570 yca_id = got_object_id_dup(resume_commit_id);
5571 if (yca_id == NULL) {
5572 error = got_error_from_errno("got_object_id_dup");
5576 error = got_ref_open(&branch, repo, argv[0], 0);
5581 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5585 if (!continue_rebase) {
5586 struct got_object_id *base_commit_id;
5588 base_commit_id = got_worktree_get_base_commit_id(worktree);
5589 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5590 base_commit_id, branch_head_commit_id, repo,
5591 check_cancelled, NULL);
5594 if (yca_id == NULL) {
5595 error = got_error_msg(GOT_ERR_ANCESTRY,
5596 "specified branch shares no common ancestry "
5597 "with work tree's branch");
5601 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5603 if (error->code != GOT_ERR_ANCESTRY)
5607 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5608 "specified branch resolves to a commit which "
5609 "is already contained in work tree's branch");
5612 error = got_worktree_rebase_prepare(&new_base_branch,
5613 &tmp_branch, &fileindex, worktree, branch, repo);
5618 commit_id = branch_head_commit_id;
5619 error = got_object_open_as_commit(&commit, repo, commit_id);
5623 parent_ids = got_object_commit_get_parent_ids(commit);
5624 pid = SIMPLEQ_FIRST(parent_ids);
5626 if (!continue_rebase) {
5628 error = got_worktree_rebase_abort(worktree, fileindex,
5629 repo, new_base_branch, update_progress,
5633 printf("Rebase of %s aborted\n",
5634 got_ref_get_name(branch));
5636 error = got_error(GOT_ERR_EMPTY_REBASE);
5639 error = collect_commits(&commits, commit_id, pid->id,
5640 yca_id, got_worktree_get_path_prefix(worktree),
5641 GOT_ERR_REBASE_PATH, repo);
5642 got_object_commit_close(commit);
5647 if (SIMPLEQ_EMPTY(&commits)) {
5648 if (continue_rebase) {
5649 error = rebase_complete(worktree, fileindex,
5650 branch, new_base_branch, tmp_branch, repo);
5653 /* Fast-forward the reference of the branch. */
5654 struct got_object_id *new_head_commit_id;
5656 error = got_ref_resolve(&new_head_commit_id, repo,
5660 error = got_object_id_str(&id_str, new_head_commit_id);
5661 printf("Forwarding %s to commit %s\n",
5662 got_ref_get_name(branch), id_str);
5664 error = got_ref_change_ref(branch,
5665 new_head_commit_id);
5672 SIMPLEQ_FOREACH(qid, &commits, entry) {
5673 commit_id = qid->id;
5674 parent_id = pid ? pid->id : yca_id;
5677 error = got_worktree_rebase_merge_files(&merged_paths,
5678 worktree, fileindex, parent_id, commit_id, repo,
5679 rebase_progress, &rebase_status, check_cancelled, NULL);
5683 if (rebase_status == GOT_STATUS_CONFLICT) {
5684 got_worktree_rebase_pathlist_free(&merged_paths);
5688 error = rebase_commit(&merged_paths, worktree, fileindex,
5689 tmp_branch, commit_id, repo);
5690 got_worktree_rebase_pathlist_free(&merged_paths);
5695 if (rebase_status == GOT_STATUS_CONFLICT) {
5696 error = got_worktree_rebase_postpone(worktree, fileindex);
5699 error = got_error_msg(GOT_ERR_CONFLICTS,
5700 "conflicts must be resolved before rebasing can continue");
5702 error = rebase_complete(worktree, fileindex, branch,
5703 new_base_branch, tmp_branch, repo);
5705 got_object_id_queue_free(&commits);
5706 free(branch_head_commit_id);
5707 free(resume_commit_id);
5710 got_object_commit_close(commit);
5712 got_ref_close(branch);
5713 if (new_base_branch)
5714 got_ref_close(new_base_branch);
5716 got_ref_close(tmp_branch);
5718 got_worktree_close(worktree);
5720 got_repo_close(repo);
5725 usage_histedit(void)
5727 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5732 #define GOT_HISTEDIT_PICK 'p'
5733 #define GOT_HISTEDIT_EDIT 'e'
5734 #define GOT_HISTEDIT_FOLD 'f'
5735 #define GOT_HISTEDIT_DROP 'd'
5736 #define GOT_HISTEDIT_MESG 'm'
5738 static struct got_histedit_cmd {
5742 } got_histedit_cmds[] = {
5743 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5744 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5745 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5746 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5747 { GOT_HISTEDIT_MESG, "mesg",
5748 "single-line log message for commit above (open editor if empty)" },
5751 struct got_histedit_list_entry {
5752 TAILQ_ENTRY(got_histedit_list_entry) entry;
5753 struct got_object_id *commit_id;
5754 const struct got_histedit_cmd *cmd;
5757 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5759 static const struct got_error *
5760 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5761 FILE *f, struct got_repository *repo)
5763 const struct got_error *err = NULL;
5764 char *logmsg = NULL, *id_str = NULL;
5765 struct got_commit_object *commit = NULL;
5768 err = got_object_open_as_commit(&commit, repo, commit_id);
5772 err = get_short_logmsg(&logmsg, 34, commit);
5776 err = got_object_id_str(&id_str, commit_id);
5780 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5782 err = got_ferror(f, GOT_ERR_IO);
5785 got_object_commit_close(commit);
5791 static const struct got_error *
5792 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5793 struct got_repository *repo)
5795 const struct got_error *err = NULL;
5796 struct got_object_qid *qid;
5798 if (SIMPLEQ_EMPTY(commits))
5799 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5801 SIMPLEQ_FOREACH(qid, commits, entry) {
5802 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5811 static const struct got_error *
5812 write_cmd_list(FILE *f)
5814 const struct got_error *err = NULL;
5817 n = fprintf(f, "# Available histedit commands:\n");
5819 return got_ferror(f, GOT_ERR_IO);
5821 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5822 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5823 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5826 err = got_ferror(f, GOT_ERR_IO);
5830 n = fprintf(f, "# Commits will be processed in order from top to "
5831 "bottom of this file.\n");
5833 return got_ferror(f, GOT_ERR_IO);
5837 static const struct got_error *
5838 histedit_syntax_error(int lineno)
5840 static char msg[42];
5843 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5845 if (ret == -1 || ret >= sizeof(msg))
5846 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5848 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5851 static const struct got_error *
5852 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5853 char *logmsg, struct got_repository *repo)
5855 const struct got_error *err;
5856 struct got_commit_object *folded_commit = NULL;
5857 char *id_str, *folded_logmsg = NULL;
5859 err = got_object_id_str(&id_str, hle->commit_id);
5863 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5867 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5870 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5871 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5872 folded_logmsg) == -1) {
5873 err = got_error_from_errno("asprintf");
5878 got_object_commit_close(folded_commit);
5880 free(folded_logmsg);
5884 static struct got_histedit_list_entry *
5885 get_folded_commits(struct got_histedit_list_entry *hle)
5887 struct got_histedit_list_entry *prev, *folded = NULL;
5889 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5890 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5891 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5892 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5894 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5900 static const struct got_error *
5901 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5902 struct got_repository *repo)
5904 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5905 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5906 const struct got_error *err = NULL;
5907 struct got_commit_object *commit = NULL;
5909 struct got_histedit_list_entry *folded = NULL;
5911 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5915 folded = get_folded_commits(hle);
5917 while (folded != hle) {
5918 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5919 folded = TAILQ_NEXT(folded, entry);
5922 err = append_folded_commit_msg(&new_msg, folded,
5928 folded = TAILQ_NEXT(folded, entry);
5932 err = got_object_id_str(&id_str, hle->commit_id);
5935 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5938 if (asprintf(&new_msg,
5939 "%s\n# original log message of commit %s: %s",
5940 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5941 err = got_error_from_errno("asprintf");
5947 err = got_object_id_str(&id_str, hle->commit_id);
5951 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5955 dprintf(fd, logmsg);
5958 err = get_editor(&editor);
5962 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5964 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5966 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5969 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5970 err = got_error_from_errno2("unlink", logmsg_path);
5976 got_object_commit_close(commit);
5980 static const struct got_error *
5981 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5982 FILE *f, struct got_repository *repo)
5984 const struct got_error *err = NULL;
5985 char *line = NULL, *p, *end;
5989 const struct got_histedit_cmd *cmd;
5990 struct got_object_id *commit_id = NULL;
5991 struct got_histedit_list_entry *hle = NULL;
5994 len = getline(&line, &size, f);
5996 const struct got_error *getline_err;
5999 getline_err = got_error_from_errno("getline");
6000 err = got_ferror(f, getline_err->code);
6005 while (isspace((unsigned char)p[0]))
6007 if (p[0] == '#' || p[0] == '\0') {
6013 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6014 cmd = &got_histedit_cmds[i];
6015 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6016 isspace((unsigned char)p[strlen(cmd->name)])) {
6017 p += strlen(cmd->name);
6020 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6025 if (i == nitems(got_histedit_cmds)) {
6026 err = histedit_syntax_error(lineno);
6029 while (isspace((unsigned char)p[0]))
6031 if (cmd->code == GOT_HISTEDIT_MESG) {
6032 if (hle == NULL || hle->logmsg != NULL) {
6033 err = got_error(GOT_ERR_HISTEDIT_CMD);
6037 err = histedit_edit_logmsg(hle, repo);
6041 hle->logmsg = strdup(p);
6042 if (hle->logmsg == NULL) {
6043 err = got_error_from_errno("strdup");
6052 while (end[0] && !isspace((unsigned char)end[0]))
6056 err = got_object_resolve_id_str(&commit_id, repo, p);
6058 /* override error code */
6059 err = histedit_syntax_error(lineno);
6063 hle = malloc(sizeof(*hle));
6065 err = got_error_from_errno("malloc");
6069 hle->commit_id = commit_id;
6074 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6082 static const struct got_error *
6083 histedit_check_script(struct got_histedit_list *histedit_cmds,
6084 struct got_object_id_queue *commits, struct got_repository *repo)
6086 const struct got_error *err = NULL;
6087 struct got_object_qid *qid;
6088 struct got_histedit_list_entry *hle;
6089 static char msg[80];
6092 if (TAILQ_EMPTY(histedit_cmds))
6093 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6094 "histedit script contains no commands");
6095 if (SIMPLEQ_EMPTY(commits))
6096 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6098 SIMPLEQ_FOREACH(qid, commits, entry) {
6099 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6100 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6104 err = got_object_id_str(&id_str, qid->id);
6107 snprintf(msg, sizeof(msg),
6108 "commit %s missing from histedit script", id_str);
6110 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6114 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6115 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6116 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6117 "last commit in histedit script cannot be folded");
6122 static const struct got_error *
6123 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6124 const char *path, struct got_object_id_queue *commits,
6125 struct got_repository *repo)
6127 const struct got_error *err = NULL;
6131 err = get_editor(&editor);
6135 if (spawn_editor(editor, path) == -1) {
6136 err = got_error_from_errno("failed spawning editor");
6140 f = fopen(path, "r");
6142 err = got_error_from_errno("fopen");
6145 err = histedit_parse_list(histedit_cmds, f, repo);
6149 err = histedit_check_script(histedit_cmds, commits, repo);
6151 if (f && fclose(f) != 0 && err == NULL)
6152 err = got_error_from_errno("fclose");
6157 static const struct got_error *
6158 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6159 struct got_object_id_queue *, const char *, struct got_repository *);
6161 static const struct got_error *
6162 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6163 struct got_object_id_queue *commits, struct got_repository *repo)
6165 const struct got_error *err;
6169 err = got_opentemp_named(&path, &f, "got-histedit");
6173 err = write_cmd_list(f);
6177 err = histedit_write_commit_list(commits, f, repo);
6181 if (fclose(f) != 0) {
6182 err = got_error_from_errno("fclose");
6187 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6189 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6190 err->code != GOT_ERR_HISTEDIT_CMD)
6192 err = histedit_edit_list_retry(histedit_cmds, err,
6193 commits, path, repo);
6196 if (f && fclose(f) != 0 && err == NULL)
6197 err = got_error_from_errno("fclose");
6198 if (path && unlink(path) != 0 && err == NULL)
6199 err = got_error_from_errno2("unlink", path);
6204 static const struct got_error *
6205 histedit_save_list(struct got_histedit_list *histedit_cmds,
6206 struct got_worktree *worktree, struct got_repository *repo)
6208 const struct got_error *err = NULL;
6211 struct got_histedit_list_entry *hle;
6212 struct got_commit_object *commit = NULL;
6214 err = got_worktree_get_histedit_script_path(&path, worktree);
6218 f = fopen(path, "w");
6220 err = got_error_from_errno2("fopen", path);
6223 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6224 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6230 int n = fprintf(f, "%c %s\n",
6231 GOT_HISTEDIT_MESG, hle->logmsg);
6233 err = got_ferror(f, GOT_ERR_IO);
6239 if (f && fclose(f) != 0 && err == NULL)
6240 err = got_error_from_errno("fclose");
6243 got_object_commit_close(commit);
6248 histedit_free_list(struct got_histedit_list *histedit_cmds)
6250 struct got_histedit_list_entry *hle;
6252 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6253 TAILQ_REMOVE(histedit_cmds, hle, entry);
6258 static const struct got_error *
6259 histedit_load_list(struct got_histedit_list *histedit_cmds,
6260 const char *path, struct got_repository *repo)
6262 const struct got_error *err = NULL;
6265 f = fopen(path, "r");
6267 err = got_error_from_errno2("fopen", path);
6271 err = histedit_parse_list(histedit_cmds, f, repo);
6273 if (f && fclose(f) != 0 && err == NULL)
6274 err = got_error_from_errno("fclose");
6278 static const struct got_error *
6279 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6280 const struct got_error *edit_err, struct got_object_id_queue *commits,
6281 const char *path, struct got_repository *repo)
6283 const struct got_error *err = NULL, *prev_err = edit_err;
6286 while (resp != 'c' && resp != 'r' && resp != 'a') {
6287 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6288 "or (a)bort: ", getprogname(), prev_err->msg);
6293 histedit_free_list(histedit_cmds);
6294 err = histedit_run_editor(histedit_cmds, path, commits,
6297 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6298 err->code != GOT_ERR_HISTEDIT_CMD)
6305 } else if (resp == 'r') {
6306 histedit_free_list(histedit_cmds);
6307 err = histedit_edit_script(histedit_cmds,
6310 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6311 err->code != GOT_ERR_HISTEDIT_CMD)
6318 } else if (resp == 'a') {
6319 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6322 printf("invalid response '%c'\n", resp);
6328 static const struct got_error *
6329 histedit_complete(struct got_worktree *worktree,
6330 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6331 struct got_reference *branch, struct got_repository *repo)
6333 printf("Switching work tree to %s\n",
6334 got_ref_get_symref_target(branch));
6335 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6339 static const struct got_error *
6340 show_histedit_progress(struct got_commit_object *commit,
6341 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6343 const struct got_error *err;
6344 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6346 err = got_object_id_str(&old_id_str, hle->commit_id);
6351 err = got_object_id_str(&new_id_str, new_id);
6356 old_id_str[12] = '\0';
6358 new_id_str[12] = '\0';
6361 logmsg = strdup(hle->logmsg);
6362 if (logmsg == NULL) {
6363 err = got_error_from_errno("strdup");
6366 trim_logmsg(logmsg, 42);
6368 err = get_short_logmsg(&logmsg, 42, commit);
6373 switch (hle->cmd->code) {
6374 case GOT_HISTEDIT_PICK:
6375 case GOT_HISTEDIT_EDIT:
6376 printf("%s -> %s: %s\n", old_id_str,
6377 new_id_str ? new_id_str : "no-op change", logmsg);
6379 case GOT_HISTEDIT_DROP:
6380 case GOT_HISTEDIT_FOLD:
6381 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6394 static const struct got_error *
6395 histedit_commit(struct got_pathlist_head *merged_paths,
6396 struct got_worktree *worktree, struct got_fileindex *fileindex,
6397 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6398 struct got_repository *repo)
6400 const struct got_error *err;
6401 struct got_commit_object *commit;
6402 struct got_object_id *new_commit_id;
6404 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6405 && hle->logmsg == NULL) {
6406 err = histedit_edit_logmsg(hle, repo);
6411 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6415 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6416 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6419 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6421 err = show_histedit_progress(commit, hle, NULL);
6423 err = show_histedit_progress(commit, hle, new_commit_id);
6424 free(new_commit_id);
6427 got_object_commit_close(commit);
6431 static const struct got_error *
6432 histedit_skip_commit(struct got_histedit_list_entry *hle,
6433 struct got_worktree *worktree, struct got_repository *repo)
6435 const struct got_error *error;
6436 struct got_commit_object *commit;
6438 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6443 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6447 error = show_histedit_progress(commit, hle, NULL);
6448 got_object_commit_close(commit);
6452 static const struct got_error *
6453 cmd_histedit(int argc, char *argv[])
6455 const struct got_error *error = NULL;
6456 struct got_worktree *worktree = NULL;
6457 struct got_fileindex *fileindex = NULL;
6458 struct got_repository *repo = NULL;
6460 struct got_reference *branch = NULL;
6461 struct got_reference *tmp_branch = NULL;
6462 struct got_object_id *resume_commit_id = NULL;
6463 struct got_object_id *base_commit_id = NULL;
6464 struct got_object_id *head_commit_id = NULL;
6465 struct got_commit_object *commit = NULL;
6466 int ch, rebase_in_progress = 0, did_something;
6467 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6468 const char *edit_script_path = NULL;
6469 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6470 struct got_object_id_queue commits;
6471 struct got_pathlist_head merged_paths;
6472 const struct got_object_id_queue *parent_ids;
6473 struct got_object_qid *pid;
6474 struct got_histedit_list histedit_cmds;
6475 struct got_histedit_list_entry *hle;
6477 SIMPLEQ_INIT(&commits);
6478 TAILQ_INIT(&histedit_cmds);
6479 TAILQ_INIT(&merged_paths);
6481 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6490 edit_script_path = optarg;
6502 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6503 "unveil", NULL) == -1)
6506 if (abort_edit && continue_edit)
6512 * This command cannot apply unveil(2) in all cases because the
6513 * user may choose to run an editor to edit the histedit script
6514 * and to edit individual commit log messages.
6515 * unveil(2) traverses exec(2); if an editor is used we have to
6516 * apply unveil after edit script and log messages have been written.
6517 * XXX TODO: Make use of unveil(2) where possible.
6520 cwd = getcwd(NULL, 0);
6522 error = got_error_from_errno("getcwd");
6525 error = got_worktree_open(&worktree, cwd);
6529 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6534 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6537 if (rebase_in_progress) {
6538 error = got_error(GOT_ERR_REBASING);
6542 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6546 if (edit_in_progress && abort_edit) {
6547 error = got_worktree_histedit_continue(&resume_commit_id,
6548 &tmp_branch, &branch, &base_commit_id, &fileindex,
6552 printf("Switching work tree to %s\n",
6553 got_ref_get_symref_target(branch));
6554 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6555 branch, base_commit_id, update_progress, &did_something);
6558 printf("Histedit of %s aborted\n",
6559 got_ref_get_symref_target(branch));
6560 goto done; /* nothing else to do */
6561 } else if (abort_edit) {
6562 error = got_error(GOT_ERR_NOT_HISTEDIT);
6566 if (continue_edit) {
6569 if (!edit_in_progress) {
6570 error = got_error(GOT_ERR_NOT_HISTEDIT);
6574 error = got_worktree_get_histedit_script_path(&path, worktree);
6578 error = histedit_load_list(&histedit_cmds, path, repo);
6583 error = got_worktree_histedit_continue(&resume_commit_id,
6584 &tmp_branch, &branch, &base_commit_id, &fileindex,
6589 error = got_ref_resolve(&head_commit_id, repo, branch);
6593 error = got_object_open_as_commit(&commit, repo,
6597 parent_ids = got_object_commit_get_parent_ids(commit);
6598 pid = SIMPLEQ_FIRST(parent_ids);
6600 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6603 error = collect_commits(&commits, head_commit_id, pid->id,
6604 base_commit_id, got_worktree_get_path_prefix(worktree),
6605 GOT_ERR_HISTEDIT_PATH, repo);
6606 got_object_commit_close(commit);
6611 if (edit_in_progress) {
6612 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6616 error = got_ref_open(&branch, repo,
6617 got_worktree_get_head_ref_name(worktree), 0);
6621 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6622 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6623 "will not edit commit history of a branch outside "
6624 "the \"refs/heads/\" reference namespace");
6628 error = got_ref_resolve(&head_commit_id, repo, branch);
6629 got_ref_close(branch);
6634 error = got_object_open_as_commit(&commit, repo,
6638 parent_ids = got_object_commit_get_parent_ids(commit);
6639 pid = SIMPLEQ_FIRST(parent_ids);
6641 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6644 error = collect_commits(&commits, head_commit_id, pid->id,
6645 got_worktree_get_base_commit_id(worktree),
6646 got_worktree_get_path_prefix(worktree),
6647 GOT_ERR_HISTEDIT_PATH, repo);
6648 got_object_commit_close(commit);
6653 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6654 &base_commit_id, &fileindex, worktree, repo);
6658 if (edit_script_path) {
6659 error = histedit_load_list(&histedit_cmds,
6660 edit_script_path, repo);
6662 got_worktree_histedit_abort(worktree, fileindex,
6663 repo, branch, base_commit_id,
6664 update_progress, &did_something);
6668 error = histedit_edit_script(&histedit_cmds, &commits,
6671 got_worktree_histedit_abort(worktree, fileindex,
6672 repo, branch, base_commit_id,
6673 update_progress, &did_something);
6679 error = histedit_save_list(&histedit_cmds, worktree,
6682 got_worktree_histedit_abort(worktree, fileindex,
6683 repo, branch, base_commit_id,
6684 update_progress, &did_something);
6690 error = histedit_check_script(&histedit_cmds, &commits, repo);
6694 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6695 if (resume_commit_id) {
6696 if (got_object_id_cmp(hle->commit_id,
6697 resume_commit_id) != 0)
6700 resume_commit_id = NULL;
6701 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6702 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6703 error = histedit_skip_commit(hle, worktree,
6706 error = histedit_commit(NULL, worktree,
6707 fileindex, tmp_branch, hle, repo);
6714 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6715 error = histedit_skip_commit(hle, worktree, repo);
6721 error = got_object_open_as_commit(&commit, repo,
6725 parent_ids = got_object_commit_get_parent_ids(commit);
6726 pid = SIMPLEQ_FIRST(parent_ids);
6728 error = got_worktree_histedit_merge_files(&merged_paths,
6729 worktree, fileindex, pid->id, hle->commit_id, repo,
6730 rebase_progress, &rebase_status, check_cancelled, NULL);
6733 got_object_commit_close(commit);
6736 if (rebase_status == GOT_STATUS_CONFLICT) {
6737 got_worktree_rebase_pathlist_free(&merged_paths);
6741 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6743 error = got_object_id_str(&id_str, hle->commit_id);
6746 printf("Stopping histedit for amending commit %s\n",
6749 got_worktree_rebase_pathlist_free(&merged_paths);
6750 error = got_worktree_histedit_postpone(worktree,
6755 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6756 error = histedit_skip_commit(hle, worktree, repo);
6762 error = histedit_commit(&merged_paths, worktree, fileindex,
6763 tmp_branch, hle, repo);
6764 got_worktree_rebase_pathlist_free(&merged_paths);
6769 if (rebase_status == GOT_STATUS_CONFLICT) {
6770 error = got_worktree_histedit_postpone(worktree, fileindex);
6773 error = got_error_msg(GOT_ERR_CONFLICTS,
6774 "conflicts must be resolved before rebasing can continue");
6776 error = histedit_complete(worktree, fileindex, tmp_branch,
6779 got_object_id_queue_free(&commits);
6780 histedit_free_list(&histedit_cmds);
6781 free(head_commit_id);
6782 free(base_commit_id);
6783 free(resume_commit_id);
6785 got_object_commit_close(commit);
6787 got_ref_close(branch);
6789 got_ref_close(tmp_branch);
6791 got_worktree_close(worktree);
6793 got_repo_close(repo);
6798 usage_integrate(void)
6800 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6804 static const struct got_error *
6805 cmd_integrate(int argc, char *argv[])
6807 const struct got_error *error = NULL;
6808 struct got_repository *repo = NULL;
6809 struct got_worktree *worktree = NULL;
6810 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6811 const char *branch_arg = NULL;
6812 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6813 struct got_fileindex *fileindex = NULL;
6814 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6815 int ch, did_something = 0;
6817 while ((ch = getopt(argc, argv, "")) != -1) {
6830 branch_arg = argv[0];
6832 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6833 "unveil", NULL) == -1)
6836 cwd = getcwd(NULL, 0);
6838 error = got_error_from_errno("getcwd");
6842 error = got_worktree_open(&worktree, cwd);
6846 error = check_rebase_or_histedit_in_progress(worktree);
6850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6855 error = apply_unveil(got_repo_get_path(repo), 0,
6856 got_worktree_get_root_path(worktree));
6860 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6861 error = got_error_from_errno("asprintf");
6865 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6866 &base_branch_ref, worktree, refname, repo);
6870 refname = strdup(got_ref_get_name(branch_ref));
6871 if (refname == NULL) {
6872 error = got_error_from_errno("strdup");
6873 got_worktree_integrate_abort(worktree, fileindex, repo,
6874 branch_ref, base_branch_ref);
6877 base_refname = strdup(got_ref_get_name(base_branch_ref));
6878 if (base_refname == NULL) {
6879 error = got_error_from_errno("strdup");
6880 got_worktree_integrate_abort(worktree, fileindex, repo,
6881 branch_ref, base_branch_ref);
6885 error = got_ref_resolve(&commit_id, repo, branch_ref);
6889 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6893 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6894 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6895 "specified branch has already been integrated");
6896 got_worktree_integrate_abort(worktree, fileindex, repo,
6897 branch_ref, base_branch_ref);
6901 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6903 if (error->code == GOT_ERR_ANCESTRY)
6904 error = got_error(GOT_ERR_REBASE_REQUIRED);
6905 got_worktree_integrate_abort(worktree, fileindex, repo,
6906 branch_ref, base_branch_ref);
6910 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6911 branch_ref, base_branch_ref, update_progress, &did_something,
6912 check_cancelled, NULL);
6916 printf("Integrated %s into %s\n", refname, base_refname);
6919 got_repo_close(repo);
6921 got_worktree_close(worktree);
6923 free(base_commit_id);
6933 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6934 "[file-path ...]\n",
6939 static const struct got_error *
6940 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6941 const char *path, struct got_object_id *blob_id,
6942 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6943 int dirfd, const char *de_name)
6945 const struct got_error *err = NULL;
6946 char *id_str = NULL;
6948 if (staged_status != GOT_STATUS_ADD &&
6949 staged_status != GOT_STATUS_MODIFY &&
6950 staged_status != GOT_STATUS_DELETE)
6953 if (staged_status == GOT_STATUS_ADD ||
6954 staged_status == GOT_STATUS_MODIFY)
6955 err = got_object_id_str(&id_str, staged_blob_id);
6957 err = got_object_id_str(&id_str, blob_id);
6961 printf("%s %c %s\n", id_str, staged_status, path);
6966 static const struct got_error *
6967 cmd_stage(int argc, char *argv[])
6969 const struct got_error *error = NULL;
6970 struct got_repository *repo = NULL;
6971 struct got_worktree *worktree = NULL;
6973 struct got_pathlist_head paths;
6974 struct got_pathlist_entry *pe;
6975 int ch, list_stage = 0, pflag = 0;
6976 FILE *patch_script_file = NULL;
6977 const char *patch_script_path = NULL;
6978 struct choose_patch_arg cpa;
6982 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6991 patch_script_path = optarg;
7003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7004 "unveil", NULL) == -1)
7007 if (list_stage && (pflag || patch_script_path))
7008 errx(1, "-l option cannot be used with other options");
7009 if (patch_script_path && !pflag)
7010 errx(1, "-F option can only be used together with -p option");
7012 cwd = getcwd(NULL, 0);
7014 error = got_error_from_errno("getcwd");
7018 error = got_worktree_open(&worktree, cwd);
7022 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7027 if (patch_script_path) {
7028 patch_script_file = fopen(patch_script_path, "r");
7029 if (patch_script_file == NULL) {
7030 error = got_error_from_errno2("fopen",
7035 error = apply_unveil(got_repo_get_path(repo), 0,
7036 got_worktree_get_root_path(worktree));
7040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7045 error = got_worktree_status(worktree, &paths, repo,
7046 print_stage, NULL, check_cancelled, NULL);
7048 cpa.patch_script_file = patch_script_file;
7049 cpa.action = "stage";
7050 error = got_worktree_stage(worktree, &paths,
7051 pflag ? NULL : print_status, NULL,
7052 pflag ? choose_patch : NULL, &cpa, repo);
7055 if (patch_script_file && fclose(patch_script_file) == EOF &&
7057 error = got_error_from_errno2("fclose", patch_script_path);
7059 got_repo_close(repo);
7061 got_worktree_close(worktree);
7062 TAILQ_FOREACH(pe, &paths, entry)
7063 free((char *)pe->path);
7064 got_pathlist_free(&paths);
7072 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7073 "[file-path ...]\n",
7079 static const struct got_error *
7080 cmd_unstage(int argc, char *argv[])
7082 const struct got_error *error = NULL;
7083 struct got_repository *repo = NULL;
7084 struct got_worktree *worktree = NULL;
7086 struct got_pathlist_head paths;
7087 struct got_pathlist_entry *pe;
7088 int ch, did_something = 0, pflag = 0;
7089 FILE *patch_script_file = NULL;
7090 const char *patch_script_path = NULL;
7091 struct choose_patch_arg cpa;
7095 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7101 patch_script_path = optarg;
7113 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7114 "unveil", NULL) == -1)
7117 if (patch_script_path && !pflag)
7118 errx(1, "-F option can only be used together with -p option");
7120 cwd = getcwd(NULL, 0);
7122 error = got_error_from_errno("getcwd");
7126 error = got_worktree_open(&worktree, cwd);
7130 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7135 if (patch_script_path) {
7136 patch_script_file = fopen(patch_script_path, "r");
7137 if (patch_script_file == NULL) {
7138 error = got_error_from_errno2("fopen",
7144 error = apply_unveil(got_repo_get_path(repo), 0,
7145 got_worktree_get_root_path(worktree));
7149 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7153 cpa.patch_script_file = patch_script_file;
7154 cpa.action = "unstage";
7155 error = got_worktree_unstage(worktree, &paths, update_progress,
7156 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7158 if (patch_script_file && fclose(patch_script_file) == EOF &&
7160 error = got_error_from_errno2("fclose", patch_script_path);
7162 got_repo_close(repo);
7164 got_worktree_close(worktree);
7165 TAILQ_FOREACH(pe, &paths, entry)
7166 free((char *)pe->path);
7167 got_pathlist_free(&paths);
7175 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7176 "arg1 [arg2 ...]\n", getprogname());
7180 static const struct got_error *
7181 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7183 const struct got_error *err;
7184 struct got_blob_object *blob;
7186 err = got_object_open_as_blob(&blob, repo, id, 8192);
7190 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7191 got_object_blob_close(blob);
7195 static const struct got_error *
7196 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7198 const struct got_error *err;
7199 struct got_tree_object *tree;
7202 err = got_object_open_as_tree(&tree, repo, id);
7206 nentries = got_object_tree_get_nentries(tree);
7207 for (i = 0; i < nentries; i++) {
7208 struct got_tree_entry *te;
7210 if (sigint_received || sigpipe_received)
7212 te = got_object_tree_get_entry(tree, i);
7213 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7216 fprintf(outfile, "%s %.7o %s\n", id_str,
7217 got_tree_entry_get_mode(te),
7218 got_tree_entry_get_name(te));
7222 got_object_tree_close(tree);
7226 static const struct got_error *
7227 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7229 const struct got_error *err;
7230 struct got_commit_object *commit;
7231 const struct got_object_id_queue *parent_ids;
7232 struct got_object_qid *pid;
7233 char *id_str = NULL;
7234 const char *logmsg = NULL;
7236 err = got_object_open_as_commit(&commit, repo, id);
7240 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7244 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7245 parent_ids = got_object_commit_get_parent_ids(commit);
7246 fprintf(outfile, "numparents %d\n",
7247 got_object_commit_get_nparents(commit));
7248 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7250 err = got_object_id_str(&pid_str, pid->id);
7253 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7256 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7257 got_object_commit_get_author(commit),
7258 got_object_commit_get_author_time(commit));
7260 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7261 got_object_commit_get_author(commit),
7262 got_object_commit_get_committer_time(commit));
7264 logmsg = got_object_commit_get_logmsg_raw(commit);
7265 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7266 fprintf(outfile, "%s", logmsg);
7269 got_object_commit_close(commit);
7273 static const struct got_error *
7274 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7276 const struct got_error *err;
7277 struct got_tag_object *tag;
7278 char *id_str = NULL;
7279 const char *tagmsg = NULL;
7281 err = got_object_open_as_tag(&tag, repo, id);
7285 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7289 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7291 switch (got_object_tag_get_object_type(tag)) {
7292 case GOT_OBJ_TYPE_BLOB:
7293 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7294 GOT_OBJ_LABEL_BLOB);
7296 case GOT_OBJ_TYPE_TREE:
7297 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7298 GOT_OBJ_LABEL_TREE);
7300 case GOT_OBJ_TYPE_COMMIT:
7301 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7302 GOT_OBJ_LABEL_COMMIT);
7304 case GOT_OBJ_TYPE_TAG:
7305 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7312 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7313 got_object_tag_get_name(tag));
7315 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7316 got_object_tag_get_tagger(tag),
7317 got_object_tag_get_tagger_time(tag));
7319 tagmsg = got_object_tag_get_message(tag);
7320 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7321 fprintf(outfile, "%s", tagmsg);
7324 got_object_tag_close(tag);
7328 static const struct got_error *
7329 cmd_cat(int argc, char *argv[])
7331 const struct got_error *error;
7332 struct got_repository *repo = NULL;
7333 struct got_worktree *worktree = NULL;
7334 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7335 const char *commit_id_str = NULL;
7336 struct got_object_id *id = NULL, *commit_id = NULL;
7337 int ch, obj_type, i, force_path = 0;
7340 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7345 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7348 commit_id_str = optarg;
7351 repo_path = realpath(optarg, NULL);
7352 if (repo_path == NULL)
7353 return got_error_from_errno2("realpath",
7355 got_path_strip_trailing_slashes(repo_path);
7369 cwd = getcwd(NULL, 0);
7371 error = got_error_from_errno("getcwd");
7374 error = got_worktree_open(&worktree, cwd);
7375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7378 if (repo_path == NULL) {
7380 got_worktree_get_repo_path(worktree));
7381 if (repo_path == NULL) {
7382 error = got_error_from_errno("strdup");
7388 if (repo_path == NULL) {
7389 repo_path = getcwd(NULL, 0);
7390 if (repo_path == NULL)
7391 return got_error_from_errno("getcwd");
7394 error = got_repo_open(&repo, repo_path, NULL);
7399 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7403 if (commit_id_str == NULL)
7404 commit_id_str = GOT_REF_HEAD;
7405 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7409 for (i = 0; i < argc; i++) {
7411 error = got_object_id_by_path(&id, repo, commit_id,
7416 error = match_object_id(&id, &label, argv[i],
7417 GOT_OBJ_TYPE_ANY, 0, repo);
7419 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7420 error->code != GOT_ERR_NOT_REF)
7422 error = got_object_id_by_path(&id, repo,
7423 commit_id, argv[i]);
7429 error = got_object_get_type(&obj_type, repo, id);
7434 case GOT_OBJ_TYPE_BLOB:
7435 error = cat_blob(id, repo, stdout);
7437 case GOT_OBJ_TYPE_TREE:
7438 error = cat_tree(id, repo, stdout);
7440 case GOT_OBJ_TYPE_COMMIT:
7441 error = cat_commit(id, repo, stdout);
7443 case GOT_OBJ_TYPE_TAG:
7444 error = cat_tag(id, repo, stdout);
7447 error = got_error(GOT_ERR_OBJ_TYPE);
7463 got_worktree_close(worktree);
7465 const struct got_error *repo_error;
7466 repo_error = got_repo_close(repo);