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);
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
375 spawn_editor(const char *editor, const char *file)
378 sig_t sighup, sigint, sigquit;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
389 execl(editor, editor, file, (char *)NULL);
393 while (waitpid(pid, &st, 0) == -1)
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
418 int content_changed = 0;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
438 return got_error_from_errno("malloc");
442 fp = fopen(logmsg_path, "r");
444 err = got_error_from_errno("fopen");
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 dprintf(fd, initial_content);
492 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 free(initial_content);
498 static const struct got_error *
499 import_progress(void *arg, const char *path)
501 printf("A %s\n", path);
505 static const struct got_error *
506 get_author(char **author, struct got_repository *repo)
508 const struct got_error *err = NULL;
509 const char *got_author, *name, *email;
513 name = got_repo_get_gitconfig_author_name(repo);
514 email = got_repo_get_gitconfig_author_email(repo);
516 if (asprintf(author, "%s <%s>", name, email) == -1)
517 return got_error_from_errno("asprintf");
521 got_author = getenv("GOT_AUTHOR");
522 if (got_author == NULL) {
523 name = got_repo_get_global_gitconfig_author_name(repo);
524 email = got_repo_get_global_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
530 /* TODO: Look up user in password database? */
531 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 *author = strdup(got_author);
536 return got_error_from_errno("strdup");
539 * Really dumb email address check; we're only doing this to
540 * avoid git's object parser breaking on commits we create.
542 while (*got_author && *got_author != '<')
544 if (*got_author != '<') {
545 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
548 while (*got_author && *got_author != '@')
550 if (*got_author != '@') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 while (*got_author && *got_author != '>')
556 if (*got_author != '>')
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
566 static const struct got_error *
567 get_gitconfig_path(char **gitconfig_path)
569 const char *homedir = getenv("HOME");
571 *gitconfig_path = NULL;
573 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 return got_error_from_errno("asprintf");
580 static const struct got_error *
581 cmd_import(int argc, char *argv[])
583 const struct got_error *error = NULL;
584 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 const char *branch_name = "main";
587 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 struct got_repository *repo = NULL;
589 struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 struct got_object_id *new_commit_id = NULL;
592 struct got_pathlist_head ignores;
593 struct got_pathlist_entry *pe;
594 int preserve_logmsg = 0;
596 TAILQ_INIT(&ignores);
598 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
601 branch_name = optarg;
604 logmsg = strdup(optarg);
605 if (logmsg == NULL) {
606 error = got_error_from_errno("strdup");
611 repo_path = realpath(optarg, NULL);
612 if (repo_path == NULL) {
613 error = got_error_from_errno2("realpath",
619 if (optarg[0] == '\0')
621 error = got_pathlist_insert(&pe, &ignores, optarg,
636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 if (repo_path == NULL) {
645 repo_path = getcwd(NULL, 0);
646 if (repo_path == NULL)
647 return got_error_from_errno("getcwd");
649 got_path_strip_trailing_slashes(repo_path);
650 error = get_gitconfig_path(&gitconfig_path);
653 error = got_repo_open(&repo, repo_path, gitconfig_path);
657 error = get_author(&author, repo);
662 * Don't let the user create a branch name with a leading '-'.
663 * While technically a valid reference name, this case is usually
664 * an unintended typo.
666 if (branch_name[0] == '-')
667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 error = got_error_from_errno("asprintf");
674 error = got_ref_open(&branch_ref, repo, refname, 0);
676 if (error->code != GOT_ERR_NOT_REF)
679 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 "import target branch already exists");
684 path_dir = realpath(argv[0], NULL);
685 if (path_dir == NULL) {
686 error = got_error_from_errno2("realpath", argv[0]);
689 got_path_strip_trailing_slashes(path_dir);
692 * unveil(2) traverses exec(2); if an editor is used we have
693 * to apply unveil after the log message has been written.
695 if (logmsg == NULL || strlen(logmsg) == 0) {
696 error = get_editor(&editor);
700 error = collect_import_msg(&logmsg, &logmsg_path, editor,
703 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 if (unveil(path_dir, "r") != 0) {
711 error = got_error_from_errno2("unveil", path_dir);
717 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 author, &ignores, repo, import_progress, NULL);
732 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 error = got_ref_write(branch_ref, repo);
746 error = got_object_id_str(&id_str, new_commit_id);
753 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 if (error->code != GOT_ERR_NOT_REF) {
761 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 error = got_ref_write(head_ref, repo);
777 printf("Created branch %s with commit %s\n",
778 got_ref_get_name(branch_ref), id_str);
780 if (preserve_logmsg) {
781 fprintf(stderr, "%s: log message preserved in %s\n",
782 getprogname(), logmsg_path);
783 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 error = got_error_from_errno2("unlink", logmsg_path);
793 free(gitconfig_path);
795 got_ref_close(branch_ref);
797 got_ref_close(head_ref);
804 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 "[-p prefix] repository-path [worktree-path]\n", getprogname());
810 show_worktree_base_ref_warning(void)
812 fprintf(stderr, "%s: warning: could not create a reference "
813 "to the work tree's base commit; the commit could be "
814 "garbage-collected by Git; making the repository "
815 "writable and running 'got update' will prevent this\n",
819 struct got_checkout_progress_arg {
820 const char *worktree_path;
821 int had_base_commit_ref_error;
824 static const struct got_error *
825 checkout_progress(void *arg, unsigned char status, const char *path)
827 struct got_checkout_progress_arg *a = arg;
829 /* Base commit bump happens silently. */
830 if (status == GOT_STATUS_BUMP_BASE)
833 if (status == GOT_STATUS_BASE_REF_ERR) {
834 a->had_base_commit_ref_error = 1;
838 while (path[0] == '/')
841 printf("%c %s/%s\n", status, a->worktree_path, path);
845 static const struct got_error *
846 check_cancelled(void *arg)
848 if (sigint_received || sigpipe_received)
849 return got_error(GOT_ERR_CANCELLED);
853 static const struct got_error *
854 check_linear_ancestry(struct got_object_id *commit_id,
855 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 struct got_repository *repo)
858 const struct got_error *err = NULL;
859 struct got_object_id *yca_id;
861 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 commit_id, base_commit_id, repo, check_cancelled, NULL);
867 return got_error(GOT_ERR_ANCESTRY);
870 * Require a straight line of history between the target commit
871 * and the work tree's base commit.
873 * Non-linear situations such as this require a rebase:
875 * (commit) D F (base_commit)
883 * 'got update' only handles linear cases:
884 * Update forwards in time: A (base/yca) - B - C - D (commit)
885 * Update backwards in time: D (base) - C - B - A (commit/yca)
887 if (allow_forwards_in_time_only) {
888 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 return got_error(GOT_ERR_ANCESTRY);
890 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
898 static const struct got_error *
899 check_same_branch(struct got_object_id *commit_id,
900 struct got_reference *head_ref, struct got_object_id *yca_id,
901 struct got_repository *repo)
903 const struct got_error *err = NULL;
904 struct got_commit_graph *graph = NULL;
905 struct got_object_id *head_commit_id = NULL;
906 int is_same_branch = 0;
908 err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
916 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
921 err = got_commit_graph_open(&graph, "/", 1);
925 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 check_cancelled, NULL);
931 struct got_object_id *id;
932 err = got_commit_graph_iter_next(&id, graph, repo,
933 check_cancelled, NULL);
935 if (err->code == GOT_ERR_ITER_COMPLETED)
941 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 if (got_object_id_cmp(id, commit_id) == 0) {
951 got_commit_graph_close(graph);
952 free(head_commit_id);
953 if (!err && !is_same_branch)
954 err = got_error(GOT_ERR_ANCESTRY);
958 static const struct got_error *
959 resolve_commit_arg(struct got_object_id **commit_id,
960 const char *commit_id_arg, struct got_repository *repo)
962 const struct got_error *err;
963 struct got_reference *ref;
964 struct got_tag_object *tag;
966 err = got_repo_object_match_tag(&tag, commit_id_arg,
967 GOT_OBJ_TYPE_COMMIT, repo);
969 *commit_id = got_object_id_dup(
970 got_object_tag_get_object_id(tag));
971 if (*commit_id == NULL)
972 err = got_error_from_errno("got_object_id_dup");
973 got_object_tag_close(tag);
975 } else if (err->code != GOT_ERR_NO_OBJ)
978 err = got_ref_open(&ref, repo, commit_id_arg, 0);
980 err = got_ref_resolve(commit_id, repo, ref);
983 if (err->code != GOT_ERR_NOT_REF)
985 err = got_repo_match_object_id_prefix(commit_id,
986 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
991 static const struct got_error *
992 cmd_checkout(int argc, char *argv[])
994 const struct got_error *error = NULL;
995 struct got_repository *repo = NULL;
996 struct got_reference *head_ref = NULL;
997 struct got_worktree *worktree = NULL;
998 char *repo_path = NULL;
999 char *worktree_path = NULL;
1000 const char *path_prefix = "";
1001 const char *branch_name = GOT_REF_HEAD;
1002 char *commit_id_str = NULL;
1003 int ch, same_path_prefix, allow_nonempty = 0;
1004 struct got_pathlist_head paths;
1005 struct got_checkout_progress_arg cpa;
1009 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1012 branch_name = optarg;
1015 commit_id_str = strdup(optarg);
1016 if (commit_id_str == NULL)
1017 return got_error_from_errno("strdup");
1023 path_prefix = optarg;
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1040 char *cwd, *base, *dotgit;
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL)
1043 return got_error_from_errno2("realpath", argv[0]);
1044 cwd = getcwd(NULL, 0);
1046 error = got_error_from_errno("getcwd");
1049 if (path_prefix[0]) {
1050 base = basename(path_prefix);
1052 error = got_error_from_errno2("basename",
1057 base = basename(repo_path);
1059 error = got_error_from_errno2("basename",
1064 dotgit = strstr(base, ".git");
1067 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1068 error = got_error_from_errno("asprintf");
1073 } else if (argc == 2) {
1074 repo_path = realpath(argv[0], NULL);
1075 if (repo_path == NULL) {
1076 error = got_error_from_errno2("realpath", argv[0]);
1079 worktree_path = realpath(argv[1], NULL);
1080 if (worktree_path == NULL) {
1081 if (errno != ENOENT) {
1082 error = got_error_from_errno2("realpath",
1086 worktree_path = strdup(argv[1]);
1087 if (worktree_path == NULL) {
1088 error = got_error_from_errno("strdup");
1095 got_path_strip_trailing_slashes(repo_path);
1096 got_path_strip_trailing_slashes(worktree_path);
1098 error = got_repo_open(&repo, repo_path, NULL);
1102 /* Pre-create work tree path for unveil(2) */
1103 error = got_path_mkdir(worktree_path);
1105 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1106 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1108 if (!allow_nonempty &&
1109 !got_path_dir_is_empty(worktree_path)) {
1110 error = got_error_path(worktree_path,
1111 GOT_ERR_DIR_NOT_EMPTY);
1116 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1120 error = got_ref_open(&head_ref, repo, branch_name, 0);
1124 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1125 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1128 error = got_worktree_open(&worktree, worktree_path);
1132 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1136 if (!same_path_prefix) {
1137 error = got_error(GOT_ERR_PATH_PREFIX);
1141 if (commit_id_str) {
1142 struct got_object_id *commit_id;
1143 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1146 error = check_linear_ancestry(commit_id,
1147 got_worktree_get_base_commit_id(worktree), 0, repo);
1148 if (error != NULL) {
1152 error = check_same_branch(commit_id, head_ref, NULL, repo);
1155 error = got_worktree_set_base_commit_id(worktree, repo,
1162 error = got_pathlist_append(&paths, "", NULL);
1165 cpa.worktree_path = worktree_path;
1166 cpa.had_base_commit_ref_error = 0;
1167 error = got_worktree_checkout_files(worktree, &paths, repo,
1168 checkout_progress, &cpa, check_cancelled, NULL);
1172 printf("Now shut up and hack\n");
1173 if (cpa.had_base_commit_ref_error)
1174 show_worktree_base_ref_warning();
1176 got_pathlist_free(&paths);
1177 free(commit_id_str);
1179 free(worktree_path);
1186 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1191 static const struct got_error *
1192 update_progress(void *arg, unsigned char status, const char *path)
1194 int *did_something = arg;
1196 if (status == GOT_STATUS_EXISTS ||
1197 status == GOT_STATUS_BASE_REF_ERR)
1202 /* Base commit bump happens silently. */
1203 if (status == GOT_STATUS_BUMP_BASE)
1206 while (path[0] == '/')
1208 printf("%c %s\n", status, path);
1212 static const struct got_error *
1213 switch_head_ref(struct got_reference *head_ref,
1214 struct got_object_id *commit_id, struct got_worktree *worktree,
1215 struct got_repository *repo)
1217 const struct got_error *err = NULL;
1219 int ref_has_moved = 0;
1221 /* Trivial case: switching between two different references. */
1222 if (strcmp(got_ref_get_name(head_ref),
1223 got_worktree_get_head_ref_name(worktree)) != 0) {
1224 printf("Switching work tree from %s to %s\n",
1225 got_worktree_get_head_ref_name(worktree),
1226 got_ref_get_name(head_ref));
1227 return got_worktree_set_head_ref(worktree, head_ref);
1230 err = check_linear_ancestry(commit_id,
1231 got_worktree_get_base_commit_id(worktree), 0, repo);
1233 if (err->code != GOT_ERR_ANCESTRY)
1240 /* Switching to a rebased branch with the same reference name. */
1241 err = got_object_id_str(&base_id_str,
1242 got_worktree_get_base_commit_id(worktree));
1245 printf("Reference %s now points at a different branch\n",
1246 got_worktree_get_head_ref_name(worktree));
1247 printf("Switching work tree from %s to %s\n", base_id_str,
1248 got_worktree_get_head_ref_name(worktree));
1252 static const struct got_error *
1253 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1255 const struct got_error *err;
1258 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1262 return got_error(GOT_ERR_REBASING);
1264 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1268 return got_error(GOT_ERR_HISTEDIT_BUSY);
1273 static const struct got_error *
1274 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1275 char *argv[], struct got_worktree *worktree)
1277 const struct got_error *err = NULL;
1284 return got_error_from_errno("strdup");
1285 return got_pathlist_append(paths, path, NULL);
1288 for (i = 0; i < argc; i++) {
1289 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1292 err = got_pathlist_append(paths, path, NULL);
1302 static const struct got_error *
1303 cmd_update(int argc, char *argv[])
1305 const struct got_error *error = NULL;
1306 struct got_repository *repo = NULL;
1307 struct got_worktree *worktree = NULL;
1308 char *worktree_path = NULL;
1309 struct got_object_id *commit_id = NULL;
1310 char *commit_id_str = NULL;
1311 const char *branch_name = NULL;
1312 struct got_reference *head_ref = NULL;
1313 struct got_pathlist_head paths;
1314 struct got_pathlist_entry *pe;
1315 int ch, did_something = 0;
1319 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1322 branch_name = optarg;
1325 commit_id_str = strdup(optarg);
1326 if (commit_id_str == NULL)
1327 return got_error_from_errno("strdup");
1339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1340 "unveil", NULL) == -1)
1343 worktree_path = getcwd(NULL, 0);
1344 if (worktree_path == NULL) {
1345 error = got_error_from_errno("getcwd");
1348 error = got_worktree_open(&worktree, worktree_path);
1352 error = check_rebase_or_histedit_in_progress(worktree);
1356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1361 error = apply_unveil(got_repo_get_path(repo), 0,
1362 got_worktree_get_root_path(worktree));
1366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1370 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1371 got_worktree_get_head_ref_name(worktree), 0);
1374 if (commit_id_str == NULL) {
1375 error = got_ref_resolve(&commit_id, repo, head_ref);
1378 error = got_object_id_str(&commit_id_str, commit_id);
1382 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1383 free(commit_id_str);
1384 commit_id_str = NULL;
1387 error = got_object_id_str(&commit_id_str, commit_id);
1393 struct got_object_id *head_commit_id;
1394 TAILQ_FOREACH(pe, &paths, entry) {
1395 if (pe->path_len == 0)
1397 error = got_error_msg(GOT_ERR_BAD_PATH,
1398 "switching between branches requires that "
1399 "the entire work tree gets updated");
1402 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1405 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1407 free(head_commit_id);
1410 error = check_same_branch(commit_id, head_ref, NULL, repo);
1413 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1417 error = check_linear_ancestry(commit_id,
1418 got_worktree_get_base_commit_id(worktree), 0, repo);
1419 if (error != NULL) {
1420 if (error->code == GOT_ERR_ANCESTRY)
1421 error = got_error(GOT_ERR_BRANCH_MOVED);
1424 error = check_same_branch(commit_id, head_ref, NULL, repo);
1429 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1437 error = got_worktree_checkout_files(worktree, &paths, repo,
1438 update_progress, &did_something, check_cancelled, NULL);
1443 printf("Updated to commit %s\n", commit_id_str);
1445 printf("Already up-to-date\n");
1447 free(worktree_path);
1448 TAILQ_FOREACH(pe, &paths, entry)
1449 free((char *)pe->path);
1450 got_pathlist_free(&paths);
1452 free(commit_id_str);
1456 static const struct got_error *
1457 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1458 const char *path, int diff_context, int ignore_whitespace,
1459 struct got_repository *repo)
1461 const struct got_error *err = NULL;
1462 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1465 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1470 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1474 while (path[0] == '/')
1476 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1477 ignore_whitespace, stdout);
1480 got_object_blob_close(blob1);
1481 got_object_blob_close(blob2);
1485 static const struct got_error *
1486 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1487 const char *path, int diff_context, int ignore_whitespace,
1488 struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1492 struct got_diff_blob_output_unidiff_arg arg;
1495 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1500 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1504 arg.diff_context = diff_context;
1505 arg.ignore_whitespace = ignore_whitespace;
1506 arg.outfile = stdout;
1507 while (path[0] == '/')
1509 err = got_diff_tree(tree1, tree2, path, path, repo,
1510 got_diff_blob_output_unidiff, &arg, 1);
1513 got_object_tree_close(tree1);
1515 got_object_tree_close(tree2);
1519 static const struct got_error *
1520 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1521 const char *path, int diff_context, struct got_repository *repo)
1523 const struct got_error *err = NULL;
1524 struct got_commit_object *pcommit = NULL;
1525 char *id_str1 = NULL, *id_str2 = NULL;
1526 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1527 struct got_object_qid *qid;
1529 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1531 err = got_object_open_as_commit(&pcommit, repo,
1537 if (path && path[0] != '\0') {
1539 err = got_object_id_by_path(&obj_id2, repo, id, path);
1542 err = got_object_id_str(&id_str2, obj_id2);
1548 err = got_object_id_by_path(&obj_id1, repo,
1554 err = got_object_id_str(&id_str1, obj_id1);
1560 err = got_object_get_type(&obj_type, repo, obj_id2);
1565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1567 case GOT_OBJ_TYPE_BLOB:
1568 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1571 case GOT_OBJ_TYPE_TREE:
1572 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1576 err = got_error(GOT_ERR_OBJ_TYPE);
1582 obj_id2 = got_object_commit_get_tree_id(commit);
1583 err = got_object_id_str(&id_str2, obj_id2);
1586 obj_id1 = got_object_commit_get_tree_id(pcommit);
1587 err = got_object_id_str(&id_str1, obj_id1);
1590 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1591 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1597 got_object_commit_close(pcommit);
1602 get_datestr(time_t *time, char *datebuf)
1604 struct tm mytm, *tm;
1607 tm = gmtime_r(time, &mytm);
1610 s = asctime_r(tm, datebuf);
1613 p = strchr(s, '\n');
1619 static const struct got_error *
1620 match_logmsg(int *have_match, struct got_object_id *id,
1621 struct got_commit_object *commit, regex_t *regex)
1623 const struct got_error *err = NULL;
1624 regmatch_t regmatch;
1625 char *id_str = NULL, *logmsg = NULL;
1629 err = got_object_id_str(&id_str, id);
1633 err = got_object_commit_get_logmsg(&logmsg, commit);
1637 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1645 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1647 static const struct got_error *
1648 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1649 struct got_repository *repo, const char *path, int show_patch,
1650 int diff_context, struct got_reflist_head *refs)
1652 const struct got_error *err = NULL;
1653 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1655 time_t committer_time;
1656 const char *author, *committer;
1657 char *refs_str = NULL;
1658 struct got_reflist_entry *re;
1660 SIMPLEQ_FOREACH(re, refs, entry) {
1663 struct got_tag_object *tag = NULL;
1666 name = got_ref_get_name(re->ref);
1667 if (strcmp(name, GOT_REF_HEAD) == 0)
1669 if (strncmp(name, "refs/", 5) == 0)
1671 if (strncmp(name, "got/", 4) == 0)
1673 if (strncmp(name, "heads/", 6) == 0)
1675 if (strncmp(name, "remotes/", 8) == 0)
1677 if (strncmp(name, "tags/", 5) == 0) {
1678 err = got_object_open_as_tag(&tag, repo, re->id);
1680 if (err->code != GOT_ERR_OBJ_TYPE)
1682 /* Ref points at something other than a tag. */
1687 cmp = got_object_id_cmp(tag ?
1688 got_object_tag_get_object_id(tag) : re->id, id);
1690 got_object_tag_close(tag);
1694 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1696 err = got_error_from_errno("asprintf");
1702 err = got_object_id_str(&id_str, id);
1706 printf(GOT_COMMIT_SEP_STR);
1707 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1708 refs_str ? refs_str : "", refs_str ? ")" : "");
1713 printf("from: %s\n", got_object_commit_get_author(commit));
1714 committer_time = got_object_commit_get_committer_time(commit);
1715 datestr = get_datestr(&committer_time, datebuf);
1717 printf("date: %s UTC\n", datestr);
1718 author = got_object_commit_get_author(commit);
1719 committer = got_object_commit_get_committer(commit);
1720 if (strcmp(author, committer) != 0)
1721 printf("via: %s\n", committer);
1722 if (got_object_commit_get_nparents(commit) > 1) {
1723 const struct got_object_id_queue *parent_ids;
1724 struct got_object_qid *qid;
1726 parent_ids = got_object_commit_get_parent_ids(commit);
1727 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1728 err = got_object_id_str(&id_str, qid->id);
1731 printf("parent %d: %s\n", n++, id_str);
1736 err = got_object_commit_get_logmsg(&logmsg0, commit);
1742 line = strsep(&logmsg, "\n");
1744 printf(" %s\n", line);
1749 err = print_patch(commit, id, path, diff_context, repo);
1754 if (fflush(stdout) != 0 && err == NULL)
1755 err = got_error_from_errno("fflush");
1759 static const struct got_error *
1760 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1761 const char *path, int show_patch, const char *search_pattern,
1762 int diff_context, int limit, int first_parent_traversal,
1763 struct got_reflist_head *refs)
1765 const struct got_error *err;
1766 struct got_commit_graph *graph;
1770 if (search_pattern &&
1771 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1772 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1774 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1777 err = got_commit_graph_iter_start(graph, root_id, repo,
1778 check_cancelled, NULL);
1782 struct got_commit_object *commit;
1783 struct got_object_id *id;
1785 if (sigint_received || sigpipe_received)
1788 err = got_commit_graph_iter_next(&id, graph, repo,
1789 check_cancelled, NULL);
1791 if (err->code == GOT_ERR_ITER_COMPLETED)
1798 err = got_object_open_as_commit(&commit, repo, id);
1802 if (search_pattern) {
1803 err = match_logmsg(&have_match, id, commit, ®ex);
1805 got_object_commit_close(commit);
1808 if (have_match == 0) {
1809 got_object_commit_close(commit);
1814 err = print_commit(commit, id, repo, path, show_patch,
1815 diff_context, refs);
1816 got_object_commit_close(commit);
1817 if (err || (limit && --limit == 0))
1823 got_commit_graph_close(graph);
1830 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1831 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1836 get_default_log_limit(void)
1838 const char *got_default_log_limit;
1842 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1843 if (got_default_log_limit == NULL)
1845 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1851 static const struct got_error *
1852 cmd_log(int argc, char *argv[])
1854 const struct got_error *error;
1855 struct got_repository *repo = NULL;
1856 struct got_worktree *worktree = NULL;
1857 struct got_commit_object *commit = NULL;
1858 struct got_object_id *id = NULL;
1859 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1860 const char *start_commit = NULL, *search_pattern = NULL;
1861 int diff_context = -1, ch;
1862 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1864 struct got_reflist_head refs;
1866 SIMPLEQ_INIT(&refs);
1869 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1875 limit = get_default_log_limit();
1877 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1883 start_commit = optarg;
1886 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1889 err(1, "-C option %s", errstr);
1892 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1894 err(1, "-l option %s", errstr);
1897 first_parent_traversal = 1;
1900 repo_path = realpath(optarg, NULL);
1901 if (repo_path == NULL)
1902 return got_error_from_errno2("realpath",
1904 got_path_strip_trailing_slashes(repo_path);
1907 search_pattern = optarg;
1918 if (diff_context == -1)
1920 else if (!show_patch)
1921 errx(1, "-C reguires -p");
1923 cwd = getcwd(NULL, 0);
1925 error = got_error_from_errno("getcwd");
1929 error = got_worktree_open(&worktree, cwd);
1930 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1937 error = got_error_from_errno("strdup");
1940 } else if (argc == 1) {
1942 error = got_worktree_resolve_path(&path, worktree,
1947 path = strdup(argv[0]);
1949 error = got_error_from_errno("strdup");
1956 if (repo_path == NULL) {
1957 repo_path = worktree ?
1958 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1960 if (repo_path == NULL) {
1961 error = got_error_from_errno("strdup");
1965 error = got_repo_open(&repo, repo_path, NULL);
1969 error = apply_unveil(got_repo_get_path(repo), 1,
1970 worktree ? got_worktree_get_root_path(worktree) : NULL);
1974 if (start_commit == NULL) {
1975 struct got_reference *head_ref;
1976 error = got_ref_open(&head_ref, repo,
1977 worktree ? got_worktree_get_head_ref_name(worktree)
1981 error = got_ref_resolve(&id, repo, head_ref);
1982 got_ref_close(head_ref);
1985 error = got_object_open_as_commit(&commit, repo, id);
1987 struct got_reference *ref;
1988 error = got_ref_open(&ref, repo, start_commit, 0);
1989 if (error == NULL) {
1991 error = got_ref_resolve(&id, repo, ref);
1995 error = got_object_get_type(&obj_type, repo, id);
1998 if (obj_type == GOT_OBJ_TYPE_TAG) {
1999 struct got_tag_object *tag;
2000 error = got_object_open_as_tag(&tag, repo, id);
2003 if (got_object_tag_get_object_type(tag) !=
2004 GOT_OBJ_TYPE_COMMIT) {
2005 got_object_tag_close(tag);
2006 error = got_error(GOT_ERR_OBJ_TYPE);
2010 id = got_object_id_dup(
2011 got_object_tag_get_object_id(tag));
2013 error = got_error_from_errno(
2014 "got_object_id_dup");
2015 got_object_tag_close(tag);
2018 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2019 error = got_error(GOT_ERR_OBJ_TYPE);
2022 error = got_object_open_as_commit(&commit, repo, id);
2026 if (commit == NULL) {
2027 error = got_repo_match_object_id_prefix(&id,
2028 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2037 const char *prefix = got_worktree_get_path_prefix(worktree);
2039 if (asprintf(&p, "%s%s%s", prefix,
2040 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2041 error = got_error_from_errno("asprintf");
2044 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2047 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2052 path = in_repo_path;
2055 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2059 error = print_commits(id, repo, path, show_patch, search_pattern,
2060 diff_context, limit, first_parent_traversal, &refs);
2067 got_worktree_close(worktree);
2069 const struct got_error *repo_error;
2070 repo_error = got_repo_close(repo);
2074 got_ref_list_free(&refs);
2081 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2082 "[-w] [object1 object2 | path]\n", getprogname());
2086 struct print_diff_arg {
2087 struct got_repository *repo;
2088 struct got_worktree *worktree;
2093 int ignore_whitespace;
2096 static const struct got_error *
2097 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2098 const char *path, struct got_object_id *blob_id,
2099 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2100 int dirfd, const char *de_name)
2102 struct print_diff_arg *a = arg;
2103 const struct got_error *err = NULL;
2104 struct got_blob_object *blob1 = NULL;
2107 char *abspath = NULL, *label1 = NULL;
2110 if (a->diff_staged) {
2111 if (staged_status != GOT_STATUS_MODIFY &&
2112 staged_status != GOT_STATUS_ADD &&
2113 staged_status != GOT_STATUS_DELETE)
2116 if (staged_status == GOT_STATUS_DELETE)
2118 if (status == GOT_STATUS_NONEXISTENT)
2119 return got_error_set_errno(ENOENT, path);
2120 if (status != GOT_STATUS_MODIFY &&
2121 status != GOT_STATUS_ADD &&
2122 status != GOT_STATUS_DELETE &&
2123 status != GOT_STATUS_CONFLICT)
2127 if (!a->header_shown) {
2128 printf("diff %s %s%s\n", a->id_str,
2129 got_worktree_get_root_path(a->worktree),
2130 a->diff_staged ? " (staged changes)" : "");
2131 a->header_shown = 1;
2134 if (a->diff_staged) {
2135 const char *label1 = NULL, *label2 = NULL;
2136 switch (staged_status) {
2137 case GOT_STATUS_MODIFY:
2141 case GOT_STATUS_ADD:
2144 case GOT_STATUS_DELETE:
2148 return got_error(GOT_ERR_FILE_STATUS);
2150 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2151 label1, label2, a->diff_context, a->ignore_whitespace,
2155 if (staged_status == GOT_STATUS_ADD ||
2156 staged_status == GOT_STATUS_MODIFY) {
2158 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2162 err = got_object_id_str(&id_str, staged_blob_id);
2165 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2166 err = got_error_from_errno("asprintf");
2171 } else if (status != GOT_STATUS_ADD) {
2172 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2177 if (status != GOT_STATUS_DELETE) {
2178 if (asprintf(&abspath, "%s/%s",
2179 got_worktree_get_root_path(a->worktree), path) == -1) {
2180 err = got_error_from_errno("asprintf");
2185 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2187 err = got_error_from_errno2("openat", abspath);
2191 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2193 err = got_error_from_errno2("open", abspath);
2197 if (fstat(fd, &sb) == -1) {
2198 err = got_error_from_errno2("fstat", abspath);
2201 f2 = fdopen(fd, "r");
2203 err = got_error_from_errno2("fdopen", abspath);
2210 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2211 a->diff_context, a->ignore_whitespace, stdout);
2214 got_object_blob_close(blob1);
2215 if (f2 && fclose(f2) == EOF && err == NULL)
2216 err = got_error_from_errno("fclose");
2217 if (fd != -1 && close(fd) == -1 && err == NULL)
2218 err = got_error_from_errno("close");
2223 static const struct got_error *
2224 match_object_id(struct got_object_id **id, char **label,
2225 const char *id_str, int obj_type, int resolve_tags,
2226 struct got_repository *repo)
2228 const struct got_error *err;
2229 struct got_tag_object *tag;
2230 struct got_reference *ref = NULL;
2236 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2239 *id = got_object_id_dup(
2240 got_object_tag_get_object_id(tag));
2242 err = got_error_from_errno("got_object_id_dup");
2243 else if (asprintf(label, "refs/tags/%s",
2244 got_object_tag_get_name(tag)) == -1) {
2245 err = got_error_from_errno("asprintf");
2249 got_object_tag_close(tag);
2251 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2252 err->code != GOT_ERR_NO_OBJ)
2256 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2258 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2260 err = got_ref_open(&ref, repo, id_str, 0);
2263 *label = strdup(got_ref_get_name(ref));
2264 if (*label == NULL) {
2265 err = got_error_from_errno("strdup");
2268 err = got_ref_resolve(id, repo, ref);
2270 err = got_object_id_str(label, *id);
2271 if (*label == NULL) {
2272 err = got_error_from_errno("strdup");
2283 static const struct got_error *
2284 cmd_diff(int argc, char *argv[])
2286 const struct got_error *error;
2287 struct got_repository *repo = NULL;
2288 struct got_worktree *worktree = NULL;
2289 char *cwd = NULL, *repo_path = NULL;
2290 struct got_object_id *id1 = NULL, *id2 = NULL;
2291 const char *id_str1 = NULL, *id_str2 = NULL;
2292 char *label1 = NULL, *label2 = NULL;
2294 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2304 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2307 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2310 err(1, "-C option %s", errstr);
2313 repo_path = realpath(optarg, NULL);
2314 if (repo_path == NULL)
2315 return got_error_from_errno2("realpath",
2317 got_path_strip_trailing_slashes(repo_path);
2323 ignore_whitespace = 1;
2334 cwd = getcwd(NULL, 0);
2336 error = got_error_from_errno("getcwd");
2339 error = got_worktree_open(&worktree, cwd);
2340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2343 if (worktree == NULL) {
2344 error = got_error(GOT_ERR_NOT_WORKTREE);
2349 "-r option can't be used when diffing a work tree");
2350 repo_path = strdup(got_worktree_get_repo_path(worktree));
2351 if (repo_path == NULL) {
2352 error = got_error_from_errno("strdup");
2356 error = got_worktree_resolve_path(&path, worktree,
2363 error = got_error_from_errno("strdup");
2367 } else if (argc == 2) {
2369 errx(1, "-s option can't be used when diffing "
2370 "objects in repository");
2373 if (worktree && repo_path == NULL) {
2375 strdup(got_worktree_get_repo_path(worktree));
2376 if (repo_path == NULL) {
2377 error = got_error_from_errno("strdup");
2384 if (repo_path == NULL) {
2385 repo_path = getcwd(NULL, 0);
2386 if (repo_path == NULL)
2387 return got_error_from_errno("getcwd");
2390 error = got_repo_open(&repo, repo_path, NULL);
2395 error = apply_unveil(got_repo_get_path(repo), 1,
2396 worktree ? got_worktree_get_root_path(worktree) : NULL);
2401 struct print_diff_arg arg;
2402 struct got_pathlist_head paths;
2407 error = got_object_id_str(&id_str,
2408 got_worktree_get_base_commit_id(worktree));
2412 arg.worktree = worktree;
2413 arg.diff_context = diff_context;
2414 arg.id_str = id_str;
2415 arg.header_shown = 0;
2416 arg.diff_staged = diff_staged;
2417 arg.ignore_whitespace = ignore_whitespace;
2419 error = got_pathlist_append(&paths, path, NULL);
2423 error = got_worktree_status(worktree, &paths, repo, print_diff,
2424 &arg, check_cancelled, NULL);
2426 got_pathlist_free(&paths);
2430 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2435 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2440 error = got_object_get_type(&type1, repo, id1);
2444 error = got_object_get_type(&type2, repo, id2);
2448 if (type1 != type2) {
2449 error = got_error(GOT_ERR_OBJ_TYPE);
2454 case GOT_OBJ_TYPE_BLOB:
2455 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2456 diff_context, ignore_whitespace, repo, stdout);
2458 case GOT_OBJ_TYPE_TREE:
2459 error = got_diff_objects_as_trees(id1, id2, "", "",
2460 diff_context, ignore_whitespace, repo, stdout);
2462 case GOT_OBJ_TYPE_COMMIT:
2463 printf("diff %s %s\n", label1, label2);
2464 error = got_diff_objects_as_commits(id1, id2, diff_context,
2465 ignore_whitespace, repo, stdout);
2468 error = got_error(GOT_ERR_OBJ_TYPE);
2477 got_worktree_close(worktree);
2479 const struct got_error *repo_error;
2480 repo_error = got_repo_close(repo);
2491 "usage: %s blame [-c commit] [-r repository-path] path\n",
2500 char datebuf[11]; /* YYYY-MM-DD + NUL */
2503 struct blame_cb_args {
2504 struct blame_line *lines;
2508 off_t *line_offsets;
2510 struct got_repository *repo;
2513 static const struct got_error *
2514 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2516 const struct got_error *err = NULL;
2517 struct blame_cb_args *a = arg;
2518 struct blame_line *bline;
2520 size_t linesize = 0;
2521 struct got_commit_object *commit = NULL;
2524 time_t committer_time;
2526 if (nlines != a->nlines ||
2527 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2528 return got_error(GOT_ERR_RANGE);
2530 if (sigint_received)
2531 return got_error(GOT_ERR_ITER_COMPLETED);
2534 return NULL; /* no change in this commit */
2536 /* Annotate this line. */
2537 bline = &a->lines[lineno - 1];
2538 if (bline->annotated)
2540 err = got_object_id_str(&bline->id_str, id);
2544 err = got_object_open_as_commit(&commit, a->repo, id);
2548 bline->committer = strdup(got_object_commit_get_committer(commit));
2549 if (bline->committer == NULL) {
2550 err = got_error_from_errno("strdup");
2554 committer_time = got_object_commit_get_committer_time(commit);
2555 if (localtime_r(&committer_time, &tm) == NULL)
2556 return got_error_from_errno("localtime_r");
2557 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2558 &tm) >= sizeof(bline->datebuf)) {
2559 err = got_error(GOT_ERR_NO_SPACE);
2562 bline->annotated = 1;
2564 /* Print lines annotated so far. */
2565 bline = &a->lines[a->lineno_cur - 1];
2566 if (!bline->annotated)
2569 offset = a->line_offsets[a->lineno_cur - 1];
2570 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2571 err = got_error_from_errno("fseeko");
2575 while (bline->annotated) {
2576 char *smallerthan, *at, *nl, *committer;
2579 if (getline(&line, &linesize, a->f) == -1) {
2581 err = got_error_from_errno("getline");
2585 committer = bline->committer;
2586 smallerthan = strchr(committer, '<');
2587 if (smallerthan && smallerthan[1] != '\0')
2588 committer = smallerthan + 1;
2589 at = strchr(committer, '@');
2592 len = strlen(committer);
2594 committer[8] = '\0';
2596 nl = strchr(line, '\n');
2599 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2600 bline->id_str, bline->datebuf, committer, line);
2603 bline = &a->lines[a->lineno_cur - 1];
2607 got_object_commit_close(commit);
2612 static const struct got_error *
2613 cmd_blame(int argc, char *argv[])
2615 const struct got_error *error;
2616 struct got_repository *repo = NULL;
2617 struct got_worktree *worktree = NULL;
2618 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2619 struct got_object_id *obj_id = NULL;
2620 struct got_object_id *commit_id = NULL;
2621 struct got_blob_object *blob = NULL;
2622 char *commit_id_str = NULL;
2623 struct blame_cb_args bca;
2624 int ch, obj_type, i;
2627 memset(&bca, 0, sizeof(bca));
2630 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2635 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2638 commit_id_str = optarg;
2641 repo_path = realpath(optarg, NULL);
2642 if (repo_path == NULL)
2643 return got_error_from_errno2("realpath",
2645 got_path_strip_trailing_slashes(repo_path);
2661 cwd = getcwd(NULL, 0);
2663 error = got_error_from_errno("getcwd");
2666 if (repo_path == NULL) {
2667 error = got_worktree_open(&worktree, cwd);
2668 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2674 strdup(got_worktree_get_repo_path(worktree));
2675 if (repo_path == NULL)
2676 error = got_error_from_errno("strdup");
2680 repo_path = strdup(cwd);
2681 if (repo_path == NULL) {
2682 error = got_error_from_errno("strdup");
2688 error = got_repo_open(&repo, repo_path, NULL);
2692 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2697 const char *prefix = got_worktree_get_path_prefix(worktree);
2698 char *p, *worktree_subdir = cwd +
2699 strlen(got_worktree_get_root_path(worktree));
2700 if (asprintf(&p, "%s%s%s%s%s",
2701 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2702 worktree_subdir, worktree_subdir[0] ? "/" : "",
2704 error = got_error_from_errno("asprintf");
2707 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2710 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2715 if (commit_id_str == NULL) {
2716 struct got_reference *head_ref;
2717 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2720 error = got_ref_resolve(&commit_id, repo, head_ref);
2721 got_ref_close(head_ref);
2725 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2730 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2733 if (obj_id == NULL) {
2734 error = got_error(GOT_ERR_NO_OBJ);
2738 error = got_object_get_type(&obj_type, repo, obj_id);
2742 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2743 error = got_error(GOT_ERR_OBJ_TYPE);
2747 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2750 bca.f = got_opentemp();
2751 if (bca.f == NULL) {
2752 error = got_error_from_errno("got_opentemp");
2755 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2756 &bca.line_offsets, bca.f, blob);
2757 if (error || bca.nlines == 0)
2760 /* Don't include \n at EOF in the blame line count. */
2761 if (bca.line_offsets[bca.nlines - 1] == filesize)
2764 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2765 if (bca.lines == NULL) {
2766 error = got_error_from_errno("calloc");
2770 bca.nlines_prec = 0;
2778 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2779 check_cancelled, NULL);
2787 got_object_blob_close(blob);
2789 got_worktree_close(worktree);
2791 const struct got_error *repo_error;
2792 repo_error = got_repo_close(repo);
2797 for (i = 0; i < bca.nlines; i++) {
2798 struct blame_line *bline = &bca.lines[i];
2799 free(bline->id_str);
2800 free(bline->committer);
2804 free(bca.line_offsets);
2805 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2806 error = got_error_from_errno("fclose");
2814 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2820 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2821 const char *root_path)
2823 int is_root_path = (strcmp(path, root_path) == 0);
2824 const char *modestr = "";
2825 mode_t mode = got_tree_entry_get_mode(te);
2827 path += strlen(root_path);
2828 while (path[0] == '/')
2831 if (got_object_tree_entry_is_submodule(te))
2833 else if (S_ISLNK(mode))
2835 else if (S_ISDIR(mode))
2837 else if (mode & S_IXUSR)
2840 printf("%s%s%s%s%s\n", id ? id : "", path,
2841 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2844 static const struct got_error *
2845 print_tree(const char *path, struct got_object_id *commit_id,
2846 int show_ids, int recurse, const char *root_path,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_object_id *tree_id = NULL;
2851 struct got_tree_object *tree = NULL;
2854 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2858 err = got_object_open_as_tree(&tree, repo, tree_id);
2861 nentries = got_object_tree_get_nentries(tree);
2862 for (i = 0; i < nentries; i++) {
2863 struct got_tree_entry *te;
2866 if (sigint_received || sigpipe_received)
2869 te = got_object_tree_get_entry(tree, i);
2872 err = got_object_id_str(&id_str,
2873 got_tree_entry_get_id(te));
2876 if (asprintf(&id, "%s ", id_str) == -1) {
2877 err = got_error_from_errno("asprintf");
2883 print_entry(te, id, path, root_path);
2886 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2888 if (asprintf(&child_path, "%s%s%s", path,
2889 path[0] == '/' && path[1] == '\0' ? "" : "/",
2890 got_tree_entry_get_name(te)) == -1) {
2891 err = got_error_from_errno("asprintf");
2894 err = print_tree(child_path, commit_id, show_ids, 1,
2903 got_object_tree_close(tree);
2908 static const struct got_error *
2909 cmd_tree(int argc, char *argv[])
2911 const struct got_error *error;
2912 struct got_repository *repo = NULL;
2913 struct got_worktree *worktree = NULL;
2915 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2916 struct got_object_id *commit_id = NULL;
2917 char *commit_id_str = NULL;
2918 int show_ids = 0, recurse = 0;
2922 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2927 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2930 commit_id_str = optarg;
2933 repo_path = realpath(optarg, NULL);
2934 if (repo_path == NULL)
2935 return got_error_from_errno2("realpath",
2937 got_path_strip_trailing_slashes(repo_path);
2961 cwd = getcwd(NULL, 0);
2963 error = got_error_from_errno("getcwd");
2966 if (repo_path == NULL) {
2967 error = got_worktree_open(&worktree, cwd);
2968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2974 strdup(got_worktree_get_repo_path(worktree));
2975 if (repo_path == NULL)
2976 error = got_error_from_errno("strdup");
2980 repo_path = strdup(cwd);
2981 if (repo_path == NULL) {
2982 error = got_error_from_errno("strdup");
2988 error = got_repo_open(&repo, repo_path, NULL);
2992 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2998 char *p, *worktree_subdir = cwd +
2999 strlen(got_worktree_get_root_path(worktree));
3000 if (asprintf(&p, "%s/%s",
3001 got_worktree_get_path_prefix(worktree),
3002 worktree_subdir) == -1) {
3003 error = got_error_from_errno("asprintf");
3006 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3013 if (in_repo_path == NULL) {
3014 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3019 if (commit_id_str == NULL) {
3020 struct got_reference *head_ref;
3021 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3024 error = got_ref_resolve(&commit_id, repo, head_ref);
3025 got_ref_close(head_ref);
3029 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3034 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3035 in_repo_path, repo);
3042 got_worktree_close(worktree);
3044 const struct got_error *repo_error;
3045 repo_error = got_repo_close(repo);
3055 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3059 static const struct got_error *
3060 print_status(void *arg, unsigned char status, unsigned char staged_status,
3061 const char *path, struct got_object_id *blob_id,
3062 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3063 int dirfd, const char *de_name)
3065 if (status == staged_status && (status == GOT_STATUS_DELETE))
3066 status = GOT_STATUS_NO_CHANGE;
3067 printf("%c%c %s\n", status, staged_status, path);
3071 static const struct got_error *
3072 cmd_status(int argc, char *argv[])
3074 const struct got_error *error = NULL;
3075 struct got_repository *repo = NULL;
3076 struct got_worktree *worktree = NULL;
3078 struct got_pathlist_head paths;
3079 struct got_pathlist_entry *pe;
3084 while ((ch = getopt(argc, argv, "")) != -1) {
3096 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3100 cwd = getcwd(NULL, 0);
3102 error = got_error_from_errno("getcwd");
3106 error = got_worktree_open(&worktree, cwd);
3110 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3115 error = apply_unveil(got_repo_get_path(repo), 1,
3116 got_worktree_get_root_path(worktree));
3120 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3124 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3125 check_cancelled, NULL);
3127 TAILQ_FOREACH(pe, &paths, entry)
3128 free((char *)pe->path);
3129 got_pathlist_free(&paths);
3138 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3143 static const struct got_error *
3144 list_refs(struct got_repository *repo)
3146 static const struct got_error *err = NULL;
3147 struct got_reflist_head refs;
3148 struct got_reflist_entry *re;
3150 SIMPLEQ_INIT(&refs);
3151 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3155 SIMPLEQ_FOREACH(re, &refs, entry) {
3157 refstr = got_ref_to_str(re->ref);
3159 return got_error_from_errno("got_ref_to_str");
3160 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3164 got_ref_list_free(&refs);
3168 static const struct got_error *
3169 delete_ref(struct got_repository *repo, const char *refname)
3171 const struct got_error *err = NULL;
3172 struct got_reference *ref;
3174 err = got_ref_open(&ref, repo, refname, 0);
3178 err = got_ref_delete(ref, repo);
3183 static const struct got_error *
3184 add_ref(struct got_repository *repo, const char *refname, const char *target)
3186 const struct got_error *err = NULL;
3187 struct got_object_id *id;
3188 struct got_reference *ref = NULL;
3191 * Don't let the user create a reference name with a leading '-'.
3192 * While technically a valid reference name, this case is usually
3193 * an unintended typo.
3195 if (refname[0] == '-')
3196 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3198 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3201 struct got_reference *target_ref;
3203 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3205 err = got_ref_open(&target_ref, repo, target, 0);
3208 err = got_ref_resolve(&id, repo, target_ref);
3209 got_ref_close(target_ref);
3214 err = got_ref_alloc(&ref, refname, id);
3218 err = got_ref_write(ref, repo);
3226 static const struct got_error *
3227 add_symref(struct got_repository *repo, const char *refname, const char *target)
3229 const struct got_error *err = NULL;
3230 struct got_reference *ref = NULL;
3231 struct got_reference *target_ref = NULL;
3234 * Don't let the user create a reference name with a leading '-'.
3235 * While technically a valid reference name, this case is usually
3236 * an unintended typo.
3238 if (refname[0] == '-')
3239 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3241 err = got_ref_open(&target_ref, repo, target, 0);
3245 err = got_ref_alloc_symref(&ref, refname, target_ref);
3249 err = got_ref_write(ref, repo);
3252 got_ref_close(target_ref);
3258 static const struct got_error *
3259 cmd_ref(int argc, char *argv[])
3261 const struct got_error *error = NULL;
3262 struct got_repository *repo = NULL;
3263 struct got_worktree *worktree = NULL;
3264 char *cwd = NULL, *repo_path = NULL;
3265 int ch, do_list = 0, create_symref = 0;
3266 const char *delref = NULL;
3268 /* TODO: Add -s option for adding symbolic references. */
3269 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3275 repo_path = realpath(optarg, NULL);
3276 if (repo_path == NULL)
3277 return got_error_from_errno2("realpath",
3279 got_path_strip_trailing_slashes(repo_path);
3293 if (do_list && delref)
3294 errx(1, "-l and -d options are mutually exclusive\n");
3299 if (do_list || delref) {
3301 errx(1, "-s option cannot be used together with the "
3302 "-l or -d options");
3305 } else if (argc != 2)
3310 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3314 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3315 "sendfd unveil", NULL) == -1)
3319 cwd = getcwd(NULL, 0);
3321 error = got_error_from_errno("getcwd");
3325 if (repo_path == NULL) {
3326 error = got_worktree_open(&worktree, cwd);
3327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3333 strdup(got_worktree_get_repo_path(worktree));
3334 if (repo_path == NULL)
3335 error = got_error_from_errno("strdup");
3339 repo_path = strdup(cwd);
3340 if (repo_path == NULL) {
3341 error = got_error_from_errno("strdup");
3347 error = got_repo_open(&repo, repo_path, NULL);
3351 error = apply_unveil(got_repo_get_path(repo), do_list,
3352 worktree ? got_worktree_get_root_path(worktree) : NULL);
3357 error = list_refs(repo);
3359 error = delete_ref(repo, delref);
3360 else if (create_symref)
3361 error = add_symref(repo, argv[0], argv[1]);
3363 error = add_ref(repo, argv[0], argv[1]);
3366 got_repo_close(repo);
3368 got_worktree_close(worktree);
3378 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3379 "[name]\n", getprogname());
3383 static const struct got_error *
3384 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3385 struct got_reference *ref)
3387 const struct got_error *err = NULL;
3388 const char *refname, *marker = " ";
3391 refname = got_ref_get_name(ref);
3392 if (worktree && strcmp(refname,
3393 got_worktree_get_head_ref_name(worktree)) == 0) {
3394 struct got_object_id *id = NULL;
3396 err = got_ref_resolve(&id, repo, ref);
3399 if (got_object_id_cmp(id,
3400 got_worktree_get_base_commit_id(worktree)) == 0)
3407 if (strncmp(refname, "refs/heads/", 11) == 0)
3409 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3412 refstr = got_ref_to_str(ref);
3414 return got_error_from_errno("got_ref_to_str");
3416 printf("%s%s: %s\n", marker, refname, refstr);
3421 static const struct got_error *
3422 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3424 const char *refname;
3426 if (worktree == NULL)
3427 return got_error(GOT_ERR_NOT_WORKTREE);
3429 refname = got_worktree_get_head_ref_name(worktree);
3431 if (strncmp(refname, "refs/heads/", 11) == 0)
3433 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3436 printf("%s\n", refname);
3441 static const struct got_error *
3442 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3444 static const struct got_error *err = NULL;
3445 struct got_reflist_head refs;
3446 struct got_reflist_entry *re;
3447 struct got_reference *temp_ref = NULL;
3448 int rebase_in_progress, histedit_in_progress;
3450 SIMPLEQ_INIT(&refs);
3453 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3458 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3463 if (rebase_in_progress || histedit_in_progress) {
3464 err = got_ref_open(&temp_ref, repo,
3465 got_worktree_get_head_ref_name(worktree), 0);
3468 list_branch(repo, worktree, temp_ref);
3469 got_ref_close(temp_ref);
3473 err = got_ref_list(&refs, repo, "refs/heads",
3474 got_ref_cmp_by_name, NULL);
3478 SIMPLEQ_FOREACH(re, &refs, entry)
3479 list_branch(repo, worktree, re->ref);
3481 got_ref_list_free(&refs);
3485 static const struct got_error *
3486 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3487 const char *branch_name)
3489 const struct got_error *err = NULL;
3490 struct got_reference *ref = NULL;
3493 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3494 return got_error_from_errno("asprintf");
3496 err = got_ref_open(&ref, repo, refname, 0);
3501 strcmp(got_worktree_get_head_ref_name(worktree),
3502 got_ref_get_name(ref)) == 0) {
3503 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3504 "will not delete this work tree's current branch");
3508 err = got_ref_delete(ref, repo);
3516 static const struct got_error *
3517 add_branch(struct got_repository *repo, const char *branch_name,
3518 struct got_object_id *base_commit_id)
3520 const struct got_error *err = NULL;
3521 struct got_reference *ref = NULL;
3522 char *base_refname = NULL, *refname = NULL;
3525 * Don't let the user create a branch name with a leading '-'.
3526 * While technically a valid reference name, this case is usually
3527 * an unintended typo.
3529 if (branch_name[0] == '-')
3530 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3532 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3533 err = got_error_from_errno("asprintf");
3537 err = got_ref_open(&ref, repo, refname, 0);
3539 err = got_error(GOT_ERR_BRANCH_EXISTS);
3541 } else if (err->code != GOT_ERR_NOT_REF)
3544 err = got_ref_alloc(&ref, refname, base_commit_id);
3548 err = got_ref_write(ref, repo);
3557 static const struct got_error *
3558 cmd_branch(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct got_repository *repo = NULL;
3562 struct got_worktree *worktree = NULL;
3563 char *cwd = NULL, *repo_path = NULL;
3564 int ch, do_list = 0, do_show = 0;
3565 const char *delref = NULL, *commit_id_arg = NULL;
3567 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3570 commit_id_arg = optarg;
3576 repo_path = realpath(optarg, NULL);
3577 if (repo_path == NULL)
3578 return got_error_from_errno2("realpath",
3580 got_path_strip_trailing_slashes(repo_path);
3591 if (do_list && delref)
3592 errx(1, "-l and -d options are mutually exclusive\n");
3597 if (!do_list && !delref && argc == 0)
3600 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3601 errx(1, "-c option can only be used when creating a branch");
3603 if (do_list || delref) {
3606 } else if (!do_show && argc != 1)
3610 if (do_list || do_show) {
3611 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3615 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3616 "sendfd unveil", NULL) == -1)
3620 cwd = getcwd(NULL, 0);
3622 error = got_error_from_errno("getcwd");
3626 if (repo_path == NULL) {
3627 error = got_worktree_open(&worktree, cwd);
3628 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3634 strdup(got_worktree_get_repo_path(worktree));
3635 if (repo_path == NULL)
3636 error = got_error_from_errno("strdup");
3640 repo_path = strdup(cwd);
3641 if (repo_path == NULL) {
3642 error = got_error_from_errno("strdup");
3648 error = got_repo_open(&repo, repo_path, NULL);
3652 error = apply_unveil(got_repo_get_path(repo), do_list,
3653 worktree ? got_worktree_get_root_path(worktree) : NULL);
3658 error = show_current_branch(repo, worktree);
3660 error = list_branches(repo, worktree);
3662 error = delete_branch(repo, worktree, delref);
3664 struct got_object_id *commit_id;
3665 if (commit_id_arg == NULL)
3666 commit_id_arg = worktree ?
3667 got_worktree_get_head_ref_name(worktree) :
3669 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3672 error = add_branch(repo, argv[0], commit_id);
3677 got_repo_close(repo);
3679 got_worktree_close(worktree);
3690 "usage: %s tag [-r repository] | -l | "
3691 "[-m message] name [commit]\n", getprogname());
3696 static const struct got_error *
3697 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3699 const struct got_error *err = NULL;
3700 struct got_reflist_entry *re, *se, *new;
3701 struct got_object_id *re_id, *se_id;
3702 struct got_tag_object *re_tag, *se_tag;
3703 time_t re_time, se_time;
3705 SIMPLEQ_FOREACH(re, tags, entry) {
3706 se = SIMPLEQ_FIRST(sorted);
3708 err = got_reflist_entry_dup(&new, re);
3711 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3714 err = got_ref_resolve(&re_id, repo, re->ref);
3717 err = got_object_open_as_tag(&re_tag, repo, re_id);
3721 re_time = got_object_tag_get_tagger_time(re_tag);
3722 got_object_tag_close(re_tag);
3726 err = got_ref_resolve(&se_id, repo, re->ref);
3729 err = got_object_open_as_tag(&se_tag, repo, se_id);
3733 se_time = got_object_tag_get_tagger_time(se_tag);
3734 got_object_tag_close(se_tag);
3736 if (se_time > re_time) {
3737 err = got_reflist_entry_dup(&new, re);
3740 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3743 se = SIMPLEQ_NEXT(se, entry);
3752 static const struct got_error *
3753 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3754 struct got_reference *ref2)
3756 const struct got_error *err = NULL;
3757 struct got_repository *repo = arg;
3758 struct got_object_id *id1, *id2 = NULL;
3759 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3760 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3761 time_t time1, time2;
3765 err = got_ref_resolve(&id1, repo, ref1);
3768 err = got_object_open_as_tag(&tag1, repo, id1);
3770 if (err->code != GOT_ERR_OBJ_TYPE)
3772 /* "lightweight" tag */
3773 err = got_object_open_as_commit(&commit1, repo, id1);
3776 time1 = got_object_commit_get_committer_time(commit1);
3778 time1 = got_object_tag_get_tagger_time(tag1);
3780 err = got_ref_resolve(&id2, repo, ref2);
3783 err = got_object_open_as_tag(&tag2, repo, id2);
3785 if (err->code != GOT_ERR_OBJ_TYPE)
3787 /* "lightweight" tag */
3788 err = got_object_open_as_commit(&commit2, repo, id2);
3791 time2 = got_object_commit_get_committer_time(commit2);
3793 time2 = got_object_tag_get_tagger_time(tag2);
3795 /* Put latest tags first. */
3798 else if (time1 > time2)
3801 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3806 got_object_tag_close(tag1);
3808 got_object_tag_close(tag2);
3810 got_object_commit_close(commit1);
3812 got_object_commit_close(commit2);
3816 static const struct got_error *
3817 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3819 static const struct got_error *err = NULL;
3820 struct got_reflist_head refs;
3821 struct got_reflist_entry *re;
3823 SIMPLEQ_INIT(&refs);
3825 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3829 SIMPLEQ_FOREACH(re, &refs, entry) {
3830 const char *refname;
3831 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3835 struct got_object_id *id;
3836 struct got_tag_object *tag;
3837 struct got_commit_object *commit = NULL;
3839 refname = got_ref_get_name(re->ref);
3840 if (strncmp(refname, "refs/tags/", 10) != 0)
3843 refstr = got_ref_to_str(re->ref);
3844 if (refstr == NULL) {
3845 err = got_error_from_errno("got_ref_to_str");
3848 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3851 err = got_ref_resolve(&id, repo, re->ref);
3854 err = got_object_open_as_tag(&tag, repo, id);
3856 if (err->code != GOT_ERR_OBJ_TYPE) {
3860 /* "lightweight" tag */
3861 err = got_object_open_as_commit(&commit, repo, id);
3866 tagger = got_object_commit_get_committer(commit);
3868 got_object_commit_get_committer_time(commit);
3869 err = got_object_id_str(&id_str, id);
3875 tagger = got_object_tag_get_tagger(tag);
3876 tagger_time = got_object_tag_get_tagger_time(tag);
3877 err = got_object_id_str(&id_str,
3878 got_object_tag_get_object_id(tag));
3882 printf("from: %s\n", tagger);
3883 datestr = get_datestr(&tagger_time, datebuf);
3885 printf("date: %s UTC\n", datestr);
3887 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3889 switch (got_object_tag_get_object_type(tag)) {
3890 case GOT_OBJ_TYPE_BLOB:
3891 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3894 case GOT_OBJ_TYPE_TREE:
3895 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3898 case GOT_OBJ_TYPE_COMMIT:
3899 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3902 case GOT_OBJ_TYPE_TAG:
3903 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3912 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3915 got_object_commit_close(commit);
3917 tagmsg0 = strdup(got_object_tag_get_message(tag));
3918 got_object_tag_close(tag);
3919 if (tagmsg0 == NULL) {
3920 err = got_error_from_errno("strdup");
3927 line = strsep(&tagmsg, "\n");
3929 printf(" %s\n", line);
3934 got_ref_list_free(&refs);
3938 static const struct got_error *
3939 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3940 const char *tag_name, const char *repo_path)
3942 const struct got_error *err = NULL;
3943 char *template = NULL, *initial_content = NULL;
3944 char *editor = NULL;
3947 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3948 err = got_error_from_errno("asprintf");
3952 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3953 commit_id_str, tag_name) == -1) {
3954 err = got_error_from_errno("asprintf");
3958 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3962 dprintf(fd, initial_content);
3965 err = get_editor(&editor);
3968 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3970 free(initial_content);
3974 /* Editor is done; we can now apply unveil(2) */
3976 err = apply_unveil(repo_path, 0, NULL);
3985 static const struct got_error *
3986 add_tag(struct got_repository *repo, const char *tag_name,
3987 const char *commit_arg, const char *tagmsg_arg)
3989 const struct got_error *err = NULL;
3990 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3991 char *label = NULL, *commit_id_str = NULL;
3992 struct got_reference *ref = NULL;
3993 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3994 char *tagmsg_path = NULL, *tag_id_str = NULL;
3995 int preserve_tagmsg = 0;
3998 * Don't let the user create a tag name with a leading '-'.
3999 * While technically a valid reference name, this case is usually
4000 * an unintended typo.
4002 if (tag_name[0] == '-')
4003 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4005 err = get_author(&tagger, repo);
4009 err = match_object_id(&commit_id, &label, commit_arg,
4010 GOT_OBJ_TYPE_COMMIT, 1, repo);
4014 err = got_object_id_str(&commit_id_str, commit_id);
4018 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4019 refname = strdup(tag_name);
4020 if (refname == NULL) {
4021 err = got_error_from_errno("strdup");
4025 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4026 err = got_error_from_errno("asprintf");
4030 err = got_ref_open(&ref, repo, refname, 0);
4032 err = got_error(GOT_ERR_TAG_EXISTS);
4034 } else if (err->code != GOT_ERR_NOT_REF)
4037 if (tagmsg_arg == NULL) {
4038 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4039 tag_name, got_repo_get_path(repo));
4041 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4042 tagmsg_path != NULL)
4043 preserve_tagmsg = 1;
4048 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4049 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4052 preserve_tagmsg = 1;
4056 err = got_ref_alloc(&ref, refname, tag_id);
4059 preserve_tagmsg = 1;
4063 err = got_ref_write(ref, repo);
4066 preserve_tagmsg = 1;
4070 err = got_object_id_str(&tag_id_str, tag_id);
4073 preserve_tagmsg = 1;
4076 printf("Created tag %s\n", tag_id_str);
4078 if (preserve_tagmsg) {
4079 fprintf(stderr, "%s: tag message preserved in %s\n",
4080 getprogname(), tagmsg_path);
4081 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4082 err = got_error_from_errno2("unlink", tagmsg_path);
4087 free(commit_id_str);
4095 static const struct got_error *
4096 cmd_tag(int argc, char *argv[])
4098 const struct got_error *error = NULL;
4099 struct got_repository *repo = NULL;
4100 struct got_worktree *worktree = NULL;
4101 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4102 char *gitconfig_path = NULL;
4103 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4104 int ch, do_list = 0;
4106 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4112 repo_path = realpath(optarg, NULL);
4113 if (repo_path == NULL)
4114 return got_error_from_errno2("realpath",
4116 got_path_strip_trailing_slashes(repo_path);
4132 errx(1, "-l and -m options are mutually exclusive\n");
4135 } else if (argc < 1 || argc > 2)
4138 commit_id_arg = argv[1];
4143 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4147 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4148 "sendfd unveil", NULL) == -1)
4152 cwd = getcwd(NULL, 0);
4154 error = got_error_from_errno("getcwd");
4158 if (repo_path == NULL) {
4159 error = got_worktree_open(&worktree, cwd);
4160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4166 strdup(got_worktree_get_repo_path(worktree));
4167 if (repo_path == NULL)
4168 error = got_error_from_errno("strdup");
4172 repo_path = strdup(cwd);
4173 if (repo_path == NULL) {
4174 error = got_error_from_errno("strdup");
4181 error = got_repo_open(&repo, repo_path, NULL);
4184 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4187 error = list_tags(repo, worktree);
4189 error = get_gitconfig_path(&gitconfig_path);
4192 error = got_repo_open(&repo, repo_path, gitconfig_path);
4197 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4202 if (commit_id_arg == NULL) {
4203 struct got_reference *head_ref;
4204 struct got_object_id *commit_id;
4205 error = got_ref_open(&head_ref, repo,
4206 worktree ? got_worktree_get_head_ref_name(worktree)
4210 error = got_ref_resolve(&commit_id, repo, head_ref);
4211 got_ref_close(head_ref);
4214 error = got_object_id_str(&commit_id_str, commit_id);
4220 error = add_tag(repo, tag_name,
4221 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4225 got_repo_close(repo);
4227 got_worktree_close(worktree);
4230 free(gitconfig_path);
4231 free(commit_id_str);
4238 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4243 static const struct got_error *
4244 add_progress(void *arg, unsigned char status, const char *path)
4246 while (path[0] == '/')
4248 printf("%c %s\n", status, path);
4252 static const struct got_error *
4253 cmd_add(int argc, char *argv[])
4255 const struct got_error *error = NULL;
4256 struct got_repository *repo = NULL;
4257 struct got_worktree *worktree = NULL;
4259 struct got_pathlist_head paths;
4260 struct got_pathlist_entry *pe;
4261 int ch, can_recurse = 0, no_ignores = 0;
4265 while ((ch = getopt(argc, argv, "IR")) != -1) {
4283 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4290 cwd = getcwd(NULL, 0);
4292 error = got_error_from_errno("getcwd");
4296 error = got_worktree_open(&worktree, cwd);
4300 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4305 error = apply_unveil(got_repo_get_path(repo), 1,
4306 got_worktree_get_root_path(worktree));
4310 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4314 if (!can_recurse && no_ignores) {
4315 error = got_error_msg(GOT_ERR_BAD_PATH,
4316 "disregarding ignores requires -R option");
4324 TAILQ_FOREACH(pe, &paths, entry) {
4325 if (asprintf(&ondisk_path, "%s/%s",
4326 got_worktree_get_root_path(worktree),
4328 error = got_error_from_errno("asprintf");
4331 if (lstat(ondisk_path, &sb) == -1) {
4332 if (errno == ENOENT) {
4336 error = got_error_from_errno2("lstat",
4342 if (S_ISDIR(sb.st_mode)) {
4343 error = got_error_msg(GOT_ERR_BAD_PATH,
4344 "adding directories requires -R option");
4350 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4351 NULL, repo, no_ignores);
4354 got_repo_close(repo);
4356 got_worktree_close(worktree);
4357 TAILQ_FOREACH(pe, &paths, entry)
4358 free((char *)pe->path);
4359 got_pathlist_free(&paths);
4367 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4372 static const struct got_error *
4373 print_remove_status(void *arg, unsigned char status,
4374 unsigned char staged_status, const char *path)
4376 while (path[0] == '/')
4378 if (status == GOT_STATUS_NONEXISTENT)
4380 if (status == staged_status && (status == GOT_STATUS_DELETE))
4381 status = GOT_STATUS_NO_CHANGE;
4382 printf("%c%c %s\n", status, staged_status, path);
4386 static const struct got_error *
4387 cmd_remove(int argc, char *argv[])
4389 const struct got_error *error = NULL;
4390 struct got_worktree *worktree = NULL;
4391 struct got_repository *repo = NULL;
4393 struct got_pathlist_head paths;
4394 struct got_pathlist_entry *pe;
4395 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4399 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4402 delete_local_mods = 1;
4420 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4427 cwd = getcwd(NULL, 0);
4429 error = got_error_from_errno("getcwd");
4432 error = got_worktree_open(&worktree, cwd);
4436 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4441 error = apply_unveil(got_repo_get_path(repo), 1,
4442 got_worktree_get_root_path(worktree));
4446 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4453 TAILQ_FOREACH(pe, &paths, entry) {
4454 if (asprintf(&ondisk_path, "%s/%s",
4455 got_worktree_get_root_path(worktree),
4457 error = got_error_from_errno("asprintf");
4460 if (lstat(ondisk_path, &sb) == -1) {
4461 if (errno == ENOENT) {
4465 error = got_error_from_errno2("lstat",
4471 if (S_ISDIR(sb.st_mode)) {
4472 error = got_error_msg(GOT_ERR_BAD_PATH,
4473 "removing directories requires -R option");
4479 error = got_worktree_schedule_delete(worktree, &paths,
4480 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4483 got_repo_close(repo);
4485 got_worktree_close(worktree);
4486 TAILQ_FOREACH(pe, &paths, entry)
4487 free((char *)pe->path);
4488 got_pathlist_free(&paths);
4496 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4497 "path ...\n", getprogname());
4501 static const struct got_error *
4502 revert_progress(void *arg, unsigned char status, const char *path)
4504 while (path[0] == '/')
4506 printf("%c %s\n", status, path);
4510 struct choose_patch_arg {
4511 FILE *patch_script_file;
4515 static const struct got_error *
4516 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4517 int nchanges, const char *action)
4520 size_t linesize = 0;
4524 case GOT_STATUS_ADD:
4525 printf("A %s\n%s this addition? [y/n] ", path, action);
4527 case GOT_STATUS_DELETE:
4528 printf("D %s\n%s this deletion? [y/n] ", path, action);
4530 case GOT_STATUS_MODIFY:
4531 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4532 return got_error_from_errno("fseek");
4533 printf(GOT_COMMIT_SEP_STR);
4534 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4536 if (ferror(patch_file))
4537 return got_error_from_errno("getline");
4538 printf(GOT_COMMIT_SEP_STR);
4539 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4540 path, n, nchanges, action);
4543 return got_error_path(path, GOT_ERR_FILE_STATUS);
4549 static const struct got_error *
4550 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4551 FILE *patch_file, int n, int nchanges)
4553 const struct got_error *err = NULL;
4555 size_t linesize = 0;
4558 struct choose_patch_arg *a = arg;
4560 *choice = GOT_PATCH_CHOICE_NONE;
4562 if (a->patch_script_file) {
4564 err = show_change(status, path, patch_file, n, nchanges,
4568 linelen = getline(&line, &linesize, a->patch_script_file);
4569 if (linelen == -1) {
4570 if (ferror(a->patch_script_file))
4571 return got_error_from_errno("getline");
4574 nl = strchr(line, '\n');
4577 if (strcmp(line, "y") == 0) {
4578 *choice = GOT_PATCH_CHOICE_YES;
4580 } else if (strcmp(line, "n") == 0) {
4581 *choice = GOT_PATCH_CHOICE_NO;
4583 } else if (strcmp(line, "q") == 0 &&
4584 status == GOT_STATUS_MODIFY) {
4585 *choice = GOT_PATCH_CHOICE_QUIT;
4588 printf("invalid response '%s'\n", line);
4593 while (resp != 'y' && resp != 'n' && resp != 'q') {
4594 err = show_change(status, path, patch_file, n, nchanges,
4601 if (status == GOT_STATUS_MODIFY) {
4602 if (resp != 'y' && resp != 'n' && resp != 'q') {
4603 printf("invalid response '%c'\n", resp);
4606 } else if (resp != 'y' && resp != 'n') {
4607 printf("invalid response '%c'\n", resp);
4613 *choice = GOT_PATCH_CHOICE_YES;
4614 else if (resp == 'n')
4615 *choice = GOT_PATCH_CHOICE_NO;
4616 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4617 *choice = GOT_PATCH_CHOICE_QUIT;
4623 static const struct got_error *
4624 cmd_revert(int argc, char *argv[])
4626 const struct got_error *error = NULL;
4627 struct got_worktree *worktree = NULL;
4628 struct got_repository *repo = NULL;
4629 char *cwd = NULL, *path = NULL;
4630 struct got_pathlist_head paths;
4631 struct got_pathlist_entry *pe;
4632 int ch, can_recurse = 0, pflag = 0;
4633 FILE *patch_script_file = NULL;
4634 const char *patch_script_path = NULL;
4635 struct choose_patch_arg cpa;
4639 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4645 patch_script_path = optarg;
4660 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4661 "unveil", NULL) == -1)
4666 if (patch_script_path && !pflag)
4667 errx(1, "-F option can only be used together with -p option");
4669 cwd = getcwd(NULL, 0);
4671 error = got_error_from_errno("getcwd");
4674 error = got_worktree_open(&worktree, cwd);
4678 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4683 if (patch_script_path) {
4684 patch_script_file = fopen(patch_script_path, "r");
4685 if (patch_script_file == NULL) {
4686 error = got_error_from_errno2("fopen",
4691 error = apply_unveil(got_repo_get_path(repo), 1,
4692 got_worktree_get_root_path(worktree));
4696 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4703 TAILQ_FOREACH(pe, &paths, entry) {
4704 if (asprintf(&ondisk_path, "%s/%s",
4705 got_worktree_get_root_path(worktree),
4707 error = got_error_from_errno("asprintf");
4710 if (lstat(ondisk_path, &sb) == -1) {
4711 if (errno == ENOENT) {
4715 error = got_error_from_errno2("lstat",
4721 if (S_ISDIR(sb.st_mode)) {
4722 error = got_error_msg(GOT_ERR_BAD_PATH,
4723 "reverting directories requires -R option");
4729 cpa.patch_script_file = patch_script_file;
4730 cpa.action = "revert";
4731 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4732 pflag ? choose_patch : NULL, &cpa, repo);
4734 if (patch_script_file && fclose(patch_script_file) == EOF &&
4736 error = got_error_from_errno2("fclose", patch_script_path);
4738 got_repo_close(repo);
4740 got_worktree_close(worktree);
4749 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4754 struct collect_commit_logmsg_arg {
4755 const char *cmdline_log;
4757 const char *worktree_path;
4758 const char *branch_name;
4759 const char *repo_path;
4764 static const struct got_error *
4765 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4768 char *initial_content = NULL;
4769 struct got_pathlist_entry *pe;
4770 const struct got_error *err = NULL;
4771 char *template = NULL;
4772 struct collect_commit_logmsg_arg *a = arg;
4776 /* if a message was specified on the command line, just use it */
4777 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4778 len = strlen(a->cmdline_log) + 1;
4779 *logmsg = malloc(len + 1);
4780 if (*logmsg == NULL)
4781 return got_error_from_errno("malloc");
4782 strlcpy(*logmsg, a->cmdline_log, len);
4786 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4787 return got_error_from_errno("asprintf");
4789 if (asprintf(&initial_content,
4790 "\n# changes to be committed on branch %s:\n",
4791 a->branch_name) == -1)
4792 return got_error_from_errno("asprintf");
4794 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4798 dprintf(fd, initial_content);
4800 TAILQ_FOREACH(pe, commitable_paths, entry) {
4801 struct got_commitable *ct = pe->data;
4802 dprintf(fd, "# %c %s\n",
4803 got_commitable_get_status(ct),
4804 got_commitable_get_path(ct));
4808 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4810 free(initial_content);
4813 /* Editor is done; we can now apply unveil(2) */
4815 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4824 static const struct got_error *
4825 cmd_commit(int argc, char *argv[])
4827 const struct got_error *error = NULL;
4828 struct got_worktree *worktree = NULL;
4829 struct got_repository *repo = NULL;
4830 char *cwd = NULL, *id_str = NULL;
4831 struct got_object_id *id = NULL;
4832 const char *logmsg = NULL;
4833 struct collect_commit_logmsg_arg cl_arg;
4834 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4835 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4836 struct got_pathlist_head paths;
4839 cl_arg.logmsg_path = NULL;
4841 while ((ch = getopt(argc, argv, "m:")) != -1) {
4856 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4857 "unveil", NULL) == -1)
4860 cwd = getcwd(NULL, 0);
4862 error = got_error_from_errno("getcwd");
4865 error = got_worktree_open(&worktree, cwd);
4869 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4872 if (rebase_in_progress) {
4873 error = got_error(GOT_ERR_REBASING);
4877 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4882 error = get_gitconfig_path(&gitconfig_path);
4885 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4890 error = get_author(&author, repo);
4895 * unveil(2) traverses exec(2); if an editor is used we have
4896 * to apply unveil after the log message has been written.
4898 if (logmsg == NULL || strlen(logmsg) == 0)
4899 error = get_editor(&editor);
4901 error = apply_unveil(got_repo_get_path(repo), 0,
4902 got_worktree_get_root_path(worktree));
4906 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4910 cl_arg.editor = editor;
4911 cl_arg.cmdline_log = logmsg;
4912 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4913 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4914 if (!histedit_in_progress) {
4915 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4916 error = got_error(GOT_ERR_COMMIT_BRANCH);
4919 cl_arg.branch_name += 11;
4921 cl_arg.repo_path = got_repo_get_path(repo);
4922 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4923 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4925 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4926 cl_arg.logmsg_path != NULL)
4927 preserve_logmsg = 1;
4931 error = got_object_id_str(&id_str, id);
4934 printf("Created commit %s\n", id_str);
4936 if (preserve_logmsg) {
4937 fprintf(stderr, "%s: log message preserved in %s\n",
4938 getprogname(), cl_arg.logmsg_path);
4939 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4941 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4942 free(cl_arg.logmsg_path);
4944 got_repo_close(repo);
4946 got_worktree_close(worktree);
4949 free(gitconfig_path);
4956 usage_cherrypick(void)
4958 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4962 static const struct got_error *
4963 cmd_cherrypick(int argc, char *argv[])
4965 const struct got_error *error = NULL;
4966 struct got_worktree *worktree = NULL;
4967 struct got_repository *repo = NULL;
4968 char *cwd = NULL, *commit_id_str = NULL;
4969 struct got_object_id *commit_id = NULL;
4970 struct got_commit_object *commit = NULL;
4971 struct got_object_qid *pid;
4972 struct got_reference *head_ref = NULL;
4973 int ch, did_something = 0;
4975 while ((ch = getopt(argc, argv, "")) != -1) {
4987 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4988 "unveil", NULL) == -1)
4994 cwd = getcwd(NULL, 0);
4996 error = got_error_from_errno("getcwd");
4999 error = got_worktree_open(&worktree, cwd);
5003 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5008 error = apply_unveil(got_repo_get_path(repo), 0,
5009 got_worktree_get_root_path(worktree));
5013 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5014 GOT_OBJ_TYPE_COMMIT, repo);
5015 if (error != NULL) {
5016 struct got_reference *ref;
5017 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5019 error = got_ref_open(&ref, repo, argv[0], 0);
5022 error = got_ref_resolve(&commit_id, repo, ref);
5027 error = got_object_id_str(&commit_id_str, commit_id);
5031 error = got_ref_open(&head_ref, repo,
5032 got_worktree_get_head_ref_name(worktree), 0);
5036 error = check_same_branch(commit_id, head_ref, NULL, repo);
5038 if (error->code != GOT_ERR_ANCESTRY)
5042 error = got_error(GOT_ERR_SAME_BRANCH);
5046 error = got_object_open_as_commit(&commit, repo, commit_id);
5049 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5050 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5051 commit_id, repo, update_progress, &did_something, check_cancelled,
5057 printf("Merged commit %s\n", commit_id_str);
5060 got_object_commit_close(commit);
5061 free(commit_id_str);
5063 got_ref_close(head_ref);
5065 got_worktree_close(worktree);
5067 got_repo_close(repo);
5074 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5078 static const struct got_error *
5079 cmd_backout(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_repository *repo = NULL;
5084 char *cwd = NULL, *commit_id_str = NULL;
5085 struct got_object_id *commit_id = NULL;
5086 struct got_commit_object *commit = NULL;
5087 struct got_object_qid *pid;
5088 struct got_reference *head_ref = NULL;
5089 int ch, did_something = 0;
5091 while ((ch = getopt(argc, argv, "")) != -1) {
5103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 "unveil", NULL) == -1)
5110 cwd = getcwd(NULL, 0);
5112 error = got_error_from_errno("getcwd");
5115 error = got_worktree_open(&worktree, cwd);
5119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5124 error = apply_unveil(got_repo_get_path(repo), 0,
5125 got_worktree_get_root_path(worktree));
5129 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 GOT_OBJ_TYPE_COMMIT, repo);
5131 if (error != NULL) {
5132 struct got_reference *ref;
5133 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5135 error = got_ref_open(&ref, repo, argv[0], 0);
5138 error = got_ref_resolve(&commit_id, repo, ref);
5143 error = got_object_id_str(&commit_id_str, commit_id);
5147 error = got_ref_open(&head_ref, repo,
5148 got_worktree_get_head_ref_name(worktree), 0);
5152 error = check_same_branch(commit_id, head_ref, NULL, repo);
5156 error = got_object_open_as_commit(&commit, repo, commit_id);
5159 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5161 error = got_error(GOT_ERR_ROOT_COMMIT);
5165 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5166 update_progress, &did_something, check_cancelled, NULL);
5171 printf("Backed out commit %s\n", commit_id_str);
5174 got_object_commit_close(commit);
5175 free(commit_id_str);
5177 got_ref_close(head_ref);
5179 got_worktree_close(worktree);
5181 got_repo_close(repo);
5188 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5194 trim_logmsg(char *logmsg, int limit)
5199 len = strlen(logmsg);
5203 nl = strchr(logmsg, '\n');
5208 static const struct got_error *
5209 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5211 const struct got_error *err;
5212 char *logmsg0 = NULL;
5215 err = got_object_commit_get_logmsg(&logmsg0, commit);
5220 while (isspace((unsigned char)s[0]))
5223 *logmsg = strdup(s);
5224 if (*logmsg == NULL) {
5225 err = got_error_from_errno("strdup");
5229 trim_logmsg(*logmsg, limit);
5235 static const struct got_error *
5236 show_rebase_progress(struct got_commit_object *commit,
5237 struct got_object_id *old_id, struct got_object_id *new_id)
5239 const struct got_error *err;
5240 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5242 err = got_object_id_str(&old_id_str, old_id);
5247 err = got_object_id_str(&new_id_str, new_id);
5252 old_id_str[12] = '\0';
5254 new_id_str[12] = '\0';
5256 err = get_short_logmsg(&logmsg, 42, commit);
5260 printf("%s -> %s: %s\n", old_id_str,
5261 new_id_str ? new_id_str : "no-op change", logmsg);
5268 static const struct got_error *
5269 rebase_progress(void *arg, unsigned char status, const char *path)
5271 unsigned char *rebase_status = arg;
5273 while (path[0] == '/')
5275 printf("%c %s\n", status, path);
5277 if (*rebase_status == GOT_STATUS_CONFLICT)
5279 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5280 *rebase_status = status;
5284 static const struct got_error *
5285 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5286 struct got_reference *branch, struct got_reference *new_base_branch,
5287 struct got_reference *tmp_branch, struct got_repository *repo)
5289 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5290 return got_worktree_rebase_complete(worktree, fileindex,
5291 new_base_branch, tmp_branch, branch, repo);
5294 static const struct got_error *
5295 rebase_commit(struct got_pathlist_head *merged_paths,
5296 struct got_worktree *worktree, struct got_fileindex *fileindex,
5297 struct got_reference *tmp_branch,
5298 struct got_object_id *commit_id, struct got_repository *repo)
5300 const struct got_error *error;
5301 struct got_commit_object *commit;
5302 struct got_object_id *new_commit_id;
5304 error = got_object_open_as_commit(&commit, repo, commit_id);
5308 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5309 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5311 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5313 error = show_rebase_progress(commit, commit_id, NULL);
5315 error = show_rebase_progress(commit, commit_id, new_commit_id);
5316 free(new_commit_id);
5319 got_object_commit_close(commit);
5323 struct check_path_prefix_arg {
5324 const char *path_prefix;
5329 static const struct got_error *
5330 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5331 struct got_blob_object *blob2, struct got_object_id *id1,
5332 struct got_object_id *id2, const char *path1, const char *path2,
5333 mode_t mode1, mode_t mode2, struct got_repository *repo)
5335 struct check_path_prefix_arg *a = arg;
5337 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5338 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5339 return got_error(a->errcode);
5344 static const struct got_error *
5345 check_path_prefix(struct got_object_id *parent_id,
5346 struct got_object_id *commit_id, const char *path_prefix,
5347 int errcode, struct got_repository *repo)
5349 const struct got_error *err;
5350 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5351 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5352 struct check_path_prefix_arg cpp_arg;
5354 if (got_path_is_root_dir(path_prefix))
5357 err = got_object_open_as_commit(&commit, repo, commit_id);
5361 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5365 err = got_object_open_as_tree(&tree1, repo,
5366 got_object_commit_get_tree_id(parent_commit));
5370 err = got_object_open_as_tree(&tree2, repo,
5371 got_object_commit_get_tree_id(commit));
5375 cpp_arg.path_prefix = path_prefix;
5376 while (cpp_arg.path_prefix[0] == '/')
5377 cpp_arg.path_prefix++;
5378 cpp_arg.len = strlen(cpp_arg.path_prefix);
5379 cpp_arg.errcode = errcode;
5380 err = got_diff_tree(tree1, tree2, "", "", repo,
5381 check_path_prefix_in_diff, &cpp_arg, 0);
5384 got_object_tree_close(tree1);
5386 got_object_tree_close(tree2);
5388 got_object_commit_close(commit);
5390 got_object_commit_close(parent_commit);
5394 static const struct got_error *
5395 collect_commits(struct got_object_id_queue *commits,
5396 struct got_object_id *initial_commit_id,
5397 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5398 const char *path_prefix, int path_prefix_errcode,
5399 struct got_repository *repo)
5401 const struct got_error *err = NULL;
5402 struct got_commit_graph *graph = NULL;
5403 struct got_object_id *parent_id = NULL;
5404 struct got_object_qid *qid;
5405 struct got_object_id *commit_id = initial_commit_id;
5407 err = got_commit_graph_open(&graph, "/", 1);
5411 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5412 check_cancelled, NULL);
5415 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5416 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5417 check_cancelled, NULL);
5419 if (err->code == GOT_ERR_ITER_COMPLETED) {
5420 err = got_error_msg(GOT_ERR_ANCESTRY,
5421 "ran out of commits to rebase before "
5422 "youngest common ancestor commit has "
5427 err = check_path_prefix(parent_id, commit_id,
5428 path_prefix, path_prefix_errcode, repo);
5432 err = got_object_qid_alloc(&qid, commit_id);
5435 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5436 commit_id = parent_id;
5440 got_commit_graph_close(graph);
5444 static const struct got_error *
5445 cmd_rebase(int argc, char *argv[])
5447 const struct got_error *error = NULL;
5448 struct got_worktree *worktree = NULL;
5449 struct got_repository *repo = NULL;
5450 struct got_fileindex *fileindex = NULL;
5452 struct got_reference *branch = NULL;
5453 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5454 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5455 struct got_object_id *resume_commit_id = NULL;
5456 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5457 struct got_commit_object *commit = NULL;
5458 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5459 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5460 struct got_object_id_queue commits;
5461 struct got_pathlist_head merged_paths;
5462 const struct got_object_id_queue *parent_ids;
5463 struct got_object_qid *qid, *pid;
5465 SIMPLEQ_INIT(&commits);
5466 TAILQ_INIT(&merged_paths);
5468 while ((ch = getopt(argc, argv, "ac")) != -1) {
5474 continue_rebase = 1;
5486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5487 "unveil", NULL) == -1)
5490 if (abort_rebase && continue_rebase)
5492 else if (abort_rebase || continue_rebase) {
5495 } else if (argc != 1)
5498 cwd = getcwd(NULL, 0);
5500 error = got_error_from_errno("getcwd");
5503 error = got_worktree_open(&worktree, cwd);
5507 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5512 error = apply_unveil(got_repo_get_path(repo), 0,
5513 got_worktree_get_root_path(worktree));
5517 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5523 if (!rebase_in_progress) {
5524 error = got_error(GOT_ERR_NOT_REBASING);
5527 error = got_worktree_rebase_continue(&resume_commit_id,
5528 &new_base_branch, &tmp_branch, &branch, &fileindex,
5532 printf("Switching work tree to %s\n",
5533 got_ref_get_symref_target(new_base_branch));
5534 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5535 new_base_branch, update_progress, &did_something);
5538 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5539 goto done; /* nothing else to do */
5542 if (continue_rebase) {
5543 if (!rebase_in_progress) {
5544 error = got_error(GOT_ERR_NOT_REBASING);
5547 error = got_worktree_rebase_continue(&resume_commit_id,
5548 &new_base_branch, &tmp_branch, &branch, &fileindex,
5553 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5554 resume_commit_id, repo);
5558 yca_id = got_object_id_dup(resume_commit_id);
5559 if (yca_id == NULL) {
5560 error = got_error_from_errno("got_object_id_dup");
5564 error = got_ref_open(&branch, repo, argv[0], 0);
5569 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5573 if (!continue_rebase) {
5574 struct got_object_id *base_commit_id;
5576 base_commit_id = got_worktree_get_base_commit_id(worktree);
5577 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5578 base_commit_id, branch_head_commit_id, repo,
5579 check_cancelled, NULL);
5582 if (yca_id == NULL) {
5583 error = got_error_msg(GOT_ERR_ANCESTRY,
5584 "specified branch shares no common ancestry "
5585 "with work tree's branch");
5589 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5591 if (error->code != GOT_ERR_ANCESTRY)
5595 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5596 "specified branch resolves to a commit which "
5597 "is already contained in work tree's branch");
5600 error = got_worktree_rebase_prepare(&new_base_branch,
5601 &tmp_branch, &fileindex, worktree, branch, repo);
5606 commit_id = branch_head_commit_id;
5607 error = got_object_open_as_commit(&commit, repo, commit_id);
5611 parent_ids = got_object_commit_get_parent_ids(commit);
5612 pid = SIMPLEQ_FIRST(parent_ids);
5614 if (!continue_rebase) {
5616 error = got_worktree_rebase_abort(worktree, fileindex,
5617 repo, new_base_branch, update_progress,
5621 printf("Rebase of %s aborted\n",
5622 got_ref_get_name(branch));
5624 error = got_error(GOT_ERR_EMPTY_REBASE);
5627 error = collect_commits(&commits, commit_id, pid->id,
5628 yca_id, got_worktree_get_path_prefix(worktree),
5629 GOT_ERR_REBASE_PATH, repo);
5630 got_object_commit_close(commit);
5635 if (SIMPLEQ_EMPTY(&commits)) {
5636 if (continue_rebase) {
5637 error = rebase_complete(worktree, fileindex,
5638 branch, new_base_branch, tmp_branch, repo);
5641 /* Fast-forward the reference of the branch. */
5642 struct got_object_id *new_head_commit_id;
5644 error = got_ref_resolve(&new_head_commit_id, repo,
5648 error = got_object_id_str(&id_str, new_head_commit_id);
5649 printf("Forwarding %s to commit %s\n",
5650 got_ref_get_name(branch), id_str);
5652 error = got_ref_change_ref(branch,
5653 new_head_commit_id);
5660 SIMPLEQ_FOREACH(qid, &commits, entry) {
5661 commit_id = qid->id;
5662 parent_id = pid ? pid->id : yca_id;
5665 error = got_worktree_rebase_merge_files(&merged_paths,
5666 worktree, fileindex, parent_id, commit_id, repo,
5667 rebase_progress, &rebase_status, check_cancelled, NULL);
5671 if (rebase_status == GOT_STATUS_CONFLICT) {
5672 got_worktree_rebase_pathlist_free(&merged_paths);
5676 error = rebase_commit(&merged_paths, worktree, fileindex,
5677 tmp_branch, commit_id, repo);
5678 got_worktree_rebase_pathlist_free(&merged_paths);
5683 if (rebase_status == GOT_STATUS_CONFLICT) {
5684 error = got_worktree_rebase_postpone(worktree, fileindex);
5687 error = got_error_msg(GOT_ERR_CONFLICTS,
5688 "conflicts must be resolved before rebasing can continue");
5690 error = rebase_complete(worktree, fileindex, branch,
5691 new_base_branch, tmp_branch, repo);
5693 got_object_id_queue_free(&commits);
5694 free(branch_head_commit_id);
5695 free(resume_commit_id);
5698 got_object_commit_close(commit);
5700 got_ref_close(branch);
5701 if (new_base_branch)
5702 got_ref_close(new_base_branch);
5704 got_ref_close(tmp_branch);
5706 got_worktree_close(worktree);
5708 got_repo_close(repo);
5713 usage_histedit(void)
5715 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5720 #define GOT_HISTEDIT_PICK 'p'
5721 #define GOT_HISTEDIT_EDIT 'e'
5722 #define GOT_HISTEDIT_FOLD 'f'
5723 #define GOT_HISTEDIT_DROP 'd'
5724 #define GOT_HISTEDIT_MESG 'm'
5726 static struct got_histedit_cmd {
5730 } got_histedit_cmds[] = {
5731 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5732 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5733 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5734 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5735 { GOT_HISTEDIT_MESG, "mesg",
5736 "single-line log message for commit above (open editor if empty)" },
5739 struct got_histedit_list_entry {
5740 TAILQ_ENTRY(got_histedit_list_entry) entry;
5741 struct got_object_id *commit_id;
5742 const struct got_histedit_cmd *cmd;
5745 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5747 static const struct got_error *
5748 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5749 FILE *f, struct got_repository *repo)
5751 const struct got_error *err = NULL;
5752 char *logmsg = NULL, *id_str = NULL;
5753 struct got_commit_object *commit = NULL;
5756 err = got_object_open_as_commit(&commit, repo, commit_id);
5760 err = get_short_logmsg(&logmsg, 34, commit);
5764 err = got_object_id_str(&id_str, commit_id);
5768 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5770 err = got_ferror(f, GOT_ERR_IO);
5773 got_object_commit_close(commit);
5779 static const struct got_error *
5780 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5781 struct got_repository *repo)
5783 const struct got_error *err = NULL;
5784 struct got_object_qid *qid;
5786 if (SIMPLEQ_EMPTY(commits))
5787 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5789 SIMPLEQ_FOREACH(qid, commits, entry) {
5790 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5799 static const struct got_error *
5800 write_cmd_list(FILE *f)
5802 const struct got_error *err = NULL;
5805 n = fprintf(f, "# Available histedit commands:\n");
5807 return got_ferror(f, GOT_ERR_IO);
5809 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5810 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5811 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5814 err = got_ferror(f, GOT_ERR_IO);
5818 n = fprintf(f, "# Commits will be processed in order from top to "
5819 "bottom of this file.\n");
5821 return got_ferror(f, GOT_ERR_IO);
5825 static const struct got_error *
5826 histedit_syntax_error(int lineno)
5828 static char msg[42];
5831 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5833 if (ret == -1 || ret >= sizeof(msg))
5834 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5836 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5839 static const struct got_error *
5840 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5841 char *logmsg, struct got_repository *repo)
5843 const struct got_error *err;
5844 struct got_commit_object *folded_commit = NULL;
5845 char *id_str, *folded_logmsg = NULL;
5847 err = got_object_id_str(&id_str, hle->commit_id);
5851 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5855 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5858 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5859 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5860 folded_logmsg) == -1) {
5861 err = got_error_from_errno("asprintf");
5865 got_object_commit_close(folded_commit);
5867 free(folded_logmsg);
5871 static struct got_histedit_list_entry *
5872 get_folded_commits(struct got_histedit_list_entry *hle)
5874 struct got_histedit_list_entry *prev, *folded = NULL;
5876 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5877 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5878 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5879 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5881 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5887 static const struct got_error *
5888 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5889 struct got_repository *repo)
5891 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5892 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5893 const struct got_error *err = NULL;
5894 struct got_commit_object *commit = NULL;
5896 struct got_histedit_list_entry *folded = NULL;
5898 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5902 folded = get_folded_commits(hle);
5904 while (folded != hle) {
5905 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5906 folded = TAILQ_NEXT(folded, entry);
5909 err = append_folded_commit_msg(&new_msg, folded,
5915 folded = TAILQ_NEXT(folded, entry);
5919 err = got_object_id_str(&id_str, hle->commit_id);
5922 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5925 if (asprintf(&new_msg,
5926 "%s\n# original log message of commit %s: %s",
5927 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5928 err = got_error_from_errno("asprintf");
5934 err = got_object_id_str(&id_str, hle->commit_id);
5938 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5942 dprintf(fd, logmsg);
5945 err = get_editor(&editor);
5949 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5951 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5953 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5956 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5957 err = got_error_from_errno2("unlink", logmsg_path);
5963 got_object_commit_close(commit);
5967 static const struct got_error *
5968 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5969 FILE *f, struct got_repository *repo)
5971 const struct got_error *err = NULL;
5972 char *line = NULL, *p, *end;
5976 const struct got_histedit_cmd *cmd;
5977 struct got_object_id *commit_id = NULL;
5978 struct got_histedit_list_entry *hle = NULL;
5981 len = getline(&line, &size, f);
5983 const struct got_error *getline_err;
5986 getline_err = got_error_from_errno("getline");
5987 err = got_ferror(f, getline_err->code);
5992 while (isspace((unsigned char)p[0]))
5994 if (p[0] == '#' || p[0] == '\0') {
6000 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6001 cmd = &got_histedit_cmds[i];
6002 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6003 isspace((unsigned char)p[strlen(cmd->name)])) {
6004 p += strlen(cmd->name);
6007 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6012 if (i == nitems(got_histedit_cmds)) {
6013 err = histedit_syntax_error(lineno);
6016 while (isspace((unsigned char)p[0]))
6018 if (cmd->code == GOT_HISTEDIT_MESG) {
6019 if (hle == NULL || hle->logmsg != NULL) {
6020 err = got_error(GOT_ERR_HISTEDIT_CMD);
6024 err = histedit_edit_logmsg(hle, repo);
6028 hle->logmsg = strdup(p);
6029 if (hle->logmsg == NULL) {
6030 err = got_error_from_errno("strdup");
6039 while (end[0] && !isspace((unsigned char)end[0]))
6043 err = got_object_resolve_id_str(&commit_id, repo, p);
6045 /* override error code */
6046 err = histedit_syntax_error(lineno);
6050 hle = malloc(sizeof(*hle));
6052 err = got_error_from_errno("malloc");
6056 hle->commit_id = commit_id;
6061 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6069 static const struct got_error *
6070 histedit_check_script(struct got_histedit_list *histedit_cmds,
6071 struct got_object_id_queue *commits, struct got_repository *repo)
6073 const struct got_error *err = NULL;
6074 struct got_object_qid *qid;
6075 struct got_histedit_list_entry *hle;
6076 static char msg[80];
6079 if (TAILQ_EMPTY(histedit_cmds))
6080 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6081 "histedit script contains no commands");
6082 if (SIMPLEQ_EMPTY(commits))
6083 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6085 SIMPLEQ_FOREACH(qid, commits, entry) {
6086 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6087 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6091 err = got_object_id_str(&id_str, qid->id);
6094 snprintf(msg, sizeof(msg),
6095 "commit %s missing from histedit script", id_str);
6097 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6101 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6102 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6103 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6104 "last commit in histedit script cannot be folded");
6109 static const struct got_error *
6110 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6111 const char *path, struct got_object_id_queue *commits,
6112 struct got_repository *repo)
6114 const struct got_error *err = NULL;
6118 err = get_editor(&editor);
6122 if (spawn_editor(editor, path) == -1) {
6123 err = got_error_from_errno("failed spawning editor");
6127 f = fopen(path, "r");
6129 err = got_error_from_errno("fopen");
6132 err = histedit_parse_list(histedit_cmds, f, repo);
6136 err = histedit_check_script(histedit_cmds, commits, repo);
6138 if (f && fclose(f) != 0 && err == NULL)
6139 err = got_error_from_errno("fclose");
6144 static const struct got_error *
6145 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6146 struct got_object_id_queue *, const char *, struct got_repository *);
6148 static const struct got_error *
6149 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6150 struct got_object_id_queue *commits, struct got_repository *repo)
6152 const struct got_error *err;
6156 err = got_opentemp_named(&path, &f, "got-histedit");
6160 err = write_cmd_list(f);
6164 err = histedit_write_commit_list(commits, f, repo);
6168 if (fclose(f) != 0) {
6169 err = got_error_from_errno("fclose");
6174 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6176 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6177 err->code != GOT_ERR_HISTEDIT_CMD)
6179 err = histedit_edit_list_retry(histedit_cmds, err,
6180 commits, path, repo);
6183 if (f && fclose(f) != 0 && err == NULL)
6184 err = got_error_from_errno("fclose");
6185 if (path && unlink(path) != 0 && err == NULL)
6186 err = got_error_from_errno2("unlink", path);
6191 static const struct got_error *
6192 histedit_save_list(struct got_histedit_list *histedit_cmds,
6193 struct got_worktree *worktree, struct got_repository *repo)
6195 const struct got_error *err = NULL;
6198 struct got_histedit_list_entry *hle;
6199 struct got_commit_object *commit = NULL;
6201 err = got_worktree_get_histedit_script_path(&path, worktree);
6205 f = fopen(path, "w");
6207 err = got_error_from_errno2("fopen", path);
6210 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6211 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6217 int n = fprintf(f, "%c %s\n",
6218 GOT_HISTEDIT_MESG, hle->logmsg);
6220 err = got_ferror(f, GOT_ERR_IO);
6226 if (f && fclose(f) != 0 && err == NULL)
6227 err = got_error_from_errno("fclose");
6230 got_object_commit_close(commit);
6235 histedit_free_list(struct got_histedit_list *histedit_cmds)
6237 struct got_histedit_list_entry *hle;
6239 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6240 TAILQ_REMOVE(histedit_cmds, hle, entry);
6245 static const struct got_error *
6246 histedit_load_list(struct got_histedit_list *histedit_cmds,
6247 const char *path, struct got_repository *repo)
6249 const struct got_error *err = NULL;
6252 f = fopen(path, "r");
6254 err = got_error_from_errno2("fopen", path);
6258 err = histedit_parse_list(histedit_cmds, f, repo);
6260 if (f && fclose(f) != 0 && err == NULL)
6261 err = got_error_from_errno("fclose");
6265 static const struct got_error *
6266 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6267 const struct got_error *edit_err, struct got_object_id_queue *commits,
6268 const char *path, struct got_repository *repo)
6270 const struct got_error *err = NULL, *prev_err = edit_err;
6273 while (resp != 'c' && resp != 'r' && resp != 'a') {
6274 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6275 "or (a)bort: ", getprogname(), prev_err->msg);
6280 histedit_free_list(histedit_cmds);
6281 err = histedit_run_editor(histedit_cmds, path, commits,
6284 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6285 err->code != GOT_ERR_HISTEDIT_CMD)
6292 } else if (resp == 'r') {
6293 histedit_free_list(histedit_cmds);
6294 err = histedit_edit_script(histedit_cmds,
6297 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6298 err->code != GOT_ERR_HISTEDIT_CMD)
6305 } else if (resp == 'a') {
6306 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6309 printf("invalid response '%c'\n", resp);
6315 static const struct got_error *
6316 histedit_complete(struct got_worktree *worktree,
6317 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6318 struct got_reference *branch, struct got_repository *repo)
6320 printf("Switching work tree to %s\n",
6321 got_ref_get_symref_target(branch));
6322 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6326 static const struct got_error *
6327 show_histedit_progress(struct got_commit_object *commit,
6328 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6330 const struct got_error *err;
6331 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6333 err = got_object_id_str(&old_id_str, hle->commit_id);
6338 err = got_object_id_str(&new_id_str, new_id);
6343 old_id_str[12] = '\0';
6345 new_id_str[12] = '\0';
6348 logmsg = strdup(hle->logmsg);
6349 if (logmsg == NULL) {
6350 err = got_error_from_errno("strdup");
6353 trim_logmsg(logmsg, 42);
6355 err = get_short_logmsg(&logmsg, 42, commit);
6360 switch (hle->cmd->code) {
6361 case GOT_HISTEDIT_PICK:
6362 case GOT_HISTEDIT_EDIT:
6363 printf("%s -> %s: %s\n", old_id_str,
6364 new_id_str ? new_id_str : "no-op change", logmsg);
6366 case GOT_HISTEDIT_DROP:
6367 case GOT_HISTEDIT_FOLD:
6368 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6380 static const struct got_error *
6381 histedit_commit(struct got_pathlist_head *merged_paths,
6382 struct got_worktree *worktree, struct got_fileindex *fileindex,
6383 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6384 struct got_repository *repo)
6386 const struct got_error *err;
6387 struct got_commit_object *commit;
6388 struct got_object_id *new_commit_id;
6390 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6391 && hle->logmsg == NULL) {
6392 err = histedit_edit_logmsg(hle, repo);
6397 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6401 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6402 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6405 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6407 err = show_histedit_progress(commit, hle, NULL);
6409 err = show_histedit_progress(commit, hle, new_commit_id);
6410 free(new_commit_id);
6413 got_object_commit_close(commit);
6417 static const struct got_error *
6418 histedit_skip_commit(struct got_histedit_list_entry *hle,
6419 struct got_worktree *worktree, struct got_repository *repo)
6421 const struct got_error *error;
6422 struct got_commit_object *commit;
6424 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6429 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6433 error = show_histedit_progress(commit, hle, NULL);
6434 got_object_commit_close(commit);
6438 static const struct got_error *
6439 cmd_histedit(int argc, char *argv[])
6441 const struct got_error *error = NULL;
6442 struct got_worktree *worktree = NULL;
6443 struct got_fileindex *fileindex = NULL;
6444 struct got_repository *repo = NULL;
6446 struct got_reference *branch = NULL;
6447 struct got_reference *tmp_branch = NULL;
6448 struct got_object_id *resume_commit_id = NULL;
6449 struct got_object_id *base_commit_id = NULL;
6450 struct got_object_id *head_commit_id = NULL;
6451 struct got_commit_object *commit = NULL;
6452 int ch, rebase_in_progress = 0, did_something;
6453 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6454 const char *edit_script_path = NULL;
6455 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6456 struct got_object_id_queue commits;
6457 struct got_pathlist_head merged_paths;
6458 const struct got_object_id_queue *parent_ids;
6459 struct got_object_qid *pid;
6460 struct got_histedit_list histedit_cmds;
6461 struct got_histedit_list_entry *hle;
6463 SIMPLEQ_INIT(&commits);
6464 TAILQ_INIT(&histedit_cmds);
6465 TAILQ_INIT(&merged_paths);
6467 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6476 edit_script_path = optarg;
6488 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6489 "unveil", NULL) == -1)
6492 if (abort_edit && continue_edit)
6498 * This command cannot apply unveil(2) in all cases because the
6499 * user may choose to run an editor to edit the histedit script
6500 * and to edit individual commit log messages.
6501 * unveil(2) traverses exec(2); if an editor is used we have to
6502 * apply unveil after edit script and log messages have been written.
6503 * XXX TODO: Make use of unveil(2) where possible.
6506 cwd = getcwd(NULL, 0);
6508 error = got_error_from_errno("getcwd");
6511 error = got_worktree_open(&worktree, cwd);
6515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6520 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6523 if (rebase_in_progress) {
6524 error = got_error(GOT_ERR_REBASING);
6528 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6532 if (edit_in_progress && abort_edit) {
6533 error = got_worktree_histedit_continue(&resume_commit_id,
6534 &tmp_branch, &branch, &base_commit_id, &fileindex,
6538 printf("Switching work tree to %s\n",
6539 got_ref_get_symref_target(branch));
6540 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6541 branch, base_commit_id, update_progress, &did_something);
6544 printf("Histedit of %s aborted\n",
6545 got_ref_get_symref_target(branch));
6546 goto done; /* nothing else to do */
6547 } else if (abort_edit) {
6548 error = got_error(GOT_ERR_NOT_HISTEDIT);
6552 if (continue_edit) {
6555 if (!edit_in_progress) {
6556 error = got_error(GOT_ERR_NOT_HISTEDIT);
6560 error = got_worktree_get_histedit_script_path(&path, worktree);
6564 error = histedit_load_list(&histedit_cmds, path, repo);
6569 error = got_worktree_histedit_continue(&resume_commit_id,
6570 &tmp_branch, &branch, &base_commit_id, &fileindex,
6575 error = got_ref_resolve(&head_commit_id, repo, branch);
6579 error = got_object_open_as_commit(&commit, repo,
6583 parent_ids = got_object_commit_get_parent_ids(commit);
6584 pid = SIMPLEQ_FIRST(parent_ids);
6586 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6589 error = collect_commits(&commits, head_commit_id, pid->id,
6590 base_commit_id, got_worktree_get_path_prefix(worktree),
6591 GOT_ERR_HISTEDIT_PATH, repo);
6592 got_object_commit_close(commit);
6597 if (edit_in_progress) {
6598 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6602 error = got_ref_open(&branch, repo,
6603 got_worktree_get_head_ref_name(worktree), 0);
6607 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6608 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6609 "will not edit commit history of a branch outside "
6610 "the \"refs/heads/\" reference namespace");
6614 error = got_ref_resolve(&head_commit_id, repo, branch);
6615 got_ref_close(branch);
6620 error = got_object_open_as_commit(&commit, repo,
6624 parent_ids = got_object_commit_get_parent_ids(commit);
6625 pid = SIMPLEQ_FIRST(parent_ids);
6627 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6630 error = collect_commits(&commits, head_commit_id, pid->id,
6631 got_worktree_get_base_commit_id(worktree),
6632 got_worktree_get_path_prefix(worktree),
6633 GOT_ERR_HISTEDIT_PATH, repo);
6634 got_object_commit_close(commit);
6639 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6640 &base_commit_id, &fileindex, worktree, repo);
6644 if (edit_script_path) {
6645 error = histedit_load_list(&histedit_cmds,
6646 edit_script_path, repo);
6648 got_worktree_histedit_abort(worktree, fileindex,
6649 repo, branch, base_commit_id,
6650 update_progress, &did_something);
6654 error = histedit_edit_script(&histedit_cmds, &commits,
6657 got_worktree_histedit_abort(worktree, fileindex,
6658 repo, branch, base_commit_id,
6659 update_progress, &did_something);
6665 error = histedit_save_list(&histedit_cmds, worktree,
6668 got_worktree_histedit_abort(worktree, fileindex,
6669 repo, branch, base_commit_id,
6670 update_progress, &did_something);
6676 error = histedit_check_script(&histedit_cmds, &commits, repo);
6680 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6681 if (resume_commit_id) {
6682 if (got_object_id_cmp(hle->commit_id,
6683 resume_commit_id) != 0)
6686 resume_commit_id = NULL;
6687 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6688 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6689 error = histedit_skip_commit(hle, worktree,
6692 error = histedit_commit(NULL, worktree,
6693 fileindex, tmp_branch, hle, repo);
6700 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6701 error = histedit_skip_commit(hle, worktree, repo);
6707 error = got_object_open_as_commit(&commit, repo,
6711 parent_ids = got_object_commit_get_parent_ids(commit);
6712 pid = SIMPLEQ_FIRST(parent_ids);
6714 error = got_worktree_histedit_merge_files(&merged_paths,
6715 worktree, fileindex, pid->id, hle->commit_id, repo,
6716 rebase_progress, &rebase_status, check_cancelled, NULL);
6719 got_object_commit_close(commit);
6722 if (rebase_status == GOT_STATUS_CONFLICT) {
6723 got_worktree_rebase_pathlist_free(&merged_paths);
6727 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6729 error = got_object_id_str(&id_str, hle->commit_id);
6732 printf("Stopping histedit for amending commit %s\n",
6735 got_worktree_rebase_pathlist_free(&merged_paths);
6736 error = got_worktree_histedit_postpone(worktree,
6741 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6742 error = histedit_skip_commit(hle, worktree, repo);
6748 error = histedit_commit(&merged_paths, worktree, fileindex,
6749 tmp_branch, hle, repo);
6750 got_worktree_rebase_pathlist_free(&merged_paths);
6755 if (rebase_status == GOT_STATUS_CONFLICT) {
6756 error = got_worktree_histedit_postpone(worktree, fileindex);
6759 error = got_error_msg(GOT_ERR_CONFLICTS,
6760 "conflicts must be resolved before rebasing can continue");
6762 error = histedit_complete(worktree, fileindex, tmp_branch,
6765 got_object_id_queue_free(&commits);
6766 histedit_free_list(&histedit_cmds);
6767 free(head_commit_id);
6768 free(base_commit_id);
6769 free(resume_commit_id);
6771 got_object_commit_close(commit);
6773 got_ref_close(branch);
6775 got_ref_close(tmp_branch);
6777 got_worktree_close(worktree);
6779 got_repo_close(repo);
6784 usage_integrate(void)
6786 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6790 static const struct got_error *
6791 cmd_integrate(int argc, char *argv[])
6793 const struct got_error *error = NULL;
6794 struct got_repository *repo = NULL;
6795 struct got_worktree *worktree = NULL;
6796 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6797 const char *branch_arg = NULL;
6798 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6799 struct got_fileindex *fileindex = NULL;
6800 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6801 int ch, did_something = 0;
6803 while ((ch = getopt(argc, argv, "")) != -1) {
6816 branch_arg = argv[0];
6818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6819 "unveil", NULL) == -1)
6822 cwd = getcwd(NULL, 0);
6824 error = got_error_from_errno("getcwd");
6828 error = got_worktree_open(&worktree, cwd);
6832 error = check_rebase_or_histedit_in_progress(worktree);
6836 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6841 error = apply_unveil(got_repo_get_path(repo), 0,
6842 got_worktree_get_root_path(worktree));
6846 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6847 error = got_error_from_errno("asprintf");
6851 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6852 &base_branch_ref, worktree, refname, repo);
6856 refname = strdup(got_ref_get_name(branch_ref));
6857 if (refname == NULL) {
6858 error = got_error_from_errno("strdup");
6859 got_worktree_integrate_abort(worktree, fileindex, repo,
6860 branch_ref, base_branch_ref);
6863 base_refname = strdup(got_ref_get_name(base_branch_ref));
6864 if (base_refname == NULL) {
6865 error = got_error_from_errno("strdup");
6866 got_worktree_integrate_abort(worktree, fileindex, repo,
6867 branch_ref, base_branch_ref);
6871 error = got_ref_resolve(&commit_id, repo, branch_ref);
6875 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6879 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6880 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6881 "specified branch has already been integrated");
6882 got_worktree_integrate_abort(worktree, fileindex, repo,
6883 branch_ref, base_branch_ref);
6887 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6889 if (error->code == GOT_ERR_ANCESTRY)
6890 error = got_error(GOT_ERR_REBASE_REQUIRED);
6891 got_worktree_integrate_abort(worktree, fileindex, repo,
6892 branch_ref, base_branch_ref);
6896 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6897 branch_ref, base_branch_ref, update_progress, &did_something,
6898 check_cancelled, NULL);
6902 printf("Integrated %s into %s\n", refname, base_refname);
6905 got_repo_close(repo);
6907 got_worktree_close(worktree);
6909 free(base_commit_id);
6919 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6920 "[file-path ...]\n",
6925 static const struct got_error *
6926 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6927 const char *path, struct got_object_id *blob_id,
6928 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6929 int dirfd, const char *de_name)
6931 const struct got_error *err = NULL;
6932 char *id_str = NULL;
6934 if (staged_status != GOT_STATUS_ADD &&
6935 staged_status != GOT_STATUS_MODIFY &&
6936 staged_status != GOT_STATUS_DELETE)
6939 if (staged_status == GOT_STATUS_ADD ||
6940 staged_status == GOT_STATUS_MODIFY)
6941 err = got_object_id_str(&id_str, staged_blob_id);
6943 err = got_object_id_str(&id_str, blob_id);
6947 printf("%s %c %s\n", id_str, staged_status, path);
6952 static const struct got_error *
6953 cmd_stage(int argc, char *argv[])
6955 const struct got_error *error = NULL;
6956 struct got_repository *repo = NULL;
6957 struct got_worktree *worktree = NULL;
6959 struct got_pathlist_head paths;
6960 struct got_pathlist_entry *pe;
6961 int ch, list_stage = 0, pflag = 0;
6962 FILE *patch_script_file = NULL;
6963 const char *patch_script_path = NULL;
6964 struct choose_patch_arg cpa;
6968 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6977 patch_script_path = optarg;
6989 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6990 "unveil", NULL) == -1)
6993 if (list_stage && (pflag || patch_script_path))
6994 errx(1, "-l option cannot be used with other options");
6995 if (patch_script_path && !pflag)
6996 errx(1, "-F option can only be used together with -p option");
6998 cwd = getcwd(NULL, 0);
7000 error = got_error_from_errno("getcwd");
7004 error = got_worktree_open(&worktree, cwd);
7008 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7013 if (patch_script_path) {
7014 patch_script_file = fopen(patch_script_path, "r");
7015 if (patch_script_file == NULL) {
7016 error = got_error_from_errno2("fopen",
7021 error = apply_unveil(got_repo_get_path(repo), 0,
7022 got_worktree_get_root_path(worktree));
7026 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7031 error = got_worktree_status(worktree, &paths, repo,
7032 print_stage, NULL, check_cancelled, NULL);
7034 cpa.patch_script_file = patch_script_file;
7035 cpa.action = "stage";
7036 error = got_worktree_stage(worktree, &paths,
7037 pflag ? NULL : print_status, NULL,
7038 pflag ? choose_patch : NULL, &cpa, repo);
7041 if (patch_script_file && fclose(patch_script_file) == EOF &&
7043 error = got_error_from_errno2("fclose", patch_script_path);
7045 got_repo_close(repo);
7047 got_worktree_close(worktree);
7048 TAILQ_FOREACH(pe, &paths, entry)
7049 free((char *)pe->path);
7050 got_pathlist_free(&paths);
7058 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7059 "[file-path ...]\n",
7065 static const struct got_error *
7066 cmd_unstage(int argc, char *argv[])
7068 const struct got_error *error = NULL;
7069 struct got_repository *repo = NULL;
7070 struct got_worktree *worktree = NULL;
7072 struct got_pathlist_head paths;
7073 struct got_pathlist_entry *pe;
7074 int ch, did_something = 0, pflag = 0;
7075 FILE *patch_script_file = NULL;
7076 const char *patch_script_path = NULL;
7077 struct choose_patch_arg cpa;
7081 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7087 patch_script_path = optarg;
7099 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7100 "unveil", NULL) == -1)
7103 if (patch_script_path && !pflag)
7104 errx(1, "-F option can only be used together with -p option");
7106 cwd = getcwd(NULL, 0);
7108 error = got_error_from_errno("getcwd");
7112 error = got_worktree_open(&worktree, cwd);
7116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7121 if (patch_script_path) {
7122 patch_script_file = fopen(patch_script_path, "r");
7123 if (patch_script_file == NULL) {
7124 error = got_error_from_errno2("fopen",
7130 error = apply_unveil(got_repo_get_path(repo), 0,
7131 got_worktree_get_root_path(worktree));
7135 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7139 cpa.patch_script_file = patch_script_file;
7140 cpa.action = "unstage";
7141 error = got_worktree_unstage(worktree, &paths, update_progress,
7142 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7144 if (patch_script_file && fclose(patch_script_file) == EOF &&
7146 error = got_error_from_errno2("fclose", patch_script_path);
7148 got_repo_close(repo);
7150 got_worktree_close(worktree);
7151 TAILQ_FOREACH(pe, &paths, entry)
7152 free((char *)pe->path);
7153 got_pathlist_free(&paths);
7161 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7162 "arg1 [arg2 ...]\n", getprogname());
7166 static const struct got_error *
7167 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7169 const struct got_error *err;
7170 struct got_blob_object *blob;
7172 err = got_object_open_as_blob(&blob, repo, id, 8192);
7176 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7177 got_object_blob_close(blob);
7181 static const struct got_error *
7182 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7184 const struct got_error *err;
7185 struct got_tree_object *tree;
7188 err = got_object_open_as_tree(&tree, repo, id);
7192 nentries = got_object_tree_get_nentries(tree);
7193 for (i = 0; i < nentries; i++) {
7194 struct got_tree_entry *te;
7196 if (sigint_received || sigpipe_received)
7198 te = got_object_tree_get_entry(tree, i);
7199 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7202 fprintf(outfile, "%s %.7o %s\n", id_str,
7203 got_tree_entry_get_mode(te),
7204 got_tree_entry_get_name(te));
7208 got_object_tree_close(tree);
7212 static const struct got_error *
7213 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7215 const struct got_error *err;
7216 struct got_commit_object *commit;
7217 const struct got_object_id_queue *parent_ids;
7218 struct got_object_qid *pid;
7219 char *id_str = NULL;
7220 const char *logmsg = NULL;
7222 err = got_object_open_as_commit(&commit, repo, id);
7226 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7230 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7231 parent_ids = got_object_commit_get_parent_ids(commit);
7232 fprintf(outfile, "numparents %d\n",
7233 got_object_commit_get_nparents(commit));
7234 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7236 err = got_object_id_str(&pid_str, pid->id);
7239 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7242 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7243 got_object_commit_get_author(commit),
7244 got_object_commit_get_author_time(commit));
7246 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7247 got_object_commit_get_author(commit),
7248 got_object_commit_get_committer_time(commit));
7250 logmsg = got_object_commit_get_logmsg_raw(commit);
7251 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7252 fprintf(outfile, "%s", logmsg);
7255 got_object_commit_close(commit);
7259 static const struct got_error *
7260 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7262 const struct got_error *err;
7263 struct got_tag_object *tag;
7264 char *id_str = NULL;
7265 const char *tagmsg = NULL;
7267 err = got_object_open_as_tag(&tag, repo, id);
7271 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7275 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7277 switch (got_object_tag_get_object_type(tag)) {
7278 case GOT_OBJ_TYPE_BLOB:
7279 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7280 GOT_OBJ_LABEL_BLOB);
7282 case GOT_OBJ_TYPE_TREE:
7283 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7284 GOT_OBJ_LABEL_TREE);
7286 case GOT_OBJ_TYPE_COMMIT:
7287 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7288 GOT_OBJ_LABEL_COMMIT);
7290 case GOT_OBJ_TYPE_TAG:
7291 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7298 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7299 got_object_tag_get_name(tag));
7301 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7302 got_object_tag_get_tagger(tag),
7303 got_object_tag_get_tagger_time(tag));
7305 tagmsg = got_object_tag_get_message(tag);
7306 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7307 fprintf(outfile, "%s", tagmsg);
7310 got_object_tag_close(tag);
7314 static const struct got_error *
7315 cmd_cat(int argc, char *argv[])
7317 const struct got_error *error;
7318 struct got_repository *repo = NULL;
7319 struct got_worktree *worktree = NULL;
7320 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7321 const char *commit_id_str = NULL;
7322 struct got_object_id *id = NULL, *commit_id = NULL;
7323 int ch, obj_type, i, force_path = 0;
7326 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7331 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7334 commit_id_str = optarg;
7337 repo_path = realpath(optarg, NULL);
7338 if (repo_path == NULL)
7339 return got_error_from_errno2("realpath",
7341 got_path_strip_trailing_slashes(repo_path);
7355 cwd = getcwd(NULL, 0);
7357 error = got_error_from_errno("getcwd");
7360 error = got_worktree_open(&worktree, cwd);
7361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7364 if (repo_path == NULL) {
7366 got_worktree_get_repo_path(worktree));
7367 if (repo_path == NULL) {
7368 error = got_error_from_errno("strdup");
7374 if (repo_path == NULL) {
7375 repo_path = getcwd(NULL, 0);
7376 if (repo_path == NULL)
7377 return got_error_from_errno("getcwd");
7380 error = got_repo_open(&repo, repo_path, NULL);
7385 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7389 if (commit_id_str == NULL)
7390 commit_id_str = GOT_REF_HEAD;
7391 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7395 for (i = 0; i < argc; i++) {
7397 error = got_object_id_by_path(&id, repo, commit_id,
7402 error = match_object_id(&id, &label, argv[i],
7403 GOT_OBJ_TYPE_ANY, 0, repo);
7405 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7406 error->code != GOT_ERR_NOT_REF)
7408 error = got_object_id_by_path(&id, repo,
7409 commit_id, argv[i]);
7415 error = got_object_get_type(&obj_type, repo, id);
7420 case GOT_OBJ_TYPE_BLOB:
7421 error = cat_blob(id, repo, stdout);
7423 case GOT_OBJ_TYPE_TREE:
7424 error = cat_tree(id, repo, stdout);
7426 case GOT_OBJ_TYPE_COMMIT:
7427 error = cat_commit(id, repo, stdout);
7429 case GOT_OBJ_TYPE_TAG:
7430 error = cat_tag(id, repo, stdout);
7433 error = got_error(GOT_ERR_OBJ_TYPE);
7448 got_worktree_close(worktree);
7450 const struct got_error *repo_error;
7451 repo_error = got_repo_close(repo);