2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 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>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_stage(void);
101 __dead static void usage_unstage(void);
102 __dead static void usage_cat(void);
104 static const struct got_error* cmd_init(int, char *[]);
105 static const struct got_error* cmd_import(int, char *[]);
106 static const struct got_error* cmd_checkout(int, char *[]);
107 static const struct got_error* cmd_update(int, char *[]);
108 static const struct got_error* cmd_log(int, char *[]);
109 static const struct got_error* cmd_diff(int, char *[]);
110 static const struct got_error* cmd_blame(int, char *[]);
111 static const struct got_error* cmd_tree(int, char *[]);
112 static const struct got_error* cmd_status(int, char *[]);
113 static const struct got_error* cmd_ref(int, char *[]);
114 static const struct got_error* cmd_branch(int, char *[]);
115 static const struct got_error* cmd_tag(int, char *[]);
116 static const struct got_error* cmd_add(int, char *[]);
117 static const struct got_error* cmd_remove(int, char *[]);
118 static const struct got_error* cmd_revert(int, char *[]);
119 static const struct got_error* cmd_commit(int, char *[]);
120 static const struct got_error* cmd_cherrypick(int, char *[]);
121 static const struct got_error* cmd_backout(int, char *[]);
122 static const struct got_error* cmd_rebase(int, char *[]);
123 static const struct got_error* cmd_histedit(int, char *[]);
124 static const struct got_error* cmd_stage(int, char *[]);
125 static const struct got_error* cmd_unstage(int, char *[]);
126 static const struct got_error* cmd_cat(int, char *[]);
128 static struct got_cmd got_commands[] = {
129 { "init", cmd_init, usage_init, "in" },
130 { "import", cmd_import, usage_import, "im" },
131 { "checkout", cmd_checkout, usage_checkout, "co" },
132 { "update", cmd_update, usage_update, "up" },
133 { "log", cmd_log, usage_log, "" },
134 { "diff", cmd_diff, usage_diff, "di" },
135 { "blame", cmd_blame, usage_blame, "bl" },
136 { "tree", cmd_tree, usage_tree, "tr" },
137 { "status", cmd_status, usage_status, "st" },
138 { "ref", cmd_ref, usage_ref, "" },
139 { "branch", cmd_branch, usage_branch, "br" },
140 { "tag", cmd_tag, usage_tag, "" },
141 { "add", cmd_add, usage_add, "" },
142 { "remove", cmd_remove, usage_remove, "rm" },
143 { "revert", cmd_revert, usage_revert, "rv" },
144 { "commit", cmd_commit, usage_commit, "ci" },
145 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 { "backout", cmd_backout, usage_backout, "bo" },
147 { "rebase", cmd_rebase, usage_rebase, "rb" },
148 { "histedit", cmd_histedit, usage_histedit, "he" },
149 { "stage", cmd_stage, usage_stage, "sg" },
150 { "unstage", cmd_unstage, usage_unstage, "ug" },
151 { "cat", cmd_cat, usage_cat, "" },
159 fprintf(stderr, "commands:");
160 for (i = 0; i < nitems(got_commands); i++) {
161 struct got_cmd *cmd = &got_commands[i];
162 fprintf(stderr, " %s", cmd->cmd_name);
168 main(int argc, char *argv[])
173 int hflag = 0, Vflag = 0;
175 setlocale(LC_CTYPE, "");
177 while ((ch = getopt(argc, argv, "hV")) != -1) {
196 got_version_print_str();
203 signal(SIGINT, catch_sigint);
204 signal(SIGPIPE, catch_sigpipe);
206 for (i = 0; i < nitems(got_commands); i++) {
207 const struct got_error *error;
209 cmd = &got_commands[i];
211 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 got_commands[i].cmd_usage();
218 error = got_commands[i].cmd_main(argc, argv);
219 if (error && !(sigint_received || sigpipe_received)) {
220 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
227 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
235 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
242 static const struct got_error *
243 get_editor(char **abspath)
245 const struct got_error *err = NULL;
250 editor = getenv("VISUAL");
252 editor = getenv("EDITOR");
255 err = got_path_find_prog(abspath, editor);
260 if (*abspath == NULL) {
261 *abspath = strdup("/bin/ed");
262 if (*abspath == NULL)
263 return got_error_from_errno("strdup");
269 static const struct got_error *
270 apply_unveil(const char *repo_path, int repo_read_only,
271 const char *worktree_path)
273 const struct got_error *err;
276 if (unveil("gmon.out", "rwc") != 0)
277 return got_error_from_errno2("unveil", "gmon.out");
279 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 return got_error_from_errno2("unveil", repo_path);
282 if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 return got_error_from_errno2("unveil", worktree_path);
285 if (unveil("/tmp", "rwc") != 0)
286 return got_error_from_errno2("unveil", "/tmp");
288 err = got_privsep_unveil_exec_helpers();
292 if (unveil(NULL, NULL) != 0)
293 return got_error_from_errno("unveil");
301 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 static const struct got_error *
306 cmd_init(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 char *repo_path = NULL;
312 while ((ch = getopt(argc, argv, "")) != -1) {
324 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
330 repo_path = strdup(argv[0]);
331 if (repo_path == NULL)
332 return got_error_from_errno("strdup");
334 got_path_strip_trailing_slashes(repo_path);
336 error = got_path_mkdir(repo_path);
338 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
341 error = apply_unveil(repo_path, 0, NULL);
345 error = got_repo_init(repo_path);
357 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 "[-r repository-path] [-I pattern] path\n", getprogname());
363 spawn_editor(const char *editor, const char *file)
366 sig_t sighup, sigint, sigquit;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
377 execl(editor, editor, file, (char *)NULL);
381 while (waitpid(pid, &st, 0) == -1)
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 const char *initial_content)
402 const struct got_error *err = NULL;
406 int content_changed = 0;
411 if (stat(logmsg_path, &st) == -1)
412 return got_error_from_errno2("stat", logmsg_path);
414 if (spawn_editor(editor, logmsg_path) == -1)
415 return got_error_from_errno("failed spawning editor");
417 if (stat(logmsg_path, &st2) == -1)
418 return got_error_from_errno("stat");
420 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 "no changes made to commit message, aborting");
424 *logmsg = malloc(st2.st_size + 1);
426 return got_error_from_errno("malloc");
430 fp = fopen(logmsg_path, "r");
432 err = got_error_from_errno("fopen");
435 while (fgets(buf, sizeof(buf), fp) != NULL) {
436 if (!content_changed && strcmp(buf, initial_content) != 0)
438 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(*logmsg, buf, st2.st_size);
444 while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 (*logmsg)[len - 1] = '\0';
449 if (len == 0 || !content_changed)
450 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 "commit message cannot be empty, aborting");
460 static const struct got_error *
461 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 const char *branch_name)
464 char *initial_content = NULL, *logmsg_path = NULL;
465 const struct got_error *err = NULL;
468 if (asprintf(&initial_content,
469 "\n# %s to be imported to branch %s\n", path_dir,
471 return got_error_from_errno("asprintf");
473 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 dprintf(fd, initial_content);
480 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
482 free(initial_content);
487 static const struct got_error *
488 import_progress(void *arg, const char *path)
490 printf("A %s\n", path);
494 static const struct got_error *
495 get_author(const char **author)
497 const char *got_author;
501 got_author = getenv("GOT_AUTHOR");
502 if (got_author == NULL) {
503 /* TODO: Look up user in password database? */
504 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
507 *author = got_author;
510 * Really dumb email address check; we're only doing this to
511 * avoid git's object parser breaking on commits we create.
513 while (*got_author && *got_author != '<')
515 if (*got_author != '<')
516 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 while (*got_author && *got_author != '@')
519 if (*got_author != '@')
520 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 while (*got_author && *got_author != '>')
523 if (*got_author != '>')
524 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
529 static const struct got_error *
530 cmd_import(int argc, char *argv[])
532 const struct got_error *error = NULL;
533 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
536 const char *branch_name = "master";
537 char *refname = NULL, *id_str = NULL;
538 struct got_repository *repo = NULL;
539 struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 struct got_object_id *new_commit_id = NULL;
542 struct got_pathlist_head ignores;
543 struct got_pathlist_entry *pe;
545 TAILQ_INIT(&ignores);
547 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
550 branch_name = optarg;
553 logmsg = strdup(optarg);
554 if (logmsg == NULL) {
555 error = got_error_from_errno("strdup");
560 repo_path = realpath(optarg, NULL);
561 if (repo_path == NULL) {
562 error = got_error_from_errno("realpath");
567 if (optarg[0] == '\0')
569 error = got_pathlist_insert(&pe, &ignores, optarg,
584 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
591 error = get_author(&author);
595 if (repo_path == NULL) {
596 repo_path = getcwd(NULL, 0);
597 if (repo_path == NULL)
598 return got_error_from_errno("getcwd");
600 got_path_strip_trailing_slashes(repo_path);
601 error = got_repo_open(&repo, repo_path);
605 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 error = got_error_from_errno("asprintf");
610 error = got_ref_open(&branch_ref, repo, refname, 0);
612 if (error->code != GOT_ERR_NOT_REF)
615 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 "import target branch already exists");
620 path_dir = realpath(argv[0], NULL);
621 if (path_dir == NULL) {
622 error = got_error_from_errno("realpath");
625 got_path_strip_trailing_slashes(path_dir);
628 * unveil(2) traverses exec(2); if an editor is used we have
629 * to apply unveil after the log message has been written.
631 if (logmsg == NULL || strlen(logmsg) == 0) {
632 error = get_editor(&editor);
635 error = collect_import_msg(&logmsg, editor, path_dir, refname);
640 if (unveil(path_dir, "r") != 0)
641 return got_error_from_errno2("unveil", path_dir);
643 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
647 error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 author, &ignores, repo, import_progress, NULL);
652 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
656 error = got_ref_write(branch_ref, repo);
660 error = got_object_id_str(&id_str, new_commit_id);
664 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
666 if (error->code != GOT_ERR_NOT_REF)
669 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
674 error = got_ref_write(head_ref, repo);
679 printf("Created branch %s with commit %s\n",
680 got_ref_get_name(branch_ref), id_str);
688 got_ref_close(branch_ref);
690 got_ref_close(head_ref);
697 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 "[-p prefix] repository-path [worktree-path]\n", getprogname());
702 static const struct got_error *
703 checkout_progress(void *arg, unsigned char status, const char *path)
705 char *worktree_path = arg;
707 /* Base commit bump happens silently. */
708 if (status == GOT_STATUS_BUMP_BASE)
711 while (path[0] == '/')
714 printf("%c %s/%s\n", status, worktree_path, path);
718 static const struct got_error *
719 check_cancelled(void *arg)
721 if (sigint_received || sigpipe_received)
722 return got_error(GOT_ERR_CANCELLED);
726 static const struct got_error *
727 check_linear_ancestry(struct got_object_id *commit_id,
728 struct got_object_id *base_commit_id, struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_object_id *yca_id;
733 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 commit_id, base_commit_id, repo, check_cancelled, NULL);
739 return got_error(GOT_ERR_ANCESTRY);
742 * Require a straight line of history between the target commit
743 * and the work tree's base commit.
745 * Non-linear situations such as this require a rebase:
747 * (commit) D F (base_commit)
755 * 'got update' only handles linear cases:
756 * Update forwards in time: A (base/yca) - B - C - D (commit)
757 * Update backwards in time: D (base) - C - B - A (commit/yca)
759 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 got_object_id_cmp(base_commit_id, yca_id) != 0)
761 return got_error(GOT_ERR_ANCESTRY);
767 static const struct got_error *
768 check_same_branch(struct got_object_id *commit_id,
769 struct got_reference *head_ref, struct got_object_id *yca_id,
770 struct got_repository *repo)
772 const struct got_error *err = NULL;
773 struct got_commit_graph *graph = NULL;
774 struct got_object_id *head_commit_id = NULL;
775 int is_same_branch = 0;
777 err = got_ref_resolve(&head_commit_id, repo, head_ref);
781 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
785 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
790 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
794 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 check_cancelled, NULL);
800 struct got_object_id *id;
801 err = got_commit_graph_iter_next(&id, graph);
803 if (err->code == GOT_ERR_ITER_COMPLETED) {
806 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
808 err = got_commit_graph_fetch_commits(graph, 1,
809 repo, check_cancelled, NULL);
815 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
817 if (got_object_id_cmp(id, commit_id) == 0) {
825 got_commit_graph_close(graph);
826 free(head_commit_id);
827 if (!err && !is_same_branch)
828 err = got_error(GOT_ERR_ANCESTRY);
832 static const struct got_error *
833 resolve_commit_arg(struct got_object_id **commit_id,
834 const char *commit_id_arg, struct got_repository *repo)
836 const struct got_error *err;
837 struct got_reference *ref;
838 struct got_tag_object *tag;
840 err = got_repo_object_match_tag(&tag, commit_id_arg,
841 GOT_OBJ_TYPE_COMMIT, repo);
843 *commit_id = got_object_id_dup(
844 got_object_tag_get_object_id(tag));
845 if (*commit_id == NULL)
846 err = got_error_from_errno("got_object_id_dup");
847 got_object_tag_close(tag);
849 } else if (err->code != GOT_ERR_NO_OBJ)
852 err = got_ref_open(&ref, repo, commit_id_arg, 0);
854 err = got_ref_resolve(commit_id, repo, ref);
857 if (err->code != GOT_ERR_NOT_REF)
859 err = got_repo_match_object_id_prefix(commit_id,
860 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
865 static const struct got_error *
866 cmd_checkout(int argc, char *argv[])
868 const struct got_error *error = NULL;
869 struct got_repository *repo = NULL;
870 struct got_reference *head_ref = NULL;
871 struct got_worktree *worktree = NULL;
872 char *repo_path = NULL;
873 char *worktree_path = NULL;
874 const char *path_prefix = "";
875 const char *branch_name = GOT_REF_HEAD;
876 char *commit_id_str = NULL;
877 int ch, same_path_prefix;
878 struct got_pathlist_head paths;
882 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
885 branch_name = optarg;
888 commit_id_str = strdup(optarg);
889 if (commit_id_str == NULL)
890 return got_error_from_errno("strdup");
893 path_prefix = optarg;
905 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 "unveil", NULL) == -1)
910 char *cwd, *base, *dotgit;
911 repo_path = realpath(argv[0], NULL);
912 if (repo_path == NULL)
913 return got_error_from_errno2("realpath", argv[0]);
914 cwd = getcwd(NULL, 0);
916 error = got_error_from_errno("getcwd");
919 if (path_prefix[0]) {
920 base = basename(path_prefix);
922 error = got_error_from_errno2("basename",
927 base = basename(repo_path);
929 error = got_error_from_errno2("basename",
934 dotgit = strstr(base, ".git");
937 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 error = got_error_from_errno("asprintf");
943 } else if (argc == 2) {
944 repo_path = realpath(argv[0], NULL);
945 if (repo_path == NULL) {
946 error = got_error_from_errno2("realpath", argv[0]);
949 worktree_path = realpath(argv[1], NULL);
950 if (worktree_path == NULL) {
951 if (errno != ENOENT) {
952 error = got_error_from_errno2("realpath",
956 worktree_path = strdup(argv[1]);
957 if (worktree_path == NULL) {
958 error = got_error_from_errno("strdup");
965 got_path_strip_trailing_slashes(repo_path);
966 got_path_strip_trailing_slashes(worktree_path);
968 error = got_repo_open(&repo, repo_path);
972 /* Pre-create work tree path for unveil(2) */
973 error = got_path_mkdir(worktree_path);
975 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
978 if (!got_path_dir_is_empty(worktree_path)) {
979 error = got_error_path(worktree_path,
980 GOT_ERR_DIR_NOT_EMPTY);
985 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
989 error = got_ref_open(&head_ref, repo, branch_name, 0);
993 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
997 error = got_worktree_open(&worktree, worktree_path);
1001 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1005 if (!same_path_prefix) {
1006 error = got_error(GOT_ERR_PATH_PREFIX);
1010 if (commit_id_str) {
1011 struct got_object_id *commit_id;
1012 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1015 error = check_linear_ancestry(commit_id,
1016 got_worktree_get_base_commit_id(worktree), repo);
1017 if (error != NULL) {
1021 error = check_same_branch(commit_id, head_ref, NULL, repo);
1024 error = got_worktree_set_base_commit_id(worktree, repo,
1031 error = got_pathlist_append(&paths, "", NULL);
1034 error = got_worktree_checkout_files(worktree, &paths, repo,
1035 checkout_progress, worktree_path, check_cancelled, NULL);
1039 printf("Now shut up and hack\n");
1042 got_pathlist_free(&paths);
1043 free(commit_id_str);
1045 free(worktree_path);
1052 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1057 static const struct got_error *
1058 update_progress(void *arg, unsigned char status, const char *path)
1060 int *did_something = arg;
1062 if (status == GOT_STATUS_EXISTS)
1067 /* Base commit bump happens silently. */
1068 if (status == GOT_STATUS_BUMP_BASE)
1071 while (path[0] == '/')
1073 printf("%c %s\n", status, path);
1077 static const struct got_error *
1078 switch_head_ref(struct got_reference *head_ref,
1079 struct got_object_id *commit_id, struct got_worktree *worktree,
1080 struct got_repository *repo)
1082 const struct got_error *err = NULL;
1084 int ref_has_moved = 0;
1086 /* Trivial case: switching between two different references. */
1087 if (strcmp(got_ref_get_name(head_ref),
1088 got_worktree_get_head_ref_name(worktree)) != 0) {
1089 printf("Switching work tree from %s to %s\n",
1090 got_worktree_get_head_ref_name(worktree),
1091 got_ref_get_name(head_ref));
1092 return got_worktree_set_head_ref(worktree, head_ref);
1095 err = check_linear_ancestry(commit_id,
1096 got_worktree_get_base_commit_id(worktree), repo);
1098 if (err->code != GOT_ERR_ANCESTRY)
1105 /* Switching to a rebased branch with the same reference name. */
1106 err = got_object_id_str(&base_id_str,
1107 got_worktree_get_base_commit_id(worktree));
1110 printf("Reference %s now points at a different branch\n",
1111 got_worktree_get_head_ref_name(worktree));
1112 printf("Switching work tree from %s to %s\n", base_id_str,
1113 got_worktree_get_head_ref_name(worktree));
1117 static const struct got_error *
1118 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1120 const struct got_error *err;
1123 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1127 return got_error(GOT_ERR_REBASING);
1129 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1133 return got_error(GOT_ERR_HISTEDIT_BUSY);
1138 static const struct got_error *
1139 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 char *argv[], struct got_worktree *worktree)
1142 const struct got_error *err = NULL;
1149 return got_error_from_errno("strdup");
1150 return got_pathlist_append(paths, path, NULL);
1153 for (i = 0; i < argc; i++) {
1154 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1157 err = got_pathlist_append(paths, path, NULL);
1167 static const struct got_error *
1168 cmd_update(int argc, char *argv[])
1170 const struct got_error *error = NULL;
1171 struct got_repository *repo = NULL;
1172 struct got_worktree *worktree = NULL;
1173 char *worktree_path = NULL;
1174 struct got_object_id *commit_id = NULL;
1175 char *commit_id_str = NULL;
1176 const char *branch_name = NULL;
1177 struct got_reference *head_ref = NULL;
1178 struct got_pathlist_head paths;
1179 struct got_pathlist_entry *pe;
1180 int ch, did_something = 0;
1184 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1187 branch_name = optarg;
1190 commit_id_str = strdup(optarg);
1191 if (commit_id_str == NULL)
1192 return got_error_from_errno("strdup");
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1208 worktree_path = getcwd(NULL, 0);
1209 if (worktree_path == NULL) {
1210 error = got_error_from_errno("getcwd");
1213 error = got_worktree_open(&worktree, worktree_path);
1217 error = check_rebase_or_histedit_in_progress(worktree);
1221 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1225 error = apply_unveil(got_repo_get_path(repo), 0,
1226 got_worktree_get_root_path(worktree));
1230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1234 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 got_worktree_get_head_ref_name(worktree), 0);
1238 if (commit_id_str == NULL) {
1239 error = got_ref_resolve(&commit_id, repo, head_ref);
1242 error = got_object_id_str(&commit_id_str, commit_id);
1246 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 free(commit_id_str);
1248 commit_id_str = NULL;
1251 error = got_object_id_str(&commit_id_str, commit_id);
1257 struct got_object_id *head_commit_id;
1258 TAILQ_FOREACH(pe, &paths, entry) {
1259 if (pe->path_len == 0)
1261 error = got_error_msg(GOT_ERR_BAD_PATH,
1262 "switching between branches requires that "
1263 "the entire work tree gets updated");
1266 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1269 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 free(head_commit_id);
1273 error = check_same_branch(commit_id, head_ref, NULL, repo);
1276 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1280 error = check_linear_ancestry(commit_id,
1281 got_worktree_get_base_commit_id(worktree), repo);
1282 if (error != NULL) {
1283 if (error->code == GOT_ERR_ANCESTRY)
1284 error = got_error(GOT_ERR_BRANCH_MOVED);
1287 error = check_same_branch(commit_id, head_ref, NULL, repo);
1292 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1294 error = got_worktree_set_base_commit_id(worktree, repo,
1300 error = got_worktree_checkout_files(worktree, &paths, repo,
1301 update_progress, &did_something, check_cancelled, NULL);
1306 printf("Updated to commit %s\n", commit_id_str);
1308 printf("Already up-to-date\n");
1310 free(worktree_path);
1311 TAILQ_FOREACH(pe, &paths, entry)
1312 free((char *)pe->path);
1313 got_pathlist_free(&paths);
1315 free(commit_id_str);
1319 static const struct got_error *
1320 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1321 int diff_context, struct got_repository *repo)
1323 const struct got_error *err = NULL;
1324 struct got_tree_object *tree1 = NULL, *tree2;
1325 struct got_object_qid *qid;
1326 char *id_str1 = NULL, *id_str2;
1327 struct got_diff_blob_output_unidiff_arg arg;
1329 err = got_object_open_as_tree(&tree2, repo,
1330 got_object_commit_get_tree_id(commit));
1334 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1336 struct got_commit_object *pcommit;
1338 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1342 err = got_object_open_as_tree(&tree1, repo,
1343 got_object_commit_get_tree_id(pcommit));
1344 got_object_commit_close(pcommit);
1348 err = got_object_id_str(&id_str1, qid->id);
1353 err = got_object_id_str(&id_str2, id);
1357 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1358 arg.diff_context = diff_context;
1359 arg.outfile = stdout;
1360 err = got_diff_tree(tree1, tree2, "", "", repo,
1361 got_diff_blob_output_unidiff, &arg, 1);
1364 got_object_tree_close(tree1);
1365 got_object_tree_close(tree2);
1372 get_datestr(time_t *time, char *datebuf)
1374 struct tm mytm, *tm;
1377 tm = gmtime_r(time, &mytm);
1380 s = asctime_r(tm, datebuf);
1383 p = strchr(s, '\n');
1389 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1391 static const struct got_error *
1392 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1393 struct got_repository *repo, int show_patch, int diff_context,
1394 struct got_reflist_head *refs)
1396 const struct got_error *err = NULL;
1397 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1399 time_t committer_time;
1400 const char *author, *committer;
1401 char *refs_str = NULL;
1402 struct got_reflist_entry *re;
1404 SIMPLEQ_FOREACH(re, refs, entry) {
1407 struct got_tag_object *tag = NULL;
1410 name = got_ref_get_name(re->ref);
1411 if (strcmp(name, GOT_REF_HEAD) == 0)
1413 if (strncmp(name, "refs/", 5) == 0)
1415 if (strncmp(name, "got/", 4) == 0)
1417 if (strncmp(name, "heads/", 6) == 0)
1419 if (strncmp(name, "remotes/", 8) == 0)
1421 if (strncmp(name, "tags/", 5) == 0) {
1422 err = got_object_open_as_tag(&tag, repo, re->id);
1424 if (err->code != GOT_ERR_OBJ_TYPE)
1426 /* Ref points at something other than a tag. */
1431 cmp = got_object_id_cmp(tag ?
1432 got_object_tag_get_object_id(tag) : re->id, id);
1434 got_object_tag_close(tag);
1438 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1440 err = got_error_from_errno("asprintf");
1446 err = got_object_id_str(&id_str, id);
1450 printf(GOT_COMMIT_SEP_STR);
1451 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1452 refs_str ? refs_str : "", refs_str ? ")" : "");
1457 printf("from: %s\n", got_object_commit_get_author(commit));
1458 committer_time = got_object_commit_get_committer_time(commit);
1459 datestr = get_datestr(&committer_time, datebuf);
1461 printf("date: %s UTC\n", datestr);
1462 author = got_object_commit_get_author(commit);
1463 committer = got_object_commit_get_committer(commit);
1464 if (strcmp(author, committer) != 0)
1465 printf("via: %s\n", committer);
1466 if (got_object_commit_get_nparents(commit) > 1) {
1467 const struct got_object_id_queue *parent_ids;
1468 struct got_object_qid *qid;
1470 parent_ids = got_object_commit_get_parent_ids(commit);
1471 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1472 err = got_object_id_str(&id_str, qid->id);
1475 printf("parent %d: %s\n", n++, id_str);
1480 err = got_object_commit_get_logmsg(&logmsg0, commit);
1486 line = strsep(&logmsg, "\n");
1488 printf(" %s\n", line);
1493 err = print_patch(commit, id, diff_context, repo);
1498 if (fflush(stdout) != 0 && err == NULL)
1499 err = got_error_from_errno("fflush");
1503 static const struct got_error *
1504 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1505 char *path, int show_patch, int diff_context, int limit,
1506 int first_parent_traversal, struct got_reflist_head *refs)
1508 const struct got_error *err;
1509 struct got_commit_graph *graph;
1511 err = got_commit_graph_open(&graph, root_id, path,
1512 first_parent_traversal, repo);
1515 err = got_commit_graph_iter_start(graph, root_id, repo,
1516 check_cancelled, NULL);
1520 struct got_commit_object *commit;
1521 struct got_object_id *id;
1523 if (sigint_received || sigpipe_received)
1526 err = got_commit_graph_iter_next(&id, graph);
1528 if (err->code == GOT_ERR_ITER_COMPLETED) {
1532 if (err->code != GOT_ERR_ITER_NEED_MORE)
1534 err = got_commit_graph_fetch_commits(graph, 1, repo,
1535 check_cancelled, NULL);
1544 err = got_object_open_as_commit(&commit, repo, id);
1547 err = print_commit(commit, id, repo, show_patch, diff_context,
1549 got_object_commit_close(commit);
1550 if (err || (limit && --limit == 0))
1554 got_commit_graph_close(graph);
1561 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1562 "[-r repository-path] [path]\n", getprogname());
1567 get_default_log_limit(void)
1569 const char *got_default_log_limit;
1573 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1574 if (got_default_log_limit == NULL)
1576 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1582 static const struct got_error *
1583 cmd_log(int argc, char *argv[])
1585 const struct got_error *error;
1586 struct got_repository *repo = NULL;
1587 struct got_worktree *worktree = NULL;
1588 struct got_commit_object *commit = NULL;
1589 struct got_object_id *id = NULL;
1590 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1591 char *start_commit = NULL;
1592 int diff_context = 3, ch;
1593 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1595 struct got_reflist_head refs;
1597 SIMPLEQ_INIT(&refs);
1600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1606 limit = get_default_log_limit();
1608 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1614 start_commit = optarg;
1617 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1620 err(1, "-C option %s", errstr);
1623 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1625 err(1, "-l option %s", errstr);
1628 first_parent_traversal = 1;
1631 repo_path = realpath(optarg, NULL);
1632 if (repo_path == NULL)
1633 err(1, "-r option");
1634 got_path_strip_trailing_slashes(repo_path);
1645 cwd = getcwd(NULL, 0);
1647 error = got_error_from_errno("getcwd");
1651 error = got_worktree_open(&worktree, cwd);
1652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1659 error = got_error_from_errno("strdup");
1662 } else if (argc == 1) {
1664 error = got_worktree_resolve_path(&path, worktree,
1669 path = strdup(argv[0]);
1671 error = got_error_from_errno("strdup");
1678 if (repo_path == NULL) {
1679 repo_path = worktree ?
1680 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno("strdup");
1687 error = got_repo_open(&repo, repo_path);
1691 error = apply_unveil(got_repo_get_path(repo), 1,
1692 worktree ? got_worktree_get_root_path(worktree) : NULL);
1696 if (start_commit == NULL) {
1697 struct got_reference *head_ref;
1698 error = got_ref_open(&head_ref, repo,
1699 worktree ? got_worktree_get_head_ref_name(worktree)
1703 error = got_ref_resolve(&id, repo, head_ref);
1704 got_ref_close(head_ref);
1707 error = got_object_open_as_commit(&commit, repo, id);
1709 struct got_reference *ref;
1710 error = got_ref_open(&ref, repo, start_commit, 0);
1711 if (error == NULL) {
1713 error = got_ref_resolve(&id, repo, ref);
1717 error = got_object_get_type(&obj_type, repo, id);
1720 if (obj_type == GOT_OBJ_TYPE_TAG) {
1721 struct got_tag_object *tag;
1722 error = got_object_open_as_tag(&tag, repo, id);
1725 if (got_object_tag_get_object_type(tag) !=
1726 GOT_OBJ_TYPE_COMMIT) {
1727 got_object_tag_close(tag);
1728 error = got_error(GOT_ERR_OBJ_TYPE);
1732 id = got_object_id_dup(
1733 got_object_tag_get_object_id(tag));
1735 error = got_error_from_errno(
1736 "got_object_id_dup");
1737 got_object_tag_close(tag);
1740 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1741 error = got_error(GOT_ERR_OBJ_TYPE);
1744 error = got_object_open_as_commit(&commit, repo, id);
1748 if (commit == NULL) {
1749 error = got_repo_match_object_id_prefix(&id,
1750 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1758 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1763 path = in_repo_path;
1766 error = got_ref_list(&refs, repo);
1770 error = print_commits(id, repo, path, show_patch,
1771 diff_context, limit, first_parent_traversal, &refs);
1778 got_worktree_close(worktree);
1780 const struct got_error *repo_error;
1781 repo_error = got_repo_close(repo);
1785 got_ref_list_free(&refs);
1792 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1793 "[object1 object2 | path]\n", getprogname());
1797 struct print_diff_arg {
1798 struct got_repository *repo;
1799 struct got_worktree *worktree;
1806 static const struct got_error *
1807 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1808 const char *path, struct got_object_id *blob_id,
1809 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1811 struct print_diff_arg *a = arg;
1812 const struct got_error *err = NULL;
1813 struct got_blob_object *blob1 = NULL;
1815 char *abspath = NULL, *label1 = NULL;
1818 if (a->diff_staged) {
1819 if (staged_status != GOT_STATUS_MODIFY &&
1820 staged_status != GOT_STATUS_ADD &&
1821 staged_status != GOT_STATUS_DELETE)
1824 if (staged_status == GOT_STATUS_DELETE)
1826 if (status != GOT_STATUS_MODIFY &&
1827 status != GOT_STATUS_ADD &&
1828 status != GOT_STATUS_DELETE &&
1829 status != GOT_STATUS_CONFLICT)
1833 if (!a->header_shown) {
1834 printf("diff %s %s%s\n", a->id_str,
1835 got_worktree_get_root_path(a->worktree),
1836 a->diff_staged ? " (staged changes)" : "");
1837 a->header_shown = 1;
1840 if (a->diff_staged) {
1841 const char *label1 = NULL, *label2 = NULL;
1842 switch (staged_status) {
1843 case GOT_STATUS_MODIFY:
1847 case GOT_STATUS_ADD:
1850 case GOT_STATUS_DELETE:
1854 return got_error(GOT_ERR_FILE_STATUS);
1856 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1857 label1, label2, a->diff_context, a->repo, stdout);
1860 if (staged_status == GOT_STATUS_ADD ||
1861 staged_status == GOT_STATUS_MODIFY) {
1863 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1867 err = got_object_id_str(&id_str, staged_blob_id);
1870 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1871 err = got_error_from_errno("asprintf");
1876 } else if (status != GOT_STATUS_ADD) {
1877 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1882 if (status != GOT_STATUS_DELETE) {
1883 if (asprintf(&abspath, "%s/%s",
1884 got_worktree_get_root_path(a->worktree), path) == -1) {
1885 err = got_error_from_errno("asprintf");
1889 f2 = fopen(abspath, "r");
1891 err = got_error_from_errno2("fopen", abspath);
1894 if (lstat(abspath, &sb) == -1) {
1895 err = got_error_from_errno2("lstat", abspath);
1901 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1902 a->diff_context, stdout);
1905 got_object_blob_close(blob1);
1906 if (f2 && fclose(f2) != 0 && err == NULL)
1907 err = got_error_from_errno("fclose");
1912 static const struct got_error *
1913 match_object_id(struct got_object_id **id, char **label,
1914 const char *id_str, int obj_type, int resolve_tags,
1915 struct got_repository *repo)
1917 const struct got_error *err;
1918 struct got_tag_object *tag;
1919 struct got_reference *ref = NULL;
1925 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1928 *id = got_object_id_dup(
1929 got_object_tag_get_object_id(tag));
1931 err = got_error_from_errno("got_object_id_dup");
1932 else if (asprintf(label, "refs/tags/%s",
1933 got_object_tag_get_name(tag)) == -1) {
1934 err = got_error_from_errno("asprintf");
1938 got_object_tag_close(tag);
1940 } else if (err->code != GOT_ERR_NO_OBJ)
1944 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1946 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1948 err = got_ref_open(&ref, repo, id_str, 0);
1951 *label = strdup(got_ref_get_name(ref));
1952 if (*label == NULL) {
1953 err = got_error_from_errno("strdup");
1956 err = got_ref_resolve(id, repo, ref);
1958 err = got_object_id_str(label, *id);
1959 if (*label == NULL) {
1960 err = got_error_from_errno("strdup");
1971 static const struct got_error *
1972 cmd_diff(int argc, char *argv[])
1974 const struct got_error *error;
1975 struct got_repository *repo = NULL;
1976 struct got_worktree *worktree = NULL;
1977 char *cwd = NULL, *repo_path = NULL;
1978 struct got_object_id *id1 = NULL, *id2 = NULL;
1979 const char *id_str1 = NULL, *id_str2 = NULL;
1980 char *label1 = NULL, *label2 = NULL;
1982 int diff_context = 3, diff_staged = 0, ch;
1987 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1992 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1995 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1997 err(1, "-C option %s", errstr);
2000 repo_path = realpath(optarg, NULL);
2001 if (repo_path == NULL)
2002 err(1, "-r option");
2003 got_path_strip_trailing_slashes(repo_path);
2017 cwd = getcwd(NULL, 0);
2019 error = got_error_from_errno("getcwd");
2022 error = got_worktree_open(&worktree, cwd);
2023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2026 if (worktree == NULL) {
2027 error = got_error(GOT_ERR_NOT_WORKTREE);
2032 "-r option can't be used when diffing a work tree");
2033 repo_path = strdup(got_worktree_get_repo_path(worktree));
2034 if (repo_path == NULL) {
2035 error = got_error_from_errno("strdup");
2039 error = got_worktree_resolve_path(&path, worktree,
2046 error = got_error_from_errno("strdup");
2050 } else if (argc == 2) {
2052 errx(1, "-s option can't be used when diffing "
2053 "objects in repository");
2056 if (worktree && repo_path == NULL) {
2058 strdup(got_worktree_get_repo_path(worktree));
2059 if (repo_path == NULL) {
2060 error = got_error_from_errno("strdup");
2067 if (repo_path == NULL) {
2068 repo_path = getcwd(NULL, 0);
2069 if (repo_path == NULL)
2070 return got_error_from_errno("getcwd");
2073 error = got_repo_open(&repo, repo_path);
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 worktree ? got_worktree_get_root_path(worktree) : NULL);
2084 struct print_diff_arg arg;
2085 struct got_pathlist_head paths;
2090 error = got_object_id_str(&id_str,
2091 got_worktree_get_base_commit_id(worktree));
2095 arg.worktree = worktree;
2096 arg.diff_context = diff_context;
2097 arg.id_str = id_str;
2098 arg.header_shown = 0;
2099 arg.diff_staged = diff_staged;
2101 error = got_pathlist_append(&paths, path, NULL);
2105 error = got_worktree_status(worktree, &paths, repo, print_diff,
2106 &arg, check_cancelled, NULL);
2108 got_pathlist_free(&paths);
2112 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2117 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2122 error = got_object_get_type(&type1, repo, id1);
2126 error = got_object_get_type(&type2, repo, id2);
2130 if (type1 != type2) {
2131 error = got_error(GOT_ERR_OBJ_TYPE);
2136 case GOT_OBJ_TYPE_BLOB:
2137 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2138 diff_context, repo, stdout);
2140 case GOT_OBJ_TYPE_TREE:
2141 error = got_diff_objects_as_trees(id1, id2, "", "",
2142 diff_context, repo, stdout);
2144 case GOT_OBJ_TYPE_COMMIT:
2145 printf("diff %s %s\n", label1, label2);
2146 error = got_diff_objects_as_commits(id1, id2, diff_context,
2150 error = got_error(GOT_ERR_OBJ_TYPE);
2160 got_worktree_close(worktree);
2162 const struct got_error *repo_error;
2163 repo_error = got_repo_close(repo);
2174 "usage: %s blame [-c commit] [-r repository-path] path\n",
2183 char datebuf[9]; /* YY-MM-DD + NUL */
2186 struct blame_cb_args {
2187 struct blame_line *lines;
2191 off_t *line_offsets;
2193 struct got_repository *repo;
2196 static const struct got_error *
2197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2199 const struct got_error *err = NULL;
2200 struct blame_cb_args *a = arg;
2201 struct blame_line *bline;
2203 size_t linesize = 0;
2204 struct got_commit_object *commit = NULL;
2207 time_t committer_time;
2209 if (nlines != a->nlines ||
2210 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2211 return got_error(GOT_ERR_RANGE);
2213 if (sigint_received)
2214 return got_error(GOT_ERR_ITER_COMPLETED);
2217 return NULL; /* no change in this commit */
2219 /* Annotate this line. */
2220 bline = &a->lines[lineno - 1];
2221 if (bline->annotated)
2223 err = got_object_id_str(&bline->id_str, id);
2227 err = got_object_open_as_commit(&commit, a->repo, id);
2231 bline->committer = strdup(got_object_commit_get_committer(commit));
2232 if (bline->committer == NULL) {
2233 err = got_error_from_errno("strdup");
2237 committer_time = got_object_commit_get_committer_time(commit);
2238 if (localtime_r(&committer_time, &tm) == NULL)
2239 return got_error_from_errno("localtime_r");
2240 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2241 &tm) >= sizeof(bline->datebuf)) {
2242 err = got_error(GOT_ERR_NO_SPACE);
2245 bline->annotated = 1;
2247 /* Print lines annotated so far. */
2248 bline = &a->lines[a->lineno_cur - 1];
2249 if (!bline->annotated)
2252 offset = a->line_offsets[a->lineno_cur - 1];
2253 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2254 err = got_error_from_errno("fseeko");
2258 while (bline->annotated) {
2259 char *smallerthan, *at, *nl, *committer;
2262 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2264 err = got_error_from_errno("getline");
2268 committer = bline->committer;
2269 smallerthan = strchr(committer, '<');
2270 if (smallerthan && smallerthan[1] != '\0')
2271 committer = smallerthan + 1;
2272 at = strchr(committer, '@');
2275 len = strlen(committer);
2277 committer[8] = '\0';
2279 nl = strchr(line, '\n');
2282 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2283 bline->id_str, bline->datebuf, committer, line);
2286 bline = &a->lines[a->lineno_cur - 1];
2290 got_object_commit_close(commit);
2295 static const struct got_error *
2296 cmd_blame(int argc, char *argv[])
2298 const struct got_error *error;
2299 struct got_repository *repo = NULL;
2300 struct got_worktree *worktree = NULL;
2301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2302 struct got_object_id *obj_id = NULL;
2303 struct got_object_id *commit_id = NULL;
2304 struct got_blob_object *blob = NULL;
2305 char *commit_id_str = NULL;
2306 struct blame_cb_args bca;
2307 int ch, obj_type, i;
2310 memset(&bca, 0, sizeof(bca));
2313 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2318 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2321 commit_id_str = optarg;
2324 repo_path = realpath(optarg, NULL);
2325 if (repo_path == NULL)
2326 err(1, "-r option");
2327 got_path_strip_trailing_slashes(repo_path);
2343 cwd = getcwd(NULL, 0);
2345 error = got_error_from_errno("getcwd");
2348 if (repo_path == NULL) {
2349 error = got_worktree_open(&worktree, cwd);
2350 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2356 strdup(got_worktree_get_repo_path(worktree));
2357 if (repo_path == NULL)
2358 error = got_error_from_errno("strdup");
2362 repo_path = strdup(cwd);
2363 if (repo_path == NULL) {
2364 error = got_error_from_errno("strdup");
2370 error = got_repo_open(&repo, repo_path);
2374 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2379 const char *prefix = got_worktree_get_path_prefix(worktree);
2380 char *p, *worktree_subdir = cwd +
2381 strlen(got_worktree_get_root_path(worktree));
2382 if (asprintf(&p, "%s%s%s%s%s",
2383 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2384 worktree_subdir, worktree_subdir[0] ? "/" : "",
2386 error = got_error_from_errno("asprintf");
2389 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2392 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2397 if (commit_id_str == NULL) {
2398 struct got_reference *head_ref;
2399 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2402 error = got_ref_resolve(&commit_id, repo, head_ref);
2403 got_ref_close(head_ref);
2407 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2412 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2415 if (obj_id == NULL) {
2416 error = got_error(GOT_ERR_NO_OBJ);
2420 error = got_object_get_type(&obj_type, repo, obj_id);
2424 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2425 error = got_error(GOT_ERR_OBJ_TYPE);
2429 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2432 bca.f = got_opentemp();
2433 if (bca.f == NULL) {
2434 error = got_error_from_errno("got_opentemp");
2437 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2438 &bca.line_offsets, bca.f, blob);
2439 if (error || bca.nlines == 0)
2442 /* Don't include \n at EOF in the blame line count. */
2443 if (bca.line_offsets[bca.nlines - 1] == filesize)
2446 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2447 if (bca.lines == NULL) {
2448 error = got_error_from_errno("calloc");
2452 bca.nlines_prec = 0;
2460 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2461 check_cancelled, NULL);
2471 got_object_blob_close(blob);
2473 got_worktree_close(worktree);
2475 const struct got_error *repo_error;
2476 repo_error = got_repo_close(repo);
2480 for (i = 0; i < bca.nlines; i++) {
2481 struct blame_line *bline = &bca.lines[i];
2482 free(bline->id_str);
2483 free(bline->committer);
2486 free(bca.line_offsets);
2487 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2488 error = got_error_from_errno("fclose");
2496 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2502 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2503 const char *root_path)
2505 int is_root_path = (strcmp(path, root_path) == 0);
2506 const char *modestr = "";
2508 path += strlen(root_path);
2509 while (path[0] == '/')
2512 if (S_ISLNK(te->mode))
2514 else if (S_ISDIR(te->mode))
2516 else if (te->mode & S_IXUSR)
2519 printf("%s%s%s%s%s\n", id ? id : "", path,
2520 is_root_path ? "" : "/", te->name, modestr);
2523 static const struct got_error *
2524 print_tree(const char *path, struct got_object_id *commit_id,
2525 int show_ids, int recurse, const char *root_path,
2526 struct got_repository *repo)
2528 const struct got_error *err = NULL;
2529 struct got_object_id *tree_id = NULL;
2530 struct got_tree_object *tree = NULL;
2531 const struct got_tree_entries *entries;
2532 struct got_tree_entry *te;
2534 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2538 err = got_object_open_as_tree(&tree, repo, tree_id);
2541 entries = got_object_tree_get_entries(tree);
2542 te = SIMPLEQ_FIRST(&entries->head);
2546 if (sigint_received || sigpipe_received)
2551 err = got_object_id_str(&id_str, te->id);
2554 if (asprintf(&id, "%s ", id_str) == -1) {
2555 err = got_error_from_errno("asprintf");
2561 print_entry(te, id, path, root_path);
2564 if (recurse && S_ISDIR(te->mode)) {
2566 if (asprintf(&child_path, "%s%s%s", path,
2567 path[0] == '/' && path[1] == '\0' ? "" : "/",
2569 err = got_error_from_errno("asprintf");
2572 err = print_tree(child_path, commit_id, show_ids, 1,
2579 te = SIMPLEQ_NEXT(te, entry);
2583 got_object_tree_close(tree);
2588 static const struct got_error *
2589 cmd_tree(int argc, char *argv[])
2591 const struct got_error *error;
2592 struct got_repository *repo = NULL;
2593 struct got_worktree *worktree = NULL;
2595 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2596 struct got_object_id *commit_id = NULL;
2597 char *commit_id_str = NULL;
2598 int show_ids = 0, recurse = 0;
2602 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2607 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2610 commit_id_str = optarg;
2613 repo_path = realpath(optarg, NULL);
2614 if (repo_path == NULL)
2615 err(1, "-r option");
2616 got_path_strip_trailing_slashes(repo_path);
2640 cwd = getcwd(NULL, 0);
2642 error = got_error_from_errno("getcwd");
2645 if (repo_path == NULL) {
2646 error = got_worktree_open(&worktree, cwd);
2647 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2653 strdup(got_worktree_get_repo_path(worktree));
2654 if (repo_path == NULL)
2655 error = got_error_from_errno("strdup");
2659 repo_path = strdup(cwd);
2660 if (repo_path == NULL) {
2661 error = got_error_from_errno("strdup");
2667 error = got_repo_open(&repo, repo_path);
2671 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2677 char *p, *worktree_subdir = cwd +
2678 strlen(got_worktree_get_root_path(worktree));
2679 if (asprintf(&p, "%s/%s",
2680 got_worktree_get_path_prefix(worktree),
2681 worktree_subdir) == -1) {
2682 error = got_error_from_errno("asprintf");
2685 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2692 if (in_repo_path == NULL) {
2693 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2698 if (commit_id_str == NULL) {
2699 struct got_reference *head_ref;
2700 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2703 error = got_ref_resolve(&commit_id, repo, head_ref);
2704 got_ref_close(head_ref);
2708 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2713 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2714 in_repo_path, repo);
2721 got_worktree_close(worktree);
2723 const struct got_error *repo_error;
2724 repo_error = got_repo_close(repo);
2734 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2738 static const struct got_error *
2739 print_status(void *arg, unsigned char status, unsigned char staged_status,
2740 const char *path, struct got_object_id *blob_id,
2741 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2743 if (status == staged_status && (status == GOT_STATUS_DELETE))
2744 status = GOT_STATUS_NO_CHANGE;
2745 printf("%c%c %s\n", status, staged_status, path);
2749 static const struct got_error *
2750 cmd_status(int argc, char *argv[])
2752 const struct got_error *error = NULL;
2753 struct got_repository *repo = NULL;
2754 struct got_worktree *worktree = NULL;
2756 struct got_pathlist_head paths;
2757 struct got_pathlist_entry *pe;
2762 while ((ch = getopt(argc, argv, "")) != -1) {
2774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2778 cwd = getcwd(NULL, 0);
2780 error = got_error_from_errno("getcwd");
2784 error = got_worktree_open(&worktree, cwd);
2788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2792 error = apply_unveil(got_repo_get_path(repo), 1,
2793 got_worktree_get_root_path(worktree));
2797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2801 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2802 check_cancelled, NULL);
2804 TAILQ_FOREACH(pe, &paths, entry)
2805 free((char *)pe->path);
2806 got_pathlist_free(&paths);
2815 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2820 static const struct got_error *
2821 list_refs(struct got_repository *repo)
2823 static const struct got_error *err = NULL;
2824 struct got_reflist_head refs;
2825 struct got_reflist_entry *re;
2827 SIMPLEQ_INIT(&refs);
2828 err = got_ref_list(&refs, repo);
2832 SIMPLEQ_FOREACH(re, &refs, entry) {
2834 refstr = got_ref_to_str(re->ref);
2836 return got_error_from_errno("got_ref_to_str");
2837 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2841 got_ref_list_free(&refs);
2845 static const struct got_error *
2846 delete_ref(struct got_repository *repo, const char *refname)
2848 const struct got_error *err = NULL;
2849 struct got_reference *ref;
2851 err = got_ref_open(&ref, repo, refname, 0);
2855 err = got_ref_delete(ref, repo);
2860 static const struct got_error *
2861 add_ref(struct got_repository *repo, const char *refname, const char *target)
2863 const struct got_error *err = NULL;
2864 struct got_object_id *id;
2865 struct got_reference *ref = NULL;
2868 * Don't let the user create a reference named '-'.
2869 * While technically a valid reference name, this case is usually
2870 * an unintended typo.
2872 if (refname[0] == '-' && refname[1] == '\0')
2873 return got_error(GOT_ERR_BAD_REF_NAME);
2875 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2878 struct got_reference *target_ref;
2880 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2882 err = got_ref_open(&target_ref, repo, target, 0);
2885 err = got_ref_resolve(&id, repo, target_ref);
2886 got_ref_close(target_ref);
2891 err = got_ref_alloc(&ref, refname, id);
2895 err = got_ref_write(ref, repo);
2903 static const struct got_error *
2904 add_symref(struct got_repository *repo, const char *refname, const char *target)
2906 const struct got_error *err = NULL;
2907 struct got_reference *ref = NULL;
2908 struct got_reference *target_ref = NULL;
2911 * Don't let the user create a reference named '-'.
2912 * While technically a valid reference name, this case is usually
2913 * an unintended typo.
2915 if (refname[0] == '-' && refname[1] == '\0')
2916 return got_error(GOT_ERR_BAD_REF_NAME);
2918 err = got_ref_open(&target_ref, repo, target, 0);
2922 err = got_ref_alloc_symref(&ref, refname, target_ref);
2926 err = got_ref_write(ref, repo);
2929 got_ref_close(target_ref);
2935 static const struct got_error *
2936 cmd_ref(int argc, char *argv[])
2938 const struct got_error *error = NULL;
2939 struct got_repository *repo = NULL;
2940 struct got_worktree *worktree = NULL;
2941 char *cwd = NULL, *repo_path = NULL;
2942 int ch, do_list = 0, create_symref = 0;
2943 const char *delref = NULL;
2945 /* TODO: Add -s option for adding symbolic references. */
2946 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2952 repo_path = realpath(optarg, NULL);
2953 if (repo_path == NULL)
2954 err(1, "-r option");
2955 got_path_strip_trailing_slashes(repo_path);
2969 if (do_list && delref)
2970 errx(1, "-l and -d options are mutually exclusive\n");
2975 if (do_list || delref) {
2977 errx(1, "-s option cannot be used together with the "
2978 "-l or -d options");
2981 } else if (argc != 2)
2986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2990 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2991 "sendfd unveil", NULL) == -1)
2995 cwd = getcwd(NULL, 0);
2997 error = got_error_from_errno("getcwd");
3001 if (repo_path == NULL) {
3002 error = got_worktree_open(&worktree, cwd);
3003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3009 strdup(got_worktree_get_repo_path(worktree));
3010 if (repo_path == NULL)
3011 error = got_error_from_errno("strdup");
3015 repo_path = strdup(cwd);
3016 if (repo_path == NULL) {
3017 error = got_error_from_errno("strdup");
3023 error = got_repo_open(&repo, repo_path);
3027 error = apply_unveil(got_repo_get_path(repo), do_list,
3028 worktree ? got_worktree_get_root_path(worktree) : NULL);
3033 error = list_refs(repo);
3035 error = delete_ref(repo, delref);
3036 else if (create_symref)
3037 error = add_symref(repo, argv[0], argv[1]);
3039 error = add_ref(repo, argv[0], argv[1]);
3042 got_repo_close(repo);
3044 got_worktree_close(worktree);
3054 "usage: %s branch [-r repository] -l | -d name | "
3055 "name [base-branch]\n", getprogname());
3059 static const struct got_error *
3060 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3062 static const struct got_error *err = NULL;
3063 struct got_reflist_head refs;
3064 struct got_reflist_entry *re;
3066 SIMPLEQ_INIT(&refs);
3068 err = got_ref_list(&refs, repo);
3072 SIMPLEQ_FOREACH(re, &refs, entry) {
3073 const char *refname, *marker = " ";
3075 refname = got_ref_get_name(re->ref);
3076 if (strncmp(refname, "refs/heads/", 11) != 0)
3078 if (worktree && strcmp(refname,
3079 got_worktree_get_head_ref_name(worktree)) == 0) {
3080 struct got_object_id *id = NULL;
3081 err = got_ref_resolve(&id, repo, re->ref);
3084 if (got_object_id_cmp(id,
3085 got_worktree_get_base_commit_id(worktree)) == 0)
3092 refstr = got_ref_to_str(re->ref);
3094 return got_error_from_errno("got_ref_to_str");
3095 printf("%s%s: %s\n", marker, refname, refstr);
3099 got_ref_list_free(&refs);
3103 static const struct got_error *
3104 delete_branch(struct got_repository *repo, const char *branch_name)
3106 const struct got_error *err = NULL;
3107 struct got_reference *ref;
3110 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = got_ref_open(&ref, repo, refname, 0);
3117 err = got_ref_delete(ref, repo);
3124 static const struct got_error *
3125 add_branch(struct got_repository *repo, const char *branch_name,
3126 const char *base_branch)
3128 const struct got_error *err = NULL;
3129 struct got_object_id *id = NULL;
3130 struct got_reference *ref = NULL;
3131 char *base_refname = NULL, *refname = NULL;
3132 struct got_reference *base_ref;
3135 * Don't let the user create a branch named '-'.
3136 * While technically a valid reference name, this case is usually
3137 * an unintended typo.
3139 if (branch_name[0] == '-' && branch_name[1] == '\0')
3140 return got_error(GOT_ERR_BAD_REF_NAME);
3142 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3143 base_refname = strdup(GOT_REF_HEAD);
3144 if (base_refname == NULL)
3145 return got_error_from_errno("strdup");
3146 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3147 return got_error_from_errno("asprintf");
3149 err = got_ref_open(&base_ref, repo, base_refname, 0);
3152 err = got_ref_resolve(&id, repo, base_ref);
3153 got_ref_close(base_ref);
3157 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3158 err = got_error_from_errno("asprintf");
3162 err = got_ref_open(&ref, repo, refname, 0);
3164 err = got_error(GOT_ERR_BRANCH_EXISTS);
3166 } else if (err->code != GOT_ERR_NOT_REF)
3169 err = got_ref_alloc(&ref, refname, id);
3173 err = got_ref_write(ref, repo);
3183 static const struct got_error *
3184 cmd_branch(int argc, char *argv[])
3186 const struct got_error *error = NULL;
3187 struct got_repository *repo = NULL;
3188 struct got_worktree *worktree = NULL;
3189 char *cwd = NULL, *repo_path = NULL;
3190 int ch, do_list = 0;
3191 const char *delref = NULL;
3193 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3199 repo_path = realpath(optarg, NULL);
3200 if (repo_path == NULL)
3201 err(1, "-r option");
3202 got_path_strip_trailing_slashes(repo_path);
3213 if (do_list && delref)
3214 errx(1, "-l and -d options are mutually exclusive\n");
3219 if (do_list || delref) {
3222 } else if (argc < 1 || argc > 2)
3227 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3231 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3232 "sendfd unveil", NULL) == -1)
3236 cwd = getcwd(NULL, 0);
3238 error = got_error_from_errno("getcwd");
3242 if (repo_path == NULL) {
3243 error = got_worktree_open(&worktree, cwd);
3244 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3250 strdup(got_worktree_get_repo_path(worktree));
3251 if (repo_path == NULL)
3252 error = got_error_from_errno("strdup");
3256 repo_path = strdup(cwd);
3257 if (repo_path == NULL) {
3258 error = got_error_from_errno("strdup");
3264 error = got_repo_open(&repo, repo_path);
3268 error = apply_unveil(got_repo_get_path(repo), do_list,
3269 worktree ? got_worktree_get_root_path(worktree) : NULL);
3274 error = list_branches(repo, worktree);
3276 error = delete_branch(repo, delref);
3278 const char *base_branch;
3280 base_branch = worktree ?
3281 got_worktree_get_head_ref_name(worktree) :
3283 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3286 base_branch = argv[1];
3287 error = add_branch(repo, argv[0], base_branch);
3291 got_repo_close(repo);
3293 got_worktree_close(worktree);
3304 "usage: %s tag [-r repository] -l | -d name | "
3305 "[-m message] name [commit]\n", getprogname());
3309 static const struct got_error *
3310 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3312 static const struct got_error *err = NULL;
3313 struct got_reflist_head refs;
3314 struct got_reflist_entry *re;
3316 SIMPLEQ_INIT(&refs);
3318 err = got_ref_list(&refs, repo);
3322 SIMPLEQ_FOREACH(re, &refs, entry) {
3323 const char *refname;
3324 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3327 struct got_object_id *id;
3328 struct got_tag_object *tag;
3330 refname = got_ref_get_name(re->ref);
3331 if (strncmp(refname, "refs/tags/", 10) != 0)
3334 refstr = got_ref_to_str(re->ref);
3335 if (refstr == NULL) {
3336 err = got_error_from_errno("got_ref_to_str");
3339 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3342 err = got_ref_resolve(&id, repo, re->ref);
3345 err = got_object_open_as_tag(&tag, repo, id);
3349 err = got_object_id_str(&id_str,
3350 got_object_tag_get_object_id(tag));
3353 switch (got_object_tag_get_object_type(tag)) {
3354 case GOT_OBJ_TYPE_BLOB:
3355 printf("%s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3357 case GOT_OBJ_TYPE_TREE:
3358 printf("%s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3360 case GOT_OBJ_TYPE_COMMIT:
3361 printf("%s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3363 case GOT_OBJ_TYPE_TAG:
3364 printf("%s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3370 printf("from: %s\n", got_object_tag_get_tagger(tag));
3371 tagger_time = got_object_tag_get_tagger_time(tag);
3372 datestr = get_datestr(&tagger_time, datebuf);
3374 printf("date: %s UTC\n", datestr);
3375 tagmsg0 = strdup(got_object_tag_get_message(tag));
3376 got_object_tag_close(tag);
3377 if (tagmsg0 == NULL) {
3378 err = got_error_from_errno("strdup");
3384 line = strsep(&tagmsg, "\n");
3386 printf(" %s\n", line);
3391 got_ref_list_free(&refs);
3395 static const struct got_error *
3396 get_tag_message(char **tagmsg, const char *commit_id_str, const char *repo_path)
3398 const struct got_error *err = NULL;
3399 char *template = NULL, *initial_content = NULL;
3400 char *tagmsg_path = NULL, *editor = NULL;
3403 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3404 err = got_error_from_errno("asprintf");
3408 if (asprintf(&initial_content, "\n# tagging commit %s\n",
3409 commit_id_str) == -1) {
3410 err = got_error_from_errno("asprintf");
3414 err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3418 dprintf(fd, initial_content);
3421 err = get_editor(&editor);
3424 err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3426 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3427 unlink(tagmsg_path);
3431 free(initial_content);
3435 /* Editor is done; we can now apply unveil(2) */
3437 err = apply_unveil(repo_path, 0, NULL);
3446 static const struct got_error *
3447 add_tag(struct got_repository *repo, const char *tag_name,
3448 const char *commit_arg, const char *tagmsg_arg)
3450 const struct got_error *err = NULL;
3451 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3452 char *label = NULL, *commit_id_str = NULL;
3453 struct got_reference *ref = NULL;
3454 char *refname = NULL, *tagmsg = NULL;
3458 * Don't let the user create a tag named '-'.
3459 * While technically a valid reference name, this case is usually
3460 * an unintended typo.
3462 if (tag_name[0] == '-' && tag_name[1] == '\0')
3463 return got_error(GOT_ERR_BAD_REF_NAME);
3465 err = get_author(&tagger);
3469 err = match_object_id(&commit_id, &label, commit_arg,
3470 GOT_OBJ_TYPE_COMMIT, 1, repo);
3474 err = got_object_id_str(&commit_id_str, commit_id);
3478 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3479 refname = strdup(tag_name);
3480 if (refname == NULL) {
3481 err = got_error_from_errno("strdup");
3485 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3486 err = got_error_from_errno("asprintf");
3490 err = got_ref_open(&ref, repo, refname, 0);
3492 err = got_error(GOT_ERR_TAG_EXISTS);
3494 } else if (err->code != GOT_ERR_NOT_REF)
3497 if (tagmsg_arg == NULL) {
3498 err = get_tag_message(&tagmsg, commit_id_str,
3499 got_repo_get_path(repo));
3504 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3505 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3509 err = got_ref_alloc(&ref, refname, tag_id);
3513 err = got_ref_write(ref, repo);
3517 err = got_object_id_str(&tag_id_str, tag_id);
3518 printf("Created tag %s\n", tag_id_str);
3525 free(commit_id_str);
3531 static const struct got_error *
3532 cmd_tag(int argc, char *argv[])
3534 const struct got_error *error = NULL;
3535 struct got_repository *repo = NULL;
3536 struct got_worktree *worktree = NULL;
3537 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3538 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3539 int ch, do_list = 0;
3541 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3547 repo_path = realpath(optarg, NULL);
3548 if (repo_path == NULL)
3549 err(1, "-r option");
3550 got_path_strip_trailing_slashes(repo_path);
3566 errx(1, "-l and -m options are mutually exclusive\n");
3569 } else if (argc < 1 || argc > 2)
3574 commit_id_arg = argv[1];
3579 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3583 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3584 "sendfd unveil", NULL) == -1)
3588 cwd = getcwd(NULL, 0);
3590 error = got_error_from_errno("getcwd");
3594 if (repo_path == NULL) {
3595 error = got_worktree_open(&worktree, cwd);
3596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3602 strdup(got_worktree_get_repo_path(worktree));
3603 if (repo_path == NULL)
3604 error = got_error_from_errno("strdup");
3608 repo_path = strdup(cwd);
3609 if (repo_path == NULL) {
3610 error = got_error_from_errno("strdup");
3616 error = got_repo_open(&repo, repo_path);
3622 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3625 error = list_tags(repo, worktree);
3628 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3633 if (commit_id_arg == NULL) {
3634 struct got_reference *head_ref;
3635 struct got_object_id *commit_id;
3636 error = got_ref_open(&head_ref, repo,
3637 worktree ? got_worktree_get_head_ref_name(worktree)
3641 error = got_ref_resolve(&commit_id, repo, head_ref);
3642 got_ref_close(head_ref);
3645 error = got_object_id_str(&commit_id_str, commit_id);
3651 error = add_tag(repo, tag_name,
3652 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3656 got_repo_close(repo);
3658 got_worktree_close(worktree);
3661 free(commit_id_str);
3668 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3672 static const struct got_error *
3673 cmd_add(int argc, char *argv[])
3675 const struct got_error *error = NULL;
3676 struct got_repository *repo = NULL;
3677 struct got_worktree *worktree = NULL;
3679 struct got_pathlist_head paths;
3680 struct got_pathlist_entry *pe;
3685 while ((ch = getopt(argc, argv, "")) != -1) {
3697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3704 cwd = getcwd(NULL, 0);
3706 error = got_error_from_errno("getcwd");
3710 error = got_worktree_open(&worktree, cwd);
3714 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3718 error = apply_unveil(got_repo_get_path(repo), 1,
3719 got_worktree_get_root_path(worktree));
3723 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3727 error = got_worktree_schedule_add(worktree, &paths, print_status,
3731 got_repo_close(repo);
3733 got_worktree_close(worktree);
3734 TAILQ_FOREACH(pe, &paths, entry)
3735 free((char *)pe->path);
3736 got_pathlist_free(&paths);
3744 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3748 static const struct got_error *
3749 cmd_remove(int argc, char *argv[])
3751 const struct got_error *error = NULL;
3752 struct got_worktree *worktree = NULL;
3753 struct got_repository *repo = NULL;
3755 struct got_pathlist_head paths;
3756 struct got_pathlist_entry *pe;
3757 int ch, delete_local_mods = 0;
3761 while ((ch = getopt(argc, argv, "f")) != -1) {
3764 delete_local_mods = 1;
3776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3783 cwd = getcwd(NULL, 0);
3785 error = got_error_from_errno("getcwd");
3788 error = got_worktree_open(&worktree, cwd);
3792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3796 error = apply_unveil(got_repo_get_path(repo), 1,
3797 got_worktree_get_root_path(worktree));
3801 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3805 error = got_worktree_schedule_delete(worktree, &paths,
3806 delete_local_mods, print_status, NULL, repo);
3811 got_repo_close(repo);
3813 got_worktree_close(worktree);
3814 TAILQ_FOREACH(pe, &paths, entry)
3815 free((char *)pe->path);
3816 got_pathlist_free(&paths);
3824 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3825 "path ...\n", getprogname());
3829 static const struct got_error *
3830 revert_progress(void *arg, unsigned char status, const char *path)
3832 while (path[0] == '/')
3834 printf("%c %s\n", status, path);
3838 struct choose_patch_arg {
3839 FILE *patch_script_file;
3843 static const struct got_error *
3844 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3845 int nchanges, const char *action)
3848 size_t linesize = 0;
3852 case GOT_STATUS_ADD:
3853 printf("A %s\n%s this addition? [y/n] ", path, action);
3855 case GOT_STATUS_DELETE:
3856 printf("D %s\n%s this deletion? [y/n] ", path, action);
3858 case GOT_STATUS_MODIFY:
3859 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3860 return got_error_from_errno("fseek");
3861 printf(GOT_COMMIT_SEP_STR);
3862 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3864 if (ferror(patch_file))
3865 return got_error_from_errno("getline");
3866 printf(GOT_COMMIT_SEP_STR);
3867 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3868 path, n, nchanges, action);
3871 return got_error_path(path, GOT_ERR_FILE_STATUS);
3877 static const struct got_error *
3878 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3879 FILE *patch_file, int n, int nchanges)
3881 const struct got_error *err = NULL;
3883 size_t linesize = 0;
3886 struct choose_patch_arg *a = arg;
3888 *choice = GOT_PATCH_CHOICE_NONE;
3890 if (a->patch_script_file) {
3892 err = show_change(status, path, patch_file, n, nchanges,
3896 linelen = getline(&line, &linesize, a->patch_script_file);
3897 if (linelen == -1) {
3898 if (ferror(a->patch_script_file))
3899 return got_error_from_errno("getline");
3902 nl = strchr(line, '\n');
3905 if (strcmp(line, "y") == 0) {
3906 *choice = GOT_PATCH_CHOICE_YES;
3908 } else if (strcmp(line, "n") == 0) {
3909 *choice = GOT_PATCH_CHOICE_NO;
3911 } else if (strcmp(line, "q") == 0 &&
3912 status == GOT_STATUS_MODIFY) {
3913 *choice = GOT_PATCH_CHOICE_QUIT;
3916 printf("invalid response '%s'\n", line);
3921 while (resp != 'y' && resp != 'n' && resp != 'q') {
3922 err = show_change(status, path, patch_file, n, nchanges,
3929 if (status == GOT_STATUS_MODIFY) {
3930 if (resp != 'y' && resp != 'n' && resp != 'q') {
3931 printf("invalid response '%c'\n", resp);
3934 } else if (resp != 'y' && resp != 'n') {
3935 printf("invalid response '%c'\n", resp);
3941 *choice = GOT_PATCH_CHOICE_YES;
3942 else if (resp == 'n')
3943 *choice = GOT_PATCH_CHOICE_NO;
3944 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3945 *choice = GOT_PATCH_CHOICE_QUIT;
3951 static const struct got_error *
3952 cmd_revert(int argc, char *argv[])
3954 const struct got_error *error = NULL;
3955 struct got_worktree *worktree = NULL;
3956 struct got_repository *repo = NULL;
3957 char *cwd = NULL, *path = NULL;
3958 struct got_pathlist_head paths;
3959 struct got_pathlist_entry *pe;
3960 int ch, can_recurse = 0, pflag = 0;
3961 FILE *patch_script_file = NULL;
3962 const char *patch_script_path = NULL;
3963 struct choose_patch_arg cpa;
3967 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3973 patch_script_path = optarg;
3988 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3989 "unveil", NULL) == -1)
3994 if (patch_script_path && !pflag)
3995 errx(1, "-F option can only be used together with -p option");
3997 cwd = getcwd(NULL, 0);
3999 error = got_error_from_errno("getcwd");
4002 error = got_worktree_open(&worktree, cwd);
4006 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4010 if (patch_script_path) {
4011 patch_script_file = fopen(patch_script_path, "r");
4012 if (patch_script_file == NULL) {
4013 error = got_error_from_errno2("fopen",
4018 error = apply_unveil(got_repo_get_path(repo), 1,
4019 got_worktree_get_root_path(worktree));
4023 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4030 TAILQ_FOREACH(pe, &paths, entry) {
4031 if (asprintf(&ondisk_path, "%s/%s",
4032 got_worktree_get_root_path(worktree),
4034 error = got_error_from_errno("asprintf");
4037 if (lstat(ondisk_path, &sb) == -1) {
4038 if (errno == ENOENT) {
4042 error = got_error_from_errno2("lstat",
4048 if (S_ISDIR(sb.st_mode)) {
4049 error = got_error_msg(GOT_ERR_BAD_PATH,
4050 "reverting directories requires -R option");
4056 cpa.patch_script_file = patch_script_file;
4057 cpa.action = "revert";
4058 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4059 pflag ? choose_patch : NULL, &cpa, repo);
4063 if (patch_script_file && fclose(patch_script_file) == EOF &&
4065 error = got_error_from_errno2("fclose", patch_script_path);
4067 got_repo_close(repo);
4069 got_worktree_close(worktree);
4078 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4083 struct collect_commit_logmsg_arg {
4084 const char *cmdline_log;
4086 const char *worktree_path;
4087 const char *branch_name;
4088 const char *repo_path;
4093 static const struct got_error *
4094 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4097 char *initial_content = NULL;
4098 struct got_pathlist_entry *pe;
4099 const struct got_error *err = NULL;
4100 char *template = NULL;
4101 struct collect_commit_logmsg_arg *a = arg;
4105 /* if a message was specified on the command line, just use it */
4106 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4107 len = strlen(a->cmdline_log) + 1;
4108 *logmsg = malloc(len + 1);
4109 if (*logmsg == NULL)
4110 return got_error_from_errno("malloc");
4111 strlcpy(*logmsg, a->cmdline_log, len);
4115 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4116 return got_error_from_errno("asprintf");
4118 if (asprintf(&initial_content,
4119 "\n# changes to be committed on branch %s:\n",
4120 a->branch_name) == -1)
4121 return got_error_from_errno("asprintf");
4123 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4127 dprintf(fd, initial_content);
4129 TAILQ_FOREACH(pe, commitable_paths, entry) {
4130 struct got_commitable *ct = pe->data;
4131 dprintf(fd, "# %c %s\n",
4132 got_commitable_get_status(ct),
4133 got_commitable_get_path(ct));
4137 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4139 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4140 unlink(a->logmsg_path);
4141 free(a->logmsg_path);
4142 a->logmsg_path = NULL;
4144 free(initial_content);
4147 /* Editor is done; we can now apply unveil(2) */
4149 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4158 static const struct got_error *
4159 cmd_commit(int argc, char *argv[])
4161 const struct got_error *error = NULL;
4162 struct got_worktree *worktree = NULL;
4163 struct got_repository *repo = NULL;
4164 char *cwd = NULL, *id_str = NULL;
4165 struct got_object_id *id = NULL;
4166 const char *logmsg = NULL;
4168 struct collect_commit_logmsg_arg cl_arg;
4169 char *editor = NULL;
4170 int ch, rebase_in_progress, histedit_in_progress;
4171 struct got_pathlist_head paths;
4174 cl_arg.logmsg_path = NULL;
4176 while ((ch = getopt(argc, argv, "m:")) != -1) {
4191 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4192 "unveil", NULL) == -1)
4195 error = get_author(&author);
4199 cwd = getcwd(NULL, 0);
4201 error = got_error_from_errno("getcwd");
4204 error = got_worktree_open(&worktree, cwd);
4208 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4211 if (rebase_in_progress) {
4212 error = got_error(GOT_ERR_REBASING);
4216 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4221 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4226 * unveil(2) traverses exec(2); if an editor is used we have
4227 * to apply unveil after the log message has been written.
4229 if (logmsg == NULL || strlen(logmsg) == 0)
4230 error = get_editor(&editor);
4232 error = apply_unveil(got_repo_get_path(repo), 0,
4233 got_worktree_get_root_path(worktree));
4237 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4241 cl_arg.editor = editor;
4242 cl_arg.cmdline_log = logmsg;
4243 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4244 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4245 if (!histedit_in_progress) {
4246 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4247 error = got_error(GOT_ERR_COMMIT_BRANCH);
4250 cl_arg.branch_name += 11;
4252 cl_arg.repo_path = got_repo_get_path(repo);
4253 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4254 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4256 if (cl_arg.logmsg_path)
4257 fprintf(stderr, "%s: log message preserved in %s\n",
4258 getprogname(), cl_arg.logmsg_path);
4262 if (cl_arg.logmsg_path)
4263 unlink(cl_arg.logmsg_path);
4265 error = got_object_id_str(&id_str, id);
4268 printf("Created commit %s\n", id_str);
4270 free(cl_arg.logmsg_path);
4272 got_repo_close(repo);
4274 got_worktree_close(worktree);
4282 usage_cherrypick(void)
4284 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4288 static const struct got_error *
4289 cmd_cherrypick(int argc, char *argv[])
4291 const struct got_error *error = NULL;
4292 struct got_worktree *worktree = NULL;
4293 struct got_repository *repo = NULL;
4294 char *cwd = NULL, *commit_id_str = NULL;
4295 struct got_object_id *commit_id = NULL;
4296 struct got_commit_object *commit = NULL;
4297 struct got_object_qid *pid;
4298 struct got_reference *head_ref = NULL;
4299 int ch, did_something = 0;
4301 while ((ch = getopt(argc, argv, "")) != -1) {
4313 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4314 "unveil", NULL) == -1)
4320 cwd = getcwd(NULL, 0);
4322 error = got_error_from_errno("getcwd");
4325 error = got_worktree_open(&worktree, cwd);
4329 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4333 error = apply_unveil(got_repo_get_path(repo), 0,
4334 got_worktree_get_root_path(worktree));
4338 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4339 GOT_OBJ_TYPE_COMMIT, repo);
4340 if (error != NULL) {
4341 struct got_reference *ref;
4342 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4344 error = got_ref_open(&ref, repo, argv[0], 0);
4347 error = got_ref_resolve(&commit_id, repo, ref);
4352 error = got_object_id_str(&commit_id_str, commit_id);
4356 error = got_ref_open(&head_ref, repo,
4357 got_worktree_get_head_ref_name(worktree), 0);
4361 error = check_same_branch(commit_id, head_ref, NULL, repo);
4363 if (error->code != GOT_ERR_ANCESTRY)
4367 error = got_error(GOT_ERR_SAME_BRANCH);
4371 error = got_object_open_as_commit(&commit, repo, commit_id);
4374 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4375 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4376 commit_id, repo, update_progress, &did_something, check_cancelled,
4382 printf("Merged commit %s\n", commit_id_str);
4385 got_object_commit_close(commit);
4386 free(commit_id_str);
4388 got_ref_close(head_ref);
4390 got_worktree_close(worktree);
4392 got_repo_close(repo);
4399 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4403 static const struct got_error *
4404 cmd_backout(int argc, char *argv[])
4406 const struct got_error *error = NULL;
4407 struct got_worktree *worktree = NULL;
4408 struct got_repository *repo = NULL;
4409 char *cwd = NULL, *commit_id_str = NULL;
4410 struct got_object_id *commit_id = NULL;
4411 struct got_commit_object *commit = NULL;
4412 struct got_object_qid *pid;
4413 struct got_reference *head_ref = NULL;
4414 int ch, did_something = 0;
4416 while ((ch = getopt(argc, argv, "")) != -1) {
4428 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4429 "unveil", NULL) == -1)
4435 cwd = getcwd(NULL, 0);
4437 error = got_error_from_errno("getcwd");
4440 error = got_worktree_open(&worktree, cwd);
4444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4448 error = apply_unveil(got_repo_get_path(repo), 0,
4449 got_worktree_get_root_path(worktree));
4453 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4454 GOT_OBJ_TYPE_COMMIT, repo);
4455 if (error != NULL) {
4456 struct got_reference *ref;
4457 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4459 error = got_ref_open(&ref, repo, argv[0], 0);
4462 error = got_ref_resolve(&commit_id, repo, ref);
4467 error = got_object_id_str(&commit_id_str, commit_id);
4471 error = got_ref_open(&head_ref, repo,
4472 got_worktree_get_head_ref_name(worktree), 0);
4476 error = check_same_branch(commit_id, head_ref, NULL, repo);
4480 error = got_object_open_as_commit(&commit, repo, commit_id);
4483 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4485 error = got_error(GOT_ERR_ROOT_COMMIT);
4489 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4490 update_progress, &did_something, check_cancelled, NULL);
4495 printf("Backed out commit %s\n", commit_id_str);
4498 got_object_commit_close(commit);
4499 free(commit_id_str);
4501 got_ref_close(head_ref);
4503 got_worktree_close(worktree);
4505 got_repo_close(repo);
4512 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4518 trim_logmsg(char *logmsg, int limit)
4523 len = strlen(logmsg);
4527 nl = strchr(logmsg, '\n');
4532 static const struct got_error *
4533 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4535 const struct got_error *err;
4536 char *logmsg0 = NULL;
4539 err = got_object_commit_get_logmsg(&logmsg0, commit);
4544 while (isspace((unsigned char)s[0]))
4547 *logmsg = strdup(s);
4548 if (*logmsg == NULL) {
4549 err = got_error_from_errno("strdup");
4553 trim_logmsg(*logmsg, limit);
4559 static const struct got_error *
4560 show_rebase_progress(struct got_commit_object *commit,
4561 struct got_object_id *old_id, struct got_object_id *new_id)
4563 const struct got_error *err;
4564 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4566 err = got_object_id_str(&old_id_str, old_id);
4571 err = got_object_id_str(&new_id_str, new_id);
4576 old_id_str[12] = '\0';
4578 new_id_str[12] = '\0';
4580 err = get_short_logmsg(&logmsg, 42, commit);
4584 printf("%s -> %s: %s\n", old_id_str,
4585 new_id_str ? new_id_str : "no-op change", logmsg);
4592 static const struct got_error *
4593 rebase_progress(void *arg, unsigned char status, const char *path)
4595 unsigned char *rebase_status = arg;
4597 while (path[0] == '/')
4599 printf("%c %s\n", status, path);
4601 if (*rebase_status == GOT_STATUS_CONFLICT)
4603 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4604 *rebase_status = status;
4608 static const struct got_error *
4609 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4610 struct got_reference *branch, struct got_reference *new_base_branch,
4611 struct got_reference *tmp_branch, struct got_repository *repo)
4613 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4614 return got_worktree_rebase_complete(worktree, fileindex,
4615 new_base_branch, tmp_branch, branch, repo);
4618 static const struct got_error *
4619 rebase_commit(struct got_pathlist_head *merged_paths,
4620 struct got_worktree *worktree, struct got_fileindex *fileindex,
4621 struct got_reference *tmp_branch,
4622 struct got_object_id *commit_id, struct got_repository *repo)
4624 const struct got_error *error;
4625 struct got_commit_object *commit;
4626 struct got_object_id *new_commit_id;
4628 error = got_object_open_as_commit(&commit, repo, commit_id);
4632 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4633 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4635 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4637 error = show_rebase_progress(commit, commit_id, NULL);
4639 error = show_rebase_progress(commit, commit_id, new_commit_id);
4640 free(new_commit_id);
4643 got_object_commit_close(commit);
4647 struct check_path_prefix_arg {
4648 const char *path_prefix;
4653 static const struct got_error *
4654 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4655 struct got_blob_object *blob2, struct got_object_id *id1,
4656 struct got_object_id *id2, const char *path1, const char *path2,
4657 struct got_repository *repo)
4659 struct check_path_prefix_arg *a = arg;
4661 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4662 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4663 return got_error(a->errcode);
4668 static const struct got_error *
4669 check_path_prefix(struct got_object_id *parent_id,
4670 struct got_object_id *commit_id, const char *path_prefix,
4671 int errcode, struct got_repository *repo)
4673 const struct got_error *err;
4674 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4675 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4676 struct check_path_prefix_arg cpp_arg;
4678 if (got_path_is_root_dir(path_prefix))
4681 err = got_object_open_as_commit(&commit, repo, commit_id);
4685 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4689 err = got_object_open_as_tree(&tree1, repo,
4690 got_object_commit_get_tree_id(parent_commit));
4694 err = got_object_open_as_tree(&tree2, repo,
4695 got_object_commit_get_tree_id(commit));
4699 cpp_arg.path_prefix = path_prefix;
4700 while (cpp_arg.path_prefix[0] == '/')
4701 cpp_arg.path_prefix++;
4702 cpp_arg.len = strlen(cpp_arg.path_prefix);
4703 cpp_arg.errcode = errcode;
4704 err = got_diff_tree(tree1, tree2, "", "", repo,
4705 check_path_prefix_in_diff, &cpp_arg, 0);
4708 got_object_tree_close(tree1);
4710 got_object_tree_close(tree2);
4712 got_object_commit_close(commit);
4714 got_object_commit_close(parent_commit);
4718 static const struct got_error *
4719 collect_commits(struct got_object_id_queue *commits,
4720 struct got_object_id *initial_commit_id,
4721 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4722 const char *path_prefix, int path_prefix_errcode,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL;
4726 struct got_commit_graph *graph = NULL;
4727 struct got_object_id *parent_id = NULL;
4728 struct got_object_qid *qid;
4729 struct got_object_id *commit_id = initial_commit_id;
4731 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4735 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4736 check_cancelled, NULL);
4739 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4740 err = got_commit_graph_iter_next(&parent_id, graph);
4742 if (err->code == GOT_ERR_ITER_COMPLETED) {
4743 err = got_error_msg(GOT_ERR_ANCESTRY,
4744 "ran out of commits to rebase before "
4745 "youngest common ancestor commit has "
4748 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4750 err = got_commit_graph_fetch_commits(graph, 1, repo,
4751 check_cancelled, NULL);
4755 err = check_path_prefix(parent_id, commit_id,
4756 path_prefix, path_prefix_errcode, repo);
4760 err = got_object_qid_alloc(&qid, commit_id);
4763 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4764 commit_id = parent_id;
4768 got_commit_graph_close(graph);
4772 static const struct got_error *
4773 cmd_rebase(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 struct got_fileindex *fileindex = NULL;
4780 struct got_reference *branch = NULL;
4781 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4782 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4783 struct got_object_id *resume_commit_id = NULL;
4784 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4785 struct got_commit_object *commit = NULL;
4786 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4787 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4788 struct got_object_id_queue commits;
4789 struct got_pathlist_head merged_paths;
4790 const struct got_object_id_queue *parent_ids;
4791 struct got_object_qid *qid, *pid;
4793 SIMPLEQ_INIT(&commits);
4794 TAILQ_INIT(&merged_paths);
4796 while ((ch = getopt(argc, argv, "ac")) != -1) {
4802 continue_rebase = 1;
4814 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4815 "unveil", NULL) == -1)
4818 if (abort_rebase && continue_rebase)
4820 else if (abort_rebase || continue_rebase) {
4823 } else if (argc != 1)
4826 cwd = getcwd(NULL, 0);
4828 error = got_error_from_errno("getcwd");
4831 error = got_worktree_open(&worktree, cwd);
4835 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4839 error = apply_unveil(got_repo_get_path(repo), 0,
4840 got_worktree_get_root_path(worktree));
4844 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4850 if (!rebase_in_progress) {
4851 error = got_error(GOT_ERR_NOT_REBASING);
4854 error = got_worktree_rebase_continue(&resume_commit_id,
4855 &new_base_branch, &tmp_branch, &branch, &fileindex,
4859 printf("Switching work tree to %s\n",
4860 got_ref_get_symref_target(new_base_branch));
4861 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4862 new_base_branch, update_progress, &did_something);
4865 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4866 goto done; /* nothing else to do */
4869 if (continue_rebase) {
4870 if (!rebase_in_progress) {
4871 error = got_error(GOT_ERR_NOT_REBASING);
4874 error = got_worktree_rebase_continue(&resume_commit_id,
4875 &new_base_branch, &tmp_branch, &branch, &fileindex,
4880 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4881 resume_commit_id, repo);
4885 yca_id = got_object_id_dup(resume_commit_id);
4886 if (yca_id == NULL) {
4887 error = got_error_from_errno("got_object_id_dup");
4891 error = got_ref_open(&branch, repo, argv[0], 0);
4896 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4900 if (!continue_rebase) {
4901 struct got_object_id *base_commit_id;
4903 base_commit_id = got_worktree_get_base_commit_id(worktree);
4904 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4905 base_commit_id, branch_head_commit_id, repo,
4906 check_cancelled, NULL);
4909 if (yca_id == NULL) {
4910 error = got_error_msg(GOT_ERR_ANCESTRY,
4911 "specified branch shares no common ancestry "
4912 "with work tree's branch");
4916 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4918 if (error->code != GOT_ERR_ANCESTRY)
4922 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4923 "specified branch resolves to a commit which "
4924 "is already contained in work tree's branch");
4927 error = got_worktree_rebase_prepare(&new_base_branch,
4928 &tmp_branch, &fileindex, worktree, branch, repo);
4933 commit_id = branch_head_commit_id;
4934 error = got_object_open_as_commit(&commit, repo, commit_id);
4938 parent_ids = got_object_commit_get_parent_ids(commit);
4939 pid = SIMPLEQ_FIRST(parent_ids);
4941 if (!continue_rebase) {
4943 error = got_worktree_rebase_abort(worktree, fileindex,
4944 repo, new_base_branch, update_progress,
4948 printf("Rebase of %s aborted\n",
4949 got_ref_get_name(branch));
4951 error = got_error(GOT_ERR_EMPTY_REBASE);
4954 error = collect_commits(&commits, commit_id, pid->id,
4955 yca_id, got_worktree_get_path_prefix(worktree),
4956 GOT_ERR_REBASE_PATH, repo);
4957 got_object_commit_close(commit);
4962 if (SIMPLEQ_EMPTY(&commits)) {
4963 if (continue_rebase)
4964 error = rebase_complete(worktree, fileindex,
4965 branch, new_base_branch, tmp_branch, repo);
4967 error = got_error(GOT_ERR_EMPTY_REBASE);
4972 SIMPLEQ_FOREACH(qid, &commits, entry) {
4973 commit_id = qid->id;
4974 parent_id = pid ? pid->id : yca_id;
4977 error = got_worktree_rebase_merge_files(&merged_paths,
4978 worktree, fileindex, parent_id, commit_id, repo,
4979 rebase_progress, &rebase_status, check_cancelled, NULL);
4983 if (rebase_status == GOT_STATUS_CONFLICT) {
4984 got_worktree_rebase_pathlist_free(&merged_paths);
4988 error = rebase_commit(&merged_paths, worktree, fileindex,
4989 tmp_branch, commit_id, repo);
4990 got_worktree_rebase_pathlist_free(&merged_paths);
4995 if (rebase_status == GOT_STATUS_CONFLICT) {
4996 error = got_worktree_rebase_postpone(worktree, fileindex);
4999 error = got_error_msg(GOT_ERR_CONFLICTS,
5000 "conflicts must be resolved before rebasing can continue");
5002 error = rebase_complete(worktree, fileindex, branch,
5003 new_base_branch, tmp_branch, repo);
5005 got_object_id_queue_free(&commits);
5006 free(branch_head_commit_id);
5007 free(resume_commit_id);
5010 got_object_commit_close(commit);
5012 got_ref_close(branch);
5013 if (new_base_branch)
5014 got_ref_close(new_base_branch);
5016 got_ref_close(tmp_branch);
5018 got_worktree_close(worktree);
5020 got_repo_close(repo);
5025 usage_histedit(void)
5027 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5032 #define GOT_HISTEDIT_PICK 'p'
5033 #define GOT_HISTEDIT_EDIT 'e'
5034 #define GOT_HISTEDIT_FOLD 'f'
5035 #define GOT_HISTEDIT_DROP 'd'
5036 #define GOT_HISTEDIT_MESG 'm'
5038 static struct got_histedit_cmd {
5042 } got_histedit_cmds[] = {
5043 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5044 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5045 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5046 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5047 { GOT_HISTEDIT_MESG, "mesg",
5048 "single-line log message for commit above (open editor if empty)" },
5051 struct got_histedit_list_entry {
5052 TAILQ_ENTRY(got_histedit_list_entry) entry;
5053 struct got_object_id *commit_id;
5054 const struct got_histedit_cmd *cmd;
5057 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5059 static const struct got_error *
5060 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5061 FILE *f, struct got_repository *repo)
5063 const struct got_error *err = NULL;
5064 char *logmsg = NULL, *id_str = NULL;
5065 struct got_commit_object *commit = NULL;
5068 err = got_object_open_as_commit(&commit, repo, commit_id);
5072 err = get_short_logmsg(&logmsg, 34, commit);
5076 err = got_object_id_str(&id_str, commit_id);
5080 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5082 err = got_ferror(f, GOT_ERR_IO);
5085 got_object_commit_close(commit);
5091 static const struct got_error *
5092 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5093 struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 struct got_object_qid *qid;
5098 if (SIMPLEQ_EMPTY(commits))
5099 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5101 SIMPLEQ_FOREACH(qid, commits, entry) {
5102 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5111 static const struct got_error *
5112 write_cmd_list(FILE *f)
5114 const struct got_error *err = NULL;
5117 n = fprintf(f, "# Available histedit commands:\n");
5119 return got_ferror(f, GOT_ERR_IO);
5121 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5122 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5123 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5126 err = got_ferror(f, GOT_ERR_IO);
5130 n = fprintf(f, "# Commits will be processed in order from top to "
5131 "bottom of this file.\n");
5133 return got_ferror(f, GOT_ERR_IO);
5137 static const struct got_error *
5138 histedit_syntax_error(int lineno)
5140 static char msg[42];
5143 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5145 if (ret == -1 || ret >= sizeof(msg))
5146 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5148 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5151 static const struct got_error *
5152 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5153 char *logmsg, struct got_repository *repo)
5155 const struct got_error *err;
5156 struct got_commit_object *folded_commit = NULL;
5157 char *id_str, *folded_logmsg = NULL;
5159 err = got_object_id_str(&id_str, hle->commit_id);
5163 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5167 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5170 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5171 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5172 folded_logmsg) == -1) {
5173 err = got_error_from_errno("asprintf");
5178 got_object_commit_close(folded_commit);
5180 free(folded_logmsg);
5184 static struct got_histedit_list_entry *
5185 get_folded_commits(struct got_histedit_list_entry *hle)
5187 struct got_histedit_list_entry *prev, *folded = NULL;
5189 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5190 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5191 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5192 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5194 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5200 static const struct got_error *
5201 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5202 struct got_repository *repo)
5204 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5205 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5206 const struct got_error *err = NULL;
5207 struct got_commit_object *commit = NULL;
5209 struct got_histedit_list_entry *folded = NULL;
5211 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5215 folded = get_folded_commits(hle);
5217 while (folded != hle) {
5218 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5219 folded = TAILQ_NEXT(folded, entry);
5222 err = append_folded_commit_msg(&new_msg, folded,
5228 folded = TAILQ_NEXT(folded, entry);
5232 err = got_object_id_str(&id_str, hle->commit_id);
5235 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5238 if (asprintf(&new_msg,
5239 "%s\n# original log message of commit %s: %s",
5240 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5241 err = got_error_from_errno("asprintf");
5247 err = got_object_id_str(&id_str, hle->commit_id);
5251 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5255 dprintf(fd, logmsg);
5258 err = get_editor(&editor);
5262 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5264 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5266 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5269 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5270 err = got_error_from_errno2("unlink", logmsg_path);
5276 got_object_commit_close(commit);
5280 static const struct got_error *
5281 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5282 FILE *f, struct got_repository *repo)
5284 const struct got_error *err = NULL;
5285 char *line = NULL, *p, *end;
5289 const struct got_histedit_cmd *cmd;
5290 struct got_object_id *commit_id = NULL;
5291 struct got_histedit_list_entry *hle = NULL;
5294 len = getline(&line, &size, f);
5296 const struct got_error *getline_err;
5299 getline_err = got_error_from_errno("getline");
5300 err = got_ferror(f, getline_err->code);
5305 while (isspace((unsigned char)p[0]))
5307 if (p[0] == '#' || p[0] == '\0') {
5313 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5314 cmd = &got_histedit_cmds[i];
5315 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5316 isspace((unsigned char)p[strlen(cmd->name)])) {
5317 p += strlen(cmd->name);
5320 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5325 if (i == nitems(got_histedit_cmds)) {
5326 err = histedit_syntax_error(lineno);
5329 while (isspace((unsigned char)p[0]))
5331 if (cmd->code == GOT_HISTEDIT_MESG) {
5332 if (hle == NULL || hle->logmsg != NULL) {
5333 err = got_error(GOT_ERR_HISTEDIT_CMD);
5337 err = histedit_edit_logmsg(hle, repo);
5341 hle->logmsg = strdup(p);
5342 if (hle->logmsg == NULL) {
5343 err = got_error_from_errno("strdup");
5352 while (end[0] && !isspace((unsigned char)end[0]))
5356 err = got_object_resolve_id_str(&commit_id, repo, p);
5358 /* override error code */
5359 err = histedit_syntax_error(lineno);
5363 hle = malloc(sizeof(*hle));
5365 err = got_error_from_errno("malloc");
5369 hle->commit_id = commit_id;
5374 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5382 static const struct got_error *
5383 histedit_check_script(struct got_histedit_list *histedit_cmds,
5384 struct got_object_id_queue *commits, struct got_repository *repo)
5386 const struct got_error *err = NULL;
5387 struct got_object_qid *qid;
5388 struct got_histedit_list_entry *hle;
5389 static char msg[80];
5392 if (TAILQ_EMPTY(histedit_cmds))
5393 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5394 "histedit script contains no commands");
5395 if (SIMPLEQ_EMPTY(commits))
5396 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5398 SIMPLEQ_FOREACH(qid, commits, entry) {
5399 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5400 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5404 err = got_object_id_str(&id_str, qid->id);
5407 snprintf(msg, sizeof(msg),
5408 "commit %s missing from histedit script", id_str);
5410 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5414 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5415 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5416 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5417 "last commit in histedit script cannot be folded");
5422 static const struct got_error *
5423 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5424 const char *path, struct got_object_id_queue *commits,
5425 struct got_repository *repo)
5427 const struct got_error *err = NULL;
5431 err = get_editor(&editor);
5435 if (spawn_editor(editor, path) == -1) {
5436 err = got_error_from_errno("failed spawning editor");
5440 f = fopen(path, "r");
5442 err = got_error_from_errno("fopen");
5445 err = histedit_parse_list(histedit_cmds, f, repo);
5449 err = histedit_check_script(histedit_cmds, commits, repo);
5451 if (f && fclose(f) != 0 && err == NULL)
5452 err = got_error_from_errno("fclose");
5457 static const struct got_error *
5458 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5459 struct got_object_id_queue *, const char *, struct got_repository *);
5461 static const struct got_error *
5462 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5463 struct got_object_id_queue *commits, struct got_repository *repo)
5465 const struct got_error *err;
5469 err = got_opentemp_named(&path, &f, "got-histedit");
5473 err = write_cmd_list(f);
5477 err = histedit_write_commit_list(commits, f, repo);
5481 if (fclose(f) != 0) {
5482 err = got_error_from_errno("fclose");
5487 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5489 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5490 err->code != GOT_ERR_HISTEDIT_CMD)
5492 err = histedit_edit_list_retry(histedit_cmds, err,
5493 commits, path, repo);
5496 if (f && fclose(f) != 0 && err == NULL)
5497 err = got_error_from_errno("fclose");
5498 if (path && unlink(path) != 0 && err == NULL)
5499 err = got_error_from_errno2("unlink", path);
5504 static const struct got_error *
5505 histedit_save_list(struct got_histedit_list *histedit_cmds,
5506 struct got_worktree *worktree, struct got_repository *repo)
5508 const struct got_error *err = NULL;
5511 struct got_histedit_list_entry *hle;
5512 struct got_commit_object *commit = NULL;
5514 err = got_worktree_get_histedit_script_path(&path, worktree);
5518 f = fopen(path, "w");
5520 err = got_error_from_errno2("fopen", path);
5523 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5524 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5530 int n = fprintf(f, "%c %s\n",
5531 GOT_HISTEDIT_MESG, hle->logmsg);
5533 err = got_ferror(f, GOT_ERR_IO);
5539 if (f && fclose(f) != 0 && err == NULL)
5540 err = got_error_from_errno("fclose");
5543 got_object_commit_close(commit);
5548 histedit_free_list(struct got_histedit_list *histedit_cmds)
5550 struct got_histedit_list_entry *hle;
5552 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5553 TAILQ_REMOVE(histedit_cmds, hle, entry);
5558 static const struct got_error *
5559 histedit_load_list(struct got_histedit_list *histedit_cmds,
5560 const char *path, struct got_repository *repo)
5562 const struct got_error *err = NULL;
5565 f = fopen(path, "r");
5567 err = got_error_from_errno2("fopen", path);
5571 err = histedit_parse_list(histedit_cmds, f, repo);
5573 if (f && fclose(f) != 0 && err == NULL)
5574 err = got_error_from_errno("fclose");
5578 static const struct got_error *
5579 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5580 const struct got_error *edit_err, struct got_object_id_queue *commits,
5581 const char *path, struct got_repository *repo)
5583 const struct got_error *err = NULL, *prev_err = edit_err;
5586 while (resp != 'c' && resp != 'r' && resp != 'a') {
5587 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5588 "or (a)bort: ", getprogname(), prev_err->msg);
5593 histedit_free_list(histedit_cmds);
5594 err = histedit_run_editor(histedit_cmds, path, commits,
5597 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5598 err->code != GOT_ERR_HISTEDIT_CMD)
5605 } else if (resp == 'r') {
5606 histedit_free_list(histedit_cmds);
5607 err = histedit_edit_script(histedit_cmds,
5610 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5611 err->code != GOT_ERR_HISTEDIT_CMD)
5618 } else if (resp == 'a') {
5619 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5622 printf("invalid response '%c'\n", resp);
5628 static const struct got_error *
5629 histedit_complete(struct got_worktree *worktree,
5630 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5631 struct got_reference *branch, struct got_repository *repo)
5633 printf("Switching work tree to %s\n",
5634 got_ref_get_symref_target(branch));
5635 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5639 static const struct got_error *
5640 show_histedit_progress(struct got_commit_object *commit,
5641 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5643 const struct got_error *err;
5644 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5646 err = got_object_id_str(&old_id_str, hle->commit_id);
5651 err = got_object_id_str(&new_id_str, new_id);
5656 old_id_str[12] = '\0';
5658 new_id_str[12] = '\0';
5661 logmsg = strdup(hle->logmsg);
5662 if (logmsg == NULL) {
5663 err = got_error_from_errno("strdup");
5666 trim_logmsg(logmsg, 42);
5668 err = get_short_logmsg(&logmsg, 42, commit);
5673 switch (hle->cmd->code) {
5674 case GOT_HISTEDIT_PICK:
5675 case GOT_HISTEDIT_EDIT:
5676 printf("%s -> %s: %s\n", old_id_str,
5677 new_id_str ? new_id_str : "no-op change", logmsg);
5679 case GOT_HISTEDIT_DROP:
5680 case GOT_HISTEDIT_FOLD:
5681 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5694 static const struct got_error *
5695 histedit_commit(struct got_pathlist_head *merged_paths,
5696 struct got_worktree *worktree, struct got_fileindex *fileindex,
5697 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5698 struct got_repository *repo)
5700 const struct got_error *err;
5701 struct got_commit_object *commit;
5702 struct got_object_id *new_commit_id;
5704 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5705 && hle->logmsg == NULL) {
5706 err = histedit_edit_logmsg(hle, repo);
5711 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5715 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5716 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5719 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5721 err = show_histedit_progress(commit, hle, NULL);
5723 err = show_histedit_progress(commit, hle, new_commit_id);
5724 free(new_commit_id);
5727 got_object_commit_close(commit);
5731 static const struct got_error *
5732 histedit_skip_commit(struct got_histedit_list_entry *hle,
5733 struct got_worktree *worktree, struct got_repository *repo)
5735 const struct got_error *error;
5736 struct got_commit_object *commit;
5738 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5743 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5747 error = show_histedit_progress(commit, hle, NULL);
5748 got_object_commit_close(commit);
5752 static const struct got_error *
5753 cmd_histedit(int argc, char *argv[])
5755 const struct got_error *error = NULL;
5756 struct got_worktree *worktree = NULL;
5757 struct got_fileindex *fileindex = NULL;
5758 struct got_repository *repo = NULL;
5760 struct got_reference *branch = NULL;
5761 struct got_reference *tmp_branch = NULL;
5762 struct got_object_id *resume_commit_id = NULL;
5763 struct got_object_id *base_commit_id = NULL;
5764 struct got_object_id *head_commit_id = NULL;
5765 struct got_commit_object *commit = NULL;
5766 int ch, rebase_in_progress = 0, did_something;
5767 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5768 const char *edit_script_path = NULL;
5769 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5770 struct got_object_id_queue commits;
5771 struct got_pathlist_head merged_paths;
5772 const struct got_object_id_queue *parent_ids;
5773 struct got_object_qid *pid;
5774 struct got_histedit_list histedit_cmds;
5775 struct got_histedit_list_entry *hle;
5777 SIMPLEQ_INIT(&commits);
5778 TAILQ_INIT(&histedit_cmds);
5779 TAILQ_INIT(&merged_paths);
5781 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5790 edit_script_path = optarg;
5802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5803 "unveil", NULL) == -1)
5806 if (abort_edit && continue_edit)
5812 * This command cannot apply unveil(2) in all cases because the
5813 * user may choose to run an editor to edit the histedit script
5814 * and to edit individual commit log messages.
5815 * unveil(2) traverses exec(2); if an editor is used we have to
5816 * apply unveil after edit script and log messages have been written.
5817 * XXX TODO: Make use of unveil(2) where possible.
5820 cwd = getcwd(NULL, 0);
5822 error = got_error_from_errno("getcwd");
5825 error = got_worktree_open(&worktree, cwd);
5829 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5833 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5836 if (rebase_in_progress) {
5837 error = got_error(GOT_ERR_REBASING);
5841 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5845 if (edit_in_progress && abort_edit) {
5846 error = got_worktree_histedit_continue(&resume_commit_id,
5847 &tmp_branch, &branch, &base_commit_id, &fileindex,
5851 printf("Switching work tree to %s\n",
5852 got_ref_get_symref_target(branch));
5853 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5854 branch, base_commit_id, update_progress, &did_something);
5857 printf("Histedit of %s aborted\n",
5858 got_ref_get_symref_target(branch));
5859 goto done; /* nothing else to do */
5860 } else if (abort_edit) {
5861 error = got_error(GOT_ERR_NOT_HISTEDIT);
5865 if (continue_edit) {
5868 if (!edit_in_progress) {
5869 error = got_error(GOT_ERR_NOT_HISTEDIT);
5873 error = got_worktree_get_histedit_script_path(&path, worktree);
5877 error = histedit_load_list(&histedit_cmds, path, repo);
5882 error = got_worktree_histedit_continue(&resume_commit_id,
5883 &tmp_branch, &branch, &base_commit_id, &fileindex,
5888 error = got_ref_resolve(&head_commit_id, repo, branch);
5892 error = got_object_open_as_commit(&commit, repo,
5896 parent_ids = got_object_commit_get_parent_ids(commit);
5897 pid = SIMPLEQ_FIRST(parent_ids);
5899 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5902 error = collect_commits(&commits, head_commit_id, pid->id,
5903 base_commit_id, got_worktree_get_path_prefix(worktree),
5904 GOT_ERR_HISTEDIT_PATH, repo);
5905 got_object_commit_close(commit);
5910 if (edit_in_progress) {
5911 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5915 error = got_ref_open(&branch, repo,
5916 got_worktree_get_head_ref_name(worktree), 0);
5920 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5921 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5922 "will not edit commit history of a branch outside "
5923 "the \"refs/heads/\" reference namespace");
5927 error = got_ref_resolve(&head_commit_id, repo, branch);
5928 got_ref_close(branch);
5933 error = got_object_open_as_commit(&commit, repo,
5937 parent_ids = got_object_commit_get_parent_ids(commit);
5938 pid = SIMPLEQ_FIRST(parent_ids);
5940 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5943 error = collect_commits(&commits, head_commit_id, pid->id,
5944 got_worktree_get_base_commit_id(worktree),
5945 got_worktree_get_path_prefix(worktree),
5946 GOT_ERR_HISTEDIT_PATH, repo);
5947 got_object_commit_close(commit);
5952 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5953 &base_commit_id, &fileindex, worktree, repo);
5957 if (edit_script_path) {
5958 error = histedit_load_list(&histedit_cmds,
5959 edit_script_path, repo);
5961 got_worktree_histedit_abort(worktree, fileindex,
5962 repo, branch, base_commit_id,
5963 update_progress, &did_something);
5967 error = histedit_edit_script(&histedit_cmds, &commits,
5970 got_worktree_histedit_abort(worktree, fileindex,
5971 repo, branch, base_commit_id,
5972 update_progress, &did_something);
5978 error = histedit_save_list(&histedit_cmds, worktree,
5981 got_worktree_histedit_abort(worktree, fileindex,
5982 repo, branch, base_commit_id,
5983 update_progress, &did_something);
5989 error = histedit_check_script(&histedit_cmds, &commits, repo);
5993 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5994 if (resume_commit_id) {
5995 if (got_object_id_cmp(hle->commit_id,
5996 resume_commit_id) != 0)
5999 resume_commit_id = NULL;
6000 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6001 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6002 error = histedit_skip_commit(hle, worktree,
6005 error = histedit_commit(NULL, worktree,
6006 fileindex, tmp_branch, hle, repo);
6013 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6014 error = histedit_skip_commit(hle, worktree, repo);
6020 error = got_object_open_as_commit(&commit, repo,
6024 parent_ids = got_object_commit_get_parent_ids(commit);
6025 pid = SIMPLEQ_FIRST(parent_ids);
6027 error = got_worktree_histedit_merge_files(&merged_paths,
6028 worktree, fileindex, pid->id, hle->commit_id, repo,
6029 rebase_progress, &rebase_status, check_cancelled, NULL);
6032 got_object_commit_close(commit);
6035 if (rebase_status == GOT_STATUS_CONFLICT) {
6036 got_worktree_rebase_pathlist_free(&merged_paths);
6040 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6042 error = got_object_id_str(&id_str, hle->commit_id);
6045 printf("Stopping histedit for amending commit %s\n",
6048 got_worktree_rebase_pathlist_free(&merged_paths);
6049 error = got_worktree_histedit_postpone(worktree,
6054 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6055 error = histedit_skip_commit(hle, worktree, repo);
6061 error = histedit_commit(&merged_paths, worktree, fileindex,
6062 tmp_branch, hle, repo);
6063 got_worktree_rebase_pathlist_free(&merged_paths);
6068 if (rebase_status == GOT_STATUS_CONFLICT) {
6069 error = got_worktree_histedit_postpone(worktree, fileindex);
6072 error = got_error_msg(GOT_ERR_CONFLICTS,
6073 "conflicts must be resolved before rebasing can continue");
6075 error = histedit_complete(worktree, fileindex, tmp_branch,
6078 got_object_id_queue_free(&commits);
6079 histedit_free_list(&histedit_cmds);
6080 free(head_commit_id);
6081 free(base_commit_id);
6082 free(resume_commit_id);
6084 got_object_commit_close(commit);
6086 got_ref_close(branch);
6088 got_ref_close(tmp_branch);
6090 got_worktree_close(worktree);
6092 got_repo_close(repo);
6099 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6100 "[file-path ...]\n",
6105 static const struct got_error *
6106 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6107 const char *path, struct got_object_id *blob_id,
6108 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6110 const struct got_error *err = NULL;
6111 char *id_str = NULL;
6113 if (staged_status != GOT_STATUS_ADD &&
6114 staged_status != GOT_STATUS_MODIFY &&
6115 staged_status != GOT_STATUS_DELETE)
6118 if (staged_status == GOT_STATUS_ADD ||
6119 staged_status == GOT_STATUS_MODIFY)
6120 err = got_object_id_str(&id_str, staged_blob_id);
6122 err = got_object_id_str(&id_str, blob_id);
6126 printf("%s %c %s\n", id_str, staged_status, path);
6131 static const struct got_error *
6132 cmd_stage(int argc, char *argv[])
6134 const struct got_error *error = NULL;
6135 struct got_repository *repo = NULL;
6136 struct got_worktree *worktree = NULL;
6138 struct got_pathlist_head paths;
6139 struct got_pathlist_entry *pe;
6140 int ch, list_stage = 0, pflag = 0;
6141 FILE *patch_script_file = NULL;
6142 const char *patch_script_path = NULL;
6143 struct choose_patch_arg cpa;
6147 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6156 patch_script_path = optarg;
6168 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6169 "unveil", NULL) == -1)
6172 if (list_stage && (pflag || patch_script_path))
6173 errx(1, "-l option cannot be used with other options");
6174 if (patch_script_path && !pflag)
6175 errx(1, "-F option can only be used together with -p option");
6177 cwd = getcwd(NULL, 0);
6179 error = got_error_from_errno("getcwd");
6183 error = got_worktree_open(&worktree, cwd);
6187 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6191 if (patch_script_path) {
6192 patch_script_file = fopen(patch_script_path, "r");
6193 if (patch_script_file == NULL) {
6194 error = got_error_from_errno2("fopen",
6199 error = apply_unveil(got_repo_get_path(repo), 1,
6200 got_worktree_get_root_path(worktree));
6204 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6209 error = got_worktree_status(worktree, &paths, repo,
6210 print_stage, NULL, check_cancelled, NULL);
6212 cpa.patch_script_file = patch_script_file;
6213 cpa.action = "stage";
6214 error = got_worktree_stage(worktree, &paths,
6215 pflag ? NULL : print_status, NULL,
6216 pflag ? choose_patch : NULL, &cpa, repo);
6219 if (patch_script_file && fclose(patch_script_file) == EOF &&
6221 error = got_error_from_errno2("fclose", patch_script_path);
6223 got_repo_close(repo);
6225 got_worktree_close(worktree);
6226 TAILQ_FOREACH(pe, &paths, entry)
6227 free((char *)pe->path);
6228 got_pathlist_free(&paths);
6236 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6237 "[file-path ...]\n",
6243 static const struct got_error *
6244 cmd_unstage(int argc, char *argv[])
6246 const struct got_error *error = NULL;
6247 struct got_repository *repo = NULL;
6248 struct got_worktree *worktree = NULL;
6250 struct got_pathlist_head paths;
6251 struct got_pathlist_entry *pe;
6252 int ch, did_something = 0, pflag = 0;
6253 FILE *patch_script_file = NULL;
6254 const char *patch_script_path = NULL;
6255 struct choose_patch_arg cpa;
6259 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6265 patch_script_path = optarg;
6277 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6278 "unveil", NULL) == -1)
6281 if (patch_script_path && !pflag)
6282 errx(1, "-F option can only be used together with -p option");
6284 cwd = getcwd(NULL, 0);
6286 error = got_error_from_errno("getcwd");
6290 error = got_worktree_open(&worktree, cwd);
6294 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6298 if (patch_script_path) {
6299 patch_script_file = fopen(patch_script_path, "r");
6300 if (patch_script_file == NULL) {
6301 error = got_error_from_errno2("fopen",
6307 error = apply_unveil(got_repo_get_path(repo), 1,
6308 got_worktree_get_root_path(worktree));
6312 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6316 cpa.patch_script_file = patch_script_file;
6317 cpa.action = "unstage";
6318 error = got_worktree_unstage(worktree, &paths, update_progress,
6319 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6321 if (patch_script_file && fclose(patch_script_file) == EOF &&
6323 error = got_error_from_errno2("fclose", patch_script_path);
6325 got_repo_close(repo);
6327 got_worktree_close(worktree);
6328 TAILQ_FOREACH(pe, &paths, entry)
6329 free((char *)pe->path);
6330 got_pathlist_free(&paths);
6338 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
6339 "[object2 ...]\n", getprogname());
6343 static const struct got_error *
6344 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6346 const struct got_error *err;
6347 struct got_blob_object *blob;
6349 err = got_object_open_as_blob(&blob, repo, id, 8192);
6353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6354 got_object_blob_close(blob);
6358 static const struct got_error *
6359 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6361 const struct got_error *err;
6362 struct got_tree_object *tree;
6363 const struct got_tree_entries *entries;
6364 struct got_tree_entry *te;
6366 err = got_object_open_as_tree(&tree, repo, id);
6370 entries = got_object_tree_get_entries(tree);
6371 te = SIMPLEQ_FIRST(&entries->head);
6374 if (sigint_received || sigpipe_received)
6376 err = got_object_id_str(&id_str, te->id);
6379 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6381 te = SIMPLEQ_NEXT(te, entry);
6384 got_object_tree_close(tree);
6388 static const struct got_error *
6389 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6391 const struct got_error *err;
6392 struct got_commit_object *commit;
6393 const struct got_object_id_queue *parent_ids;
6394 struct got_object_qid *pid;
6395 char *id_str = NULL;
6396 const char *logmsg = NULL;
6398 err = got_object_open_as_commit(&commit, repo, id);
6402 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6406 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6407 parent_ids = got_object_commit_get_parent_ids(commit);
6408 fprintf(outfile, "numparents %d\n",
6409 got_object_commit_get_nparents(commit));
6410 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6412 err = got_object_id_str(&pid_str, pid->id);
6415 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6418 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6419 got_object_commit_get_author(commit),
6420 got_object_commit_get_author_time(commit));
6422 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6423 got_object_commit_get_author(commit),
6424 got_object_commit_get_committer_time(commit));
6426 logmsg = got_object_commit_get_logmsg_raw(commit);
6427 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6428 fprintf(outfile, "%s", logmsg);
6431 got_object_commit_close(commit);
6435 static const struct got_error *
6436 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6438 const struct got_error *err;
6439 struct got_tag_object *tag;
6440 char *id_str = NULL;
6441 const char *tagmsg = NULL;
6443 err = got_object_open_as_tag(&tag, repo, id);
6447 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6451 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6453 switch (got_object_tag_get_object_type(tag)) {
6454 case GOT_OBJ_TYPE_BLOB:
6455 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6456 GOT_OBJ_LABEL_BLOB);
6458 case GOT_OBJ_TYPE_TREE:
6459 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6460 GOT_OBJ_LABEL_TREE);
6462 case GOT_OBJ_TYPE_COMMIT:
6463 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6464 GOT_OBJ_LABEL_COMMIT);
6466 case GOT_OBJ_TYPE_TAG:
6467 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6474 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6475 got_object_tag_get_name(tag));
6477 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6478 got_object_tag_get_tagger(tag),
6479 got_object_tag_get_tagger_time(tag));
6481 tagmsg = got_object_tag_get_message(tag);
6482 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6483 fprintf(outfile, "%s", tagmsg);
6486 got_object_tag_close(tag);
6490 static const struct got_error *
6491 cmd_cat(int argc, char *argv[])
6493 const struct got_error *error;
6494 struct got_repository *repo = NULL;
6495 struct got_worktree *worktree = NULL;
6496 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6497 struct got_object_id *id = NULL;
6498 int ch, obj_type, i;
6501 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6506 while ((ch = getopt(argc, argv, "r:")) != -1) {
6509 repo_path = realpath(optarg, NULL);
6510 if (repo_path == NULL)
6511 err(1, "-r option");
6512 got_path_strip_trailing_slashes(repo_path);
6523 cwd = getcwd(NULL, 0);
6525 error = got_error_from_errno("getcwd");
6528 error = got_worktree_open(&worktree, cwd);
6529 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6532 if (repo_path == NULL) {
6534 got_worktree_get_repo_path(worktree));
6535 if (repo_path == NULL) {
6536 error = got_error_from_errno("strdup");
6542 if (repo_path == NULL) {
6543 repo_path = getcwd(NULL, 0);
6544 if (repo_path == NULL)
6545 return got_error_from_errno("getcwd");
6548 error = got_repo_open(&repo, repo_path);
6553 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6557 for (i = 0; i < argc; i++) {
6558 error = match_object_id(&id, &label, argv[i],
6559 GOT_OBJ_TYPE_ANY, 0, repo);
6563 error = got_object_get_type(&obj_type, repo, id);
6568 case GOT_OBJ_TYPE_BLOB:
6569 error = cat_blob(id, repo, stdout);
6571 case GOT_OBJ_TYPE_TREE:
6572 error = cat_tree(id, repo, stdout);
6574 case GOT_OBJ_TYPE_COMMIT:
6575 error = cat_commit(id, repo, stdout);
6577 case GOT_OBJ_TYPE_TAG:
6578 error = cat_tag(id, repo, stdout);
6581 error = got_error(GOT_ERR_OBJ_TYPE);
6596 got_worktree_close(worktree);
6598 const struct got_error *repo_error;
6599 repo_error = got_repo_close(repo);