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/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_worktree.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
58 catch_sigint(int signo)
64 catch_sigpipe(int signo)
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
154 main(int argc, char *argv[])
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
225 editor = getenv("VISUAL");
227 editor = getenv("EDITOR");
230 err = got_path_find_prog(abspath, editor);
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
276 fprintf(stderr, "usage: %s init path\n", getprogname());
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
287 while ((ch = getopt(argc, argv, "")) != -1) {
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
316 error = apply_unveil(repo_path, 0, NULL);
320 error = got_repo_init(repo_path);
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
338 spawn_editor(const char *editor, const char *file)
341 sig_t sighup, sigint, sigquit;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
352 execl(editor, editor, file, (char *)NULL);
356 while (waitpid(pid, &st, 0) == -1)
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
381 int content_changed = 0;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
401 return got_error_from_errno("malloc");
405 fp = fopen(logmsg_path, "r");
407 err = got_error_from_errno("fopen");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
452 dprintf(fd, initial_content);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
457 free(initial_content);
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
490 branch_name = optarg;
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
507 if (optarg[0] == '\0')
509 error = got_pathlist_insert(&pe, &ignores, optarg,
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
552 error = got_ref_open(&branch_ref, repo, refname, 0);
554 if (error->code != GOT_ERR_NOT_REF)
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
567 got_path_strip_trailing_slashes(path_dir);
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
598 error = got_ref_write(branch_ref, repo);
602 error = got_object_id_str(&id_str, new_commit_id);
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
608 if (error->code != GOT_ERR_NOT_REF)
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
616 error = got_ref_write(head_ref, repo);
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
630 got_ref_close(branch_ref);
632 got_ref_close(head_ref);
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
653 while (path[0] == '/')
656 printf("%c %s/%s\n", status, worktree_path, path);
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
681 return got_error(GOT_ERR_ANCESTRY);
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
739 err = got_commit_graph_fetch_commits(graph, 1,
746 if (got_object_id_cmp(id, commit_id) == 0) {
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
761 static const struct got_error *
762 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
770 err = got_ref_resolve(commit_id, repo, ref);
773 if (err->code != GOT_ERR_NOT_REF)
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
795 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
798 branch_name = optarg;
801 commit_id_str = strdup(optarg);
802 if (commit_id_str == NULL)
803 return got_error_from_errno("strdup");
806 path_prefix = optarg;
818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
819 "unveil", NULL) == -1)
823 char *cwd, *base, *dotgit;
824 repo_path = realpath(argv[0], NULL);
825 if (repo_path == NULL)
826 return got_error_from_errno2("realpath", argv[0]);
827 cwd = getcwd(NULL, 0);
829 error = got_error_from_errno("getcwd");
832 if (path_prefix[0]) {
833 base = basename(path_prefix);
835 error = got_error_from_errno2("basename",
840 base = basename(repo_path);
842 error = got_error_from_errno2("basename",
847 dotgit = strstr(base, ".git");
850 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
851 error = got_error_from_errno("asprintf");
856 } else if (argc == 2) {
857 repo_path = realpath(argv[0], NULL);
858 if (repo_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[0]);
862 worktree_path = realpath(argv[1], NULL);
863 if (worktree_path == NULL) {
864 if (errno != ENOENT) {
865 error = got_error_from_errno2("realpath",
869 worktree_path = strdup(argv[1]);
870 if (worktree_path == NULL) {
871 error = got_error_from_errno("strdup");
878 got_path_strip_trailing_slashes(repo_path);
879 got_path_strip_trailing_slashes(worktree_path);
881 error = got_repo_open(&repo, repo_path);
885 /* Pre-create work tree path for unveil(2) */
886 error = got_path_mkdir(worktree_path);
888 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
890 if (!got_path_dir_is_empty(worktree_path)) {
891 error = got_error_path(worktree_path,
892 GOT_ERR_DIR_NOT_EMPTY);
897 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
901 error = got_ref_open(&head_ref, repo, branch_name, 0);
905 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
906 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
909 error = got_worktree_open(&worktree, worktree_path);
913 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
917 if (!same_path_prefix) {
918 error = got_error(GOT_ERR_PATH_PREFIX);
923 struct got_object_id *commit_id;
924 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
927 error = check_linear_ancestry(commit_id,
928 got_worktree_get_base_commit_id(worktree), repo);
933 error = check_same_branch(commit_id, head_ref, repo);
936 error = got_worktree_set_base_commit_id(worktree, repo,
943 error = got_worktree_checkout_files(worktree, "", repo,
944 checkout_progress, worktree_path, check_cancelled, NULL);
948 printf("Now shut up and hack\n");
960 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
965 static const struct got_error *
966 update_progress(void *arg, unsigned char status, const char *path)
968 int *did_something = arg;
970 if (status == GOT_STATUS_EXISTS)
975 /* Base commit bump happens silently. */
976 if (status == GOT_STATUS_BUMP_BASE)
979 while (path[0] == '/')
981 printf("%c %s\n", status, path);
985 static const struct got_error *
986 switch_head_ref(struct got_reference *head_ref,
987 struct got_object_id *commit_id, struct got_worktree *worktree,
988 struct got_repository *repo)
990 const struct got_error *err = NULL;
992 int ref_has_moved = 0;
994 /* Trivial case: switching between two different references. */
995 if (strcmp(got_ref_get_name(head_ref),
996 got_worktree_get_head_ref_name(worktree)) != 0) {
997 printf("Switching work tree from %s to %s\n",
998 got_worktree_get_head_ref_name(worktree),
999 got_ref_get_name(head_ref));
1000 return got_worktree_set_head_ref(worktree, head_ref);
1003 err = check_linear_ancestry(commit_id,
1004 got_worktree_get_base_commit_id(worktree), repo);
1006 if (err->code != GOT_ERR_ANCESTRY)
1013 /* Switching to a rebased branch with the same reference name. */
1014 err = got_object_id_str(&base_id_str,
1015 got_worktree_get_base_commit_id(worktree));
1018 printf("Reference %s now points at a different branch\n",
1019 got_worktree_get_head_ref_name(worktree));
1020 printf("Switching work tree from %s to %s\n", base_id_str,
1021 got_worktree_get_head_ref_name(worktree));
1025 static const struct got_error *
1026 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1028 const struct got_error *err;
1031 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1035 return got_error(GOT_ERR_REBASING);
1037 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1041 return got_error(GOT_ERR_HISTEDIT_BUSY);
1046 static const struct got_error *
1047 cmd_update(int argc, char *argv[])
1049 const struct got_error *error = NULL;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 char *worktree_path = NULL, *path = NULL;
1053 struct got_object_id *commit_id = NULL;
1054 char *commit_id_str = NULL;
1055 const char *branch_name = NULL;
1056 struct got_reference *head_ref = NULL;
1057 int ch, did_something = 0;
1059 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1062 branch_name = optarg;
1065 commit_id_str = strdup(optarg);
1066 if (commit_id_str == NULL)
1067 return got_error_from_errno("strdup");
1079 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1080 "unveil", NULL) == -1)
1083 worktree_path = getcwd(NULL, 0);
1084 if (worktree_path == NULL) {
1085 error = got_error_from_errno("getcwd");
1088 error = got_worktree_open(&worktree, worktree_path);
1092 error = check_rebase_or_histedit_in_progress(worktree);
1099 error = got_error_from_errno("strdup");
1102 } else if (argc == 1) {
1103 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1113 error = apply_unveil(got_repo_get_path(repo), 0,
1114 got_worktree_get_root_path(worktree));
1118 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1119 got_worktree_get_head_ref_name(worktree), 0);
1122 if (commit_id_str == NULL) {
1123 error = got_ref_resolve(&commit_id, repo, head_ref);
1126 error = got_object_id_str(&commit_id_str, commit_id);
1130 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1131 free(commit_id_str);
1132 commit_id_str = NULL;
1135 error = got_object_id_str(&commit_id_str, commit_id);
1141 struct got_object_id *head_commit_id;
1142 if (strlen(path) != 0) {
1143 fprintf(stderr, "%s: switching between branches "
1144 "requires that the entire work tree "
1145 "gets updated, not just '%s'\n",
1146 getprogname(), path);
1147 error = got_error(GOT_ERR_BAD_PATH);
1150 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1153 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1154 free(head_commit_id);
1157 error = check_same_branch(commit_id, head_ref, repo);
1160 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1164 error = check_linear_ancestry(commit_id,
1165 got_worktree_get_base_commit_id(worktree), repo);
1166 if (error != NULL) {
1167 if (error->code == GOT_ERR_ANCESTRY)
1168 error = got_error(GOT_ERR_BRANCH_MOVED);
1171 error = check_same_branch(commit_id, head_ref, repo);
1176 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1178 error = got_worktree_set_base_commit_id(worktree, repo,
1184 error = got_worktree_checkout_files(worktree, path, repo,
1185 update_progress, &did_something, check_cancelled, NULL);
1190 printf("Updated to commit %s\n", commit_id_str);
1192 printf("Already up-to-date\n");
1194 free(worktree_path);
1197 free(commit_id_str);
1201 static const struct got_error *
1202 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1203 int diff_context, struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 struct got_tree_object *tree1 = NULL, *tree2;
1207 struct got_object_qid *qid;
1208 char *id_str1 = NULL, *id_str2;
1209 struct got_diff_blob_output_unidiff_arg arg;
1211 err = got_object_open_as_tree(&tree2, repo,
1212 got_object_commit_get_tree_id(commit));
1216 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1218 struct got_commit_object *pcommit;
1220 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1224 err = got_object_open_as_tree(&tree1, repo,
1225 got_object_commit_get_tree_id(pcommit));
1226 got_object_commit_close(pcommit);
1230 err = got_object_id_str(&id_str1, qid->id);
1235 err = got_object_id_str(&id_str2, id);
1239 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1240 arg.diff_context = diff_context;
1241 arg.outfile = stdout;
1242 err = got_diff_tree(tree1, tree2, "", "", repo,
1243 got_diff_blob_output_unidiff, &arg);
1246 got_object_tree_close(tree1);
1247 got_object_tree_close(tree2);
1254 get_datestr(time_t *time, char *datebuf)
1256 char *p, *s = ctime_r(time, datebuf);
1257 p = strchr(s, '\n');
1263 static const struct got_error *
1264 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1265 struct got_repository *repo, int show_patch, int diff_context,
1266 struct got_reflist_head *refs)
1268 const struct got_error *err = NULL;
1269 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1271 time_t committer_time;
1272 const char *author, *committer;
1273 char *refs_str = NULL;
1274 struct got_reflist_entry *re;
1276 SIMPLEQ_FOREACH(re, refs, entry) {
1279 if (got_object_id_cmp(re->id, id) != 0)
1281 name = got_ref_get_name(re->ref);
1282 if (strcmp(name, GOT_REF_HEAD) == 0)
1284 if (strncmp(name, "refs/", 5) == 0)
1286 if (strncmp(name, "got/", 4) == 0)
1288 if (strncmp(name, "heads/", 6) == 0)
1290 if (strncmp(name, "remotes/", 8) == 0)
1293 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1295 err = got_error_from_errno("asprintf");
1301 err = got_object_id_str(&id_str, id);
1305 printf("-----------------------------------------------\n");
1306 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1307 refs_str ? refs_str : "", refs_str ? ")" : "");
1312 printf("from: %s\n", got_object_commit_get_author(commit));
1313 committer_time = got_object_commit_get_committer_time(commit);
1314 datestr = get_datestr(&committer_time, datebuf);
1315 printf("date: %s UTC\n", datestr);
1316 author = got_object_commit_get_author(commit);
1317 committer = got_object_commit_get_committer(commit);
1318 if (strcmp(author, committer) != 0)
1319 printf("via: %s\n", committer);
1320 if (got_object_commit_get_nparents(commit) > 1) {
1321 const struct got_object_id_queue *parent_ids;
1322 struct got_object_qid *qid;
1324 parent_ids = got_object_commit_get_parent_ids(commit);
1325 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1326 err = got_object_id_str(&id_str, qid->id);
1329 printf("parent %d: %s\n", n++, id_str);
1334 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1335 if (logmsg0 == NULL)
1336 return got_error_from_errno("strdup");
1340 line = strsep(&logmsg, "\n");
1342 printf(" %s\n", line);
1347 err = print_patch(commit, id, diff_context, repo);
1352 if (fflush(stdout) != 0 && err == NULL)
1353 err = got_error_from_errno("fflush");
1357 static const struct got_error *
1358 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1359 char *path, int show_patch, int diff_context, int limit,
1360 int first_parent_traversal, struct got_reflist_head *refs)
1362 const struct got_error *err;
1363 struct got_commit_graph *graph;
1365 err = got_commit_graph_open(&graph, root_id, path,
1366 first_parent_traversal, repo);
1369 err = got_commit_graph_iter_start(graph, root_id, repo);
1373 struct got_commit_object *commit;
1374 struct got_object_id *id;
1376 if (sigint_received || sigpipe_received)
1379 err = got_commit_graph_iter_next(&id, graph);
1381 if (err->code == GOT_ERR_ITER_COMPLETED) {
1385 if (err->code != GOT_ERR_ITER_NEED_MORE)
1387 err = got_commit_graph_fetch_commits(graph, 1, repo);
1396 err = got_object_open_as_commit(&commit, repo, id);
1399 err = print_commit(commit, id, repo, show_patch, diff_context,
1401 got_object_commit_close(commit);
1402 if (err || (limit && --limit == 0))
1406 got_commit_graph_close(graph);
1413 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1414 "[-r repository-path] [path]\n", getprogname());
1418 static const struct got_error *
1419 cmd_log(int argc, char *argv[])
1421 const struct got_error *error;
1422 struct got_repository *repo = NULL;
1423 struct got_worktree *worktree = NULL;
1424 struct got_commit_object *commit = NULL;
1425 struct got_object_id *id = NULL;
1426 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1427 char *start_commit = NULL;
1428 int diff_context = 3, ch;
1429 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1431 struct got_reflist_head refs;
1433 SIMPLEQ_INIT(&refs);
1436 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1442 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1448 start_commit = optarg;
1451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1454 err(1, "-C option %s", errstr);
1457 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1459 err(1, "-l option %s", errstr);
1462 first_parent_traversal = 1;
1465 repo_path = realpath(optarg, NULL);
1466 if (repo_path == NULL)
1467 err(1, "-r option");
1468 got_path_strip_trailing_slashes(repo_path);
1479 cwd = getcwd(NULL, 0);
1481 error = got_error_from_errno("getcwd");
1485 error = got_worktree_open(&worktree, cwd);
1486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1493 error = got_error_from_errno("strdup");
1496 } else if (argc == 1) {
1498 error = got_worktree_resolve_path(&path, worktree,
1503 path = strdup(argv[0]);
1505 error = got_error_from_errno("strdup");
1512 if (repo_path == NULL) {
1513 repo_path = worktree ?
1514 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1516 if (repo_path == NULL) {
1517 error = got_error_from_errno("strdup");
1521 error = got_repo_open(&repo, repo_path);
1525 error = apply_unveil(got_repo_get_path(repo), 1,
1526 worktree ? got_worktree_get_root_path(worktree) : NULL);
1530 if (start_commit == NULL) {
1531 struct got_reference *head_ref;
1532 error = got_ref_open(&head_ref, repo,
1533 worktree ? got_worktree_get_head_ref_name(worktree)
1537 error = got_ref_resolve(&id, repo, head_ref);
1538 got_ref_close(head_ref);
1541 error = got_object_open_as_commit(&commit, repo, id);
1543 struct got_reference *ref;
1544 error = got_ref_open(&ref, repo, start_commit, 0);
1545 if (error == NULL) {
1547 error = got_ref_resolve(&id, repo, ref);
1551 error = got_object_get_type(&obj_type, repo, id);
1554 if (obj_type == GOT_OBJ_TYPE_TAG) {
1555 struct got_tag_object *tag;
1556 error = got_object_open_as_tag(&tag, repo, id);
1559 if (got_object_tag_get_object_type(tag) !=
1560 GOT_OBJ_TYPE_COMMIT) {
1561 got_object_tag_close(tag);
1562 error = got_error(GOT_ERR_OBJ_TYPE);
1566 id = got_object_id_dup(
1567 got_object_tag_get_object_id(tag));
1569 error = got_error_from_errno(
1570 "got_object_id_dup");
1571 got_object_tag_close(tag);
1574 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1575 error = got_error(GOT_ERR_OBJ_TYPE);
1578 error = got_object_open_as_commit(&commit, repo, id);
1582 if (commit == NULL) {
1583 error = got_repo_match_object_id_prefix(&id,
1584 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1592 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1597 path = in_repo_path;
1600 error = got_ref_list(&refs, repo);
1604 error = print_commits(id, repo, path, show_patch,
1605 diff_context, limit, first_parent_traversal, &refs);
1612 got_worktree_close(worktree);
1614 const struct got_error *repo_error;
1615 repo_error = got_repo_close(repo);
1619 got_ref_list_free(&refs);
1626 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1627 "[object1 object2 | path]\n", getprogname());
1631 struct print_diff_arg {
1632 struct got_repository *repo;
1633 struct got_worktree *worktree;
1639 static const struct got_error *
1640 print_diff(void *arg, unsigned char status, const char *path,
1641 struct got_object_id *blob_id, struct got_object_id *commit_id)
1643 struct print_diff_arg *a = arg;
1644 const struct got_error *err = NULL;
1645 struct got_blob_object *blob1 = NULL;
1647 char *abspath = NULL;
1650 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1651 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1654 if (!a->header_shown) {
1655 printf("diff %s %s\n", a->id_str,
1656 got_worktree_get_root_path(a->worktree));
1657 a->header_shown = 1;
1660 if (status != GOT_STATUS_ADD) {
1661 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1667 if (status != GOT_STATUS_DELETE) {
1668 if (asprintf(&abspath, "%s/%s",
1669 got_worktree_get_root_path(a->worktree), path) == -1) {
1670 err = got_error_from_errno("asprintf");
1674 f2 = fopen(abspath, "r");
1676 err = got_error_from_errno2("fopen", abspath);
1679 if (lstat(abspath, &sb) == -1) {
1680 err = got_error_from_errno2("lstat", abspath);
1686 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1690 got_object_blob_close(blob1);
1691 if (f2 && fclose(f2) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1697 static const struct got_error *
1698 cmd_diff(int argc, char *argv[])
1700 const struct got_error *error;
1701 struct got_repository *repo = NULL;
1702 struct got_worktree *worktree = NULL;
1703 char *cwd = NULL, *repo_path = NULL;
1704 struct got_object_id *id1 = NULL, *id2 = NULL;
1705 const char *id_str1 = NULL, *id_str2 = NULL;
1706 char *label1 = NULL, *label2 = NULL;
1708 int diff_context = 3, ch;
1713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1718 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1721 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1723 err(1, "-C option %s", errstr);
1726 repo_path = realpath(optarg, NULL);
1727 if (repo_path == NULL)
1728 err(1, "-r option");
1729 got_path_strip_trailing_slashes(repo_path);
1740 cwd = getcwd(NULL, 0);
1742 error = got_error_from_errno("getcwd");
1745 error = got_worktree_open(&worktree, cwd);
1746 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1749 if (worktree == NULL) {
1750 error = got_error(GOT_ERR_NOT_WORKTREE);
1755 "-r option can't be used when diffing a work tree");
1756 repo_path = strdup(got_worktree_get_repo_path(worktree));
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno("strdup");
1762 error = got_worktree_resolve_path(&path, worktree,
1769 error = got_error_from_errno("strdup");
1773 } else if (argc == 2) {
1776 if (worktree && repo_path == NULL) {
1778 strdup(got_worktree_get_repo_path(worktree));
1779 if (repo_path == NULL) {
1780 error = got_error_from_errno("strdup");
1787 if (repo_path == NULL) {
1788 repo_path = getcwd(NULL, 0);
1789 if (repo_path == NULL)
1790 return got_error_from_errno("getcwd");
1793 error = got_repo_open(&repo, repo_path);
1798 error = apply_unveil(got_repo_get_path(repo), 1,
1799 worktree ? got_worktree_get_root_path(worktree) : NULL);
1804 struct print_diff_arg arg;
1806 error = got_object_id_str(&id_str,
1807 got_worktree_get_base_commit_id(worktree));
1811 arg.worktree = worktree;
1812 arg.diff_context = diff_context;
1813 arg.id_str = id_str;
1814 arg.header_shown = 0;
1816 error = got_worktree_status(worktree, path, repo, print_diff,
1817 &arg, check_cancelled, NULL);
1822 error = got_repo_match_object_id_prefix(&id1, id_str1,
1823 GOT_OBJ_TYPE_ANY, repo);
1825 struct got_reference *ref;
1826 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1828 error = got_ref_open(&ref, repo, id_str1, 0);
1831 label1 = strdup(got_ref_get_name(ref));
1832 if (label1 == NULL) {
1833 error = got_error_from_errno("strdup");
1836 error = got_ref_resolve(&id1, repo, ref);
1841 error = got_object_id_str(&label1, id1);
1842 if (label1 == NULL) {
1843 error = got_error_from_errno("strdup");
1848 error = got_repo_match_object_id_prefix(&id2, id_str2,
1849 GOT_OBJ_TYPE_ANY, repo);
1851 struct got_reference *ref;
1852 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1854 error = got_ref_open(&ref, repo, id_str2, 0);
1857 label2 = strdup(got_ref_get_name(ref));
1858 if (label2 == NULL) {
1859 error = got_error_from_errno("strdup");
1862 error = got_ref_resolve(&id2, repo, ref);
1867 error = got_object_id_str(&label2, id2);
1868 if (label2 == NULL) {
1869 error = got_error_from_errno("strdup");
1874 error = got_object_get_type(&type1, repo, id1);
1878 error = got_object_get_type(&type2, repo, id2);
1882 if (type1 != type2) {
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1888 case GOT_OBJ_TYPE_BLOB:
1889 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1890 diff_context, repo, stdout);
1892 case GOT_OBJ_TYPE_TREE:
1893 error = got_diff_objects_as_trees(id1, id2, "", "",
1894 diff_context, repo, stdout);
1896 case GOT_OBJ_TYPE_COMMIT:
1897 printf("diff %s %s\n", label1, label2);
1898 error = got_diff_objects_as_commits(id1, id2, diff_context,
1902 error = got_error(GOT_ERR_OBJ_TYPE);
1912 got_worktree_close(worktree);
1914 const struct got_error *repo_error;
1915 repo_error = got_repo_close(repo);
1926 "usage: %s blame [-c commit] [-r repository-path] path\n",
1931 static const struct got_error *
1932 cmd_blame(int argc, char *argv[])
1934 const struct got_error *error;
1935 struct got_repository *repo = NULL;
1936 struct got_worktree *worktree = NULL;
1937 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1938 struct got_object_id *commit_id = NULL;
1939 char *commit_id_str = NULL;
1943 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1948 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1951 commit_id_str = optarg;
1954 repo_path = realpath(optarg, NULL);
1955 if (repo_path == NULL)
1956 err(1, "-r option");
1957 got_path_strip_trailing_slashes(repo_path);
1973 cwd = getcwd(NULL, 0);
1975 error = got_error_from_errno("getcwd");
1978 if (repo_path == NULL) {
1979 error = got_worktree_open(&worktree, cwd);
1980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1986 strdup(got_worktree_get_repo_path(worktree));
1987 if (repo_path == NULL)
1988 error = got_error_from_errno("strdup");
1992 repo_path = strdup(cwd);
1993 if (repo_path == NULL) {
1994 error = got_error_from_errno("strdup");
2000 error = got_repo_open(&repo, repo_path);
2004 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2009 const char *prefix = got_worktree_get_path_prefix(worktree);
2010 char *p, *worktree_subdir = cwd +
2011 strlen(got_worktree_get_root_path(worktree));
2012 if (asprintf(&p, "%s%s%s%s%s",
2013 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2014 worktree_subdir, worktree_subdir[0] ? "/" : "",
2016 error = got_error_from_errno("asprintf");
2019 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2022 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2027 if (commit_id_str == NULL) {
2028 struct got_reference *head_ref;
2029 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2032 error = got_ref_resolve(&commit_id, repo, head_ref);
2033 got_ref_close(head_ref);
2037 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2042 error = got_blame(in_repo_path, commit_id, repo, stdout);
2049 got_worktree_close(worktree);
2051 const struct got_error *repo_error;
2052 repo_error = got_repo_close(repo);
2063 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2069 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2070 const char *root_path)
2072 int is_root_path = (strcmp(path, root_path) == 0);
2074 path += strlen(root_path);
2075 while (path[0] == '/')
2078 printf("%s%s%s%s%s\n", id ? id : "", path,
2079 is_root_path ? "" : "/", te->name,
2080 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2083 static const struct got_error *
2084 print_tree(const char *path, struct got_object_id *commit_id,
2085 int show_ids, int recurse, const char *root_path,
2086 struct got_repository *repo)
2088 const struct got_error *err = NULL;
2089 struct got_object_id *tree_id = NULL;
2090 struct got_tree_object *tree = NULL;
2091 const struct got_tree_entries *entries;
2092 struct got_tree_entry *te;
2094 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2098 err = got_object_open_as_tree(&tree, repo, tree_id);
2101 entries = got_object_tree_get_entries(tree);
2102 te = SIMPLEQ_FIRST(&entries->head);
2106 if (sigint_received || sigpipe_received)
2111 err = got_object_id_str(&id_str, te->id);
2114 if (asprintf(&id, "%s ", id_str) == -1) {
2115 err = got_error_from_errno("asprintf");
2121 print_entry(te, id, path, root_path);
2124 if (recurse && S_ISDIR(te->mode)) {
2126 if (asprintf(&child_path, "%s%s%s", path,
2127 path[0] == '/' && path[1] == '\0' ? "" : "/",
2129 err = got_error_from_errno("asprintf");
2132 err = print_tree(child_path, commit_id, show_ids, 1,
2139 te = SIMPLEQ_NEXT(te, entry);
2143 got_object_tree_close(tree);
2148 static const struct got_error *
2149 cmd_tree(int argc, char *argv[])
2151 const struct got_error *error;
2152 struct got_repository *repo = NULL;
2153 struct got_worktree *worktree = NULL;
2155 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2156 struct got_object_id *commit_id = NULL;
2157 char *commit_id_str = NULL;
2158 int show_ids = 0, recurse = 0;
2162 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2167 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2170 commit_id_str = optarg;
2173 repo_path = realpath(optarg, NULL);
2174 if (repo_path == NULL)
2175 err(1, "-r option");
2176 got_path_strip_trailing_slashes(repo_path);
2200 cwd = getcwd(NULL, 0);
2202 error = got_error_from_errno("getcwd");
2205 if (repo_path == NULL) {
2206 error = got_worktree_open(&worktree, cwd);
2207 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2213 strdup(got_worktree_get_repo_path(worktree));
2214 if (repo_path == NULL)
2215 error = got_error_from_errno("strdup");
2219 repo_path = strdup(cwd);
2220 if (repo_path == NULL) {
2221 error = got_error_from_errno("strdup");
2227 error = got_repo_open(&repo, repo_path);
2231 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2237 char *p, *worktree_subdir = cwd +
2238 strlen(got_worktree_get_root_path(worktree));
2239 if (asprintf(&p, "%s/%s",
2240 got_worktree_get_path_prefix(worktree),
2241 worktree_subdir) == -1) {
2242 error = got_error_from_errno("asprintf");
2245 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2252 if (in_repo_path == NULL) {
2253 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2258 if (commit_id_str == NULL) {
2259 struct got_reference *head_ref;
2260 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2263 error = got_ref_resolve(&commit_id, repo, head_ref);
2264 got_ref_close(head_ref);
2268 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2273 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2274 in_repo_path, repo);
2281 got_worktree_close(worktree);
2283 const struct got_error *repo_error;
2284 repo_error = got_repo_close(repo);
2294 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2298 static const struct got_error *
2299 print_status(void *arg, unsigned char status, const char *path,
2300 struct got_object_id *blob_id, struct got_object_id *commit_id)
2302 printf("%c %s\n", status, path);
2306 static const struct got_error *
2307 cmd_status(int argc, char *argv[])
2309 const struct got_error *error = NULL;
2310 struct got_repository *repo = NULL;
2311 struct got_worktree *worktree = NULL;
2312 char *cwd = NULL, *path = NULL;
2315 while ((ch = getopt(argc, argv, "")) != -1) {
2327 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2331 cwd = getcwd(NULL, 0);
2333 error = got_error_from_errno("getcwd");
2337 error = got_worktree_open(&worktree, cwd);
2344 error = got_error_from_errno("strdup");
2347 } else if (argc == 1) {
2348 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2358 error = apply_unveil(got_repo_get_path(repo), 1,
2359 got_worktree_get_root_path(worktree));
2363 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2364 check_cancelled, NULL);
2375 "usage: %s ref [-r repository] -l | -d name | name target\n",
2380 static const struct got_error *
2381 list_refs(struct got_repository *repo)
2383 static const struct got_error *err = NULL;
2384 struct got_reflist_head refs;
2385 struct got_reflist_entry *re;
2387 SIMPLEQ_INIT(&refs);
2388 err = got_ref_list(&refs, repo);
2392 SIMPLEQ_FOREACH(re, &refs, entry) {
2394 refstr = got_ref_to_str(re->ref);
2396 return got_error_from_errno("got_ref_to_str");
2397 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2401 got_ref_list_free(&refs);
2405 static const struct got_error *
2406 delete_ref(struct got_repository *repo, const char *refname)
2408 const struct got_error *err = NULL;
2409 struct got_reference *ref;
2411 err = got_ref_open(&ref, repo, refname, 0);
2415 err = got_ref_delete(ref, repo);
2420 static const struct got_error *
2421 add_ref(struct got_repository *repo, const char *refname, const char *target)
2423 const struct got_error *err = NULL;
2424 struct got_object_id *id;
2425 struct got_reference *ref = NULL;
2428 * Don't let the user create a reference named '-'.
2429 * While technically a valid reference name, this case is usually
2430 * an unintended typo.
2432 if (refname[0] == '-' && refname[1] == '\0')
2433 return got_error(GOT_ERR_BAD_REF_NAME);
2435 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2438 struct got_reference *target_ref;
2440 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2442 err = got_ref_open(&target_ref, repo, target, 0);
2445 err = got_ref_resolve(&id, repo, target_ref);
2446 got_ref_close(target_ref);
2451 err = got_ref_alloc(&ref, refname, id);
2455 err = got_ref_write(ref, repo);
2463 static const struct got_error *
2464 cmd_ref(int argc, char *argv[])
2466 const struct got_error *error = NULL;
2467 struct got_repository *repo = NULL;
2468 struct got_worktree *worktree = NULL;
2469 char *cwd = NULL, *repo_path = NULL;
2470 int ch, do_list = 0;
2471 const char *delref = NULL;
2473 /* TODO: Add -s option for adding symbolic references. */
2474 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2480 repo_path = realpath(optarg, NULL);
2481 if (repo_path == NULL)
2482 err(1, "-r option");
2483 got_path_strip_trailing_slashes(repo_path);
2494 if (do_list && delref)
2495 errx(1, "-l and -d options are mutually exclusive\n");
2500 if (do_list || delref) {
2503 } else if (argc != 2)
2508 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2512 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2513 "sendfd unveil", NULL) == -1)
2517 cwd = getcwd(NULL, 0);
2519 error = got_error_from_errno("getcwd");
2523 if (repo_path == NULL) {
2524 error = got_worktree_open(&worktree, cwd);
2525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2531 strdup(got_worktree_get_repo_path(worktree));
2532 if (repo_path == NULL)
2533 error = got_error_from_errno("strdup");
2537 repo_path = strdup(cwd);
2538 if (repo_path == NULL) {
2539 error = got_error_from_errno("strdup");
2545 error = got_repo_open(&repo, repo_path);
2549 error = apply_unveil(got_repo_get_path(repo), do_list,
2550 worktree ? got_worktree_get_root_path(worktree) : NULL);
2555 error = list_refs(repo);
2557 error = delete_ref(repo, delref);
2559 error = add_ref(repo, argv[0], argv[1]);
2562 got_repo_close(repo);
2564 got_worktree_close(worktree);
2574 "usage: %s branch [-r repository] -l | -d name | "
2575 "name [base-branch]\n", getprogname());
2579 static const struct got_error *
2580 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2582 static const struct got_error *err = NULL;
2583 struct got_reflist_head refs;
2584 struct got_reflist_entry *re;
2586 SIMPLEQ_INIT(&refs);
2588 err = got_ref_list(&refs, repo);
2592 SIMPLEQ_FOREACH(re, &refs, entry) {
2593 const char *refname, *marker = " ";
2595 refname = got_ref_get_name(re->ref);
2596 if (strncmp(refname, "refs/heads/", 11) != 0)
2598 if (worktree && strcmp(refname,
2599 got_worktree_get_head_ref_name(worktree)) == 0) {
2600 struct got_object_id *id = NULL;
2601 err = got_ref_resolve(&id, repo, re->ref);
2604 if (got_object_id_cmp(id,
2605 got_worktree_get_base_commit_id(worktree)) == 0)
2612 refstr = got_ref_to_str(re->ref);
2614 return got_error_from_errno("got_ref_to_str");
2615 printf("%s%s: %s\n", marker, refname, refstr);
2619 got_ref_list_free(&refs);
2623 static const struct got_error *
2624 delete_branch(struct got_repository *repo, const char *branch_name)
2626 const struct got_error *err = NULL;
2627 struct got_reference *ref;
2630 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2631 return got_error_from_errno("asprintf");
2633 err = got_ref_open(&ref, repo, refname, 0);
2637 err = got_ref_delete(ref, repo);
2644 static const struct got_error *
2645 add_branch(struct got_repository *repo, const char *branch_name,
2646 const char *base_branch)
2648 const struct got_error *err = NULL;
2649 struct got_object_id *id = NULL;
2650 struct got_reference *ref = NULL;
2651 char *base_refname = NULL, *refname = NULL;
2652 struct got_reference *base_ref;
2655 * Don't let the user create a branch named '-'.
2656 * While technically a valid reference name, this case is usually
2657 * an unintended typo.
2659 if (branch_name[0] == '-' && branch_name[1] == '\0')
2660 return got_error(GOT_ERR_BAD_REF_NAME);
2662 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2663 base_refname = strdup(GOT_REF_HEAD);
2664 if (base_refname == NULL)
2665 return got_error_from_errno("strdup");
2666 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2667 return got_error_from_errno("asprintf");
2669 err = got_ref_open(&base_ref, repo, base_refname, 0);
2672 err = got_ref_resolve(&id, repo, base_ref);
2673 got_ref_close(base_ref);
2677 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2678 err = got_error_from_errno("asprintf");
2682 err = got_ref_open(&ref, repo, refname, 0);
2684 err = got_error(GOT_ERR_BRANCH_EXISTS);
2686 } else if (err->code != GOT_ERR_NOT_REF)
2689 err = got_ref_alloc(&ref, refname, id);
2693 err = got_ref_write(ref, repo);
2703 static const struct got_error *
2704 cmd_branch(int argc, char *argv[])
2706 const struct got_error *error = NULL;
2707 struct got_repository *repo = NULL;
2708 struct got_worktree *worktree = NULL;
2709 char *cwd = NULL, *repo_path = NULL;
2710 int ch, do_list = 0;
2711 const char *delref = NULL;
2713 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2719 repo_path = realpath(optarg, NULL);
2720 if (repo_path == NULL)
2721 err(1, "-r option");
2722 got_path_strip_trailing_slashes(repo_path);
2733 if (do_list && delref)
2734 errx(1, "-l and -d options are mutually exclusive\n");
2739 if (do_list || delref) {
2742 } else if (argc < 1 || argc > 2)
2747 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2751 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2752 "sendfd unveil", NULL) == -1)
2756 cwd = getcwd(NULL, 0);
2758 error = got_error_from_errno("getcwd");
2762 if (repo_path == NULL) {
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2770 strdup(got_worktree_get_repo_path(worktree));
2771 if (repo_path == NULL)
2772 error = got_error_from_errno("strdup");
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2784 error = got_repo_open(&repo, repo_path);
2788 error = apply_unveil(got_repo_get_path(repo), do_list,
2789 worktree ? got_worktree_get_root_path(worktree) : NULL);
2794 error = list_branches(repo, worktree);
2796 error = delete_branch(repo, delref);
2798 const char *base_branch;
2800 base_branch = worktree ?
2801 got_worktree_get_head_ref_name(worktree) :
2804 base_branch = argv[1];
2805 error = add_branch(repo, argv[0], base_branch);
2809 got_repo_close(repo);
2811 got_worktree_close(worktree);
2820 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2824 static const struct got_error *
2825 cmd_add(int argc, char *argv[])
2827 const struct got_error *error = NULL;
2828 struct got_repository *repo = NULL;
2829 struct got_worktree *worktree = NULL;
2831 struct got_pathlist_head paths;
2832 struct got_pathlist_entry *pe;
2837 while ((ch = getopt(argc, argv, "")) != -1) {
2849 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2856 cwd = getcwd(NULL, 0);
2858 error = got_error_from_errno("getcwd");
2862 error = got_worktree_open(&worktree, cwd);
2866 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2870 error = apply_unveil(got_repo_get_path(repo), 1,
2871 got_worktree_get_root_path(worktree));
2875 for (x = 0; x < argc; x++) {
2876 char *path = realpath(argv[x], NULL);
2878 error = got_error_from_errno2("realpath", argv[x]);
2882 got_path_strip_trailing_slashes(path);
2883 error = got_pathlist_insert(&pe, &paths, path, NULL);
2889 error = got_worktree_schedule_add(worktree, &paths, print_status,
2893 got_repo_close(repo);
2895 got_worktree_close(worktree);
2896 TAILQ_FOREACH(pe, &paths, entry)
2897 free((char *)pe->path);
2898 got_pathlist_free(&paths);
2906 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2910 static const struct got_error *
2911 cmd_remove(int argc, char *argv[])
2913 const struct got_error *error = NULL;
2914 struct got_worktree *worktree = NULL;
2915 struct got_repository *repo = NULL;
2917 struct got_pathlist_head paths;
2918 struct got_pathlist_entry *pe;
2919 int ch, i, delete_local_mods = 0;
2923 while ((ch = getopt(argc, argv, "f")) != -1) {
2926 delete_local_mods = 1;
2938 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2945 cwd = getcwd(NULL, 0);
2947 error = got_error_from_errno("getcwd");
2950 error = got_worktree_open(&worktree, cwd);
2954 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2958 error = apply_unveil(got_repo_get_path(repo), 1,
2959 got_worktree_get_root_path(worktree));
2963 for (i = 0; i < argc; i++) {
2964 char *path = realpath(argv[i], NULL);
2966 error = got_error_from_errno2("realpath", argv[i]);
2970 got_path_strip_trailing_slashes(path);
2971 error = got_pathlist_insert(&pe, &paths, path, NULL);
2977 error = got_worktree_schedule_delete(worktree, &paths,
2978 delete_local_mods, print_status, NULL, repo);
2983 got_repo_close(repo);
2985 got_worktree_close(worktree);
2986 TAILQ_FOREACH(pe, &paths, entry)
2987 free((char *)pe->path);
2988 got_pathlist_free(&paths);
2996 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3000 static const struct got_error *
3001 revert_progress(void *arg, unsigned char status, const char *path)
3003 while (path[0] == '/')
3005 printf("%c %s\n", status, path);
3009 static const struct got_error *
3010 cmd_revert(int argc, char *argv[])
3012 const struct got_error *error = NULL;
3013 struct got_worktree *worktree = NULL;
3014 struct got_repository *repo = NULL;
3015 char *cwd = NULL, *path = NULL;
3016 struct got_pathlist_head paths;
3017 struct got_pathlist_entry *pe;
3022 while ((ch = getopt(argc, argv, "")) != -1) {
3034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3035 "unveil", NULL) == -1)
3041 for (i = 0; i < argc; i++) {
3042 char *path = realpath(argv[i], NULL);
3044 error = got_error_from_errno2("realpath", argv[i]);
3048 got_path_strip_trailing_slashes(path);
3049 error = got_pathlist_insert(&pe, &paths, path, NULL);
3056 cwd = getcwd(NULL, 0);
3058 error = got_error_from_errno("getcwd");
3061 error = got_worktree_open(&worktree, cwd);
3065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3069 error = apply_unveil(got_repo_get_path(repo), 1,
3070 got_worktree_get_root_path(worktree));
3074 error = got_worktree_revert(worktree, &paths,
3075 revert_progress, NULL, repo);
3080 got_repo_close(repo);
3082 got_worktree_close(worktree);
3091 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3095 struct collect_commit_logmsg_arg {
3096 const char *cmdline_log;
3098 const char *worktree_path;
3099 const char *branch_name;
3100 const char *repo_path;
3105 static const struct got_error *
3106 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3109 char *initial_content = NULL;
3110 struct got_pathlist_entry *pe;
3111 const struct got_error *err = NULL;
3112 char *template = NULL;
3113 struct collect_commit_logmsg_arg *a = arg;
3117 /* if a message was specified on the command line, just use it */
3118 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3119 len = strlen(a->cmdline_log) + 1;
3120 *logmsg = malloc(len + 1);
3121 if (*logmsg == NULL)
3122 return got_error_from_errno("malloc");
3123 strlcpy(*logmsg, a->cmdline_log, len);
3127 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3128 return got_error_from_errno("asprintf");
3130 if (asprintf(&initial_content,
3131 "\n# changes to be committed on branch %s:\n",
3132 a->branch_name) == -1)
3133 return got_error_from_errno("asprintf");
3135 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3139 dprintf(fd, initial_content);
3141 TAILQ_FOREACH(pe, commitable_paths, entry) {
3142 struct got_commitable *ct = pe->data;
3143 dprintf(fd, "# %c %s\n",
3144 got_commitable_get_status(ct),
3145 got_commitable_get_path(ct));
3149 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3151 unlink(a->logmsg_path);
3152 free(a->logmsg_path);
3153 free(initial_content);
3156 /* Editor is done; we can now apply unveil(2) */
3158 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3167 static const struct got_error *
3168 cmd_commit(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_worktree *worktree = NULL;
3172 struct got_repository *repo = NULL;
3173 char *cwd = NULL, *path = NULL, *id_str = NULL;
3174 struct got_object_id *id = NULL;
3175 const char *logmsg = NULL;
3176 const char *got_author = getenv("GOT_AUTHOR");
3177 struct collect_commit_logmsg_arg cl_arg;
3178 char *editor = NULL;
3179 int ch, rebase_in_progress;
3181 while ((ch = getopt(argc, argv, "m:")) != -1) {
3196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3197 "unveil", NULL) == -1)
3201 path = realpath(argv[0], NULL);
3203 error = got_error_from_errno2("realpath", argv[0]);
3206 got_path_strip_trailing_slashes(path);
3207 } else if (argc != 0)
3210 if (got_author == NULL) {
3211 /* TODO: Look current user up in password database */
3212 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3216 cwd = getcwd(NULL, 0);
3218 error = got_error_from_errno("getcwd");
3221 error = got_worktree_open(&worktree, cwd);
3225 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3228 if (rebase_in_progress) {
3229 error = got_error(GOT_ERR_REBASING);
3233 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3238 * unveil(2) traverses exec(2); if an editor is used we have
3239 * to apply unveil after the log message has been written.
3241 if (logmsg == NULL || strlen(logmsg) == 0)
3242 error = get_editor(&editor);
3244 error = apply_unveil(got_repo_get_path(repo), 0,
3245 got_worktree_get_root_path(worktree));
3249 cl_arg.editor = editor;
3250 cl_arg.cmdline_log = logmsg;
3251 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3252 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3253 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3254 cl_arg.branch_name += 5;
3255 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3256 cl_arg.branch_name += 6;
3257 cl_arg.repo_path = got_repo_get_path(repo);
3258 cl_arg.logmsg_path = NULL;
3259 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3260 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3262 if (cl_arg.logmsg_path)
3263 fprintf(stderr, "%s: log message preserved in %s\n",
3264 getprogname(), cl_arg.logmsg_path);
3268 if (cl_arg.logmsg_path)
3269 unlink(cl_arg.logmsg_path);
3271 error = got_object_id_str(&id_str, id);
3274 printf("Created commit %s\n", id_str);
3277 got_repo_close(repo);
3279 got_worktree_close(worktree);
3288 usage_cherrypick(void)
3290 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3294 static const struct got_error *
3295 cmd_cherrypick(int argc, char *argv[])
3297 const struct got_error *error = NULL;
3298 struct got_worktree *worktree = NULL;
3299 struct got_repository *repo = NULL;
3300 char *cwd = NULL, *commit_id_str = NULL;
3301 struct got_object_id *commit_id = NULL;
3302 struct got_commit_object *commit = NULL;
3303 struct got_object_qid *pid;
3304 struct got_reference *head_ref = NULL;
3305 int ch, did_something = 0;
3307 while ((ch = getopt(argc, argv, "")) != -1) {
3319 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3320 "unveil", NULL) == -1)
3326 cwd = getcwd(NULL, 0);
3328 error = got_error_from_errno("getcwd");
3331 error = got_worktree_open(&worktree, cwd);
3335 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3339 error = apply_unveil(got_repo_get_path(repo), 0,
3340 got_worktree_get_root_path(worktree));
3344 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3345 GOT_OBJ_TYPE_COMMIT, repo);
3346 if (error != NULL) {
3347 struct got_reference *ref;
3348 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3350 error = got_ref_open(&ref, repo, argv[0], 0);
3353 error = got_ref_resolve(&commit_id, repo, ref);
3358 error = got_object_id_str(&commit_id_str, commit_id);
3362 error = got_ref_open(&head_ref, repo,
3363 got_worktree_get_head_ref_name(worktree), 0);
3367 error = check_same_branch(commit_id, head_ref, repo);
3369 if (error->code != GOT_ERR_ANCESTRY)
3373 error = got_error(GOT_ERR_SAME_BRANCH);
3377 error = got_object_open_as_commit(&commit, repo, commit_id);
3380 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3381 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3382 commit_id, repo, update_progress, &did_something, check_cancelled,
3388 printf("Merged commit %s\n", commit_id_str);
3391 got_object_commit_close(commit);
3392 free(commit_id_str);
3394 got_ref_close(head_ref);
3396 got_worktree_close(worktree);
3398 got_repo_close(repo);
3405 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3409 static const struct got_error *
3410 cmd_backout(int argc, char *argv[])
3412 const struct got_error *error = NULL;
3413 struct got_worktree *worktree = NULL;
3414 struct got_repository *repo = NULL;
3415 char *cwd = NULL, *commit_id_str = NULL;
3416 struct got_object_id *commit_id = NULL;
3417 struct got_commit_object *commit = NULL;
3418 struct got_object_qid *pid;
3419 struct got_reference *head_ref = NULL;
3420 int ch, did_something = 0;
3422 while ((ch = getopt(argc, argv, "")) != -1) {
3434 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3435 "unveil", NULL) == -1)
3441 cwd = getcwd(NULL, 0);
3443 error = got_error_from_errno("getcwd");
3446 error = got_worktree_open(&worktree, cwd);
3450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3454 error = apply_unveil(got_repo_get_path(repo), 0,
3455 got_worktree_get_root_path(worktree));
3459 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3460 GOT_OBJ_TYPE_COMMIT, repo);
3461 if (error != NULL) {
3462 struct got_reference *ref;
3463 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3465 error = got_ref_open(&ref, repo, argv[0], 0);
3468 error = got_ref_resolve(&commit_id, repo, ref);
3473 error = got_object_id_str(&commit_id_str, commit_id);
3477 error = got_ref_open(&head_ref, repo,
3478 got_worktree_get_head_ref_name(worktree), 0);
3482 error = check_same_branch(commit_id, head_ref, repo);
3486 error = got_object_open_as_commit(&commit, repo, commit_id);
3489 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3491 error = got_error(GOT_ERR_ROOT_COMMIT);
3495 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3496 update_progress, &did_something, check_cancelled, NULL);
3501 printf("Backed out commit %s\n", commit_id_str);
3504 got_object_commit_close(commit);
3505 free(commit_id_str);
3507 got_ref_close(head_ref);
3509 got_worktree_close(worktree);
3511 got_repo_close(repo);
3518 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3524 trim_logmsg(char *logmsg, int limit)
3529 len = strlen(logmsg);
3533 nl = strchr(logmsg, '\n');
3538 static const struct got_error *
3539 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3541 const char *logmsg0 = NULL;
3543 logmsg0 = got_object_commit_get_logmsg(commit);
3545 while (isspace((unsigned char)logmsg0[0]))
3548 *logmsg = strdup(logmsg0);
3549 if (*logmsg == NULL)
3550 return got_error_from_errno("strdup");
3552 trim_logmsg(*logmsg, limit);
3556 static const struct got_error *
3557 show_rebase_progress(struct got_commit_object *commit,
3558 struct got_object_id *old_id, struct got_object_id *new_id)
3560 const struct got_error *err;
3561 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3563 err = got_object_id_str(&old_id_str, old_id);
3568 err = got_object_id_str(&new_id_str, new_id);
3573 old_id_str[12] = '\0';
3575 new_id_str[12] = '\0';
3577 err = get_short_logmsg(&logmsg, 42, commit);
3581 printf("%s -> %s: %s\n", old_id_str,
3582 new_id_str ? new_id_str : "no-op change", logmsg);
3589 static const struct got_error *
3590 rebase_progress(void *arg, unsigned char status, const char *path)
3592 unsigned char *rebase_status = arg;
3594 while (path[0] == '/')
3596 printf("%c %s\n", status, path);
3598 if (*rebase_status == GOT_STATUS_CONFLICT)
3600 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3601 *rebase_status = status;
3605 static const struct got_error *
3606 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3607 struct got_reference *branch, struct got_reference *new_base_branch,
3608 struct got_reference *tmp_branch, struct got_repository *repo)
3610 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3611 return got_worktree_rebase_complete(worktree, fileindex,
3612 new_base_branch, tmp_branch, branch, repo);
3615 static const struct got_error *
3616 rebase_commit(struct got_pathlist_head *merged_paths,
3617 struct got_worktree *worktree, struct got_fileindex *fileindex,
3618 struct got_reference *tmp_branch,
3619 struct got_object_id *commit_id, struct got_repository *repo)
3621 const struct got_error *error;
3622 struct got_commit_object *commit;
3623 struct got_object_id *new_commit_id;
3625 error = got_object_open_as_commit(&commit, repo, commit_id);
3629 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3630 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3632 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3634 error = show_rebase_progress(commit, commit_id, NULL);
3636 error = show_rebase_progress(commit, commit_id, new_commit_id);
3637 free(new_commit_id);
3640 got_object_commit_close(commit);
3644 struct check_path_prefix_arg {
3645 const char *path_prefix;
3650 static const struct got_error *
3651 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3652 struct got_blob_object *blob2, struct got_object_id *id1,
3653 struct got_object_id *id2, const char *path1, const char *path2,
3654 struct got_repository *repo)
3656 struct check_path_prefix_arg *a = arg;
3658 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3659 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3660 return got_error(a->errcode);
3665 static const struct got_error *
3666 check_path_prefix(struct got_object_id *parent_id,
3667 struct got_object_id *commit_id, const char *path_prefix,
3668 int errcode, struct got_repository *repo)
3670 const struct got_error *err;
3671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3673 struct check_path_prefix_arg cpp_arg;
3675 if (got_path_is_root_dir(path_prefix))
3678 err = got_object_open_as_commit(&commit, repo, commit_id);
3682 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3686 err = got_object_open_as_tree(&tree1, repo,
3687 got_object_commit_get_tree_id(parent_commit));
3691 err = got_object_open_as_tree(&tree2, repo,
3692 got_object_commit_get_tree_id(commit));
3696 cpp_arg.path_prefix = path_prefix;
3697 while (cpp_arg.path_prefix[0] == '/')
3698 cpp_arg.path_prefix++;
3699 cpp_arg.len = strlen(cpp_arg.path_prefix);
3700 cpp_arg.errcode = errcode;
3701 err = got_diff_tree(tree1, tree2, "", "", repo,
3702 check_path_prefix_in_diff, &cpp_arg);
3705 got_object_tree_close(tree1);
3707 got_object_tree_close(tree2);
3709 got_object_commit_close(commit);
3711 got_object_commit_close(parent_commit);
3715 static const struct got_error *
3716 collect_commits(struct got_object_id_queue *commits,
3717 struct got_object_id *initial_commit_id,
3718 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3719 const char *path_prefix, int path_prefix_errcode,
3720 struct got_repository *repo)
3722 const struct got_error *err = NULL;
3723 struct got_commit_graph *graph = NULL;
3724 struct got_object_id *parent_id = NULL;
3725 struct got_object_qid *qid;
3726 struct got_object_id *commit_id = initial_commit_id;
3728 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3732 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3735 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3736 err = got_commit_graph_iter_next(&parent_id, graph);
3738 if (err->code == GOT_ERR_ITER_COMPLETED) {
3739 err = got_error_msg(GOT_ERR_ANCESTRY,
3740 "ran out of commits to rebase before "
3741 "youngest common ancestor commit has "
3744 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3746 err = got_commit_graph_fetch_commits(graph, 1, repo);
3750 err = check_path_prefix(parent_id, commit_id,
3751 path_prefix, path_prefix_errcode, repo);
3755 err = got_object_qid_alloc(&qid, commit_id);
3758 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3759 commit_id = parent_id;
3763 got_commit_graph_close(graph);
3767 static const struct got_error *
3768 cmd_rebase(int argc, char *argv[])
3770 const struct got_error *error = NULL;
3771 struct got_worktree *worktree = NULL;
3772 struct got_repository *repo = NULL;
3773 struct got_fileindex *fileindex = NULL;
3775 struct got_reference *branch = NULL;
3776 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3777 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3778 struct got_object_id *resume_commit_id = NULL;
3779 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3780 struct got_commit_object *commit = NULL;
3781 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3782 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3783 struct got_object_id_queue commits;
3784 struct got_pathlist_head merged_paths;
3785 const struct got_object_id_queue *parent_ids;
3786 struct got_object_qid *qid, *pid;
3788 SIMPLEQ_INIT(&commits);
3789 TAILQ_INIT(&merged_paths);
3791 while ((ch = getopt(argc, argv, "ac")) != -1) {
3797 continue_rebase = 1;
3809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 "unveil", NULL) == -1)
3813 if (abort_rebase && continue_rebase)
3815 else if (abort_rebase || continue_rebase) {
3818 } else if (argc != 1)
3821 cwd = getcwd(NULL, 0);
3823 error = got_error_from_errno("getcwd");
3826 error = got_worktree_open(&worktree, cwd);
3830 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3834 error = apply_unveil(got_repo_get_path(repo), 0,
3835 got_worktree_get_root_path(worktree));
3839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3845 if (!rebase_in_progress) {
3846 error = got_error(GOT_ERR_NOT_REBASING);
3849 error = got_worktree_rebase_continue(&resume_commit_id,
3850 &new_base_branch, &tmp_branch, &branch, &fileindex,
3854 printf("Switching work tree to %s\n",
3855 got_ref_get_symref_target(new_base_branch));
3856 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3857 new_base_branch, update_progress, &did_something);
3860 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3861 goto done; /* nothing else to do */
3864 if (continue_rebase) {
3865 if (!rebase_in_progress) {
3866 error = got_error(GOT_ERR_NOT_REBASING);
3869 error = got_worktree_rebase_continue(&resume_commit_id,
3870 &new_base_branch, &tmp_branch, &branch, &fileindex,
3875 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3876 resume_commit_id, repo);
3880 yca_id = got_object_id_dup(resume_commit_id);
3881 if (yca_id == NULL) {
3882 error = got_error_from_errno("got_object_id_dup");
3886 error = got_ref_open(&branch, repo, argv[0], 0);
3890 error = check_same_branch(
3891 got_worktree_get_base_commit_id(worktree), branch, repo);
3893 if (error->code != GOT_ERR_ANCESTRY)
3897 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3898 "specified branch resolves to a commit which "
3899 "is already contained in work tree's branch");
3904 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3908 if (!continue_rebase) {
3909 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3910 got_worktree_get_base_commit_id(worktree),
3911 branch_head_commit_id, repo);
3914 if (yca_id == NULL) {
3915 error = got_error_msg(GOT_ERR_ANCESTRY,
3916 "specified branch shares no common ancestry "
3917 "with work tree's branch");
3921 error = got_worktree_rebase_prepare(&new_base_branch,
3922 &tmp_branch, &fileindex, worktree, branch, repo);
3927 commit_id = branch_head_commit_id;
3928 error = got_object_open_as_commit(&commit, repo, commit_id);
3932 parent_ids = got_object_commit_get_parent_ids(commit);
3933 pid = SIMPLEQ_FIRST(parent_ids);
3934 error = collect_commits(&commits, commit_id, pid->id,
3935 yca_id, got_worktree_get_path_prefix(worktree),
3936 GOT_ERR_REBASE_PATH, repo);
3937 got_object_commit_close(commit);
3942 if (SIMPLEQ_EMPTY(&commits)) {
3943 if (continue_rebase)
3944 error = rebase_complete(worktree, fileindex,
3945 branch, new_base_branch, tmp_branch, repo);
3947 error = got_error(GOT_ERR_EMPTY_REBASE);
3952 SIMPLEQ_FOREACH(qid, &commits, entry) {
3953 commit_id = qid->id;
3954 parent_id = pid ? pid->id : yca_id;
3957 error = got_worktree_rebase_merge_files(&merged_paths,
3958 worktree, fileindex, parent_id, commit_id, repo,
3959 rebase_progress, &rebase_status, check_cancelled, NULL);
3963 if (rebase_status == GOT_STATUS_CONFLICT) {
3964 got_worktree_rebase_pathlist_free(&merged_paths);
3968 error = rebase_commit(&merged_paths, worktree, fileindex,
3969 tmp_branch, commit_id, repo);
3970 got_worktree_rebase_pathlist_free(&merged_paths);
3975 if (rebase_status == GOT_STATUS_CONFLICT) {
3976 error = got_worktree_rebase_postpone(worktree, fileindex);
3979 error = got_error_msg(GOT_ERR_CONFLICTS,
3980 "conflicts must be resolved before rebasing can continue");
3982 error = rebase_complete(worktree, fileindex, branch,
3983 new_base_branch, tmp_branch, repo);
3985 got_object_id_queue_free(&commits);
3986 free(branch_head_commit_id);
3987 free(resume_commit_id);
3990 got_object_commit_close(commit);
3992 got_ref_close(branch);
3993 if (new_base_branch)
3994 got_ref_close(new_base_branch);
3996 got_ref_close(tmp_branch);
3998 got_worktree_close(worktree);
4000 got_repo_close(repo);
4005 usage_histedit(void)
4007 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4012 #define GOT_HISTEDIT_PICK 'p'
4013 #define GOT_HISTEDIT_EDIT 'e'
4014 #define GOT_HISTEDIT_FOLD 'f'
4015 #define GOT_HISTEDIT_DROP 'd'
4016 #define GOT_HISTEDIT_MESG 'm'
4018 static struct got_histedit_cmd {
4022 } got_histedit_cmds[] = {
4023 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4024 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4025 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4026 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4027 { GOT_HISTEDIT_MESG, "mesg",
4028 "single-line log message for commit above (open editor if empty)" },
4031 struct got_histedit_list_entry {
4032 TAILQ_ENTRY(got_histedit_list_entry) entry;
4033 struct got_object_id *commit_id;
4034 const struct got_histedit_cmd *cmd;
4037 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4039 static const struct got_error *
4040 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4041 FILE *f, struct got_repository *repo)
4043 const struct got_error *err = NULL;
4044 char *logmsg = NULL, *id_str = NULL;
4045 struct got_commit_object *commit = NULL;
4048 err = got_object_open_as_commit(&commit, repo, commit_id);
4052 err = get_short_logmsg(&logmsg, 34, commit);
4056 err = got_object_id_str(&id_str, commit_id);
4060 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4062 err = got_ferror(f, GOT_ERR_IO);
4065 got_object_commit_close(commit);
4071 static const struct got_error *
4072 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4073 struct got_repository *repo)
4075 const struct got_error *err = NULL;
4076 struct got_object_qid *qid;
4078 if (SIMPLEQ_EMPTY(commits))
4079 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4081 SIMPLEQ_FOREACH(qid, commits, entry) {
4082 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4091 static const struct got_error *
4092 write_cmd_list(FILE *f)
4094 const struct got_error *err = NULL;
4097 n = fprintf(f, "# Available histedit commands:\n");
4099 return got_ferror(f, GOT_ERR_IO);
4101 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4102 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4103 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4106 err = got_ferror(f, GOT_ERR_IO);
4110 n = fprintf(f, "# Commits will be processed in order from top to "
4111 "bottom of this file.\n");
4113 return got_ferror(f, GOT_ERR_IO);
4117 static const struct got_error *
4118 histedit_syntax_error(int lineno)
4120 static char msg[42];
4123 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4125 if (ret == -1 || ret >= sizeof(msg))
4126 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4128 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4131 static const struct got_error *
4132 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4133 char *logmsg, struct got_repository *repo)
4135 const struct got_error *err;
4136 struct got_commit_object *folded_commit = NULL;
4139 err = got_object_id_str(&id_str, hle->commit_id);
4143 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4147 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4148 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4149 got_object_commit_get_logmsg(folded_commit)) == -1) {
4150 err = got_error_from_errno("asprintf");
4155 got_object_commit_close(folded_commit);
4160 static struct got_histedit_list_entry *
4161 get_folded_commits(struct got_histedit_list_entry *hle)
4163 struct got_histedit_list_entry *prev, *folded = NULL;
4165 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4166 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4167 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4168 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4170 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4176 static const struct got_error *
4177 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4178 struct got_repository *repo)
4180 char *logmsg_path = NULL, *id_str = NULL;
4181 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4182 const struct got_error *err = NULL;
4183 struct got_commit_object *commit = NULL;
4185 struct got_histedit_list_entry *folded = NULL;
4187 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4191 folded = get_folded_commits(hle);
4193 while (folded != hle) {
4194 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4195 folded = TAILQ_NEXT(folded, entry);
4198 err = append_folded_commit_msg(&new_msg, folded,
4204 folded = TAILQ_NEXT(folded, entry);
4208 err = got_object_id_str(&id_str, hle->commit_id);
4211 if (asprintf(&new_msg,
4212 "%s\n# original log message of commit %s: %s",
4213 logmsg ? logmsg : "", id_str,
4214 got_object_commit_get_logmsg(commit)) == -1) {
4215 err = got_error_from_errno("asprintf");
4221 err = got_object_id_str(&id_str, hle->commit_id);
4225 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4229 dprintf(fd, logmsg);
4232 err = get_editor(&editor);
4236 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4238 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4241 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4242 if (hle->logmsg == NULL)
4243 err = got_error_from_errno("strdup");
4246 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4247 err = got_error_from_errno2("unlink", logmsg_path);
4252 got_object_commit_close(commit);
4256 static const struct got_error *
4257 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4258 FILE *f, struct got_repository *repo)
4260 const struct got_error *err = NULL;
4261 char *line = NULL, *p, *end;
4265 const struct got_histedit_cmd *cmd;
4266 struct got_object_id *commit_id = NULL;
4267 struct got_histedit_list_entry *hle = NULL;
4270 len = getline(&line, &size, f);
4272 const struct got_error *getline_err;
4275 getline_err = got_error_from_errno("getline");
4276 err = got_ferror(f, getline_err->code);
4281 while (isspace((unsigned char)p[0]))
4283 if (p[0] == '#' || p[0] == '\0') {
4289 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4290 cmd = &got_histedit_cmds[i];
4291 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4292 isspace((unsigned char)p[strlen(cmd->name)])) {
4293 p += strlen(cmd->name);
4296 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4301 if (i == nitems(got_histedit_cmds)) {
4302 err = histedit_syntax_error(lineno);
4305 while (isspace((unsigned char)p[0]))
4307 if (cmd->code == GOT_HISTEDIT_MESG) {
4308 if (hle == NULL || hle->logmsg != NULL) {
4309 err = got_error(GOT_ERR_HISTEDIT_CMD);
4313 err = histedit_edit_logmsg(hle, repo);
4317 hle->logmsg = strdup(p);
4318 if (hle->logmsg == NULL) {
4319 err = got_error_from_errno("strdup");
4328 while (end[0] && !isspace((unsigned char)end[0]))
4332 err = got_object_resolve_id_str(&commit_id, repo, p);
4334 /* override error code */
4335 err = histedit_syntax_error(lineno);
4339 hle = malloc(sizeof(*hle));
4341 err = got_error_from_errno("malloc");
4345 hle->commit_id = commit_id;
4350 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4358 static const struct got_error *
4359 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4360 const char *path, struct got_repository *repo)
4362 const struct got_error *err = NULL;
4366 err = get_editor(&editor);
4370 if (spawn_editor(editor, path) == -1) {
4371 err = got_error_from_errno("failed spawning editor");
4375 f = fopen(path, "r");
4377 err = got_error_from_errno("fopen");
4380 err = histedit_parse_list(histedit_cmds, f, repo);
4382 if (f && fclose(f) != 0 && err == NULL)
4383 err = got_error_from_errno("fclose");
4388 static const struct got_error *
4389 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4390 struct got_object_id_queue *, const char *, struct got_repository *);
4392 static const struct got_error *
4393 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4394 struct got_object_id_queue *commits, struct got_repository *repo)
4396 const struct got_error *err;
4400 err = got_opentemp_named(&path, &f, "got-histedit");
4404 err = write_cmd_list(f);
4408 err = histedit_write_commit_list(commits, f, repo);
4412 if (fclose(f) != 0) {
4413 err = got_error_from_errno("fclose");
4418 err = histedit_run_editor(histedit_cmds, path, repo);
4420 const char *errmsg = err->msg;
4421 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4423 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4424 commits, path, repo);
4427 if (f && fclose(f) != 0 && err == NULL)
4428 err = got_error_from_errno("fclose");
4429 if (path && unlink(path) != 0 && err == NULL)
4430 err = got_error_from_errno2("unlink", path);
4435 static const struct got_error *
4436 histedit_save_list(struct got_histedit_list *histedit_cmds,
4437 struct got_worktree *worktree, struct got_repository *repo)
4439 const struct got_error *err = NULL;
4442 struct got_histedit_list_entry *hle;
4443 struct got_commit_object *commit = NULL;
4445 err = got_worktree_get_histedit_list_path(&path, worktree);
4449 f = fopen(path, "w");
4451 err = got_error_from_errno2("fopen", path);
4454 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4455 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4461 int n = fprintf(f, "%c %s\n",
4462 GOT_HISTEDIT_MESG, hle->logmsg);
4464 err = got_ferror(f, GOT_ERR_IO);
4470 if (f && fclose(f) != 0 && err == NULL)
4471 err = got_error_from_errno("fclose");
4474 got_object_commit_close(commit);
4478 static const struct got_error *
4479 histedit_load_list(struct got_histedit_list *histedit_cmds,
4480 const char *path, struct got_repository *repo)
4482 const struct got_error *err = NULL;
4485 f = fopen(path, "r");
4487 err = got_error_from_errno2("fopen", path);
4491 err = histedit_parse_list(histedit_cmds, f, repo);
4493 if (f && fclose(f) != 0 && err == NULL)
4494 err = got_error_from_errno("fclose");
4498 static const struct got_error *
4499 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4500 const char *errmsg, struct got_object_id_queue *commits,
4501 const char *path, struct got_repository *repo)
4503 const struct got_error *err = NULL;
4506 while (resp != 'c' && resp != 'r' && resp != 'a') {
4507 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4508 "or (a)bort: ", getprogname(), errmsg);
4511 err = histedit_run_editor(histedit_cmds, path, repo);
4513 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4518 } else if (resp == 'r') {
4519 err = histedit_edit_script(histedit_cmds,
4522 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4527 } else if (resp == 'a') {
4528 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4531 printf("invalid response '%c'\n", resp);
4537 static const struct got_error *
4538 histedit_complete(struct got_worktree *worktree,
4539 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4540 struct got_reference *branch, struct got_repository *repo)
4542 printf("Switching work tree to %s\n",
4543 got_ref_get_symref_target(branch));
4544 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4548 static const struct got_error *
4549 show_histedit_progress(struct got_commit_object *commit,
4550 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4552 const struct got_error *err;
4553 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4555 err = got_object_id_str(&old_id_str, hle->commit_id);
4560 err = got_object_id_str(&new_id_str, new_id);
4565 old_id_str[12] = '\0';
4567 new_id_str[12] = '\0';
4570 logmsg = strdup(hle->logmsg);
4571 if (logmsg == NULL) {
4572 err = got_error_from_errno("strdup");
4575 trim_logmsg(logmsg, 42);
4577 err = get_short_logmsg(&logmsg, 42, commit);
4582 switch (hle->cmd->code) {
4583 case GOT_HISTEDIT_PICK:
4584 case GOT_HISTEDIT_EDIT:
4585 printf("%s -> %s: %s\n", old_id_str,
4586 new_id_str ? new_id_str : "no-op change", logmsg);
4588 case GOT_HISTEDIT_DROP:
4589 case GOT_HISTEDIT_FOLD:
4590 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4603 static const struct got_error *
4604 histedit_commit(struct got_pathlist_head *merged_paths,
4605 struct got_worktree *worktree, struct got_fileindex *fileindex,
4606 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4607 struct got_repository *repo)
4609 const struct got_error *err;
4610 struct got_commit_object *commit;
4611 struct got_object_id *new_commit_id;
4613 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4614 && hle->logmsg == NULL) {
4615 err = histedit_edit_logmsg(hle, repo);
4620 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4624 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4625 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4628 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4630 err = show_histedit_progress(commit, hle, NULL);
4632 err = show_histedit_progress(commit, hle, new_commit_id);
4633 free(new_commit_id);
4636 got_object_commit_close(commit);
4640 static const struct got_error *
4641 histedit_skip_commit(struct got_histedit_list_entry *hle,
4642 struct got_worktree *worktree, struct got_repository *repo)
4644 const struct got_error *error;
4645 struct got_commit_object *commit;
4647 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4652 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4656 error = show_histedit_progress(commit, hle, NULL);
4657 got_object_commit_close(commit);
4661 static const struct got_error *
4662 histedit_check_script(struct got_histedit_list *histedit_cmds,
4663 struct got_object_id_queue *commits, struct got_repository *repo)
4665 const struct got_error *err = NULL;
4666 struct got_object_qid *qid;
4667 struct got_histedit_list_entry *hle;
4668 static char msg[80];
4671 if (TAILQ_EMPTY(histedit_cmds))
4672 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4674 SIMPLEQ_FOREACH(qid, commits, entry) {
4675 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4676 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4680 err = got_object_id_str(&id_str, qid->id);
4683 snprintf(msg, sizeof(msg),
4684 "commit %s missing from histedit script", id_str);
4686 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4690 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4691 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4692 "last commit in histedit script cannot be folded");
4697 static const struct got_error *
4698 cmd_histedit(int argc, char *argv[])
4700 const struct got_error *error = NULL;
4701 struct got_worktree *worktree = NULL;
4702 struct got_fileindex *fileindex = NULL;
4703 struct got_repository *repo = NULL;
4705 struct got_reference *branch = NULL;
4706 struct got_reference *tmp_branch = NULL;
4707 struct got_object_id *resume_commit_id = NULL;
4708 struct got_object_id *base_commit_id = NULL;
4709 struct got_object_id *head_commit_id = NULL;
4710 struct got_commit_object *commit = NULL;
4711 int ch, rebase_in_progress = 0;
4712 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4713 const char *edit_script_path = NULL;
4714 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4715 struct got_object_id_queue commits;
4716 struct got_pathlist_head merged_paths;
4717 const struct got_object_id_queue *parent_ids;
4718 struct got_object_qid *pid;
4719 struct got_histedit_list histedit_cmds;
4720 struct got_histedit_list_entry *hle;
4722 SIMPLEQ_INIT(&commits);
4723 TAILQ_INIT(&histedit_cmds);
4724 TAILQ_INIT(&merged_paths);
4726 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4735 edit_script_path = optarg;
4747 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4748 "unveil", NULL) == -1)
4751 if (abort_edit && continue_edit)
4757 * This command cannot apply unveil(2) in all cases because the
4758 * user may choose to run an editor to edit the histedit script
4759 * and to edit individual commit log messages.
4760 * unveil(2) traverses exec(2); if an editor is used we have to
4761 * apply unveil after edit script and log messages have been written.
4762 * XXX TODO: Make use of unveil(2) where possible.
4765 cwd = getcwd(NULL, 0);
4767 error = got_error_from_errno("getcwd");
4770 error = got_worktree_open(&worktree, cwd);
4774 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4778 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4781 if (rebase_in_progress) {
4782 error = got_error(GOT_ERR_REBASING);
4786 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4790 if (edit_in_progress && abort_edit) {
4792 error = got_worktree_histedit_continue(&resume_commit_id,
4793 &tmp_branch, &branch, &base_commit_id, &fileindex,
4797 printf("Switching work tree to %s\n",
4798 got_ref_get_symref_target(branch));
4799 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4800 branch, base_commit_id, update_progress, &did_something);
4803 printf("Histedit of %s aborted\n",
4804 got_ref_get_symref_target(branch));
4805 goto done; /* nothing else to do */
4806 } else if (abort_edit) {
4807 error = got_error(GOT_ERR_NOT_HISTEDIT);
4811 if (continue_edit) {
4814 if (!edit_in_progress) {
4815 error = got_error(GOT_ERR_NOT_HISTEDIT);
4819 error = got_worktree_get_histedit_list_path(&path, worktree);
4823 error = histedit_load_list(&histedit_cmds, path, repo);
4828 error = got_worktree_histedit_continue(&resume_commit_id,
4829 &tmp_branch, &branch, &base_commit_id, &fileindex,
4834 error = got_ref_resolve(&head_commit_id, repo, branch);
4838 error = got_object_open_as_commit(&commit, repo,
4842 parent_ids = got_object_commit_get_parent_ids(commit);
4843 pid = SIMPLEQ_FIRST(parent_ids);
4845 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4848 error = collect_commits(&commits, head_commit_id, pid->id,
4849 base_commit_id, got_worktree_get_path_prefix(worktree),
4850 GOT_ERR_HISTEDIT_PATH, repo);
4851 got_object_commit_close(commit);
4856 if (edit_in_progress) {
4857 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4861 error = got_ref_open(&branch, repo,
4862 got_worktree_get_head_ref_name(worktree), 0);
4866 error = got_ref_resolve(&head_commit_id, repo, branch);
4870 error = got_object_open_as_commit(&commit, repo,
4874 parent_ids = got_object_commit_get_parent_ids(commit);
4875 pid = SIMPLEQ_FIRST(parent_ids);
4877 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4880 error = collect_commits(&commits, head_commit_id, pid->id,
4881 got_worktree_get_base_commit_id(worktree),
4882 got_worktree_get_path_prefix(worktree),
4883 GOT_ERR_HISTEDIT_PATH, repo);
4884 got_object_commit_close(commit);
4889 if (edit_script_path) {
4890 error = histedit_load_list(&histedit_cmds,
4891 edit_script_path, repo);
4895 error = histedit_edit_script(&histedit_cmds, &commits,
4902 error = histedit_save_list(&histedit_cmds, worktree,
4907 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4908 &base_commit_id, &fileindex, worktree, repo);
4914 error = histedit_check_script(&histedit_cmds, &commits, repo);
4918 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4919 if (resume_commit_id) {
4920 if (got_object_id_cmp(hle->commit_id,
4921 resume_commit_id) != 0)
4924 resume_commit_id = NULL;
4925 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4926 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4927 error = histedit_skip_commit(hle, worktree,
4930 error = histedit_commit(NULL, worktree,
4931 fileindex, tmp_branch, hle, repo);
4938 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4939 error = histedit_skip_commit(hle, worktree, repo);
4945 error = got_object_open_as_commit(&commit, repo,
4949 parent_ids = got_object_commit_get_parent_ids(commit);
4950 pid = SIMPLEQ_FIRST(parent_ids);
4952 error = got_worktree_histedit_merge_files(&merged_paths,
4953 worktree, fileindex, pid->id, hle->commit_id, repo,
4954 rebase_progress, &rebase_status, check_cancelled, NULL);
4957 got_object_commit_close(commit);
4960 if (rebase_status == GOT_STATUS_CONFLICT) {
4961 got_worktree_rebase_pathlist_free(&merged_paths);
4965 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4967 error = got_object_id_str(&id_str, hle->commit_id);
4970 printf("Stopping histedit for amending commit %s\n",
4973 got_worktree_rebase_pathlist_free(&merged_paths);
4974 error = got_worktree_histedit_postpone(worktree,
4979 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4980 error = histedit_skip_commit(hle, worktree, repo);
4986 error = histedit_commit(&merged_paths, worktree, fileindex,
4987 tmp_branch, hle, repo);
4988 got_worktree_rebase_pathlist_free(&merged_paths);
4993 if (rebase_status == GOT_STATUS_CONFLICT) {
4994 error = got_worktree_histedit_postpone(worktree, fileindex);
4997 error = got_error_msg(GOT_ERR_CONFLICTS,
4998 "conflicts must be resolved before rebasing can continue");
5000 error = histedit_complete(worktree, fileindex, tmp_branch,
5003 got_object_id_queue_free(&commits);
5004 free(head_commit_id);
5005 free(base_commit_id);
5006 free(resume_commit_id);
5008 got_object_commit_close(commit);
5010 got_ref_close(branch);
5012 got_ref_close(tmp_branch);
5014 got_worktree_close(worktree);
5016 got_repo_close(repo);