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_add(void);
92 __dead static void usage_remove(void);
93 __dead static void usage_revert(void);
94 __dead static void usage_commit(void);
95 __dead static void usage_cherrypick(void);
96 __dead static void usage_backout(void);
97 __dead static void usage_rebase(void);
98 __dead static void usage_histedit(void);
99 __dead static void usage_stage(void);
100 __dead static void usage_unstage(void);
101 __dead static void usage_cat(void);
103 static const struct got_error* cmd_init(int, char *[]);
104 static const struct got_error* cmd_import(int, char *[]);
105 static const struct got_error* cmd_checkout(int, char *[]);
106 static const struct got_error* cmd_update(int, char *[]);
107 static const struct got_error* cmd_log(int, char *[]);
108 static const struct got_error* cmd_diff(int, char *[]);
109 static const struct got_error* cmd_blame(int, char *[]);
110 static const struct got_error* cmd_tree(int, char *[]);
111 static const struct got_error* cmd_status(int, char *[]);
112 static const struct got_error* cmd_ref(int, char *[]);
113 static const struct got_error* cmd_branch(int, char *[]);
114 static const struct got_error* cmd_add(int, char *[]);
115 static const struct got_error* cmd_remove(int, char *[]);
116 static const struct got_error* cmd_revert(int, char *[]);
117 static const struct got_error* cmd_commit(int, char *[]);
118 static const struct got_error* cmd_cherrypick(int, char *[]);
119 static const struct got_error* cmd_backout(int, char *[]);
120 static const struct got_error* cmd_rebase(int, char *[]);
121 static const struct got_error* cmd_histedit(int, char *[]);
122 static const struct got_error* cmd_stage(int, char *[]);
123 static const struct got_error* cmd_unstage(int, char *[]);
124 static const struct got_error* cmd_cat(int, char *[]);
126 static struct got_cmd got_commands[] = {
127 { "init", cmd_init, usage_init, "in" },
128 { "import", cmd_import, usage_import, "im" },
129 { "checkout", cmd_checkout, usage_checkout, "co" },
130 { "update", cmd_update, usage_update, "up" },
131 { "log", cmd_log, usage_log, "" },
132 { "diff", cmd_diff, usage_diff, "di" },
133 { "blame", cmd_blame, usage_blame, "bl" },
134 { "tree", cmd_tree, usage_tree, "tr" },
135 { "status", cmd_status, usage_status, "st" },
136 { "ref", cmd_ref, usage_ref, "" },
137 { "branch", cmd_branch, usage_branch, "br" },
138 { "add", cmd_add, usage_add, "" },
139 { "remove", cmd_remove, usage_remove, "rm" },
140 { "revert", cmd_revert, usage_revert, "rv" },
141 { "commit", cmd_commit, usage_commit, "ci" },
142 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
143 { "backout", cmd_backout, usage_backout, "bo" },
144 { "rebase", cmd_rebase, usage_rebase, "rb" },
145 { "histedit", cmd_histedit, usage_histedit, "he" },
146 { "stage", cmd_stage, usage_stage, "sg" },
147 { "unstage", cmd_unstage, usage_unstage, "ug" },
148 { "cat", cmd_cat, usage_cat, "" },
156 fprintf(stderr, "commands:");
157 for (i = 0; i < nitems(got_commands); i++) {
158 struct got_cmd *cmd = &got_commands[i];
159 fprintf(stderr, " %s", cmd->cmd_name);
165 main(int argc, char *argv[])
170 int hflag = 0, Vflag = 0;
172 setlocale(LC_CTYPE, "");
174 while ((ch = getopt(argc, argv, "hV")) != -1) {
193 got_version_print_str();
200 signal(SIGINT, catch_sigint);
201 signal(SIGPIPE, catch_sigpipe);
203 for (i = 0; i < nitems(got_commands); i++) {
204 const struct got_error *error;
206 cmd = &got_commands[i];
208 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
209 strcmp(cmd->cmd_alias, argv[0]) != 0)
213 got_commands[i].cmd_usage();
215 error = got_commands[i].cmd_main(argc, argv);
216 if (error && !(sigint_received || sigpipe_received)) {
217 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
232 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 static const struct got_error *
240 get_editor(char **abspath)
242 const struct got_error *err = NULL;
247 editor = getenv("VISUAL");
249 editor = getenv("EDITOR");
252 err = got_path_find_prog(abspath, editor);
257 if (*abspath == NULL) {
258 *abspath = strdup("/bin/ed");
259 if (*abspath == NULL)
260 return got_error_from_errno("strdup");
266 static const struct got_error *
267 apply_unveil(const char *repo_path, int repo_read_only,
268 const char *worktree_path)
270 const struct got_error *err;
273 if (unveil("gmon.out", "rwc") != 0)
274 return got_error_from_errno2("unveil", "gmon.out");
276 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
277 return got_error_from_errno2("unveil", repo_path);
279 if (worktree_path && unveil(worktree_path, "rwc") != 0)
280 return got_error_from_errno2("unveil", worktree_path);
282 if (unveil("/tmp", "rwc") != 0)
283 return got_error_from_errno2("unveil", "/tmp");
285 err = got_privsep_unveil_exec_helpers();
289 if (unveil(NULL, NULL) != 0)
290 return got_error_from_errno("unveil");
298 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 static const struct got_error *
303 cmd_init(int argc, char *argv[])
305 const struct got_error *error = NULL;
306 char *repo_path = NULL;
309 while ((ch = getopt(argc, argv, "")) != -1) {
321 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
327 repo_path = strdup(argv[0]);
328 if (repo_path == NULL)
329 return got_error_from_errno("strdup");
331 got_path_strip_trailing_slashes(repo_path);
333 error = got_path_mkdir(repo_path);
335 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
338 error = apply_unveil(repo_path, 0, NULL);
342 error = got_repo_init(repo_path);
354 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
355 "[-r repository-path] [-I pattern] path\n", getprogname());
360 spawn_editor(const char *editor, const char *file)
363 sig_t sighup, sigint, sigquit;
366 sighup = signal(SIGHUP, SIG_IGN);
367 sigint = signal(SIGINT, SIG_IGN);
368 sigquit = signal(SIGQUIT, SIG_IGN);
370 switch (pid = fork()) {
374 execl(editor, editor, file, (char *)NULL);
378 while (waitpid(pid, &st, 0) == -1)
383 (void)signal(SIGHUP, sighup);
384 (void)signal(SIGINT, sigint);
385 (void)signal(SIGQUIT, sigquit);
387 if (!WIFEXITED(st)) {
392 return WEXITSTATUS(st);
395 static const struct got_error *
396 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
397 const char *initial_content)
399 const struct got_error *err = NULL;
403 int content_changed = 0;
408 if (stat(logmsg_path, &st) == -1)
409 return got_error_from_errno2("stat", logmsg_path);
411 if (spawn_editor(editor, logmsg_path) == -1)
412 return got_error_from_errno("failed spawning editor");
414 if (stat(logmsg_path, &st2) == -1)
415 return got_error_from_errno("stat");
417 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
421 *logmsg = malloc(st2.st_size + 1);
423 return got_error_from_errno("malloc");
427 fp = fopen(logmsg_path, "r");
429 err = got_error_from_errno("fopen");
432 while (fgets(buf, sizeof(buf), fp) != NULL) {
433 if (!content_changed && strcmp(buf, initial_content) != 0)
435 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
436 continue; /* remove comments and leading empty lines */
437 len = strlcat(*logmsg, buf, st2.st_size);
441 while (len > 0 && (*logmsg)[len - 1] == '\n') {
442 (*logmsg)[len - 1] = '\0';
446 if (len == 0 || !content_changed)
447 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
448 "commit message cannot be empty, aborting");
457 static const struct got_error *
458 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
459 const char *branch_name)
461 char *initial_content = NULL, *logmsg_path = NULL;
462 const struct got_error *err = NULL;
465 if (asprintf(&initial_content,
466 "\n# %s to be imported to branch %s\n", path_dir,
468 return got_error_from_errno("asprintf");
470 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 dprintf(fd, initial_content);
477 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
479 free(initial_content);
484 static const struct got_error *
485 import_progress(void *arg, const char *path)
487 printf("A %s\n", path);
491 static const struct got_error *
492 get_author(const char **author)
494 const char *got_author;
498 got_author = getenv("GOT_AUTHOR");
499 if (got_author == NULL) {
500 /* TODO: Look up user in password database? */
501 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
504 *author = got_author;
507 * Really dumb email address check; we're only doing this to
508 * avoid git's object parser breaking on commits we create.
510 while (*got_author && *got_author != '<')
512 if (*got_author != '<')
513 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
514 while (*got_author && *got_author != '@')
516 if (*got_author != '@')
517 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
518 while (*got_author && *got_author != '>')
520 if (*got_author != '>')
521 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
526 static const struct got_error *
527 cmd_import(int argc, char *argv[])
529 const struct got_error *error = NULL;
530 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
533 const char *branch_name = "master";
534 char *refname = NULL, *id_str = NULL;
535 struct got_repository *repo = NULL;
536 struct got_reference *branch_ref = NULL, *head_ref = NULL;
537 struct got_object_id *new_commit_id = NULL;
539 struct got_pathlist_head ignores;
540 struct got_pathlist_entry *pe;
542 TAILQ_INIT(&ignores);
544 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
547 branch_name = optarg;
550 logmsg = strdup(optarg);
551 if (logmsg == NULL) {
552 error = got_error_from_errno("strdup");
557 repo_path = realpath(optarg, NULL);
558 if (repo_path == NULL) {
559 error = got_error_from_errno("realpath");
564 if (optarg[0] == '\0')
566 error = got_pathlist_insert(&pe, &ignores, optarg,
581 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
588 error = get_author(&author);
592 if (repo_path == NULL) {
593 repo_path = getcwd(NULL, 0);
594 if (repo_path == NULL)
595 return got_error_from_errno("getcwd");
597 got_path_strip_trailing_slashes(repo_path);
598 error = got_repo_open(&repo, repo_path);
602 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
603 error = got_error_from_errno("asprintf");
607 error = got_ref_open(&branch_ref, repo, refname, 0);
609 if (error->code != GOT_ERR_NOT_REF)
612 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
613 "import target branch already exists");
617 path_dir = realpath(argv[0], NULL);
618 if (path_dir == NULL) {
619 error = got_error_from_errno("realpath");
622 got_path_strip_trailing_slashes(path_dir);
625 * unveil(2) traverses exec(2); if an editor is used we have
626 * to apply unveil after the log message has been written.
628 if (logmsg == NULL || strlen(logmsg) == 0) {
629 error = get_editor(&editor);
632 error = collect_import_msg(&logmsg, editor, path_dir, refname);
637 if (unveil(path_dir, "r") != 0)
638 return got_error_from_errno2("unveil", path_dir);
640 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
644 error = got_repo_import(&new_commit_id, path_dir, logmsg,
645 author, &ignores, repo, import_progress, NULL);
649 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
653 error = got_ref_write(branch_ref, repo);
657 error = got_object_id_str(&id_str, new_commit_id);
661 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
663 if (error->code != GOT_ERR_NOT_REF)
666 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
671 error = got_ref_write(head_ref, repo);
676 printf("Created branch %s with commit %s\n",
677 got_ref_get_name(branch_ref), id_str);
685 got_ref_close(branch_ref);
687 got_ref_close(head_ref);
694 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
695 "[-p prefix] repository-path [worktree-path]\n", getprogname());
699 static const struct got_error *
700 checkout_progress(void *arg, unsigned char status, const char *path)
702 char *worktree_path = arg;
704 /* Base commit bump happens silently. */
705 if (status == GOT_STATUS_BUMP_BASE)
708 while (path[0] == '/')
711 printf("%c %s/%s\n", status, worktree_path, path);
715 static const struct got_error *
716 check_cancelled(void *arg)
718 if (sigint_received || sigpipe_received)
719 return got_error(GOT_ERR_CANCELLED);
723 static const struct got_error *
724 check_linear_ancestry(struct got_object_id *commit_id,
725 struct got_object_id *base_commit_id, struct got_repository *repo)
727 const struct got_error *err = NULL;
728 struct got_object_id *yca_id;
730 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
731 commit_id, base_commit_id, repo, check_cancelled, NULL);
736 return got_error(GOT_ERR_ANCESTRY);
739 * Require a straight line of history between the target commit
740 * and the work tree's base commit.
742 * Non-linear situations such as this require a rebase:
744 * (commit) D F (base_commit)
752 * 'got update' only handles linear cases:
753 * Update forwards in time: A (base/yca) - B - C - D (commit)
754 * Update backwards in time: D (base) - C - B - A (commit/yca)
756 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
757 got_object_id_cmp(base_commit_id, yca_id) != 0)
758 return got_error(GOT_ERR_ANCESTRY);
764 static const struct got_error *
765 check_same_branch(struct got_object_id *commit_id,
766 struct got_reference *head_ref, struct got_object_id *yca_id,
767 struct got_repository *repo)
769 const struct got_error *err = NULL;
770 struct got_commit_graph *graph = NULL;
771 struct got_object_id *head_commit_id = NULL;
772 int is_same_branch = 0;
774 err = got_ref_resolve(&head_commit_id, repo, head_ref);
778 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
782 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
787 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
791 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
792 check_cancelled, NULL);
797 struct got_object_id *id;
798 err = got_commit_graph_iter_next(&id, graph);
800 if (err->code == GOT_ERR_ITER_COMPLETED) {
803 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
805 err = got_commit_graph_fetch_commits(graph, 1,
806 repo, check_cancelled, NULL);
812 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
814 if (got_object_id_cmp(id, commit_id) == 0) {
822 got_commit_graph_close(graph);
823 free(head_commit_id);
824 if (!err && !is_same_branch)
825 err = got_error(GOT_ERR_ANCESTRY);
829 static const struct got_error *
830 resolve_commit_arg(struct got_object_id **commit_id,
831 const char *commit_id_arg, struct got_repository *repo)
833 const struct got_error *err;
834 struct got_reference *ref;
835 struct got_tag_object *tag;
837 err = got_repo_object_match_tag(&tag, commit_id_arg,
838 GOT_OBJ_TYPE_COMMIT, repo);
840 *commit_id = got_object_id_dup(
841 got_object_tag_get_object_id(tag));
842 if (*commit_id == NULL)
843 err = got_error_from_errno("got_object_id_dup");
844 got_object_tag_close(tag);
846 } else if (err->code != GOT_ERR_NO_OBJ)
849 err = got_ref_open(&ref, repo, commit_id_arg, 0);
851 err = got_ref_resolve(commit_id, repo, ref);
854 if (err->code != GOT_ERR_NOT_REF)
856 err = got_repo_match_object_id_prefix(commit_id,
857 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
862 static const struct got_error *
863 cmd_checkout(int argc, char *argv[])
865 const struct got_error *error = NULL;
866 struct got_repository *repo = NULL;
867 struct got_reference *head_ref = NULL;
868 struct got_worktree *worktree = NULL;
869 char *repo_path = NULL;
870 char *worktree_path = NULL;
871 const char *path_prefix = "";
872 const char *branch_name = GOT_REF_HEAD;
873 char *commit_id_str = NULL;
874 int ch, same_path_prefix;
875 struct got_pathlist_head paths;
879 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
882 branch_name = optarg;
885 commit_id_str = strdup(optarg);
886 if (commit_id_str == NULL)
887 return got_error_from_errno("strdup");
890 path_prefix = optarg;
902 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
903 "unveil", NULL) == -1)
907 char *cwd, *base, *dotgit;
908 repo_path = realpath(argv[0], NULL);
909 if (repo_path == NULL)
910 return got_error_from_errno2("realpath", argv[0]);
911 cwd = getcwd(NULL, 0);
913 error = got_error_from_errno("getcwd");
916 if (path_prefix[0]) {
917 base = basename(path_prefix);
919 error = got_error_from_errno2("basename",
924 base = basename(repo_path);
926 error = got_error_from_errno2("basename",
931 dotgit = strstr(base, ".git");
934 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
935 error = got_error_from_errno("asprintf");
940 } else if (argc == 2) {
941 repo_path = realpath(argv[0], NULL);
942 if (repo_path == NULL) {
943 error = got_error_from_errno2("realpath", argv[0]);
946 worktree_path = realpath(argv[1], NULL);
947 if (worktree_path == NULL) {
948 if (errno != ENOENT) {
949 error = got_error_from_errno2("realpath",
953 worktree_path = strdup(argv[1]);
954 if (worktree_path == NULL) {
955 error = got_error_from_errno("strdup");
962 got_path_strip_trailing_slashes(repo_path);
963 got_path_strip_trailing_slashes(worktree_path);
965 error = got_repo_open(&repo, repo_path);
969 /* Pre-create work tree path for unveil(2) */
970 error = got_path_mkdir(worktree_path);
972 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
973 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
975 if (!got_path_dir_is_empty(worktree_path)) {
976 error = got_error_path(worktree_path,
977 GOT_ERR_DIR_NOT_EMPTY);
982 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
986 error = got_ref_open(&head_ref, repo, branch_name, 0);
990 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
991 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
994 error = got_worktree_open(&worktree, worktree_path);
998 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1002 if (!same_path_prefix) {
1003 error = got_error(GOT_ERR_PATH_PREFIX);
1007 if (commit_id_str) {
1008 struct got_object_id *commit_id;
1009 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1012 error = check_linear_ancestry(commit_id,
1013 got_worktree_get_base_commit_id(worktree), repo);
1014 if (error != NULL) {
1018 error = check_same_branch(commit_id, head_ref, NULL, repo);
1021 error = got_worktree_set_base_commit_id(worktree, repo,
1028 error = got_pathlist_append(&paths, "", NULL);
1031 error = got_worktree_checkout_files(worktree, &paths, repo,
1032 checkout_progress, worktree_path, check_cancelled, NULL);
1036 printf("Now shut up and hack\n");
1039 got_pathlist_free(&paths);
1040 free(commit_id_str);
1042 free(worktree_path);
1049 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1054 static const struct got_error *
1055 update_progress(void *arg, unsigned char status, const char *path)
1057 int *did_something = arg;
1059 if (status == GOT_STATUS_EXISTS)
1064 /* Base commit bump happens silently. */
1065 if (status == GOT_STATUS_BUMP_BASE)
1068 while (path[0] == '/')
1070 printf("%c %s\n", status, path);
1074 static const struct got_error *
1075 switch_head_ref(struct got_reference *head_ref,
1076 struct got_object_id *commit_id, struct got_worktree *worktree,
1077 struct got_repository *repo)
1079 const struct got_error *err = NULL;
1081 int ref_has_moved = 0;
1083 /* Trivial case: switching between two different references. */
1084 if (strcmp(got_ref_get_name(head_ref),
1085 got_worktree_get_head_ref_name(worktree)) != 0) {
1086 printf("Switching work tree from %s to %s\n",
1087 got_worktree_get_head_ref_name(worktree),
1088 got_ref_get_name(head_ref));
1089 return got_worktree_set_head_ref(worktree, head_ref);
1092 err = check_linear_ancestry(commit_id,
1093 got_worktree_get_base_commit_id(worktree), repo);
1095 if (err->code != GOT_ERR_ANCESTRY)
1102 /* Switching to a rebased branch with the same reference name. */
1103 err = got_object_id_str(&base_id_str,
1104 got_worktree_get_base_commit_id(worktree));
1107 printf("Reference %s now points at a different branch\n",
1108 got_worktree_get_head_ref_name(worktree));
1109 printf("Switching work tree from %s to %s\n", base_id_str,
1110 got_worktree_get_head_ref_name(worktree));
1114 static const struct got_error *
1115 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1117 const struct got_error *err;
1120 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1124 return got_error(GOT_ERR_REBASING);
1126 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1130 return got_error(GOT_ERR_HISTEDIT_BUSY);
1135 static const struct got_error *
1136 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1137 char *argv[], struct got_worktree *worktree)
1139 const struct got_error *err = NULL;
1146 return got_error_from_errno("strdup");
1147 return got_pathlist_append(paths, path, NULL);
1150 for (i = 0; i < argc; i++) {
1151 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1154 err = got_pathlist_append(paths, path, NULL);
1164 static const struct got_error *
1165 cmd_update(int argc, char *argv[])
1167 const struct got_error *error = NULL;
1168 struct got_repository *repo = NULL;
1169 struct got_worktree *worktree = NULL;
1170 char *worktree_path = NULL;
1171 struct got_object_id *commit_id = NULL;
1172 char *commit_id_str = NULL;
1173 const char *branch_name = NULL;
1174 struct got_reference *head_ref = NULL;
1175 struct got_pathlist_head paths;
1176 struct got_pathlist_entry *pe;
1177 int ch, did_something = 0;
1181 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1184 branch_name = optarg;
1187 commit_id_str = strdup(optarg);
1188 if (commit_id_str == NULL)
1189 return got_error_from_errno("strdup");
1201 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1202 "unveil", NULL) == -1)
1205 worktree_path = getcwd(NULL, 0);
1206 if (worktree_path == NULL) {
1207 error = got_error_from_errno("getcwd");
1210 error = got_worktree_open(&worktree, worktree_path);
1214 error = check_rebase_or_histedit_in_progress(worktree);
1218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1222 error = apply_unveil(got_repo_get_path(repo), 0,
1223 got_worktree_get_root_path(worktree));
1227 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1231 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1232 got_worktree_get_head_ref_name(worktree), 0);
1235 if (commit_id_str == NULL) {
1236 error = got_ref_resolve(&commit_id, repo, head_ref);
1239 error = got_object_id_str(&commit_id_str, commit_id);
1243 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1244 free(commit_id_str);
1245 commit_id_str = NULL;
1248 error = got_object_id_str(&commit_id_str, commit_id);
1254 struct got_object_id *head_commit_id;
1255 TAILQ_FOREACH(pe, &paths, entry) {
1256 if (pe->path_len == 0)
1258 error = got_error_msg(GOT_ERR_BAD_PATH,
1259 "switching between branches requires that "
1260 "the entire work tree gets updated");
1263 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1266 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1267 free(head_commit_id);
1270 error = check_same_branch(commit_id, head_ref, NULL, repo);
1273 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1277 error = check_linear_ancestry(commit_id,
1278 got_worktree_get_base_commit_id(worktree), repo);
1279 if (error != NULL) {
1280 if (error->code == GOT_ERR_ANCESTRY)
1281 error = got_error(GOT_ERR_BRANCH_MOVED);
1284 error = check_same_branch(commit_id, head_ref, NULL, repo);
1289 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1291 error = got_worktree_set_base_commit_id(worktree, repo,
1297 error = got_worktree_checkout_files(worktree, &paths, repo,
1298 update_progress, &did_something, check_cancelled, NULL);
1303 printf("Updated to commit %s\n", commit_id_str);
1305 printf("Already up-to-date\n");
1307 free(worktree_path);
1308 TAILQ_FOREACH(pe, &paths, entry)
1309 free((char *)pe->path);
1310 got_pathlist_free(&paths);
1312 free(commit_id_str);
1316 static const struct got_error *
1317 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1318 int diff_context, struct got_repository *repo)
1320 const struct got_error *err = NULL;
1321 struct got_tree_object *tree1 = NULL, *tree2;
1322 struct got_object_qid *qid;
1323 char *id_str1 = NULL, *id_str2;
1324 struct got_diff_blob_output_unidiff_arg arg;
1326 err = got_object_open_as_tree(&tree2, repo,
1327 got_object_commit_get_tree_id(commit));
1331 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1333 struct got_commit_object *pcommit;
1335 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1339 err = got_object_open_as_tree(&tree1, repo,
1340 got_object_commit_get_tree_id(pcommit));
1341 got_object_commit_close(pcommit);
1345 err = got_object_id_str(&id_str1, qid->id);
1350 err = got_object_id_str(&id_str2, id);
1354 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1355 arg.diff_context = diff_context;
1356 arg.outfile = stdout;
1357 err = got_diff_tree(tree1, tree2, "", "", repo,
1358 got_diff_blob_output_unidiff, &arg, 1);
1361 got_object_tree_close(tree1);
1362 got_object_tree_close(tree2);
1369 get_datestr(time_t *time, char *datebuf)
1371 struct tm mytm, *tm;
1374 tm = gmtime_r(time, &mytm);
1377 s = asctime_r(tm, datebuf);
1380 p = strchr(s, '\n');
1386 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1388 static const struct got_error *
1389 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1390 struct got_repository *repo, int show_patch, int diff_context,
1391 struct got_reflist_head *refs)
1393 const struct got_error *err = NULL;
1394 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1396 time_t committer_time;
1397 const char *author, *committer;
1398 char *refs_str = NULL;
1399 struct got_reflist_entry *re;
1401 SIMPLEQ_FOREACH(re, refs, entry) {
1404 struct got_tag_object *tag = NULL;
1407 name = got_ref_get_name(re->ref);
1408 if (strcmp(name, GOT_REF_HEAD) == 0)
1410 if (strncmp(name, "refs/", 5) == 0)
1412 if (strncmp(name, "got/", 4) == 0)
1414 if (strncmp(name, "heads/", 6) == 0)
1416 if (strncmp(name, "remotes/", 8) == 0)
1418 if (strncmp(name, "tags/", 5) == 0) {
1419 err = got_object_open_as_tag(&tag, repo, re->id);
1421 if (err->code != GOT_ERR_OBJ_TYPE)
1423 /* Ref points at something other than a tag. */
1428 cmp = got_object_id_cmp(tag ?
1429 got_object_tag_get_object_id(tag) : re->id, id);
1431 got_object_tag_close(tag);
1435 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1437 err = got_error_from_errno("asprintf");
1443 err = got_object_id_str(&id_str, id);
1447 printf(GOT_COMMIT_SEP_STR);
1448 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1449 refs_str ? refs_str : "", refs_str ? ")" : "");
1454 printf("from: %s\n", got_object_commit_get_author(commit));
1455 committer_time = got_object_commit_get_committer_time(commit);
1456 datestr = get_datestr(&committer_time, datebuf);
1458 printf("date: %s UTC\n", datestr);
1459 author = got_object_commit_get_author(commit);
1460 committer = got_object_commit_get_committer(commit);
1461 if (strcmp(author, committer) != 0)
1462 printf("via: %s\n", committer);
1463 if (got_object_commit_get_nparents(commit) > 1) {
1464 const struct got_object_id_queue *parent_ids;
1465 struct got_object_qid *qid;
1467 parent_ids = got_object_commit_get_parent_ids(commit);
1468 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1469 err = got_object_id_str(&id_str, qid->id);
1472 printf("parent %d: %s\n", n++, id_str);
1477 err = got_object_commit_get_logmsg(&logmsg0, commit);
1483 line = strsep(&logmsg, "\n");
1485 printf(" %s\n", line);
1490 err = print_patch(commit, id, diff_context, repo);
1495 if (fflush(stdout) != 0 && err == NULL)
1496 err = got_error_from_errno("fflush");
1500 static const struct got_error *
1501 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1502 char *path, int show_patch, int diff_context, int limit,
1503 int first_parent_traversal, struct got_reflist_head *refs)
1505 const struct got_error *err;
1506 struct got_commit_graph *graph;
1508 err = got_commit_graph_open(&graph, root_id, path,
1509 first_parent_traversal, repo);
1512 err = got_commit_graph_iter_start(graph, root_id, repo,
1513 check_cancelled, NULL);
1517 struct got_commit_object *commit;
1518 struct got_object_id *id;
1520 if (sigint_received || sigpipe_received)
1523 err = got_commit_graph_iter_next(&id, graph);
1525 if (err->code == GOT_ERR_ITER_COMPLETED) {
1529 if (err->code != GOT_ERR_ITER_NEED_MORE)
1531 err = got_commit_graph_fetch_commits(graph, 1, repo,
1532 check_cancelled, NULL);
1541 err = got_object_open_as_commit(&commit, repo, id);
1544 err = print_commit(commit, id, repo, show_patch, diff_context,
1546 got_object_commit_close(commit);
1547 if (err || (limit && --limit == 0))
1551 got_commit_graph_close(graph);
1558 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1559 "[-r repository-path] [path]\n", getprogname());
1564 get_default_log_limit(void)
1566 const char *got_default_log_limit;
1570 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1571 if (got_default_log_limit == NULL)
1573 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1579 static const struct got_error *
1580 cmd_log(int argc, char *argv[])
1582 const struct got_error *error;
1583 struct got_repository *repo = NULL;
1584 struct got_worktree *worktree = NULL;
1585 struct got_commit_object *commit = NULL;
1586 struct got_object_id *id = NULL;
1587 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1588 char *start_commit = NULL;
1589 int diff_context = 3, ch;
1590 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1592 struct got_reflist_head refs;
1594 SIMPLEQ_INIT(&refs);
1597 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1603 limit = get_default_log_limit();
1605 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1611 start_commit = optarg;
1614 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1617 err(1, "-C option %s", errstr);
1620 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1622 err(1, "-l option %s", errstr);
1625 first_parent_traversal = 1;
1628 repo_path = realpath(optarg, NULL);
1629 if (repo_path == NULL)
1630 err(1, "-r option");
1631 got_path_strip_trailing_slashes(repo_path);
1642 cwd = getcwd(NULL, 0);
1644 error = got_error_from_errno("getcwd");
1648 error = got_worktree_open(&worktree, cwd);
1649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1656 error = got_error_from_errno("strdup");
1659 } else if (argc == 1) {
1661 error = got_worktree_resolve_path(&path, worktree,
1666 path = strdup(argv[0]);
1668 error = got_error_from_errno("strdup");
1675 if (repo_path == NULL) {
1676 repo_path = worktree ?
1677 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1679 if (repo_path == NULL) {
1680 error = got_error_from_errno("strdup");
1684 error = got_repo_open(&repo, repo_path);
1688 error = apply_unveil(got_repo_get_path(repo), 1,
1689 worktree ? got_worktree_get_root_path(worktree) : NULL);
1693 if (start_commit == NULL) {
1694 struct got_reference *head_ref;
1695 error = got_ref_open(&head_ref, repo,
1696 worktree ? got_worktree_get_head_ref_name(worktree)
1700 error = got_ref_resolve(&id, repo, head_ref);
1701 got_ref_close(head_ref);
1704 error = got_object_open_as_commit(&commit, repo, id);
1706 struct got_reference *ref;
1707 error = got_ref_open(&ref, repo, start_commit, 0);
1708 if (error == NULL) {
1710 error = got_ref_resolve(&id, repo, ref);
1714 error = got_object_get_type(&obj_type, repo, id);
1717 if (obj_type == GOT_OBJ_TYPE_TAG) {
1718 struct got_tag_object *tag;
1719 error = got_object_open_as_tag(&tag, repo, id);
1722 if (got_object_tag_get_object_type(tag) !=
1723 GOT_OBJ_TYPE_COMMIT) {
1724 got_object_tag_close(tag);
1725 error = got_error(GOT_ERR_OBJ_TYPE);
1729 id = got_object_id_dup(
1730 got_object_tag_get_object_id(tag));
1732 error = got_error_from_errno(
1733 "got_object_id_dup");
1734 got_object_tag_close(tag);
1737 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1738 error = got_error(GOT_ERR_OBJ_TYPE);
1741 error = got_object_open_as_commit(&commit, repo, id);
1745 if (commit == NULL) {
1746 error = got_repo_match_object_id_prefix(&id,
1747 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1755 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1760 path = in_repo_path;
1763 error = got_ref_list(&refs, repo);
1767 error = print_commits(id, repo, path, show_patch,
1768 diff_context, limit, first_parent_traversal, &refs);
1775 got_worktree_close(worktree);
1777 const struct got_error *repo_error;
1778 repo_error = got_repo_close(repo);
1782 got_ref_list_free(&refs);
1789 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1790 "[object1 object2 | path]\n", getprogname());
1794 struct print_diff_arg {
1795 struct got_repository *repo;
1796 struct got_worktree *worktree;
1803 static const struct got_error *
1804 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1805 const char *path, struct got_object_id *blob_id,
1806 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1808 struct print_diff_arg *a = arg;
1809 const struct got_error *err = NULL;
1810 struct got_blob_object *blob1 = NULL;
1812 char *abspath = NULL, *label1 = NULL;
1815 if (a->diff_staged) {
1816 if (staged_status != GOT_STATUS_MODIFY &&
1817 staged_status != GOT_STATUS_ADD &&
1818 staged_status != GOT_STATUS_DELETE)
1821 if (staged_status == GOT_STATUS_DELETE)
1823 if (status != GOT_STATUS_MODIFY &&
1824 status != GOT_STATUS_ADD &&
1825 status != GOT_STATUS_DELETE &&
1826 status != GOT_STATUS_CONFLICT)
1830 if (!a->header_shown) {
1831 printf("diff %s %s%s\n", a->id_str,
1832 got_worktree_get_root_path(a->worktree),
1833 a->diff_staged ? " (staged changes)" : "");
1834 a->header_shown = 1;
1837 if (a->diff_staged) {
1838 const char *label1 = NULL, *label2 = NULL;
1839 switch (staged_status) {
1840 case GOT_STATUS_MODIFY:
1844 case GOT_STATUS_ADD:
1847 case GOT_STATUS_DELETE:
1851 return got_error(GOT_ERR_FILE_STATUS);
1853 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1854 label1, label2, a->diff_context, a->repo, stdout);
1857 if (staged_status == GOT_STATUS_ADD ||
1858 staged_status == GOT_STATUS_MODIFY) {
1860 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1864 err = got_object_id_str(&id_str, staged_blob_id);
1867 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1868 err = got_error_from_errno("asprintf");
1873 } else if (status != GOT_STATUS_ADD) {
1874 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1879 if (status != GOT_STATUS_DELETE) {
1880 if (asprintf(&abspath, "%s/%s",
1881 got_worktree_get_root_path(a->worktree), path) == -1) {
1882 err = got_error_from_errno("asprintf");
1886 f2 = fopen(abspath, "r");
1888 err = got_error_from_errno2("fopen", abspath);
1891 if (lstat(abspath, &sb) == -1) {
1892 err = got_error_from_errno2("lstat", abspath);
1898 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1899 a->diff_context, stdout);
1902 got_object_blob_close(blob1);
1903 if (f2 && fclose(f2) != 0 && err == NULL)
1904 err = got_error_from_errno("fclose");
1909 static const struct got_error *
1910 match_object_id(struct got_object_id **id, char **label,
1911 const char *id_str, int obj_type, int resolve_tags,
1912 struct got_repository *repo)
1914 const struct got_error *err;
1915 struct got_tag_object *tag;
1916 struct got_reference *ref = NULL;
1922 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1925 *id = got_object_id_dup(
1926 got_object_tag_get_object_id(tag));
1928 err = got_error_from_errno("got_object_id_dup");
1929 else if (asprintf(label, "refs/tags/%s",
1930 got_object_tag_get_name(tag)) == -1) {
1931 err = got_error_from_errno("asprintf");
1935 got_object_tag_close(tag);
1937 } else if (err->code != GOT_ERR_NO_OBJ)
1941 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1943 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1945 err = got_ref_open(&ref, repo, id_str, 0);
1948 *label = strdup(got_ref_get_name(ref));
1949 if (*label == NULL) {
1950 err = got_error_from_errno("strdup");
1953 err = got_ref_resolve(id, repo, ref);
1955 err = got_object_id_str(label, *id);
1956 if (*label == NULL) {
1957 err = got_error_from_errno("strdup");
1968 static const struct got_error *
1969 cmd_diff(int argc, char *argv[])
1971 const struct got_error *error;
1972 struct got_repository *repo = NULL;
1973 struct got_worktree *worktree = NULL;
1974 char *cwd = NULL, *repo_path = NULL;
1975 struct got_object_id *id1 = NULL, *id2 = NULL;
1976 const char *id_str1 = NULL, *id_str2 = NULL;
1977 char *label1 = NULL, *label2 = NULL;
1979 int diff_context = 3, diff_staged = 0, ch;
1984 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1989 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1992 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1994 err(1, "-C option %s", errstr);
1997 repo_path = realpath(optarg, NULL);
1998 if (repo_path == NULL)
1999 err(1, "-r option");
2000 got_path_strip_trailing_slashes(repo_path);
2014 cwd = getcwd(NULL, 0);
2016 error = got_error_from_errno("getcwd");
2019 error = got_worktree_open(&worktree, cwd);
2020 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2023 if (worktree == NULL) {
2024 error = got_error(GOT_ERR_NOT_WORKTREE);
2029 "-r option can't be used when diffing a work tree");
2030 repo_path = strdup(got_worktree_get_repo_path(worktree));
2031 if (repo_path == NULL) {
2032 error = got_error_from_errno("strdup");
2036 error = got_worktree_resolve_path(&path, worktree,
2043 error = got_error_from_errno("strdup");
2047 } else if (argc == 2) {
2049 errx(1, "-s option can't be used when diffing "
2050 "objects in repository");
2053 if (worktree && repo_path == NULL) {
2055 strdup(got_worktree_get_repo_path(worktree));
2056 if (repo_path == NULL) {
2057 error = got_error_from_errno("strdup");
2064 if (repo_path == NULL) {
2065 repo_path = getcwd(NULL, 0);
2066 if (repo_path == NULL)
2067 return got_error_from_errno("getcwd");
2070 error = got_repo_open(&repo, repo_path);
2075 error = apply_unveil(got_repo_get_path(repo), 1,
2076 worktree ? got_worktree_get_root_path(worktree) : NULL);
2081 struct print_diff_arg arg;
2082 struct got_pathlist_head paths;
2087 error = got_object_id_str(&id_str,
2088 got_worktree_get_base_commit_id(worktree));
2092 arg.worktree = worktree;
2093 arg.diff_context = diff_context;
2094 arg.id_str = id_str;
2095 arg.header_shown = 0;
2096 arg.diff_staged = diff_staged;
2098 error = got_pathlist_append(&paths, path, NULL);
2102 error = got_worktree_status(worktree, &paths, repo, print_diff,
2103 &arg, check_cancelled, NULL);
2105 got_pathlist_free(&paths);
2109 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2114 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2119 error = got_object_get_type(&type1, repo, id1);
2123 error = got_object_get_type(&type2, repo, id2);
2127 if (type1 != type2) {
2128 error = got_error(GOT_ERR_OBJ_TYPE);
2133 case GOT_OBJ_TYPE_BLOB:
2134 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2135 diff_context, repo, stdout);
2137 case GOT_OBJ_TYPE_TREE:
2138 error = got_diff_objects_as_trees(id1, id2, "", "",
2139 diff_context, repo, stdout);
2141 case GOT_OBJ_TYPE_COMMIT:
2142 printf("diff %s %s\n", label1, label2);
2143 error = got_diff_objects_as_commits(id1, id2, diff_context,
2147 error = got_error(GOT_ERR_OBJ_TYPE);
2157 got_worktree_close(worktree);
2159 const struct got_error *repo_error;
2160 repo_error = got_repo_close(repo);
2171 "usage: %s blame [-c commit] [-r repository-path] path\n",
2180 char datebuf[9]; /* YY-MM-DD + NUL */
2183 struct blame_cb_args {
2184 struct blame_line *lines;
2188 off_t *line_offsets;
2190 struct got_repository *repo;
2193 static const struct got_error *
2194 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2196 const struct got_error *err = NULL;
2197 struct blame_cb_args *a = arg;
2198 struct blame_line *bline;
2200 size_t linesize = 0;
2201 struct got_commit_object *commit = NULL;
2204 time_t committer_time;
2206 if (nlines != a->nlines ||
2207 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2208 return got_error(GOT_ERR_RANGE);
2210 if (sigint_received)
2211 return got_error(GOT_ERR_ITER_COMPLETED);
2214 return NULL; /* no change in this commit */
2216 /* Annotate this line. */
2217 bline = &a->lines[lineno - 1];
2218 if (bline->annotated)
2220 err = got_object_id_str(&bline->id_str, id);
2224 err = got_object_open_as_commit(&commit, a->repo, id);
2228 bline->committer = strdup(got_object_commit_get_committer(commit));
2229 if (bline->committer == NULL) {
2230 err = got_error_from_errno("strdup");
2234 committer_time = got_object_commit_get_committer_time(commit);
2235 if (localtime_r(&committer_time, &tm) == NULL)
2236 return got_error_from_errno("localtime_r");
2237 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2238 &tm) >= sizeof(bline->datebuf)) {
2239 err = got_error(GOT_ERR_NO_SPACE);
2242 bline->annotated = 1;
2244 /* Print lines annotated so far. */
2245 bline = &a->lines[a->lineno_cur - 1];
2246 if (!bline->annotated)
2249 offset = a->line_offsets[a->lineno_cur - 1];
2250 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2251 err = got_error_from_errno("fseeko");
2255 while (bline->annotated) {
2256 char *smallerthan, *at, *nl, *committer;
2259 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2261 err = got_error_from_errno("getline");
2265 committer = bline->committer;
2266 smallerthan = strchr(committer, '<');
2267 if (smallerthan && smallerthan[1] != '\0')
2268 committer = smallerthan + 1;
2269 at = strchr(committer, '@');
2272 len = strlen(committer);
2274 committer[8] = '\0';
2276 nl = strchr(line, '\n');
2279 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2280 bline->id_str, bline->datebuf, committer, line);
2283 bline = &a->lines[a->lineno_cur - 1];
2287 got_object_commit_close(commit);
2292 static const struct got_error *
2293 cmd_blame(int argc, char *argv[])
2295 const struct got_error *error;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2299 struct got_object_id *obj_id = NULL;
2300 struct got_object_id *commit_id = NULL;
2301 struct got_blob_object *blob = NULL;
2302 char *commit_id_str = NULL;
2303 struct blame_cb_args bca;
2304 int ch, obj_type, i;
2307 memset(&bca, 0, sizeof(bca));
2310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2315 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2318 commit_id_str = optarg;
2321 repo_path = realpath(optarg, NULL);
2322 if (repo_path == NULL)
2323 err(1, "-r option");
2324 got_path_strip_trailing_slashes(repo_path);
2340 cwd = getcwd(NULL, 0);
2342 error = got_error_from_errno("getcwd");
2345 if (repo_path == NULL) {
2346 error = got_worktree_open(&worktree, cwd);
2347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2353 strdup(got_worktree_get_repo_path(worktree));
2354 if (repo_path == NULL)
2355 error = got_error_from_errno("strdup");
2359 repo_path = strdup(cwd);
2360 if (repo_path == NULL) {
2361 error = got_error_from_errno("strdup");
2367 error = got_repo_open(&repo, repo_path);
2371 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2376 const char *prefix = got_worktree_get_path_prefix(worktree);
2377 char *p, *worktree_subdir = cwd +
2378 strlen(got_worktree_get_root_path(worktree));
2379 if (asprintf(&p, "%s%s%s%s%s",
2380 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2381 worktree_subdir, worktree_subdir[0] ? "/" : "",
2383 error = got_error_from_errno("asprintf");
2386 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2389 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2394 if (commit_id_str == NULL) {
2395 struct got_reference *head_ref;
2396 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2399 error = got_ref_resolve(&commit_id, repo, head_ref);
2400 got_ref_close(head_ref);
2404 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2409 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2412 if (obj_id == NULL) {
2413 error = got_error(GOT_ERR_NO_OBJ);
2417 error = got_object_get_type(&obj_type, repo, obj_id);
2421 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2422 error = got_error(GOT_ERR_OBJ_TYPE);
2426 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2429 bca.f = got_opentemp();
2430 if (bca.f == NULL) {
2431 error = got_error_from_errno("got_opentemp");
2434 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2435 &bca.line_offsets, bca.f, blob);
2436 if (error || bca.nlines == 0)
2439 /* Don't include \n at EOF in the blame line count. */
2440 if (bca.line_offsets[bca.nlines - 1] == filesize)
2443 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2444 if (bca.lines == NULL) {
2445 error = got_error_from_errno("calloc");
2449 bca.nlines_prec = 0;
2457 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2458 check_cancelled, NULL);
2468 got_object_blob_close(blob);
2470 got_worktree_close(worktree);
2472 const struct got_error *repo_error;
2473 repo_error = got_repo_close(repo);
2477 for (i = 0; i < bca.nlines; i++) {
2478 struct blame_line *bline = &bca.lines[i];
2479 free(bline->id_str);
2480 free(bline->committer);
2483 free(bca.line_offsets);
2484 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2485 error = got_error_from_errno("fclose");
2493 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2499 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2500 const char *root_path)
2502 int is_root_path = (strcmp(path, root_path) == 0);
2503 const char *modestr = "";
2505 path += strlen(root_path);
2506 while (path[0] == '/')
2509 if (S_ISLNK(te->mode))
2511 else if (S_ISDIR(te->mode))
2513 else if (te->mode & S_IXUSR)
2516 printf("%s%s%s%s%s\n", id ? id : "", path,
2517 is_root_path ? "" : "/", te->name, modestr);
2520 static const struct got_error *
2521 print_tree(const char *path, struct got_object_id *commit_id,
2522 int show_ids, int recurse, const char *root_path,
2523 struct got_repository *repo)
2525 const struct got_error *err = NULL;
2526 struct got_object_id *tree_id = NULL;
2527 struct got_tree_object *tree = NULL;
2528 const struct got_tree_entries *entries;
2529 struct got_tree_entry *te;
2531 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2535 err = got_object_open_as_tree(&tree, repo, tree_id);
2538 entries = got_object_tree_get_entries(tree);
2539 te = SIMPLEQ_FIRST(&entries->head);
2543 if (sigint_received || sigpipe_received)
2548 err = got_object_id_str(&id_str, te->id);
2551 if (asprintf(&id, "%s ", id_str) == -1) {
2552 err = got_error_from_errno("asprintf");
2558 print_entry(te, id, path, root_path);
2561 if (recurse && S_ISDIR(te->mode)) {
2563 if (asprintf(&child_path, "%s%s%s", path,
2564 path[0] == '/' && path[1] == '\0' ? "" : "/",
2566 err = got_error_from_errno("asprintf");
2569 err = print_tree(child_path, commit_id, show_ids, 1,
2576 te = SIMPLEQ_NEXT(te, entry);
2580 got_object_tree_close(tree);
2585 static const struct got_error *
2586 cmd_tree(int argc, char *argv[])
2588 const struct got_error *error;
2589 struct got_repository *repo = NULL;
2590 struct got_worktree *worktree = NULL;
2592 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2593 struct got_object_id *commit_id = NULL;
2594 char *commit_id_str = NULL;
2595 int show_ids = 0, recurse = 0;
2599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2604 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2607 commit_id_str = optarg;
2610 repo_path = realpath(optarg, NULL);
2611 if (repo_path == NULL)
2612 err(1, "-r option");
2613 got_path_strip_trailing_slashes(repo_path);
2637 cwd = getcwd(NULL, 0);
2639 error = got_error_from_errno("getcwd");
2642 if (repo_path == NULL) {
2643 error = got_worktree_open(&worktree, cwd);
2644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2650 strdup(got_worktree_get_repo_path(worktree));
2651 if (repo_path == NULL)
2652 error = got_error_from_errno("strdup");
2656 repo_path = strdup(cwd);
2657 if (repo_path == NULL) {
2658 error = got_error_from_errno("strdup");
2664 error = got_repo_open(&repo, repo_path);
2668 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2674 char *p, *worktree_subdir = cwd +
2675 strlen(got_worktree_get_root_path(worktree));
2676 if (asprintf(&p, "%s/%s",
2677 got_worktree_get_path_prefix(worktree),
2678 worktree_subdir) == -1) {
2679 error = got_error_from_errno("asprintf");
2682 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2689 if (in_repo_path == NULL) {
2690 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2695 if (commit_id_str == NULL) {
2696 struct got_reference *head_ref;
2697 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2700 error = got_ref_resolve(&commit_id, repo, head_ref);
2701 got_ref_close(head_ref);
2705 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2710 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2711 in_repo_path, repo);
2718 got_worktree_close(worktree);
2720 const struct got_error *repo_error;
2721 repo_error = got_repo_close(repo);
2731 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2735 static const struct got_error *
2736 print_status(void *arg, unsigned char status, unsigned char staged_status,
2737 const char *path, struct got_object_id *blob_id,
2738 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2740 if (status == staged_status && (status == GOT_STATUS_DELETE))
2741 status = GOT_STATUS_NO_CHANGE;
2742 printf("%c%c %s\n", status, staged_status, path);
2746 static const struct got_error *
2747 cmd_status(int argc, char *argv[])
2749 const struct got_error *error = NULL;
2750 struct got_repository *repo = NULL;
2751 struct got_worktree *worktree = NULL;
2753 struct got_pathlist_head paths;
2754 struct got_pathlist_entry *pe;
2759 while ((ch = getopt(argc, argv, "")) != -1) {
2771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2775 cwd = getcwd(NULL, 0);
2777 error = got_error_from_errno("getcwd");
2781 error = got_worktree_open(&worktree, cwd);
2785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2789 error = apply_unveil(got_repo_get_path(repo), 1,
2790 got_worktree_get_root_path(worktree));
2794 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2798 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2799 check_cancelled, NULL);
2801 TAILQ_FOREACH(pe, &paths, entry)
2802 free((char *)pe->path);
2803 got_pathlist_free(&paths);
2812 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2817 static const struct got_error *
2818 list_refs(struct got_repository *repo)
2820 static const struct got_error *err = NULL;
2821 struct got_reflist_head refs;
2822 struct got_reflist_entry *re;
2824 SIMPLEQ_INIT(&refs);
2825 err = got_ref_list(&refs, repo);
2829 SIMPLEQ_FOREACH(re, &refs, entry) {
2831 refstr = got_ref_to_str(re->ref);
2833 return got_error_from_errno("got_ref_to_str");
2834 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2838 got_ref_list_free(&refs);
2842 static const struct got_error *
2843 delete_ref(struct got_repository *repo, const char *refname)
2845 const struct got_error *err = NULL;
2846 struct got_reference *ref;
2848 err = got_ref_open(&ref, repo, refname, 0);
2852 err = got_ref_delete(ref, repo);
2857 static const struct got_error *
2858 add_ref(struct got_repository *repo, const char *refname, const char *target)
2860 const struct got_error *err = NULL;
2861 struct got_object_id *id;
2862 struct got_reference *ref = NULL;
2865 * Don't let the user create a reference named '-'.
2866 * While technically a valid reference name, this case is usually
2867 * an unintended typo.
2869 if (refname[0] == '-' && refname[1] == '\0')
2870 return got_error(GOT_ERR_BAD_REF_NAME);
2872 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2875 struct got_reference *target_ref;
2877 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2879 err = got_ref_open(&target_ref, repo, target, 0);
2882 err = got_ref_resolve(&id, repo, target_ref);
2883 got_ref_close(target_ref);
2888 err = got_ref_alloc(&ref, refname, id);
2892 err = got_ref_write(ref, repo);
2900 static const struct got_error *
2901 add_symref(struct got_repository *repo, const char *refname, const char *target)
2903 const struct got_error *err = NULL;
2904 struct got_reference *ref = NULL;
2905 struct got_reference *target_ref = NULL;
2908 * Don't let the user create a reference named '-'.
2909 * While technically a valid reference name, this case is usually
2910 * an unintended typo.
2912 if (refname[0] == '-' && refname[1] == '\0')
2913 return got_error(GOT_ERR_BAD_REF_NAME);
2915 err = got_ref_open(&target_ref, repo, target, 0);
2919 err = got_ref_alloc_symref(&ref, refname, target_ref);
2923 err = got_ref_write(ref, repo);
2926 got_ref_close(target_ref);
2932 static const struct got_error *
2933 cmd_ref(int argc, char *argv[])
2935 const struct got_error *error = NULL;
2936 struct got_repository *repo = NULL;
2937 struct got_worktree *worktree = NULL;
2938 char *cwd = NULL, *repo_path = NULL;
2939 int ch, do_list = 0, create_symref = 0;
2940 const char *delref = NULL;
2942 /* TODO: Add -s option for adding symbolic references. */
2943 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 err(1, "-r option");
2952 got_path_strip_trailing_slashes(repo_path);
2966 if (do_list && delref)
2967 errx(1, "-l and -d options are mutually exclusive\n");
2972 if (do_list || delref) {
2974 errx(1, "-s option cannot be used together with the "
2975 "-l or -d options");
2978 } else if (argc != 2)
2983 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2987 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2988 "sendfd unveil", NULL) == -1)
2992 cwd = getcwd(NULL, 0);
2994 error = got_error_from_errno("getcwd");
2998 if (repo_path == NULL) {
2999 error = got_worktree_open(&worktree, cwd);
3000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3006 strdup(got_worktree_get_repo_path(worktree));
3007 if (repo_path == NULL)
3008 error = got_error_from_errno("strdup");
3012 repo_path = strdup(cwd);
3013 if (repo_path == NULL) {
3014 error = got_error_from_errno("strdup");
3020 error = got_repo_open(&repo, repo_path);
3024 error = apply_unveil(got_repo_get_path(repo), do_list,
3025 worktree ? got_worktree_get_root_path(worktree) : NULL);
3030 error = list_refs(repo);
3032 error = delete_ref(repo, delref);
3033 else if (create_symref)
3034 error = add_symref(repo, argv[0], argv[1]);
3036 error = add_ref(repo, argv[0], argv[1]);
3039 got_repo_close(repo);
3041 got_worktree_close(worktree);
3051 "usage: %s branch [-r repository] -l | -d name | "
3052 "name [base-branch]\n", getprogname());
3056 static const struct got_error *
3057 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3059 static const struct got_error *err = NULL;
3060 struct got_reflist_head refs;
3061 struct got_reflist_entry *re;
3063 SIMPLEQ_INIT(&refs);
3065 err = got_ref_list(&refs, repo);
3069 SIMPLEQ_FOREACH(re, &refs, entry) {
3070 const char *refname, *marker = " ";
3072 refname = got_ref_get_name(re->ref);
3073 if (strncmp(refname, "refs/heads/", 11) != 0)
3075 if (worktree && strcmp(refname,
3076 got_worktree_get_head_ref_name(worktree)) == 0) {
3077 struct got_object_id *id = NULL;
3078 err = got_ref_resolve(&id, repo, re->ref);
3081 if (got_object_id_cmp(id,
3082 got_worktree_get_base_commit_id(worktree)) == 0)
3089 refstr = got_ref_to_str(re->ref);
3091 return got_error_from_errno("got_ref_to_str");
3092 printf("%s%s: %s\n", marker, refname, refstr);
3096 got_ref_list_free(&refs);
3100 static const struct got_error *
3101 delete_branch(struct got_repository *repo, const char *branch_name)
3103 const struct got_error *err = NULL;
3104 struct got_reference *ref;
3107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3108 return got_error_from_errno("asprintf");
3110 err = got_ref_open(&ref, repo, refname, 0);
3114 err = got_ref_delete(ref, repo);
3121 static const struct got_error *
3122 add_branch(struct got_repository *repo, const char *branch_name,
3123 const char *base_branch)
3125 const struct got_error *err = NULL;
3126 struct got_object_id *id = NULL;
3127 struct got_reference *ref = NULL;
3128 char *base_refname = NULL, *refname = NULL;
3129 struct got_reference *base_ref;
3132 * Don't let the user create a branch named '-'.
3133 * While technically a valid reference name, this case is usually
3134 * an unintended typo.
3136 if (branch_name[0] == '-' && branch_name[1] == '\0')
3137 return got_error(GOT_ERR_BAD_REF_NAME);
3139 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3140 base_refname = strdup(GOT_REF_HEAD);
3141 if (base_refname == NULL)
3142 return got_error_from_errno("strdup");
3143 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3144 return got_error_from_errno("asprintf");
3146 err = got_ref_open(&base_ref, repo, base_refname, 0);
3149 err = got_ref_resolve(&id, repo, base_ref);
3150 got_ref_close(base_ref);
3154 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3155 err = got_error_from_errno("asprintf");
3159 err = got_ref_open(&ref, repo, refname, 0);
3161 err = got_error(GOT_ERR_BRANCH_EXISTS);
3163 } else if (err->code != GOT_ERR_NOT_REF)
3166 err = got_ref_alloc(&ref, refname, id);
3170 err = got_ref_write(ref, repo);
3180 static const struct got_error *
3181 cmd_branch(int argc, char *argv[])
3183 const struct got_error *error = NULL;
3184 struct got_repository *repo = NULL;
3185 struct got_worktree *worktree = NULL;
3186 char *cwd = NULL, *repo_path = NULL;
3187 int ch, do_list = 0;
3188 const char *delref = NULL;
3190 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3196 repo_path = realpath(optarg, NULL);
3197 if (repo_path == NULL)
3198 err(1, "-r option");
3199 got_path_strip_trailing_slashes(repo_path);
3210 if (do_list && delref)
3211 errx(1, "-l and -d options are mutually exclusive\n");
3216 if (do_list || delref) {
3219 } else if (argc < 1 || argc > 2)
3224 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3228 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3229 "sendfd unveil", NULL) == -1)
3233 cwd = getcwd(NULL, 0);
3235 error = got_error_from_errno("getcwd");
3239 if (repo_path == NULL) {
3240 error = got_worktree_open(&worktree, cwd);
3241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3247 strdup(got_worktree_get_repo_path(worktree));
3248 if (repo_path == NULL)
3249 error = got_error_from_errno("strdup");
3253 repo_path = strdup(cwd);
3254 if (repo_path == NULL) {
3255 error = got_error_from_errno("strdup");
3261 error = got_repo_open(&repo, repo_path);
3265 error = apply_unveil(got_repo_get_path(repo), do_list,
3266 worktree ? got_worktree_get_root_path(worktree) : NULL);
3271 error = list_branches(repo, worktree);
3273 error = delete_branch(repo, delref);
3275 const char *base_branch;
3277 base_branch = worktree ?
3278 got_worktree_get_head_ref_name(worktree) :
3280 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3283 base_branch = argv[1];
3284 error = add_branch(repo, argv[0], base_branch);
3288 got_repo_close(repo);
3290 got_worktree_close(worktree);
3299 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3303 static const struct got_error *
3304 cmd_add(int argc, char *argv[])
3306 const struct got_error *error = NULL;
3307 struct got_repository *repo = NULL;
3308 struct got_worktree *worktree = NULL;
3310 struct got_pathlist_head paths;
3311 struct got_pathlist_entry *pe;
3316 while ((ch = getopt(argc, argv, "")) != -1) {
3328 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3335 cwd = getcwd(NULL, 0);
3337 error = got_error_from_errno("getcwd");
3341 error = got_worktree_open(&worktree, cwd);
3345 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3349 error = apply_unveil(got_repo_get_path(repo), 1,
3350 got_worktree_get_root_path(worktree));
3354 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3358 error = got_worktree_schedule_add(worktree, &paths, print_status,
3362 got_repo_close(repo);
3364 got_worktree_close(worktree);
3365 TAILQ_FOREACH(pe, &paths, entry)
3366 free((char *)pe->path);
3367 got_pathlist_free(&paths);
3375 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3379 static const struct got_error *
3380 cmd_remove(int argc, char *argv[])
3382 const struct got_error *error = NULL;
3383 struct got_worktree *worktree = NULL;
3384 struct got_repository *repo = NULL;
3386 struct got_pathlist_head paths;
3387 struct got_pathlist_entry *pe;
3388 int ch, delete_local_mods = 0;
3392 while ((ch = getopt(argc, argv, "f")) != -1) {
3395 delete_local_mods = 1;
3407 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3414 cwd = getcwd(NULL, 0);
3416 error = got_error_from_errno("getcwd");
3419 error = got_worktree_open(&worktree, cwd);
3423 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3427 error = apply_unveil(got_repo_get_path(repo), 1,
3428 got_worktree_get_root_path(worktree));
3432 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3436 error = got_worktree_schedule_delete(worktree, &paths,
3437 delete_local_mods, print_status, NULL, repo);
3442 got_repo_close(repo);
3444 got_worktree_close(worktree);
3445 TAILQ_FOREACH(pe, &paths, entry)
3446 free((char *)pe->path);
3447 got_pathlist_free(&paths);
3455 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3456 "path ...\n", getprogname());
3460 static const struct got_error *
3461 revert_progress(void *arg, unsigned char status, const char *path)
3463 while (path[0] == '/')
3465 printf("%c %s\n", status, path);
3469 struct choose_patch_arg {
3470 FILE *patch_script_file;
3474 static const struct got_error *
3475 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3476 int nchanges, const char *action)
3479 size_t linesize = 0;
3483 case GOT_STATUS_ADD:
3484 printf("A %s\n%s this addition? [y/n] ", path, action);
3486 case GOT_STATUS_DELETE:
3487 printf("D %s\n%s this deletion? [y/n] ", path, action);
3489 case GOT_STATUS_MODIFY:
3490 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3491 return got_error_from_errno("fseek");
3492 printf(GOT_COMMIT_SEP_STR);
3493 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3495 if (ferror(patch_file))
3496 return got_error_from_errno("getline");
3497 printf(GOT_COMMIT_SEP_STR);
3498 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3499 path, n, nchanges, action);
3502 return got_error_path(path, GOT_ERR_FILE_STATUS);
3508 static const struct got_error *
3509 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3510 FILE *patch_file, int n, int nchanges)
3512 const struct got_error *err = NULL;
3514 size_t linesize = 0;
3517 struct choose_patch_arg *a = arg;
3519 *choice = GOT_PATCH_CHOICE_NONE;
3521 if (a->patch_script_file) {
3523 err = show_change(status, path, patch_file, n, nchanges,
3527 linelen = getline(&line, &linesize, a->patch_script_file);
3528 if (linelen == -1) {
3529 if (ferror(a->patch_script_file))
3530 return got_error_from_errno("getline");
3533 nl = strchr(line, '\n');
3536 if (strcmp(line, "y") == 0) {
3537 *choice = GOT_PATCH_CHOICE_YES;
3539 } else if (strcmp(line, "n") == 0) {
3540 *choice = GOT_PATCH_CHOICE_NO;
3542 } else if (strcmp(line, "q") == 0 &&
3543 status == GOT_STATUS_MODIFY) {
3544 *choice = GOT_PATCH_CHOICE_QUIT;
3547 printf("invalid response '%s'\n", line);
3552 while (resp != 'y' && resp != 'n' && resp != 'q') {
3553 err = show_change(status, path, patch_file, n, nchanges,
3560 if (status == GOT_STATUS_MODIFY) {
3561 if (resp != 'y' && resp != 'n' && resp != 'q') {
3562 printf("invalid response '%c'\n", resp);
3565 } else if (resp != 'y' && resp != 'n') {
3566 printf("invalid response '%c'\n", resp);
3572 *choice = GOT_PATCH_CHOICE_YES;
3573 else if (resp == 'n')
3574 *choice = GOT_PATCH_CHOICE_NO;
3575 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3576 *choice = GOT_PATCH_CHOICE_QUIT;
3582 static const struct got_error *
3583 cmd_revert(int argc, char *argv[])
3585 const struct got_error *error = NULL;
3586 struct got_worktree *worktree = NULL;
3587 struct got_repository *repo = NULL;
3588 char *cwd = NULL, *path = NULL;
3589 struct got_pathlist_head paths;
3590 struct got_pathlist_entry *pe;
3591 int ch, can_recurse = 0, pflag = 0;
3592 FILE *patch_script_file = NULL;
3593 const char *patch_script_path = NULL;
3594 struct choose_patch_arg cpa;
3598 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3604 patch_script_path = optarg;
3619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3620 "unveil", NULL) == -1)
3625 if (patch_script_path && !pflag)
3626 errx(1, "-F option can only be used together with -p option");
3628 cwd = getcwd(NULL, 0);
3630 error = got_error_from_errno("getcwd");
3633 error = got_worktree_open(&worktree, cwd);
3637 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3641 if (patch_script_path) {
3642 patch_script_file = fopen(patch_script_path, "r");
3643 if (patch_script_file == NULL) {
3644 error = got_error_from_errno2("fopen",
3649 error = apply_unveil(got_repo_get_path(repo), 1,
3650 got_worktree_get_root_path(worktree));
3654 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3661 TAILQ_FOREACH(pe, &paths, entry) {
3662 if (asprintf(&ondisk_path, "%s/%s",
3663 got_worktree_get_root_path(worktree),
3665 error = got_error_from_errno("asprintf");
3668 if (lstat(ondisk_path, &sb) == -1) {
3669 if (errno == ENOENT) {
3673 error = got_error_from_errno2("lstat",
3679 if (S_ISDIR(sb.st_mode)) {
3680 error = got_error_msg(GOT_ERR_BAD_PATH,
3681 "reverting directories requires -R option");
3687 cpa.patch_script_file = patch_script_file;
3688 cpa.action = "revert";
3689 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3690 pflag ? choose_patch : NULL, &cpa, repo);
3694 if (patch_script_file && fclose(patch_script_file) == EOF &&
3696 error = got_error_from_errno2("fclose", patch_script_path);
3698 got_repo_close(repo);
3700 got_worktree_close(worktree);
3709 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3714 struct collect_commit_logmsg_arg {
3715 const char *cmdline_log;
3717 const char *worktree_path;
3718 const char *branch_name;
3719 const char *repo_path;
3724 static const struct got_error *
3725 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3728 char *initial_content = NULL;
3729 struct got_pathlist_entry *pe;
3730 const struct got_error *err = NULL;
3731 char *template = NULL;
3732 struct collect_commit_logmsg_arg *a = arg;
3736 /* if a message was specified on the command line, just use it */
3737 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3738 len = strlen(a->cmdline_log) + 1;
3739 *logmsg = malloc(len + 1);
3740 if (*logmsg == NULL)
3741 return got_error_from_errno("malloc");
3742 strlcpy(*logmsg, a->cmdline_log, len);
3746 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3747 return got_error_from_errno("asprintf");
3749 if (asprintf(&initial_content,
3750 "\n# changes to be committed on branch %s:\n",
3751 a->branch_name) == -1)
3752 return got_error_from_errno("asprintf");
3754 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3758 dprintf(fd, initial_content);
3760 TAILQ_FOREACH(pe, commitable_paths, entry) {
3761 struct got_commitable *ct = pe->data;
3762 dprintf(fd, "# %c %s\n",
3763 got_commitable_get_status(ct),
3764 got_commitable_get_path(ct));
3768 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3770 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3771 unlink(a->logmsg_path);
3772 free(a->logmsg_path);
3773 a->logmsg_path = NULL;
3775 free(initial_content);
3778 /* Editor is done; we can now apply unveil(2) */
3780 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3789 static const struct got_error *
3790 cmd_commit(int argc, char *argv[])
3792 const struct got_error *error = NULL;
3793 struct got_worktree *worktree = NULL;
3794 struct got_repository *repo = NULL;
3795 char *cwd = NULL, *id_str = NULL;
3796 struct got_object_id *id = NULL;
3797 const char *logmsg = NULL;
3799 struct collect_commit_logmsg_arg cl_arg;
3800 char *editor = NULL;
3801 int ch, rebase_in_progress, histedit_in_progress;
3802 struct got_pathlist_head paths;
3805 cl_arg.logmsg_path = NULL;
3807 while ((ch = getopt(argc, argv, "m:")) != -1) {
3822 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3823 "unveil", NULL) == -1)
3826 error = get_author(&author);
3830 cwd = getcwd(NULL, 0);
3832 error = got_error_from_errno("getcwd");
3835 error = got_worktree_open(&worktree, cwd);
3839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3842 if (rebase_in_progress) {
3843 error = got_error(GOT_ERR_REBASING);
3847 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3852 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3857 * unveil(2) traverses exec(2); if an editor is used we have
3858 * to apply unveil after the log message has been written.
3860 if (logmsg == NULL || strlen(logmsg) == 0)
3861 error = get_editor(&editor);
3863 error = apply_unveil(got_repo_get_path(repo), 0,
3864 got_worktree_get_root_path(worktree));
3868 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3872 cl_arg.editor = editor;
3873 cl_arg.cmdline_log = logmsg;
3874 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3875 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3876 if (!histedit_in_progress) {
3877 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3878 error = got_error(GOT_ERR_COMMIT_BRANCH);
3881 cl_arg.branch_name += 11;
3883 cl_arg.repo_path = got_repo_get_path(repo);
3884 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3885 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3887 if (cl_arg.logmsg_path)
3888 fprintf(stderr, "%s: log message preserved in %s\n",
3889 getprogname(), cl_arg.logmsg_path);
3893 if (cl_arg.logmsg_path)
3894 unlink(cl_arg.logmsg_path);
3896 error = got_object_id_str(&id_str, id);
3899 printf("Created commit %s\n", id_str);
3901 free(cl_arg.logmsg_path);
3903 got_repo_close(repo);
3905 got_worktree_close(worktree);
3913 usage_cherrypick(void)
3915 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3919 static const struct got_error *
3920 cmd_cherrypick(int argc, char *argv[])
3922 const struct got_error *error = NULL;
3923 struct got_worktree *worktree = NULL;
3924 struct got_repository *repo = NULL;
3925 char *cwd = NULL, *commit_id_str = NULL;
3926 struct got_object_id *commit_id = NULL;
3927 struct got_commit_object *commit = NULL;
3928 struct got_object_qid *pid;
3929 struct got_reference *head_ref = NULL;
3930 int ch, did_something = 0;
3932 while ((ch = getopt(argc, argv, "")) != -1) {
3944 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3945 "unveil", NULL) == -1)
3951 cwd = getcwd(NULL, 0);
3953 error = got_error_from_errno("getcwd");
3956 error = got_worktree_open(&worktree, cwd);
3960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3964 error = apply_unveil(got_repo_get_path(repo), 0,
3965 got_worktree_get_root_path(worktree));
3969 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3970 GOT_OBJ_TYPE_COMMIT, repo);
3971 if (error != NULL) {
3972 struct got_reference *ref;
3973 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3975 error = got_ref_open(&ref, repo, argv[0], 0);
3978 error = got_ref_resolve(&commit_id, repo, ref);
3983 error = got_object_id_str(&commit_id_str, commit_id);
3987 error = got_ref_open(&head_ref, repo,
3988 got_worktree_get_head_ref_name(worktree), 0);
3992 error = check_same_branch(commit_id, head_ref, NULL, repo);
3994 if (error->code != GOT_ERR_ANCESTRY)
3998 error = got_error(GOT_ERR_SAME_BRANCH);
4002 error = got_object_open_as_commit(&commit, repo, commit_id);
4005 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4006 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4007 commit_id, repo, update_progress, &did_something, check_cancelled,
4013 printf("Merged commit %s\n", commit_id_str);
4016 got_object_commit_close(commit);
4017 free(commit_id_str);
4019 got_ref_close(head_ref);
4021 got_worktree_close(worktree);
4023 got_repo_close(repo);
4030 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4034 static const struct got_error *
4035 cmd_backout(int argc, char *argv[])
4037 const struct got_error *error = NULL;
4038 struct got_worktree *worktree = NULL;
4039 struct got_repository *repo = NULL;
4040 char *cwd = NULL, *commit_id_str = NULL;
4041 struct got_object_id *commit_id = NULL;
4042 struct got_commit_object *commit = NULL;
4043 struct got_object_qid *pid;
4044 struct got_reference *head_ref = NULL;
4045 int ch, did_something = 0;
4047 while ((ch = getopt(argc, argv, "")) != -1) {
4059 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4060 "unveil", NULL) == -1)
4066 cwd = getcwd(NULL, 0);
4068 error = got_error_from_errno("getcwd");
4071 error = got_worktree_open(&worktree, cwd);
4075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4079 error = apply_unveil(got_repo_get_path(repo), 0,
4080 got_worktree_get_root_path(worktree));
4084 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4085 GOT_OBJ_TYPE_COMMIT, repo);
4086 if (error != NULL) {
4087 struct got_reference *ref;
4088 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4090 error = got_ref_open(&ref, repo, argv[0], 0);
4093 error = got_ref_resolve(&commit_id, repo, ref);
4098 error = got_object_id_str(&commit_id_str, commit_id);
4102 error = got_ref_open(&head_ref, repo,
4103 got_worktree_get_head_ref_name(worktree), 0);
4107 error = check_same_branch(commit_id, head_ref, NULL, repo);
4111 error = got_object_open_as_commit(&commit, repo, commit_id);
4114 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4116 error = got_error(GOT_ERR_ROOT_COMMIT);
4120 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4121 update_progress, &did_something, check_cancelled, NULL);
4126 printf("Backed out commit %s\n", commit_id_str);
4129 got_object_commit_close(commit);
4130 free(commit_id_str);
4132 got_ref_close(head_ref);
4134 got_worktree_close(worktree);
4136 got_repo_close(repo);
4143 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4149 trim_logmsg(char *logmsg, int limit)
4154 len = strlen(logmsg);
4158 nl = strchr(logmsg, '\n');
4163 static const struct got_error *
4164 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4166 const struct got_error *err;
4167 char *logmsg0 = NULL;
4170 err = got_object_commit_get_logmsg(&logmsg0, commit);
4175 while (isspace((unsigned char)s[0]))
4178 *logmsg = strdup(s);
4179 if (*logmsg == NULL) {
4180 err = got_error_from_errno("strdup");
4184 trim_logmsg(*logmsg, limit);
4190 static const struct got_error *
4191 show_rebase_progress(struct got_commit_object *commit,
4192 struct got_object_id *old_id, struct got_object_id *new_id)
4194 const struct got_error *err;
4195 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4197 err = got_object_id_str(&old_id_str, old_id);
4202 err = got_object_id_str(&new_id_str, new_id);
4207 old_id_str[12] = '\0';
4209 new_id_str[12] = '\0';
4211 err = get_short_logmsg(&logmsg, 42, commit);
4215 printf("%s -> %s: %s\n", old_id_str,
4216 new_id_str ? new_id_str : "no-op change", logmsg);
4223 static const struct got_error *
4224 rebase_progress(void *arg, unsigned char status, const char *path)
4226 unsigned char *rebase_status = arg;
4228 while (path[0] == '/')
4230 printf("%c %s\n", status, path);
4232 if (*rebase_status == GOT_STATUS_CONFLICT)
4234 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4235 *rebase_status = status;
4239 static const struct got_error *
4240 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4241 struct got_reference *branch, struct got_reference *new_base_branch,
4242 struct got_reference *tmp_branch, struct got_repository *repo)
4244 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4245 return got_worktree_rebase_complete(worktree, fileindex,
4246 new_base_branch, tmp_branch, branch, repo);
4249 static const struct got_error *
4250 rebase_commit(struct got_pathlist_head *merged_paths,
4251 struct got_worktree *worktree, struct got_fileindex *fileindex,
4252 struct got_reference *tmp_branch,
4253 struct got_object_id *commit_id, struct got_repository *repo)
4255 const struct got_error *error;
4256 struct got_commit_object *commit;
4257 struct got_object_id *new_commit_id;
4259 error = got_object_open_as_commit(&commit, repo, commit_id);
4263 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4264 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4266 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4268 error = show_rebase_progress(commit, commit_id, NULL);
4270 error = show_rebase_progress(commit, commit_id, new_commit_id);
4271 free(new_commit_id);
4274 got_object_commit_close(commit);
4278 struct check_path_prefix_arg {
4279 const char *path_prefix;
4284 static const struct got_error *
4285 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4286 struct got_blob_object *blob2, struct got_object_id *id1,
4287 struct got_object_id *id2, const char *path1, const char *path2,
4288 struct got_repository *repo)
4290 struct check_path_prefix_arg *a = arg;
4292 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4293 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4294 return got_error(a->errcode);
4299 static const struct got_error *
4300 check_path_prefix(struct got_object_id *parent_id,
4301 struct got_object_id *commit_id, const char *path_prefix,
4302 int errcode, struct got_repository *repo)
4304 const struct got_error *err;
4305 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4306 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4307 struct check_path_prefix_arg cpp_arg;
4309 if (got_path_is_root_dir(path_prefix))
4312 err = got_object_open_as_commit(&commit, repo, commit_id);
4316 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4320 err = got_object_open_as_tree(&tree1, repo,
4321 got_object_commit_get_tree_id(parent_commit));
4325 err = got_object_open_as_tree(&tree2, repo,
4326 got_object_commit_get_tree_id(commit));
4330 cpp_arg.path_prefix = path_prefix;
4331 while (cpp_arg.path_prefix[0] == '/')
4332 cpp_arg.path_prefix++;
4333 cpp_arg.len = strlen(cpp_arg.path_prefix);
4334 cpp_arg.errcode = errcode;
4335 err = got_diff_tree(tree1, tree2, "", "", repo,
4336 check_path_prefix_in_diff, &cpp_arg, 0);
4339 got_object_tree_close(tree1);
4341 got_object_tree_close(tree2);
4343 got_object_commit_close(commit);
4345 got_object_commit_close(parent_commit);
4349 static const struct got_error *
4350 collect_commits(struct got_object_id_queue *commits,
4351 struct got_object_id *initial_commit_id,
4352 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4353 const char *path_prefix, int path_prefix_errcode,
4354 struct got_repository *repo)
4356 const struct got_error *err = NULL;
4357 struct got_commit_graph *graph = NULL;
4358 struct got_object_id *parent_id = NULL;
4359 struct got_object_qid *qid;
4360 struct got_object_id *commit_id = initial_commit_id;
4362 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4366 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4367 check_cancelled, NULL);
4370 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4371 err = got_commit_graph_iter_next(&parent_id, graph);
4373 if (err->code == GOT_ERR_ITER_COMPLETED) {
4374 err = got_error_msg(GOT_ERR_ANCESTRY,
4375 "ran out of commits to rebase before "
4376 "youngest common ancestor commit has "
4379 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4381 err = got_commit_graph_fetch_commits(graph, 1, repo,
4382 check_cancelled, NULL);
4386 err = check_path_prefix(parent_id, commit_id,
4387 path_prefix, path_prefix_errcode, repo);
4391 err = got_object_qid_alloc(&qid, commit_id);
4394 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4395 commit_id = parent_id;
4399 got_commit_graph_close(graph);
4403 static const struct got_error *
4404 cmd_rebase(int argc, char *argv[])
4406 const struct got_error *error = NULL;
4407 struct got_worktree *worktree = NULL;
4408 struct got_repository *repo = NULL;
4409 struct got_fileindex *fileindex = NULL;
4411 struct got_reference *branch = NULL;
4412 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4413 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4414 struct got_object_id *resume_commit_id = NULL;
4415 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4416 struct got_commit_object *commit = NULL;
4417 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4418 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4419 struct got_object_id_queue commits;
4420 struct got_pathlist_head merged_paths;
4421 const struct got_object_id_queue *parent_ids;
4422 struct got_object_qid *qid, *pid;
4424 SIMPLEQ_INIT(&commits);
4425 TAILQ_INIT(&merged_paths);
4427 while ((ch = getopt(argc, argv, "ac")) != -1) {
4433 continue_rebase = 1;
4445 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4446 "unveil", NULL) == -1)
4449 if (abort_rebase && continue_rebase)
4451 else if (abort_rebase || continue_rebase) {
4454 } else if (argc != 1)
4457 cwd = getcwd(NULL, 0);
4459 error = got_error_from_errno("getcwd");
4462 error = got_worktree_open(&worktree, cwd);
4466 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4470 error = apply_unveil(got_repo_get_path(repo), 0,
4471 got_worktree_get_root_path(worktree));
4475 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4481 if (!rebase_in_progress) {
4482 error = got_error(GOT_ERR_NOT_REBASING);
4485 error = got_worktree_rebase_continue(&resume_commit_id,
4486 &new_base_branch, &tmp_branch, &branch, &fileindex,
4490 printf("Switching work tree to %s\n",
4491 got_ref_get_symref_target(new_base_branch));
4492 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4493 new_base_branch, update_progress, &did_something);
4496 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4497 goto done; /* nothing else to do */
4500 if (continue_rebase) {
4501 if (!rebase_in_progress) {
4502 error = got_error(GOT_ERR_NOT_REBASING);
4505 error = got_worktree_rebase_continue(&resume_commit_id,
4506 &new_base_branch, &tmp_branch, &branch, &fileindex,
4511 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4512 resume_commit_id, repo);
4516 yca_id = got_object_id_dup(resume_commit_id);
4517 if (yca_id == NULL) {
4518 error = got_error_from_errno("got_object_id_dup");
4522 error = got_ref_open(&branch, repo, argv[0], 0);
4527 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4531 if (!continue_rebase) {
4532 struct got_object_id *base_commit_id;
4534 base_commit_id = got_worktree_get_base_commit_id(worktree);
4535 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4536 base_commit_id, branch_head_commit_id, repo,
4537 check_cancelled, NULL);
4540 if (yca_id == NULL) {
4541 error = got_error_msg(GOT_ERR_ANCESTRY,
4542 "specified branch shares no common ancestry "
4543 "with work tree's branch");
4547 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4549 if (error->code != GOT_ERR_ANCESTRY)
4553 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4554 "specified branch resolves to a commit which "
4555 "is already contained in work tree's branch");
4558 error = got_worktree_rebase_prepare(&new_base_branch,
4559 &tmp_branch, &fileindex, worktree, branch, repo);
4564 commit_id = branch_head_commit_id;
4565 error = got_object_open_as_commit(&commit, repo, commit_id);
4569 parent_ids = got_object_commit_get_parent_ids(commit);
4570 pid = SIMPLEQ_FIRST(parent_ids);
4572 if (!continue_rebase) {
4574 error = got_worktree_rebase_abort(worktree, fileindex,
4575 repo, new_base_branch, update_progress,
4579 printf("Rebase of %s aborted\n",
4580 got_ref_get_name(branch));
4582 error = got_error(GOT_ERR_EMPTY_REBASE);
4585 error = collect_commits(&commits, commit_id, pid->id,
4586 yca_id, got_worktree_get_path_prefix(worktree),
4587 GOT_ERR_REBASE_PATH, repo);
4588 got_object_commit_close(commit);
4593 if (SIMPLEQ_EMPTY(&commits)) {
4594 if (continue_rebase)
4595 error = rebase_complete(worktree, fileindex,
4596 branch, new_base_branch, tmp_branch, repo);
4598 error = got_error(GOT_ERR_EMPTY_REBASE);
4603 SIMPLEQ_FOREACH(qid, &commits, entry) {
4604 commit_id = qid->id;
4605 parent_id = pid ? pid->id : yca_id;
4608 error = got_worktree_rebase_merge_files(&merged_paths,
4609 worktree, fileindex, parent_id, commit_id, repo,
4610 rebase_progress, &rebase_status, check_cancelled, NULL);
4614 if (rebase_status == GOT_STATUS_CONFLICT) {
4615 got_worktree_rebase_pathlist_free(&merged_paths);
4619 error = rebase_commit(&merged_paths, worktree, fileindex,
4620 tmp_branch, commit_id, repo);
4621 got_worktree_rebase_pathlist_free(&merged_paths);
4626 if (rebase_status == GOT_STATUS_CONFLICT) {
4627 error = got_worktree_rebase_postpone(worktree, fileindex);
4630 error = got_error_msg(GOT_ERR_CONFLICTS,
4631 "conflicts must be resolved before rebasing can continue");
4633 error = rebase_complete(worktree, fileindex, branch,
4634 new_base_branch, tmp_branch, repo);
4636 got_object_id_queue_free(&commits);
4637 free(branch_head_commit_id);
4638 free(resume_commit_id);
4641 got_object_commit_close(commit);
4643 got_ref_close(branch);
4644 if (new_base_branch)
4645 got_ref_close(new_base_branch);
4647 got_ref_close(tmp_branch);
4649 got_worktree_close(worktree);
4651 got_repo_close(repo);
4656 usage_histedit(void)
4658 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4663 #define GOT_HISTEDIT_PICK 'p'
4664 #define GOT_HISTEDIT_EDIT 'e'
4665 #define GOT_HISTEDIT_FOLD 'f'
4666 #define GOT_HISTEDIT_DROP 'd'
4667 #define GOT_HISTEDIT_MESG 'm'
4669 static struct got_histedit_cmd {
4673 } got_histedit_cmds[] = {
4674 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4675 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4676 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4677 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4678 { GOT_HISTEDIT_MESG, "mesg",
4679 "single-line log message for commit above (open editor if empty)" },
4682 struct got_histedit_list_entry {
4683 TAILQ_ENTRY(got_histedit_list_entry) entry;
4684 struct got_object_id *commit_id;
4685 const struct got_histedit_cmd *cmd;
4688 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4690 static const struct got_error *
4691 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4692 FILE *f, struct got_repository *repo)
4694 const struct got_error *err = NULL;
4695 char *logmsg = NULL, *id_str = NULL;
4696 struct got_commit_object *commit = NULL;
4699 err = got_object_open_as_commit(&commit, repo, commit_id);
4703 err = get_short_logmsg(&logmsg, 34, commit);
4707 err = got_object_id_str(&id_str, commit_id);
4711 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4713 err = got_ferror(f, GOT_ERR_IO);
4716 got_object_commit_close(commit);
4722 static const struct got_error *
4723 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4724 struct got_repository *repo)
4726 const struct got_error *err = NULL;
4727 struct got_object_qid *qid;
4729 if (SIMPLEQ_EMPTY(commits))
4730 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4732 SIMPLEQ_FOREACH(qid, commits, entry) {
4733 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4742 static const struct got_error *
4743 write_cmd_list(FILE *f)
4745 const struct got_error *err = NULL;
4748 n = fprintf(f, "# Available histedit commands:\n");
4750 return got_ferror(f, GOT_ERR_IO);
4752 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4753 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4754 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4757 err = got_ferror(f, GOT_ERR_IO);
4761 n = fprintf(f, "# Commits will be processed in order from top to "
4762 "bottom of this file.\n");
4764 return got_ferror(f, GOT_ERR_IO);
4768 static const struct got_error *
4769 histedit_syntax_error(int lineno)
4771 static char msg[42];
4774 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4776 if (ret == -1 || ret >= sizeof(msg))
4777 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4779 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4782 static const struct got_error *
4783 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4784 char *logmsg, struct got_repository *repo)
4786 const struct got_error *err;
4787 struct got_commit_object *folded_commit = NULL;
4788 char *id_str, *folded_logmsg = NULL;
4790 err = got_object_id_str(&id_str, hle->commit_id);
4794 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4798 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4801 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4802 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4803 folded_logmsg) == -1) {
4804 err = got_error_from_errno("asprintf");
4809 got_object_commit_close(folded_commit);
4811 free(folded_logmsg);
4815 static struct got_histedit_list_entry *
4816 get_folded_commits(struct got_histedit_list_entry *hle)
4818 struct got_histedit_list_entry *prev, *folded = NULL;
4820 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4821 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4822 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4823 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4825 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4831 static const struct got_error *
4832 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4833 struct got_repository *repo)
4835 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4836 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4837 const struct got_error *err = NULL;
4838 struct got_commit_object *commit = NULL;
4840 struct got_histedit_list_entry *folded = NULL;
4842 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4846 folded = get_folded_commits(hle);
4848 while (folded != hle) {
4849 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4850 folded = TAILQ_NEXT(folded, entry);
4853 err = append_folded_commit_msg(&new_msg, folded,
4859 folded = TAILQ_NEXT(folded, entry);
4863 err = got_object_id_str(&id_str, hle->commit_id);
4866 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4869 if (asprintf(&new_msg,
4870 "%s\n# original log message of commit %s: %s",
4871 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4872 err = got_error_from_errno("asprintf");
4878 err = got_object_id_str(&id_str, hle->commit_id);
4882 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4886 dprintf(fd, logmsg);
4889 err = get_editor(&editor);
4893 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4895 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4897 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4900 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4901 err = got_error_from_errno2("unlink", logmsg_path);
4907 got_object_commit_close(commit);
4911 static const struct got_error *
4912 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4913 FILE *f, struct got_repository *repo)
4915 const struct got_error *err = NULL;
4916 char *line = NULL, *p, *end;
4920 const struct got_histedit_cmd *cmd;
4921 struct got_object_id *commit_id = NULL;
4922 struct got_histedit_list_entry *hle = NULL;
4925 len = getline(&line, &size, f);
4927 const struct got_error *getline_err;
4930 getline_err = got_error_from_errno("getline");
4931 err = got_ferror(f, getline_err->code);
4936 while (isspace((unsigned char)p[0]))
4938 if (p[0] == '#' || p[0] == '\0') {
4944 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4945 cmd = &got_histedit_cmds[i];
4946 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4947 isspace((unsigned char)p[strlen(cmd->name)])) {
4948 p += strlen(cmd->name);
4951 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4956 if (i == nitems(got_histedit_cmds)) {
4957 err = histedit_syntax_error(lineno);
4960 while (isspace((unsigned char)p[0]))
4962 if (cmd->code == GOT_HISTEDIT_MESG) {
4963 if (hle == NULL || hle->logmsg != NULL) {
4964 err = got_error(GOT_ERR_HISTEDIT_CMD);
4968 err = histedit_edit_logmsg(hle, repo);
4972 hle->logmsg = strdup(p);
4973 if (hle->logmsg == NULL) {
4974 err = got_error_from_errno("strdup");
4983 while (end[0] && !isspace((unsigned char)end[0]))
4987 err = got_object_resolve_id_str(&commit_id, repo, p);
4989 /* override error code */
4990 err = histedit_syntax_error(lineno);
4994 hle = malloc(sizeof(*hle));
4996 err = got_error_from_errno("malloc");
5000 hle->commit_id = commit_id;
5005 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5013 static const struct got_error *
5014 histedit_check_script(struct got_histedit_list *histedit_cmds,
5015 struct got_object_id_queue *commits, struct got_repository *repo)
5017 const struct got_error *err = NULL;
5018 struct got_object_qid *qid;
5019 struct got_histedit_list_entry *hle;
5020 static char msg[80];
5023 if (TAILQ_EMPTY(histedit_cmds))
5024 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5025 "histedit script contains no commands");
5026 if (SIMPLEQ_EMPTY(commits))
5027 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5029 SIMPLEQ_FOREACH(qid, commits, entry) {
5030 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5031 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5035 err = got_object_id_str(&id_str, qid->id);
5038 snprintf(msg, sizeof(msg),
5039 "commit %s missing from histedit script", id_str);
5041 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5045 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5046 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5047 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5048 "last commit in histedit script cannot be folded");
5053 static const struct got_error *
5054 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5055 const char *path, struct got_object_id_queue *commits,
5056 struct got_repository *repo)
5058 const struct got_error *err = NULL;
5062 err = get_editor(&editor);
5066 if (spawn_editor(editor, path) == -1) {
5067 err = got_error_from_errno("failed spawning editor");
5071 f = fopen(path, "r");
5073 err = got_error_from_errno("fopen");
5076 err = histedit_parse_list(histedit_cmds, f, repo);
5080 err = histedit_check_script(histedit_cmds, commits, repo);
5082 if (f && fclose(f) != 0 && err == NULL)
5083 err = got_error_from_errno("fclose");
5088 static const struct got_error *
5089 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5090 struct got_object_id_queue *, const char *, struct got_repository *);
5092 static const struct got_error *
5093 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5094 struct got_object_id_queue *commits, struct got_repository *repo)
5096 const struct got_error *err;
5100 err = got_opentemp_named(&path, &f, "got-histedit");
5104 err = write_cmd_list(f);
5108 err = histedit_write_commit_list(commits, f, repo);
5112 if (fclose(f) != 0) {
5113 err = got_error_from_errno("fclose");
5118 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5120 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5121 err->code != GOT_ERR_HISTEDIT_CMD)
5123 err = histedit_edit_list_retry(histedit_cmds, err,
5124 commits, path, repo);
5127 if (f && fclose(f) != 0 && err == NULL)
5128 err = got_error_from_errno("fclose");
5129 if (path && unlink(path) != 0 && err == NULL)
5130 err = got_error_from_errno2("unlink", path);
5135 static const struct got_error *
5136 histedit_save_list(struct got_histedit_list *histedit_cmds,
5137 struct got_worktree *worktree, struct got_repository *repo)
5139 const struct got_error *err = NULL;
5142 struct got_histedit_list_entry *hle;
5143 struct got_commit_object *commit = NULL;
5145 err = got_worktree_get_histedit_script_path(&path, worktree);
5149 f = fopen(path, "w");
5151 err = got_error_from_errno2("fopen", path);
5154 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5155 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5161 int n = fprintf(f, "%c %s\n",
5162 GOT_HISTEDIT_MESG, hle->logmsg);
5164 err = got_ferror(f, GOT_ERR_IO);
5170 if (f && fclose(f) != 0 && err == NULL)
5171 err = got_error_from_errno("fclose");
5174 got_object_commit_close(commit);
5179 histedit_free_list(struct got_histedit_list *histedit_cmds)
5181 struct got_histedit_list_entry *hle;
5183 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5184 TAILQ_REMOVE(histedit_cmds, hle, entry);
5189 static const struct got_error *
5190 histedit_load_list(struct got_histedit_list *histedit_cmds,
5191 const char *path, struct got_repository *repo)
5193 const struct got_error *err = NULL;
5196 f = fopen(path, "r");
5198 err = got_error_from_errno2("fopen", path);
5202 err = histedit_parse_list(histedit_cmds, f, repo);
5204 if (f && fclose(f) != 0 && err == NULL)
5205 err = got_error_from_errno("fclose");
5209 static const struct got_error *
5210 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5211 const struct got_error *edit_err, struct got_object_id_queue *commits,
5212 const char *path, struct got_repository *repo)
5214 const struct got_error *err = NULL, *prev_err = edit_err;
5217 while (resp != 'c' && resp != 'r' && resp != 'a') {
5218 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5219 "or (a)bort: ", getprogname(), prev_err->msg);
5224 histedit_free_list(histedit_cmds);
5225 err = histedit_run_editor(histedit_cmds, path, commits,
5228 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5229 err->code != GOT_ERR_HISTEDIT_CMD)
5236 } else if (resp == 'r') {
5237 histedit_free_list(histedit_cmds);
5238 err = histedit_edit_script(histedit_cmds,
5241 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5242 err->code != GOT_ERR_HISTEDIT_CMD)
5249 } else if (resp == 'a') {
5250 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5253 printf("invalid response '%c'\n", resp);
5259 static const struct got_error *
5260 histedit_complete(struct got_worktree *worktree,
5261 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5262 struct got_reference *branch, struct got_repository *repo)
5264 printf("Switching work tree to %s\n",
5265 got_ref_get_symref_target(branch));
5266 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5270 static const struct got_error *
5271 show_histedit_progress(struct got_commit_object *commit,
5272 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5274 const struct got_error *err;
5275 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5277 err = got_object_id_str(&old_id_str, hle->commit_id);
5282 err = got_object_id_str(&new_id_str, new_id);
5287 old_id_str[12] = '\0';
5289 new_id_str[12] = '\0';
5292 logmsg = strdup(hle->logmsg);
5293 if (logmsg == NULL) {
5294 err = got_error_from_errno("strdup");
5297 trim_logmsg(logmsg, 42);
5299 err = get_short_logmsg(&logmsg, 42, commit);
5304 switch (hle->cmd->code) {
5305 case GOT_HISTEDIT_PICK:
5306 case GOT_HISTEDIT_EDIT:
5307 printf("%s -> %s: %s\n", old_id_str,
5308 new_id_str ? new_id_str : "no-op change", logmsg);
5310 case GOT_HISTEDIT_DROP:
5311 case GOT_HISTEDIT_FOLD:
5312 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5325 static const struct got_error *
5326 histedit_commit(struct got_pathlist_head *merged_paths,
5327 struct got_worktree *worktree, struct got_fileindex *fileindex,
5328 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5329 struct got_repository *repo)
5331 const struct got_error *err;
5332 struct got_commit_object *commit;
5333 struct got_object_id *new_commit_id;
5335 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5336 && hle->logmsg == NULL) {
5337 err = histedit_edit_logmsg(hle, repo);
5342 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5346 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5347 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5350 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5352 err = show_histedit_progress(commit, hle, NULL);
5354 err = show_histedit_progress(commit, hle, new_commit_id);
5355 free(new_commit_id);
5358 got_object_commit_close(commit);
5362 static const struct got_error *
5363 histedit_skip_commit(struct got_histedit_list_entry *hle,
5364 struct got_worktree *worktree, struct got_repository *repo)
5366 const struct got_error *error;
5367 struct got_commit_object *commit;
5369 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5374 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5378 error = show_histedit_progress(commit, hle, NULL);
5379 got_object_commit_close(commit);
5383 static const struct got_error *
5384 cmd_histedit(int argc, char *argv[])
5386 const struct got_error *error = NULL;
5387 struct got_worktree *worktree = NULL;
5388 struct got_fileindex *fileindex = NULL;
5389 struct got_repository *repo = NULL;
5391 struct got_reference *branch = NULL;
5392 struct got_reference *tmp_branch = NULL;
5393 struct got_object_id *resume_commit_id = NULL;
5394 struct got_object_id *base_commit_id = NULL;
5395 struct got_object_id *head_commit_id = NULL;
5396 struct got_commit_object *commit = NULL;
5397 int ch, rebase_in_progress = 0, did_something;
5398 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5399 const char *edit_script_path = NULL;
5400 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5401 struct got_object_id_queue commits;
5402 struct got_pathlist_head merged_paths;
5403 const struct got_object_id_queue *parent_ids;
5404 struct got_object_qid *pid;
5405 struct got_histedit_list histedit_cmds;
5406 struct got_histedit_list_entry *hle;
5408 SIMPLEQ_INIT(&commits);
5409 TAILQ_INIT(&histedit_cmds);
5410 TAILQ_INIT(&merged_paths);
5412 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5421 edit_script_path = optarg;
5433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5434 "unveil", NULL) == -1)
5437 if (abort_edit && continue_edit)
5443 * This command cannot apply unveil(2) in all cases because the
5444 * user may choose to run an editor to edit the histedit script
5445 * and to edit individual commit log messages.
5446 * unveil(2) traverses exec(2); if an editor is used we have to
5447 * apply unveil after edit script and log messages have been written.
5448 * XXX TODO: Make use of unveil(2) where possible.
5451 cwd = getcwd(NULL, 0);
5453 error = got_error_from_errno("getcwd");
5456 error = got_worktree_open(&worktree, cwd);
5460 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5464 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5467 if (rebase_in_progress) {
5468 error = got_error(GOT_ERR_REBASING);
5472 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5476 if (edit_in_progress && abort_edit) {
5477 error = got_worktree_histedit_continue(&resume_commit_id,
5478 &tmp_branch, &branch, &base_commit_id, &fileindex,
5482 printf("Switching work tree to %s\n",
5483 got_ref_get_symref_target(branch));
5484 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5485 branch, base_commit_id, update_progress, &did_something);
5488 printf("Histedit of %s aborted\n",
5489 got_ref_get_symref_target(branch));
5490 goto done; /* nothing else to do */
5491 } else if (abort_edit) {
5492 error = got_error(GOT_ERR_NOT_HISTEDIT);
5496 if (continue_edit) {
5499 if (!edit_in_progress) {
5500 error = got_error(GOT_ERR_NOT_HISTEDIT);
5504 error = got_worktree_get_histedit_script_path(&path, worktree);
5508 error = histedit_load_list(&histedit_cmds, path, repo);
5513 error = got_worktree_histedit_continue(&resume_commit_id,
5514 &tmp_branch, &branch, &base_commit_id, &fileindex,
5519 error = got_ref_resolve(&head_commit_id, repo, branch);
5523 error = got_object_open_as_commit(&commit, repo,
5527 parent_ids = got_object_commit_get_parent_ids(commit);
5528 pid = SIMPLEQ_FIRST(parent_ids);
5530 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5533 error = collect_commits(&commits, head_commit_id, pid->id,
5534 base_commit_id, got_worktree_get_path_prefix(worktree),
5535 GOT_ERR_HISTEDIT_PATH, repo);
5536 got_object_commit_close(commit);
5541 if (edit_in_progress) {
5542 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5546 error = got_ref_open(&branch, repo,
5547 got_worktree_get_head_ref_name(worktree), 0);
5551 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5552 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5553 "will not edit commit history of a branch outside "
5554 "the \"refs/heads/\" reference namespace");
5558 error = got_ref_resolve(&head_commit_id, repo, branch);
5559 got_ref_close(branch);
5564 error = got_object_open_as_commit(&commit, repo,
5568 parent_ids = got_object_commit_get_parent_ids(commit);
5569 pid = SIMPLEQ_FIRST(parent_ids);
5571 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5574 error = collect_commits(&commits, head_commit_id, pid->id,
5575 got_worktree_get_base_commit_id(worktree),
5576 got_worktree_get_path_prefix(worktree),
5577 GOT_ERR_HISTEDIT_PATH, repo);
5578 got_object_commit_close(commit);
5583 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5584 &base_commit_id, &fileindex, worktree, repo);
5588 if (edit_script_path) {
5589 error = histedit_load_list(&histedit_cmds,
5590 edit_script_path, repo);
5592 got_worktree_histedit_abort(worktree, fileindex,
5593 repo, branch, base_commit_id,
5594 update_progress, &did_something);
5598 error = histedit_edit_script(&histedit_cmds, &commits,
5601 got_worktree_histedit_abort(worktree, fileindex,
5602 repo, branch, base_commit_id,
5603 update_progress, &did_something);
5609 error = histedit_save_list(&histedit_cmds, worktree,
5612 got_worktree_histedit_abort(worktree, fileindex,
5613 repo, branch, base_commit_id,
5614 update_progress, &did_something);
5620 error = histedit_check_script(&histedit_cmds, &commits, repo);
5624 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5625 if (resume_commit_id) {
5626 if (got_object_id_cmp(hle->commit_id,
5627 resume_commit_id) != 0)
5630 resume_commit_id = NULL;
5631 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5632 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5633 error = histedit_skip_commit(hle, worktree,
5636 error = histedit_commit(NULL, worktree,
5637 fileindex, tmp_branch, hle, repo);
5644 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5645 error = histedit_skip_commit(hle, worktree, repo);
5651 error = got_object_open_as_commit(&commit, repo,
5655 parent_ids = got_object_commit_get_parent_ids(commit);
5656 pid = SIMPLEQ_FIRST(parent_ids);
5658 error = got_worktree_histedit_merge_files(&merged_paths,
5659 worktree, fileindex, pid->id, hle->commit_id, repo,
5660 rebase_progress, &rebase_status, check_cancelled, NULL);
5663 got_object_commit_close(commit);
5666 if (rebase_status == GOT_STATUS_CONFLICT) {
5667 got_worktree_rebase_pathlist_free(&merged_paths);
5671 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5673 error = got_object_id_str(&id_str, hle->commit_id);
5676 printf("Stopping histedit for amending commit %s\n",
5679 got_worktree_rebase_pathlist_free(&merged_paths);
5680 error = got_worktree_histedit_postpone(worktree,
5685 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5686 error = histedit_skip_commit(hle, worktree, repo);
5692 error = histedit_commit(&merged_paths, worktree, fileindex,
5693 tmp_branch, hle, repo);
5694 got_worktree_rebase_pathlist_free(&merged_paths);
5699 if (rebase_status == GOT_STATUS_CONFLICT) {
5700 error = got_worktree_histedit_postpone(worktree, fileindex);
5703 error = got_error_msg(GOT_ERR_CONFLICTS,
5704 "conflicts must be resolved before rebasing can continue");
5706 error = histedit_complete(worktree, fileindex, tmp_branch,
5709 got_object_id_queue_free(&commits);
5710 histedit_free_list(&histedit_cmds);
5711 free(head_commit_id);
5712 free(base_commit_id);
5713 free(resume_commit_id);
5715 got_object_commit_close(commit);
5717 got_ref_close(branch);
5719 got_ref_close(tmp_branch);
5721 got_worktree_close(worktree);
5723 got_repo_close(repo);
5730 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5731 "[file-path ...]\n",
5736 static const struct got_error *
5737 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5738 const char *path, struct got_object_id *blob_id,
5739 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5741 const struct got_error *err = NULL;
5742 char *id_str = NULL;
5744 if (staged_status != GOT_STATUS_ADD &&
5745 staged_status != GOT_STATUS_MODIFY &&
5746 staged_status != GOT_STATUS_DELETE)
5749 if (staged_status == GOT_STATUS_ADD ||
5750 staged_status == GOT_STATUS_MODIFY)
5751 err = got_object_id_str(&id_str, staged_blob_id);
5753 err = got_object_id_str(&id_str, blob_id);
5757 printf("%s %c %s\n", id_str, staged_status, path);
5762 static const struct got_error *
5763 cmd_stage(int argc, char *argv[])
5765 const struct got_error *error = NULL;
5766 struct got_repository *repo = NULL;
5767 struct got_worktree *worktree = NULL;
5769 struct got_pathlist_head paths;
5770 struct got_pathlist_entry *pe;
5771 int ch, list_stage = 0, pflag = 0;
5772 FILE *patch_script_file = NULL;
5773 const char *patch_script_path = NULL;
5774 struct choose_patch_arg cpa;
5778 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5787 patch_script_path = optarg;
5799 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5800 "unveil", NULL) == -1)
5803 if (list_stage && (pflag || patch_script_path))
5804 errx(1, "-l option cannot be used with other options");
5805 if (patch_script_path && !pflag)
5806 errx(1, "-F option can only be used together with -p option");
5808 cwd = getcwd(NULL, 0);
5810 error = got_error_from_errno("getcwd");
5814 error = got_worktree_open(&worktree, cwd);
5818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5822 if (patch_script_path) {
5823 patch_script_file = fopen(patch_script_path, "r");
5824 if (patch_script_file == NULL) {
5825 error = got_error_from_errno2("fopen",
5830 error = apply_unveil(got_repo_get_path(repo), 1,
5831 got_worktree_get_root_path(worktree));
5835 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5840 error = got_worktree_status(worktree, &paths, repo,
5841 print_stage, NULL, check_cancelled, NULL);
5843 cpa.patch_script_file = patch_script_file;
5844 cpa.action = "stage";
5845 error = got_worktree_stage(worktree, &paths,
5846 pflag ? NULL : print_status, NULL,
5847 pflag ? choose_patch : NULL, &cpa, repo);
5850 if (patch_script_file && fclose(patch_script_file) == EOF &&
5852 error = got_error_from_errno2("fclose", patch_script_path);
5854 got_repo_close(repo);
5856 got_worktree_close(worktree);
5857 TAILQ_FOREACH(pe, &paths, entry)
5858 free((char *)pe->path);
5859 got_pathlist_free(&paths);
5867 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5868 "[file-path ...]\n",
5874 static const struct got_error *
5875 cmd_unstage(int argc, char *argv[])
5877 const struct got_error *error = NULL;
5878 struct got_repository *repo = NULL;
5879 struct got_worktree *worktree = NULL;
5881 struct got_pathlist_head paths;
5882 struct got_pathlist_entry *pe;
5883 int ch, did_something = 0, pflag = 0;
5884 FILE *patch_script_file = NULL;
5885 const char *patch_script_path = NULL;
5886 struct choose_patch_arg cpa;
5890 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5896 patch_script_path = optarg;
5908 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5909 "unveil", NULL) == -1)
5912 if (patch_script_path && !pflag)
5913 errx(1, "-F option can only be used together with -p option");
5915 cwd = getcwd(NULL, 0);
5917 error = got_error_from_errno("getcwd");
5921 error = got_worktree_open(&worktree, cwd);
5925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5929 if (patch_script_path) {
5930 patch_script_file = fopen(patch_script_path, "r");
5931 if (patch_script_file == NULL) {
5932 error = got_error_from_errno2("fopen",
5938 error = apply_unveil(got_repo_get_path(repo), 1,
5939 got_worktree_get_root_path(worktree));
5943 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5947 cpa.patch_script_file = patch_script_file;
5948 cpa.action = "unstage";
5949 error = got_worktree_unstage(worktree, &paths, update_progress,
5950 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5952 if (patch_script_file && fclose(patch_script_file) == EOF &&
5954 error = got_error_from_errno2("fclose", patch_script_path);
5956 got_repo_close(repo);
5958 got_worktree_close(worktree);
5959 TAILQ_FOREACH(pe, &paths, entry)
5960 free((char *)pe->path);
5961 got_pathlist_free(&paths);
5969 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
5970 "[object2 ...]\n", getprogname());
5974 static const struct got_error *
5975 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5977 const struct got_error *err;
5978 struct got_blob_object *blob;
5980 err = got_object_open_as_blob(&blob, repo, id, 8192);
5984 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
5985 got_object_blob_close(blob);
5989 static const struct got_error *
5990 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5992 const struct got_error *err;
5993 struct got_tree_object *tree;
5994 const struct got_tree_entries *entries;
5995 struct got_tree_entry *te;
5997 err = got_object_open_as_tree(&tree, repo, id);
6001 entries = got_object_tree_get_entries(tree);
6002 te = SIMPLEQ_FIRST(&entries->head);
6005 if (sigint_received || sigpipe_received)
6007 err = got_object_id_str(&id_str, te->id);
6010 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6012 te = SIMPLEQ_NEXT(te, entry);
6015 got_object_tree_close(tree);
6019 static const struct got_error *
6020 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6022 const struct got_error *err;
6023 struct got_commit_object *commit;
6024 const struct got_object_id_queue *parent_ids;
6025 struct got_object_qid *pid;
6026 char *id_str = NULL;
6027 const char *logmsg = NULL;
6030 err = got_object_open_as_commit(&commit, repo, id);
6034 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6038 fprintf(outfile, "tree: %s\n", id_str);
6039 parent_ids = got_object_commit_get_parent_ids(commit);
6040 fprintf(outfile, "parents: %d\n",
6041 got_object_commit_get_nparents(commit));
6043 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6045 err = got_object_id_str(&pid_str, pid->id);
6048 fprintf(outfile, "parent %d: %s\n", i++, pid_str);
6051 fprintf(outfile, "author: %s\n",
6052 got_object_commit_get_author(commit));
6053 fprintf(outfile, "author-time: %lld\n",
6054 got_object_commit_get_author_time(commit));
6056 fprintf(outfile, "committer: %s\n",
6057 got_object_commit_get_author(commit));
6058 fprintf(outfile, "committer-time: %lld\n",
6059 got_object_commit_get_committer_time(commit));
6061 logmsg = got_object_commit_get_logmsg_raw(commit);
6062 fprintf(outfile, "log-message: %zd bytes\n", strlen(logmsg));
6063 fprintf(outfile, "%s", logmsg);
6066 got_object_commit_close(commit);
6070 static const struct got_error *
6071 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6073 const struct got_error *err;
6074 struct got_tag_object *tag;
6075 char *id_str = NULL;
6076 const char *tagmsg = NULL;
6078 err = got_object_open_as_tag(&tag, repo, id);
6082 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6086 fprintf(outfile, "tag-name: %s\n", got_object_tag_get_name(tag));
6087 switch (got_object_tag_get_object_type(tag)) {
6088 case GOT_OBJ_TYPE_BLOB:
6089 fprintf(outfile, "tagged-object-type: blob\n");
6091 case GOT_OBJ_TYPE_TREE:
6092 fprintf(outfile, "tagged-object-type: tree\n");
6094 case GOT_OBJ_TYPE_COMMIT:
6095 fprintf(outfile, "tagged-object-type: commit\n");
6097 case GOT_OBJ_TYPE_TAG:
6098 fprintf(outfile, "tagged-object-type: tag\n");
6103 fprintf(outfile, "tagged-object: %s\n", id_str);
6105 fprintf(outfile, "tagger: %s\n",
6106 got_object_tag_get_tagger(tag));
6107 fprintf(outfile, "tagger-time: %lld %lld\n",
6108 got_object_tag_get_tagger_time(tag),
6109 got_object_tag_get_tagger_gmtoff(tag));
6111 tagmsg = got_object_tag_get_message(tag);
6112 fprintf(outfile, "tag-message: %zd bytes\n", strlen(tagmsg));
6113 fprintf(outfile, "%s", tagmsg);
6116 got_object_tag_close(tag);
6120 static const struct got_error *
6121 cmd_cat(int argc, char *argv[])
6123 const struct got_error *error;
6124 struct got_repository *repo = NULL;
6125 struct got_worktree *worktree = NULL;
6126 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6127 struct got_object_id *id = NULL;
6128 int ch, obj_type, i;
6131 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6136 while ((ch = getopt(argc, argv, "r:")) != -1) {
6139 repo_path = realpath(optarg, NULL);
6140 if (repo_path == NULL)
6141 err(1, "-r option");
6142 got_path_strip_trailing_slashes(repo_path);
6153 cwd = getcwd(NULL, 0);
6155 error = got_error_from_errno("getcwd");
6158 error = got_worktree_open(&worktree, cwd);
6159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6162 if (repo_path == NULL) {
6164 got_worktree_get_repo_path(worktree));
6165 if (repo_path == NULL) {
6166 error = got_error_from_errno("strdup");
6172 if (repo_path == NULL) {
6173 repo_path = getcwd(NULL, 0);
6174 if (repo_path == NULL)
6175 return got_error_from_errno("getcwd");
6178 error = got_repo_open(&repo, repo_path);
6183 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6187 for (i = 0; i < argc; i++) {
6188 error = match_object_id(&id, &label, argv[i],
6189 GOT_OBJ_TYPE_ANY, 0, repo);
6193 error = got_object_get_type(&obj_type, repo, id);
6198 case GOT_OBJ_TYPE_BLOB:
6199 error = cat_blob(id, repo, stdout);
6201 case GOT_OBJ_TYPE_TREE:
6202 error = cat_tree(id, repo, stdout);
6204 case GOT_OBJ_TYPE_COMMIT:
6205 error = cat_commit(id, repo, stdout);
6207 case GOT_OBJ_TYPE_TAG:
6208 error = cat_tag(id, repo, stdout);
6211 error = got_error(GOT_ERR_OBJ_TYPE);
6226 got_worktree_close(worktree);
6228 const struct got_error *repo_error;
6229 repo_error = got_repo_close(repo);