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);
1134 error = got_object_id_str(&commit_id_str, commit_id);
1140 struct got_object_id *head_commit_id;
1141 if (strlen(path) != 0) {
1142 fprintf(stderr, "%s: switching between branches "
1143 "requires that the entire work tree "
1144 "gets updated, not just '%s'\n",
1145 getprogname(), path);
1146 error = got_error(GOT_ERR_BAD_PATH);
1149 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1152 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1153 free(head_commit_id);
1156 error = check_same_branch(commit_id, head_ref, repo);
1159 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1163 error = check_linear_ancestry(commit_id,
1164 got_worktree_get_base_commit_id(worktree), repo);
1165 if (error != NULL) {
1166 if (error->code == GOT_ERR_ANCESTRY)
1167 error = got_error(GOT_ERR_BRANCH_MOVED);
1170 error = check_same_branch(commit_id, head_ref, repo);
1175 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1177 error = got_worktree_set_base_commit_id(worktree, repo,
1183 error = got_worktree_checkout_files(worktree, path, repo,
1184 update_progress, &did_something, check_cancelled, NULL);
1189 printf("Updated to commit %s\n", commit_id_str);
1191 printf("Already up-to-date\n");
1193 free(worktree_path);
1196 free(commit_id_str);
1200 static const struct got_error *
1201 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1202 int diff_context, struct got_repository *repo)
1204 const struct got_error *err = NULL;
1205 struct got_tree_object *tree1 = NULL, *tree2;
1206 struct got_object_qid *qid;
1207 char *id_str1 = NULL, *id_str2;
1208 struct got_diff_blob_output_unidiff_arg arg;
1210 err = got_object_open_as_tree(&tree2, repo,
1211 got_object_commit_get_tree_id(commit));
1215 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1217 struct got_commit_object *pcommit;
1219 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1223 err = got_object_open_as_tree(&tree1, repo,
1224 got_object_commit_get_tree_id(pcommit));
1225 got_object_commit_close(pcommit);
1229 err = got_object_id_str(&id_str1, qid->id);
1234 err = got_object_id_str(&id_str2, id);
1238 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1239 arg.diff_context = diff_context;
1240 arg.outfile = stdout;
1241 err = got_diff_tree(tree1, tree2, "", "", repo,
1242 got_diff_blob_output_unidiff, &arg);
1245 got_object_tree_close(tree1);
1246 got_object_tree_close(tree2);
1253 get_datestr(time_t *time, char *datebuf)
1255 char *p, *s = ctime_r(time, datebuf);
1256 p = strchr(s, '\n');
1262 static const struct got_error *
1263 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1264 struct got_repository *repo, int show_patch, int diff_context,
1265 struct got_reflist_head *refs)
1267 const struct got_error *err = NULL;
1268 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1270 time_t committer_time;
1271 const char *author, *committer;
1272 char *refs_str = NULL;
1273 struct got_reflist_entry *re;
1275 SIMPLEQ_FOREACH(re, refs, entry) {
1278 if (got_object_id_cmp(re->id, id) != 0)
1280 name = got_ref_get_name(re->ref);
1281 if (strcmp(name, GOT_REF_HEAD) == 0)
1283 if (strncmp(name, "refs/", 5) == 0)
1285 if (strncmp(name, "got/", 4) == 0)
1287 if (strncmp(name, "heads/", 6) == 0)
1289 if (strncmp(name, "remotes/", 8) == 0)
1292 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1294 err = got_error_from_errno("asprintf");
1300 err = got_object_id_str(&id_str, id);
1304 printf("-----------------------------------------------\n");
1305 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1306 refs_str ? refs_str : "", refs_str ? ")" : "");
1311 printf("from: %s\n", got_object_commit_get_author(commit));
1312 committer_time = got_object_commit_get_committer_time(commit);
1313 datestr = get_datestr(&committer_time, datebuf);
1314 printf("date: %s UTC\n", datestr);
1315 author = got_object_commit_get_author(commit);
1316 committer = got_object_commit_get_committer(commit);
1317 if (strcmp(author, committer) != 0)
1318 printf("via: %s\n", committer);
1319 if (got_object_commit_get_nparents(commit) > 1) {
1320 const struct got_object_id_queue *parent_ids;
1321 struct got_object_qid *qid;
1323 parent_ids = got_object_commit_get_parent_ids(commit);
1324 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1325 err = got_object_id_str(&id_str, qid->id);
1328 printf("parent %d: %s\n", n++, id_str);
1333 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1334 if (logmsg0 == NULL)
1335 return got_error_from_errno("strdup");
1339 line = strsep(&logmsg, "\n");
1341 printf(" %s\n", line);
1346 err = print_patch(commit, id, diff_context, repo);
1351 if (fflush(stdout) != 0 && err == NULL)
1352 err = got_error_from_errno("fflush");
1356 static const struct got_error *
1357 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1358 char *path, int show_patch, int diff_context, int limit,
1359 int first_parent_traversal, struct got_reflist_head *refs)
1361 const struct got_error *err;
1362 struct got_commit_graph *graph;
1364 err = got_commit_graph_open(&graph, root_id, path,
1365 first_parent_traversal, repo);
1368 err = got_commit_graph_iter_start(graph, root_id, repo);
1372 struct got_commit_object *commit;
1373 struct got_object_id *id;
1375 if (sigint_received || sigpipe_received)
1378 err = got_commit_graph_iter_next(&id, graph);
1380 if (err->code == GOT_ERR_ITER_COMPLETED) {
1384 if (err->code != GOT_ERR_ITER_NEED_MORE)
1386 err = got_commit_graph_fetch_commits(graph, 1, repo);
1395 err = got_object_open_as_commit(&commit, repo, id);
1398 err = print_commit(commit, id, repo, show_patch, diff_context,
1400 got_object_commit_close(commit);
1401 if (err || (limit && --limit == 0))
1405 got_commit_graph_close(graph);
1412 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1413 "[-r repository-path] [path]\n", getprogname());
1417 static const struct got_error *
1418 cmd_log(int argc, char *argv[])
1420 const struct got_error *error;
1421 struct got_repository *repo = NULL;
1422 struct got_worktree *worktree = NULL;
1423 struct got_commit_object *commit = NULL;
1424 struct got_object_id *id = NULL;
1425 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1426 char *start_commit = NULL;
1427 int diff_context = 3, ch;
1428 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1430 struct got_reflist_head refs;
1432 SIMPLEQ_INIT(&refs);
1435 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1441 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1447 start_commit = optarg;
1450 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1453 err(1, "-C option %s", errstr);
1456 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1458 err(1, "-l option %s", errstr);
1461 first_parent_traversal = 1;
1464 repo_path = realpath(optarg, NULL);
1465 if (repo_path == NULL)
1466 err(1, "-r option");
1467 got_path_strip_trailing_slashes(repo_path);
1478 cwd = getcwd(NULL, 0);
1480 error = got_error_from_errno("getcwd");
1484 error = got_worktree_open(&worktree, cwd);
1485 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1492 error = got_error_from_errno("strdup");
1495 } else if (argc == 1) {
1497 error = got_worktree_resolve_path(&path, worktree,
1502 path = strdup(argv[0]);
1504 error = got_error_from_errno("strdup");
1511 if (repo_path == NULL) {
1512 repo_path = worktree ?
1513 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1515 if (repo_path == NULL) {
1516 error = got_error_from_errno("strdup");
1520 error = got_repo_open(&repo, repo_path);
1524 error = apply_unveil(got_repo_get_path(repo), 1,
1525 worktree ? got_worktree_get_root_path(worktree) : NULL);
1529 if (start_commit == NULL) {
1530 struct got_reference *head_ref;
1531 error = got_ref_open(&head_ref, repo,
1532 worktree ? got_worktree_get_head_ref_name(worktree)
1536 error = got_ref_resolve(&id, repo, head_ref);
1537 got_ref_close(head_ref);
1540 error = got_object_open_as_commit(&commit, repo, id);
1542 struct got_reference *ref;
1543 error = got_ref_open(&ref, repo, start_commit, 0);
1544 if (error == NULL) {
1546 error = got_ref_resolve(&id, repo, ref);
1550 error = got_object_get_type(&obj_type, repo, id);
1553 if (obj_type == GOT_OBJ_TYPE_TAG) {
1554 struct got_tag_object *tag;
1555 error = got_object_open_as_tag(&tag, repo, id);
1558 if (got_object_tag_get_object_type(tag) !=
1559 GOT_OBJ_TYPE_COMMIT) {
1560 got_object_tag_close(tag);
1561 error = got_error(GOT_ERR_OBJ_TYPE);
1565 id = got_object_id_dup(
1566 got_object_tag_get_object_id(tag));
1568 error = got_error_from_errno(
1569 "got_object_id_dup");
1570 got_object_tag_close(tag);
1573 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1574 error = got_error(GOT_ERR_OBJ_TYPE);
1577 error = got_object_open_as_commit(&commit, repo, id);
1581 if (commit == NULL) {
1582 error = got_repo_match_object_id_prefix(&id,
1583 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1591 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1596 path = in_repo_path;
1599 error = got_ref_list(&refs, repo);
1603 error = print_commits(id, repo, path, show_patch,
1604 diff_context, limit, first_parent_traversal, &refs);
1611 got_worktree_close(worktree);
1613 const struct got_error *repo_error;
1614 repo_error = got_repo_close(repo);
1618 got_ref_list_free(&refs);
1625 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1626 "[object1 object2 | path]\n", getprogname());
1630 struct print_diff_arg {
1631 struct got_repository *repo;
1632 struct got_worktree *worktree;
1638 static const struct got_error *
1639 print_diff(void *arg, unsigned char status, const char *path,
1640 struct got_object_id *blob_id, struct got_object_id *commit_id)
1642 struct print_diff_arg *a = arg;
1643 const struct got_error *err = NULL;
1644 struct got_blob_object *blob1 = NULL;
1646 char *abspath = NULL;
1649 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1650 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1653 if (!a->header_shown) {
1654 printf("diff %s %s\n", a->id_str,
1655 got_worktree_get_root_path(a->worktree));
1656 a->header_shown = 1;
1659 if (status != GOT_STATUS_ADD) {
1660 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1666 if (status != GOT_STATUS_DELETE) {
1667 if (asprintf(&abspath, "%s/%s",
1668 got_worktree_get_root_path(a->worktree), path) == -1) {
1669 err = got_error_from_errno("asprintf");
1673 f2 = fopen(abspath, "r");
1675 err = got_error_from_errno2("fopen", abspath);
1678 if (lstat(abspath, &sb) == -1) {
1679 err = got_error_from_errno2("lstat", abspath);
1685 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1689 got_object_blob_close(blob1);
1690 if (f2 && fclose(f2) != 0 && err == NULL)
1691 err = got_error_from_errno("fclose");
1696 static const struct got_error *
1697 cmd_diff(int argc, char *argv[])
1699 const struct got_error *error;
1700 struct got_repository *repo = NULL;
1701 struct got_worktree *worktree = NULL;
1702 char *cwd = NULL, *repo_path = NULL;
1703 struct got_object_id *id1 = NULL, *id2 = NULL;
1704 const char *id_str1 = NULL, *id_str2 = NULL;
1705 char *label1 = NULL, *label2 = NULL;
1707 int diff_context = 3, ch;
1712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1717 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1720 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1722 err(1, "-C option %s", errstr);
1725 repo_path = realpath(optarg, NULL);
1726 if (repo_path == NULL)
1727 err(1, "-r option");
1728 got_path_strip_trailing_slashes(repo_path);
1739 cwd = getcwd(NULL, 0);
1741 error = got_error_from_errno("getcwd");
1744 error = got_worktree_open(&worktree, cwd);
1745 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1748 if (worktree == NULL) {
1749 error = got_error(GOT_ERR_NOT_WORKTREE);
1754 "-r option can't be used when diffing a work tree");
1755 repo_path = strdup(got_worktree_get_repo_path(worktree));
1756 if (repo_path == NULL) {
1757 error = got_error_from_errno("strdup");
1761 error = got_worktree_resolve_path(&path, worktree,
1768 error = got_error_from_errno("strdup");
1772 } else if (argc == 2) {
1775 if (worktree && repo_path == NULL) {
1777 strdup(got_worktree_get_repo_path(worktree));
1778 if (repo_path == NULL) {
1779 error = got_error_from_errno("strdup");
1786 if (repo_path == NULL) {
1787 repo_path = getcwd(NULL, 0);
1788 if (repo_path == NULL)
1789 return got_error_from_errno("getcwd");
1792 error = got_repo_open(&repo, repo_path);
1797 error = apply_unveil(got_repo_get_path(repo), 1,
1798 worktree ? got_worktree_get_root_path(worktree) : NULL);
1803 struct print_diff_arg arg;
1805 error = got_object_id_str(&id_str,
1806 got_worktree_get_base_commit_id(worktree));
1810 arg.worktree = worktree;
1811 arg.diff_context = diff_context;
1812 arg.id_str = id_str;
1813 arg.header_shown = 0;
1815 error = got_worktree_status(worktree, path, repo, print_diff,
1816 &arg, check_cancelled, NULL);
1821 error = got_repo_match_object_id_prefix(&id1, id_str1,
1822 GOT_OBJ_TYPE_ANY, repo);
1824 struct got_reference *ref;
1825 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1827 error = got_ref_open(&ref, repo, id_str1, 0);
1830 label1 = strdup(got_ref_get_name(ref));
1831 if (label1 == NULL) {
1832 error = got_error_from_errno("strdup");
1835 error = got_ref_resolve(&id1, repo, ref);
1840 error = got_object_id_str(&label1, id1);
1841 if (label1 == NULL) {
1842 error = got_error_from_errno("strdup");
1847 error = got_repo_match_object_id_prefix(&id2, id_str2,
1848 GOT_OBJ_TYPE_ANY, repo);
1850 struct got_reference *ref;
1851 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1853 error = got_ref_open(&ref, repo, id_str2, 0);
1856 label2 = strdup(got_ref_get_name(ref));
1857 if (label2 == NULL) {
1858 error = got_error_from_errno("strdup");
1861 error = got_ref_resolve(&id2, repo, ref);
1866 error = got_object_id_str(&label2, id2);
1867 if (label2 == NULL) {
1868 error = got_error_from_errno("strdup");
1873 error = got_object_get_type(&type1, repo, id1);
1877 error = got_object_get_type(&type2, repo, id2);
1881 if (type1 != type2) {
1882 error = got_error(GOT_ERR_OBJ_TYPE);
1887 case GOT_OBJ_TYPE_BLOB:
1888 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1889 diff_context, repo, stdout);
1891 case GOT_OBJ_TYPE_TREE:
1892 error = got_diff_objects_as_trees(id1, id2, "", "",
1893 diff_context, repo, stdout);
1895 case GOT_OBJ_TYPE_COMMIT:
1896 printf("diff %s %s\n", label1, label2);
1897 error = got_diff_objects_as_commits(id1, id2, diff_context,
1901 error = got_error(GOT_ERR_OBJ_TYPE);
1911 got_worktree_close(worktree);
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1925 "usage: %s blame [-c commit] [-r repository-path] path\n",
1930 static const struct got_error *
1931 cmd_blame(int argc, char *argv[])
1933 const struct got_error *error;
1934 struct got_repository *repo = NULL;
1935 struct got_worktree *worktree = NULL;
1936 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1937 struct got_object_id *commit_id = NULL;
1938 char *commit_id_str = NULL;
1942 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1947 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1950 commit_id_str = optarg;
1953 repo_path = realpath(optarg, NULL);
1954 if (repo_path == NULL)
1955 err(1, "-r option");
1956 got_path_strip_trailing_slashes(repo_path);
1972 cwd = getcwd(NULL, 0);
1974 error = got_error_from_errno("getcwd");
1977 if (repo_path == NULL) {
1978 error = got_worktree_open(&worktree, cwd);
1979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1985 strdup(got_worktree_get_repo_path(worktree));
1986 if (repo_path == NULL)
1987 error = got_error_from_errno("strdup");
1991 repo_path = strdup(cwd);
1992 if (repo_path == NULL) {
1993 error = got_error_from_errno("strdup");
1999 error = got_repo_open(&repo, repo_path);
2003 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2008 const char *prefix = got_worktree_get_path_prefix(worktree);
2009 char *p, *worktree_subdir = cwd +
2010 strlen(got_worktree_get_root_path(worktree));
2011 if (asprintf(&p, "%s%s%s%s%s",
2012 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2013 worktree_subdir, worktree_subdir[0] ? "/" : "",
2015 error = got_error_from_errno("asprintf");
2018 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2021 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2026 if (commit_id_str == NULL) {
2027 struct got_reference *head_ref;
2028 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2031 error = got_ref_resolve(&commit_id, repo, head_ref);
2032 got_ref_close(head_ref);
2036 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2041 error = got_blame(in_repo_path, commit_id, repo, stdout);
2048 got_worktree_close(worktree);
2050 const struct got_error *repo_error;
2051 repo_error = got_repo_close(repo);
2062 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2068 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2069 const char *root_path)
2071 int is_root_path = (strcmp(path, root_path) == 0);
2073 path += strlen(root_path);
2074 while (path[0] == '/')
2077 printf("%s%s%s%s%s\n", id ? id : "", path,
2078 is_root_path ? "" : "/", te->name,
2079 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2082 static const struct got_error *
2083 print_tree(const char *path, struct got_object_id *commit_id,
2084 int show_ids, int recurse, const char *root_path,
2085 struct got_repository *repo)
2087 const struct got_error *err = NULL;
2088 struct got_object_id *tree_id = NULL;
2089 struct got_tree_object *tree = NULL;
2090 const struct got_tree_entries *entries;
2091 struct got_tree_entry *te;
2093 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2097 err = got_object_open_as_tree(&tree, repo, tree_id);
2100 entries = got_object_tree_get_entries(tree);
2101 te = SIMPLEQ_FIRST(&entries->head);
2105 if (sigint_received || sigpipe_received)
2110 err = got_object_id_str(&id_str, te->id);
2113 if (asprintf(&id, "%s ", id_str) == -1) {
2114 err = got_error_from_errno("asprintf");
2120 print_entry(te, id, path, root_path);
2123 if (recurse && S_ISDIR(te->mode)) {
2125 if (asprintf(&child_path, "%s%s%s", path,
2126 path[0] == '/' && path[1] == '\0' ? "" : "/",
2128 err = got_error_from_errno("asprintf");
2131 err = print_tree(child_path, commit_id, show_ids, 1,
2138 te = SIMPLEQ_NEXT(te, entry);
2142 got_object_tree_close(tree);
2147 static const struct got_error *
2148 cmd_tree(int argc, char *argv[])
2150 const struct got_error *error;
2151 struct got_repository *repo = NULL;
2152 struct got_worktree *worktree = NULL;
2154 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2155 struct got_object_id *commit_id = NULL;
2156 char *commit_id_str = NULL;
2157 int show_ids = 0, recurse = 0;
2161 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2166 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2169 commit_id_str = optarg;
2172 repo_path = realpath(optarg, NULL);
2173 if (repo_path == NULL)
2174 err(1, "-r option");
2175 got_path_strip_trailing_slashes(repo_path);
2199 cwd = getcwd(NULL, 0);
2201 error = got_error_from_errno("getcwd");
2204 if (repo_path == NULL) {
2205 error = got_worktree_open(&worktree, cwd);
2206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2212 strdup(got_worktree_get_repo_path(worktree));
2213 if (repo_path == NULL)
2214 error = got_error_from_errno("strdup");
2218 repo_path = strdup(cwd);
2219 if (repo_path == NULL) {
2220 error = got_error_from_errno("strdup");
2226 error = got_repo_open(&repo, repo_path);
2230 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2236 char *p, *worktree_subdir = cwd +
2237 strlen(got_worktree_get_root_path(worktree));
2238 if (asprintf(&p, "%s/%s",
2239 got_worktree_get_path_prefix(worktree),
2240 worktree_subdir) == -1) {
2241 error = got_error_from_errno("asprintf");
2244 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2251 if (in_repo_path == NULL) {
2252 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2257 if (commit_id_str == NULL) {
2258 struct got_reference *head_ref;
2259 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2262 error = got_ref_resolve(&commit_id, repo, head_ref);
2263 got_ref_close(head_ref);
2267 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2272 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2273 in_repo_path, repo);
2280 got_worktree_close(worktree);
2282 const struct got_error *repo_error;
2283 repo_error = got_repo_close(repo);
2293 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2297 static const struct got_error *
2298 print_status(void *arg, unsigned char status, const char *path,
2299 struct got_object_id *blob_id, struct got_object_id *commit_id)
2301 printf("%c %s\n", status, path);
2305 static const struct got_error *
2306 cmd_status(int argc, char *argv[])
2308 const struct got_error *error = NULL;
2309 struct got_repository *repo = NULL;
2310 struct got_worktree *worktree = NULL;
2311 char *cwd = NULL, *path = NULL;
2314 while ((ch = getopt(argc, argv, "")) != -1) {
2326 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2330 cwd = getcwd(NULL, 0);
2332 error = got_error_from_errno("getcwd");
2336 error = got_worktree_open(&worktree, cwd);
2343 error = got_error_from_errno("strdup");
2346 } else if (argc == 1) {
2347 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2353 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2357 error = apply_unveil(got_repo_get_path(repo), 1,
2358 got_worktree_get_root_path(worktree));
2362 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2363 check_cancelled, NULL);
2374 "usage: %s ref [-r repository] -l | -d name | name target\n",
2379 static const struct got_error *
2380 list_refs(struct got_repository *repo)
2382 static const struct got_error *err = NULL;
2383 struct got_reflist_head refs;
2384 struct got_reflist_entry *re;
2386 SIMPLEQ_INIT(&refs);
2387 err = got_ref_list(&refs, repo);
2391 SIMPLEQ_FOREACH(re, &refs, entry) {
2393 refstr = got_ref_to_str(re->ref);
2395 return got_error_from_errno("got_ref_to_str");
2396 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2400 got_ref_list_free(&refs);
2404 static const struct got_error *
2405 delete_ref(struct got_repository *repo, const char *refname)
2407 const struct got_error *err = NULL;
2408 struct got_reference *ref;
2410 err = got_ref_open(&ref, repo, refname, 0);
2414 err = got_ref_delete(ref, repo);
2419 static const struct got_error *
2420 add_ref(struct got_repository *repo, const char *refname, const char *target)
2422 const struct got_error *err = NULL;
2423 struct got_object_id *id;
2424 struct got_reference *ref = NULL;
2427 * Don't let the user create a reference named '-'.
2428 * While technically a valid reference name, this case is usually
2429 * an unintended typo.
2431 if (refname[0] == '-' && refname[1] == '\0')
2432 return got_error(GOT_ERR_BAD_REF_NAME);
2434 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2437 struct got_reference *target_ref;
2439 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2441 err = got_ref_open(&target_ref, repo, target, 0);
2444 err = got_ref_resolve(&id, repo, target_ref);
2445 got_ref_close(target_ref);
2450 err = got_ref_alloc(&ref, refname, id);
2454 err = got_ref_write(ref, repo);
2462 static const struct got_error *
2463 cmd_ref(int argc, char *argv[])
2465 const struct got_error *error = NULL;
2466 struct got_repository *repo = NULL;
2467 struct got_worktree *worktree = NULL;
2468 char *cwd = NULL, *repo_path = NULL;
2469 int ch, do_list = 0;
2470 const char *delref = NULL;
2472 /* TODO: Add -s option for adding symbolic references. */
2473 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2479 repo_path = realpath(optarg, NULL);
2480 if (repo_path == NULL)
2481 err(1, "-r option");
2482 got_path_strip_trailing_slashes(repo_path);
2493 if (do_list && delref)
2494 errx(1, "-l and -d options are mutually exclusive\n");
2499 if (do_list || delref) {
2502 } else if (argc != 2)
2507 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2512 "sendfd unveil", NULL) == -1)
2516 cwd = getcwd(NULL, 0);
2518 error = got_error_from_errno("getcwd");
2522 if (repo_path == NULL) {
2523 error = got_worktree_open(&worktree, cwd);
2524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2530 strdup(got_worktree_get_repo_path(worktree));
2531 if (repo_path == NULL)
2532 error = got_error_from_errno("strdup");
2536 repo_path = strdup(cwd);
2537 if (repo_path == NULL) {
2538 error = got_error_from_errno("strdup");
2544 error = got_repo_open(&repo, repo_path);
2548 error = apply_unveil(got_repo_get_path(repo), do_list,
2549 worktree ? got_worktree_get_root_path(worktree) : NULL);
2554 error = list_refs(repo);
2556 error = delete_ref(repo, delref);
2558 error = add_ref(repo, argv[0], argv[1]);
2561 got_repo_close(repo);
2563 got_worktree_close(worktree);
2573 "usage: %s branch [-r repository] -l | -d name | "
2574 "name [base-branch]\n", getprogname());
2578 static const struct got_error *
2579 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2581 static const struct got_error *err = NULL;
2582 struct got_reflist_head refs;
2583 struct got_reflist_entry *re;
2585 SIMPLEQ_INIT(&refs);
2587 err = got_ref_list(&refs, repo);
2591 SIMPLEQ_FOREACH(re, &refs, entry) {
2592 const char *refname, *marker = " ";
2594 refname = got_ref_get_name(re->ref);
2595 if (strncmp(refname, "refs/heads/", 11) != 0)
2597 if (worktree && strcmp(refname,
2598 got_worktree_get_head_ref_name(worktree)) == 0) {
2599 struct got_object_id *id = NULL;
2600 err = got_ref_resolve(&id, repo, re->ref);
2603 if (got_object_id_cmp(id,
2604 got_worktree_get_base_commit_id(worktree)) == 0)
2611 refstr = got_ref_to_str(re->ref);
2613 return got_error_from_errno("got_ref_to_str");
2614 printf("%s%s: %s\n", marker, refname, refstr);
2618 got_ref_list_free(&refs);
2622 static const struct got_error *
2623 delete_branch(struct got_repository *repo, const char *branch_name)
2625 const struct got_error *err = NULL;
2626 struct got_reference *ref;
2629 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2630 return got_error_from_errno("asprintf");
2632 err = got_ref_open(&ref, repo, refname, 0);
2636 err = got_ref_delete(ref, repo);
2643 static const struct got_error *
2644 add_branch(struct got_repository *repo, const char *branch_name,
2645 const char *base_branch)
2647 const struct got_error *err = NULL;
2648 struct got_object_id *id = NULL;
2649 struct got_reference *ref = NULL;
2650 char *base_refname = NULL, *refname = NULL;
2651 struct got_reference *base_ref;
2654 * Don't let the user create a branch named '-'.
2655 * While technically a valid reference name, this case is usually
2656 * an unintended typo.
2658 if (branch_name[0] == '-' && branch_name[1] == '\0')
2659 return got_error(GOT_ERR_BAD_REF_NAME);
2661 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2662 base_refname = strdup(GOT_REF_HEAD);
2663 if (base_refname == NULL)
2664 return got_error_from_errno("strdup");
2665 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2666 return got_error_from_errno("asprintf");
2668 err = got_ref_open(&base_ref, repo, base_refname, 0);
2671 err = got_ref_resolve(&id, repo, base_ref);
2672 got_ref_close(base_ref);
2676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2677 err = got_error_from_errno("asprintf");
2681 err = got_ref_open(&ref, repo, refname, 0);
2683 err = got_error(GOT_ERR_BRANCH_EXISTS);
2685 } else if (err->code != GOT_ERR_NOT_REF)
2688 err = got_ref_alloc(&ref, refname, id);
2692 err = got_ref_write(ref, repo);
2702 static const struct got_error *
2703 cmd_branch(int argc, char *argv[])
2705 const struct got_error *error = NULL;
2706 struct got_repository *repo = NULL;
2707 struct got_worktree *worktree = NULL;
2708 char *cwd = NULL, *repo_path = NULL;
2709 int ch, do_list = 0;
2710 const char *delref = NULL;
2712 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2732 if (do_list && delref)
2733 errx(1, "-l and -d options are mutually exclusive\n");
2738 if (do_list || delref) {
2741 } else if (argc < 1 || argc > 2)
2746 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2750 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2751 "sendfd unveil", NULL) == -1)
2755 cwd = getcwd(NULL, 0);
2757 error = got_error_from_errno("getcwd");
2761 if (repo_path == NULL) {
2762 error = got_worktree_open(&worktree, cwd);
2763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2769 strdup(got_worktree_get_repo_path(worktree));
2770 if (repo_path == NULL)
2771 error = got_error_from_errno("strdup");
2775 repo_path = strdup(cwd);
2776 if (repo_path == NULL) {
2777 error = got_error_from_errno("strdup");
2783 error = got_repo_open(&repo, repo_path);
2787 error = apply_unveil(got_repo_get_path(repo), do_list,
2788 worktree ? got_worktree_get_root_path(worktree) : NULL);
2793 error = list_branches(repo, worktree);
2795 error = delete_branch(repo, delref);
2797 const char *base_branch;
2799 base_branch = worktree ?
2800 got_worktree_get_head_ref_name(worktree) :
2803 base_branch = argv[1];
2804 error = add_branch(repo, argv[0], base_branch);
2808 got_repo_close(repo);
2810 got_worktree_close(worktree);
2819 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2823 static const struct got_error *
2824 cmd_add(int argc, char *argv[])
2826 const struct got_error *error = NULL;
2827 struct got_repository *repo = NULL;
2828 struct got_worktree *worktree = NULL;
2830 struct got_pathlist_head paths;
2831 struct got_pathlist_entry *pe;
2836 while ((ch = getopt(argc, argv, "")) != -1) {
2848 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2855 cwd = getcwd(NULL, 0);
2857 error = got_error_from_errno("getcwd");
2861 error = got_worktree_open(&worktree, cwd);
2865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2869 error = apply_unveil(got_repo_get_path(repo), 1,
2870 got_worktree_get_root_path(worktree));
2874 for (x = 0; x < argc; x++) {
2875 char *path = realpath(argv[x], NULL);
2877 error = got_error_from_errno2("realpath", argv[x]);
2881 got_path_strip_trailing_slashes(path);
2882 error = got_pathlist_insert(&pe, &paths, path, NULL);
2888 error = got_worktree_schedule_add(worktree, &paths, print_status,
2892 got_repo_close(repo);
2894 got_worktree_close(worktree);
2895 TAILQ_FOREACH(pe, &paths, entry)
2896 free((char *)pe->path);
2897 got_pathlist_free(&paths);
2905 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2909 static const struct got_error *
2910 cmd_remove(int argc, char *argv[])
2912 const struct got_error *error = NULL;
2913 struct got_worktree *worktree = NULL;
2914 struct got_repository *repo = NULL;
2916 struct got_pathlist_head paths;
2917 struct got_pathlist_entry *pe;
2918 int ch, i, delete_local_mods = 0;
2922 while ((ch = getopt(argc, argv, "f")) != -1) {
2925 delete_local_mods = 1;
2937 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2944 cwd = getcwd(NULL, 0);
2946 error = got_error_from_errno("getcwd");
2949 error = got_worktree_open(&worktree, cwd);
2953 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2957 error = apply_unveil(got_repo_get_path(repo), 1,
2958 got_worktree_get_root_path(worktree));
2962 for (i = 0; i < argc; i++) {
2963 char *path = realpath(argv[i], NULL);
2965 error = got_error_from_errno2("realpath", argv[i]);
2969 got_path_strip_trailing_slashes(path);
2970 error = got_pathlist_insert(&pe, &paths, path, NULL);
2976 error = got_worktree_schedule_delete(worktree, &paths,
2977 delete_local_mods, print_status, NULL, repo);
2982 got_repo_close(repo);
2984 got_worktree_close(worktree);
2985 TAILQ_FOREACH(pe, &paths, entry)
2986 free((char *)pe->path);
2987 got_pathlist_free(&paths);
2995 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2999 static const struct got_error *
3000 revert_progress(void *arg, unsigned char status, const char *path)
3002 while (path[0] == '/')
3004 printf("%c %s\n", status, path);
3008 static const struct got_error *
3009 cmd_revert(int argc, char *argv[])
3011 const struct got_error *error = NULL;
3012 struct got_worktree *worktree = NULL;
3013 struct got_repository *repo = NULL;
3014 char *cwd = NULL, *path = NULL;
3015 struct got_pathlist_head paths;
3016 struct got_pathlist_entry *pe;
3021 while ((ch = getopt(argc, argv, "")) != -1) {
3033 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3034 "unveil", NULL) == -1)
3040 for (i = 0; i < argc; i++) {
3041 char *path = realpath(argv[i], NULL);
3043 error = got_error_from_errno2("realpath", argv[i]);
3047 got_path_strip_trailing_slashes(path);
3048 error = got_pathlist_insert(&pe, &paths, path, NULL);
3055 cwd = getcwd(NULL, 0);
3057 error = got_error_from_errno("getcwd");
3060 error = got_worktree_open(&worktree, cwd);
3064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3068 error = apply_unveil(got_repo_get_path(repo), 1,
3069 got_worktree_get_root_path(worktree));
3073 error = got_worktree_revert(worktree, &paths,
3074 revert_progress, NULL, repo);
3079 got_repo_close(repo);
3081 got_worktree_close(worktree);
3090 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3094 struct collect_commit_logmsg_arg {
3095 const char *cmdline_log;
3097 const char *worktree_path;
3098 const char *branch_name;
3099 const char *repo_path;
3104 static const struct got_error *
3105 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3108 char *initial_content = NULL;
3109 struct got_pathlist_entry *pe;
3110 const struct got_error *err = NULL;
3111 char *template = NULL;
3112 struct collect_commit_logmsg_arg *a = arg;
3116 /* if a message was specified on the command line, just use it */
3117 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3118 len = strlen(a->cmdline_log) + 1;
3119 *logmsg = malloc(len + 1);
3120 if (*logmsg == NULL)
3121 return got_error_from_errno("malloc");
3122 strlcpy(*logmsg, a->cmdline_log, len);
3126 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3127 return got_error_from_errno("asprintf");
3129 if (asprintf(&initial_content,
3130 "\n# changes to be committed on branch %s:\n",
3131 a->branch_name) == -1)
3132 return got_error_from_errno("asprintf");
3134 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3138 dprintf(fd, initial_content);
3140 TAILQ_FOREACH(pe, commitable_paths, entry) {
3141 struct got_commitable *ct = pe->data;
3142 dprintf(fd, "# %c %s\n",
3143 got_commitable_get_status(ct),
3144 got_commitable_get_path(ct));
3148 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3150 unlink(a->logmsg_path);
3151 free(a->logmsg_path);
3152 free(initial_content);
3155 /* Editor is done; we can now apply unveil(2) */
3157 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3166 static const struct got_error *
3167 cmd_commit(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct got_worktree *worktree = NULL;
3171 struct got_repository *repo = NULL;
3172 char *cwd = NULL, *path = NULL, *id_str = NULL;
3173 struct got_object_id *id = NULL;
3174 const char *logmsg = NULL;
3175 const char *got_author = getenv("GOT_AUTHOR");
3176 struct collect_commit_logmsg_arg cl_arg;
3177 char *editor = NULL;
3180 while ((ch = getopt(argc, argv, "m:")) != -1) {
3195 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3196 "unveil", NULL) == -1)
3200 path = realpath(argv[0], NULL);
3202 error = got_error_from_errno2("realpath", argv[0]);
3205 got_path_strip_trailing_slashes(path);
3206 } else if (argc != 0)
3209 if (got_author == NULL) {
3210 /* TODO: Look current user up in password database */
3211 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3215 cwd = getcwd(NULL, 0);
3217 error = got_error_from_errno("getcwd");
3220 error = got_worktree_open(&worktree, cwd);
3224 error = check_rebase_or_histedit_in_progress(worktree);
3228 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3233 * unveil(2) traverses exec(2); if an editor is used we have
3234 * to apply unveil after the log message has been written.
3236 if (logmsg == NULL || strlen(logmsg) == 0)
3237 error = get_editor(&editor);
3239 error = apply_unveil(got_repo_get_path(repo), 0,
3240 got_worktree_get_root_path(worktree));
3244 cl_arg.editor = editor;
3245 cl_arg.cmdline_log = logmsg;
3246 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3247 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3248 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3249 cl_arg.branch_name += 5;
3250 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3251 cl_arg.branch_name += 6;
3252 cl_arg.repo_path = got_repo_get_path(repo);
3253 cl_arg.logmsg_path = NULL;
3254 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3255 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3257 if (cl_arg.logmsg_path)
3258 fprintf(stderr, "%s: log message preserved in %s\n",
3259 getprogname(), cl_arg.logmsg_path);
3263 if (cl_arg.logmsg_path)
3264 unlink(cl_arg.logmsg_path);
3266 error = got_object_id_str(&id_str, id);
3269 printf("Created commit %s\n", id_str);
3272 got_repo_close(repo);
3274 got_worktree_close(worktree);
3283 usage_cherrypick(void)
3285 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3289 static const struct got_error *
3290 cmd_cherrypick(int argc, char *argv[])
3292 const struct got_error *error = NULL;
3293 struct got_worktree *worktree = NULL;
3294 struct got_repository *repo = NULL;
3295 char *cwd = NULL, *commit_id_str = NULL;
3296 struct got_object_id *commit_id = NULL;
3297 struct got_commit_object *commit = NULL;
3298 struct got_object_qid *pid;
3299 struct got_reference *head_ref = NULL;
3300 int ch, did_something = 0;
3302 while ((ch = getopt(argc, argv, "")) != -1) {
3314 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3315 "unveil", NULL) == -1)
3321 cwd = getcwd(NULL, 0);
3323 error = got_error_from_errno("getcwd");
3326 error = got_worktree_open(&worktree, cwd);
3330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3334 error = apply_unveil(got_repo_get_path(repo), 0,
3335 got_worktree_get_root_path(worktree));
3339 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3340 GOT_OBJ_TYPE_COMMIT, repo);
3341 if (error != NULL) {
3342 struct got_reference *ref;
3343 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3345 error = got_ref_open(&ref, repo, argv[0], 0);
3348 error = got_ref_resolve(&commit_id, repo, ref);
3353 error = got_object_id_str(&commit_id_str, commit_id);
3357 error = got_ref_open(&head_ref, repo,
3358 got_worktree_get_head_ref_name(worktree), 0);
3362 error = check_same_branch(commit_id, head_ref, repo);
3364 if (error->code != GOT_ERR_ANCESTRY)
3368 error = got_error(GOT_ERR_SAME_BRANCH);
3372 error = got_object_open_as_commit(&commit, repo, commit_id);
3375 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3376 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3377 commit_id, repo, update_progress, &did_something, check_cancelled,
3383 printf("Merged commit %s\n", commit_id_str);
3386 got_object_commit_close(commit);
3387 free(commit_id_str);
3389 got_ref_close(head_ref);
3391 got_worktree_close(worktree);
3393 got_repo_close(repo);
3400 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3404 static const struct got_error *
3405 cmd_backout(int argc, char *argv[])
3407 const struct got_error *error = NULL;
3408 struct got_worktree *worktree = NULL;
3409 struct got_repository *repo = NULL;
3410 char *cwd = NULL, *commit_id_str = NULL;
3411 struct got_object_id *commit_id = NULL;
3412 struct got_commit_object *commit = NULL;
3413 struct got_object_qid *pid;
3414 struct got_reference *head_ref = NULL;
3415 int ch, did_something = 0;
3417 while ((ch = getopt(argc, argv, "")) != -1) {
3429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3430 "unveil", NULL) == -1)
3436 cwd = getcwd(NULL, 0);
3438 error = got_error_from_errno("getcwd");
3441 error = got_worktree_open(&worktree, cwd);
3445 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3449 error = apply_unveil(got_repo_get_path(repo), 0,
3450 got_worktree_get_root_path(worktree));
3454 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3455 GOT_OBJ_TYPE_COMMIT, repo);
3456 if (error != NULL) {
3457 struct got_reference *ref;
3458 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3460 error = got_ref_open(&ref, repo, argv[0], 0);
3463 error = got_ref_resolve(&commit_id, repo, ref);
3468 error = got_object_id_str(&commit_id_str, commit_id);
3472 error = got_ref_open(&head_ref, repo,
3473 got_worktree_get_head_ref_name(worktree), 0);
3477 error = check_same_branch(commit_id, head_ref, repo);
3481 error = got_object_open_as_commit(&commit, repo, commit_id);
3484 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3486 error = got_error(GOT_ERR_ROOT_COMMIT);
3490 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3491 update_progress, &did_something, check_cancelled, NULL);
3496 printf("Backed out commit %s\n", commit_id_str);
3499 got_object_commit_close(commit);
3500 free(commit_id_str);
3502 got_ref_close(head_ref);
3504 got_worktree_close(worktree);
3506 got_repo_close(repo);
3513 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3519 trim_logmsg(char *logmsg, int limit)
3524 len = strlen(logmsg);
3528 nl = strchr(logmsg, '\n');
3533 static const struct got_error *
3534 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3536 const char *logmsg0 = NULL;
3538 logmsg0 = got_object_commit_get_logmsg(commit);
3540 while (isspace((unsigned char)logmsg0[0]))
3543 *logmsg = strdup(logmsg0);
3544 if (*logmsg == NULL)
3545 return got_error_from_errno("strdup");
3547 trim_logmsg(*logmsg, limit);
3551 static const struct got_error *
3552 show_rebase_progress(struct got_commit_object *commit,
3553 struct got_object_id *old_id, struct got_object_id *new_id)
3555 const struct got_error *err;
3556 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3558 err = got_object_id_str(&old_id_str, old_id);
3563 err = got_object_id_str(&new_id_str, new_id);
3568 old_id_str[12] = '\0';
3570 new_id_str[12] = '\0';
3572 err = get_short_logmsg(&logmsg, 42, commit);
3576 printf("%s -> %s: %s\n", old_id_str,
3577 new_id_str ? new_id_str : "no-op change", logmsg);
3584 static const struct got_error *
3585 rebase_progress(void *arg, unsigned char status, const char *path)
3587 unsigned char *rebase_status = arg;
3589 while (path[0] == '/')
3591 printf("%c %s\n", status, path);
3593 if (*rebase_status == GOT_STATUS_CONFLICT)
3595 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3596 *rebase_status = status;
3600 static const struct got_error *
3601 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3602 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3603 struct got_repository *repo)
3605 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3606 return got_worktree_rebase_complete(worktree,
3607 new_base_branch, tmp_branch, branch, repo);
3610 static const struct got_error *
3611 rebase_commit(struct got_pathlist_head *merged_paths,
3612 struct got_worktree *worktree, struct got_reference *tmp_branch,
3613 struct got_object_id *commit_id, struct got_repository *repo)
3615 const struct got_error *error;
3616 struct got_commit_object *commit;
3617 struct got_object_id *new_commit_id;
3619 error = got_object_open_as_commit(&commit, repo, commit_id);
3623 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3624 worktree, tmp_branch, commit, commit_id, repo);
3626 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3628 error = show_rebase_progress(commit, commit_id, NULL);
3630 error = show_rebase_progress(commit, commit_id, new_commit_id);
3631 free(new_commit_id);
3634 got_object_commit_close(commit);
3638 struct check_path_prefix_arg {
3639 const char *path_prefix;
3644 static const struct got_error *
3645 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3646 struct got_blob_object *blob2, struct got_object_id *id1,
3647 struct got_object_id *id2, const char *path1, const char *path2,
3648 struct got_repository *repo)
3650 struct check_path_prefix_arg *a = arg;
3652 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3653 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3654 return got_error(a->errcode);
3659 static const struct got_error *
3660 check_path_prefix(struct got_object_id *parent_id,
3661 struct got_object_id *commit_id, const char *path_prefix,
3662 int errcode, struct got_repository *repo)
3664 const struct got_error *err;
3665 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3666 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3667 struct check_path_prefix_arg cpp_arg;
3669 if (got_path_is_root_dir(path_prefix))
3672 err = got_object_open_as_commit(&commit, repo, commit_id);
3676 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3680 err = got_object_open_as_tree(&tree1, repo,
3681 got_object_commit_get_tree_id(parent_commit));
3685 err = got_object_open_as_tree(&tree2, repo,
3686 got_object_commit_get_tree_id(commit));
3690 cpp_arg.path_prefix = path_prefix;
3691 while (cpp_arg.path_prefix[0] == '/')
3692 cpp_arg.path_prefix++;
3693 cpp_arg.len = strlen(cpp_arg.path_prefix);
3694 cpp_arg.errcode = errcode;
3695 err = got_diff_tree(tree1, tree2, "", "", repo,
3696 check_path_prefix_in_diff, &cpp_arg);
3699 got_object_tree_close(tree1);
3701 got_object_tree_close(tree2);
3703 got_object_commit_close(commit);
3705 got_object_commit_close(parent_commit);
3709 static const struct got_error *
3710 collect_commits(struct got_object_id_queue *commits,
3711 struct got_object_id *initial_commit_id,
3712 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3713 const char *path_prefix, int path_prefix_errcode,
3714 struct got_repository *repo)
3716 const struct got_error *err = NULL;
3717 struct got_commit_graph *graph = NULL;
3718 struct got_object_id *parent_id = NULL;
3719 struct got_object_qid *qid;
3720 struct got_object_id *commit_id = initial_commit_id;
3722 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3726 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3729 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3730 err = got_commit_graph_iter_next(&parent_id, graph);
3732 if (err->code == GOT_ERR_ITER_COMPLETED) {
3733 err = got_error_msg(GOT_ERR_ANCESTRY,
3734 "ran out of commits to rebase before "
3735 "youngest common ancestor commit has "
3738 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3740 err = got_commit_graph_fetch_commits(graph, 1, repo);
3744 err = check_path_prefix(parent_id, commit_id,
3745 path_prefix, path_prefix_errcode, repo);
3749 err = got_object_qid_alloc(&qid, commit_id);
3752 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3753 commit_id = parent_id;
3757 got_commit_graph_close(graph);
3761 static const struct got_error *
3762 cmd_rebase(int argc, char *argv[])
3764 const struct got_error *error = NULL;
3765 struct got_worktree *worktree = NULL;
3766 struct got_repository *repo = NULL;
3768 struct got_reference *branch = NULL;
3769 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3770 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3771 struct got_object_id *resume_commit_id = NULL;
3772 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3773 struct got_commit_object *commit = NULL;
3774 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3775 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3776 struct got_object_id_queue commits;
3777 struct got_pathlist_head merged_paths;
3778 const struct got_object_id_queue *parent_ids;
3779 struct got_object_qid *qid, *pid;
3781 SIMPLEQ_INIT(&commits);
3782 TAILQ_INIT(&merged_paths);
3784 while ((ch = getopt(argc, argv, "ac")) != -1) {
3790 continue_rebase = 1;
3802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3803 "unveil", NULL) == -1)
3806 if (abort_rebase && continue_rebase)
3808 else if (abort_rebase || continue_rebase) {
3811 } else if (argc != 1)
3814 cwd = getcwd(NULL, 0);
3816 error = got_error_from_errno("getcwd");
3819 error = got_worktree_open(&worktree, cwd);
3823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3827 error = apply_unveil(got_repo_get_path(repo), 0,
3828 got_worktree_get_root_path(worktree));
3832 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3838 if (!rebase_in_progress) {
3839 error = got_error(GOT_ERR_NOT_REBASING);
3842 error = got_worktree_rebase_continue(&resume_commit_id,
3843 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3846 printf("Switching work tree to %s\n",
3847 got_ref_get_symref_target(new_base_branch));
3848 error = got_worktree_rebase_abort(worktree, repo,
3849 new_base_branch, update_progress, &did_something);
3852 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3853 goto done; /* nothing else to do */
3856 if (continue_rebase) {
3857 if (!rebase_in_progress) {
3858 error = got_error(GOT_ERR_NOT_REBASING);
3861 error = got_worktree_rebase_continue(&resume_commit_id,
3862 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3866 error = rebase_commit(NULL, worktree, tmp_branch,
3867 resume_commit_id, repo);
3871 yca_id = got_object_id_dup(resume_commit_id);
3872 if (yca_id == NULL) {
3873 error = got_error_from_errno("got_object_id_dup");
3877 error = got_ref_open(&branch, repo, argv[0], 0);
3881 error = check_same_branch(
3882 got_worktree_get_base_commit_id(worktree), branch, repo);
3884 if (error->code != GOT_ERR_ANCESTRY)
3888 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3889 "specified branch resolves to a commit which "
3890 "is already contained in work tree's branch");
3895 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3899 if (!continue_rebase) {
3900 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3901 got_worktree_get_base_commit_id(worktree),
3902 branch_head_commit_id, repo);
3905 if (yca_id == NULL) {
3906 error = got_error_msg(GOT_ERR_ANCESTRY,
3907 "specified branch shares no common ancestry "
3908 "with work tree's branch");
3912 error = got_worktree_rebase_prepare(&new_base_branch,
3913 &tmp_branch, worktree, branch, repo);
3918 commit_id = branch_head_commit_id;
3919 error = got_object_open_as_commit(&commit, repo, commit_id);
3923 parent_ids = got_object_commit_get_parent_ids(commit);
3924 pid = SIMPLEQ_FIRST(parent_ids);
3925 error = collect_commits(&commits, commit_id, pid->id,
3926 yca_id, got_worktree_get_path_prefix(worktree),
3927 GOT_ERR_REBASE_PATH, repo);
3928 got_object_commit_close(commit);
3933 if (SIMPLEQ_EMPTY(&commits)) {
3934 if (continue_rebase)
3935 error = rebase_complete(worktree, branch,
3936 new_base_branch, tmp_branch, repo);
3938 error = got_error(GOT_ERR_EMPTY_REBASE);
3943 SIMPLEQ_FOREACH(qid, &commits, entry) {
3944 commit_id = qid->id;
3945 parent_id = pid ? pid->id : yca_id;
3948 error = got_worktree_rebase_merge_files(&merged_paths,
3949 worktree, parent_id, commit_id, repo, rebase_progress,
3950 &rebase_status, check_cancelled, NULL);
3954 if (rebase_status == GOT_STATUS_CONFLICT) {
3955 got_worktree_rebase_pathlist_free(&merged_paths);
3959 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3961 got_worktree_rebase_pathlist_free(&merged_paths);
3966 if (rebase_status == GOT_STATUS_CONFLICT) {
3967 error = got_worktree_rebase_postpone(worktree);
3970 error = got_error_msg(GOT_ERR_CONFLICTS,
3971 "conflicts must be resolved before rebasing can continue");
3973 error = rebase_complete(worktree, branch, new_base_branch,
3976 got_object_id_queue_free(&commits);
3977 free(branch_head_commit_id);
3978 free(resume_commit_id);
3981 got_object_commit_close(commit);
3983 got_ref_close(branch);
3984 if (new_base_branch)
3985 got_ref_close(new_base_branch);
3987 got_ref_close(tmp_branch);
3989 got_worktree_close(worktree);
3991 got_repo_close(repo);
3996 usage_histedit(void)
3998 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4003 #define GOT_HISTEDIT_PICK 'p'
4004 #define GOT_HISTEDIT_EDIT 'e'
4005 #define GOT_HISTEDIT_FOLD 'f'
4006 #define GOT_HISTEDIT_DROP 'd'
4007 #define GOT_HISTEDIT_MESG 'm'
4009 static struct got_histedit_cmd {
4013 } got_histedit_cmds[] = {
4014 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4015 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4016 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4017 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4018 { GOT_HISTEDIT_MESG, "mesg",
4019 "single-line log message for commit above (open editor if empty)" },
4022 struct got_histedit_list_entry {
4023 TAILQ_ENTRY(got_histedit_list_entry) entry;
4024 struct got_object_id *commit_id;
4025 const struct got_histedit_cmd *cmd;
4028 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4030 static const struct got_error *
4031 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4032 FILE *f, struct got_repository *repo)
4034 const struct got_error *err = NULL;
4035 char *logmsg = NULL, *id_str = NULL;
4036 struct got_commit_object *commit = NULL;
4039 err = got_object_open_as_commit(&commit, repo, commit_id);
4043 err = get_short_logmsg(&logmsg, 34, commit);
4047 err = got_object_id_str(&id_str, commit_id);
4051 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4053 err = got_ferror(f, GOT_ERR_IO);
4056 got_object_commit_close(commit);
4062 static const struct got_error *
4063 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4064 struct got_repository *repo)
4066 const struct got_error *err = NULL;
4067 struct got_object_qid *qid;
4069 if (SIMPLEQ_EMPTY(commits))
4070 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4072 SIMPLEQ_FOREACH(qid, commits, entry) {
4073 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4082 static const struct got_error *
4083 write_cmd_list(FILE *f)
4085 const struct got_error *err = NULL;
4088 n = fprintf(f, "# Available histedit commands:\n");
4090 return got_ferror(f, GOT_ERR_IO);
4092 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4093 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4094 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4097 err = got_ferror(f, GOT_ERR_IO);
4101 n = fprintf(f, "# Commits will be processed in order from top to "
4102 "bottom of this file.\n");
4104 return got_ferror(f, GOT_ERR_IO);
4108 static const struct got_error *
4109 histedit_syntax_error(int lineno)
4111 static char msg[42];
4114 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4116 if (ret == -1 || ret >= sizeof(msg))
4117 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4119 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4122 static const struct got_error *
4123 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4124 char *logmsg, struct got_repository *repo)
4126 const struct got_error *err;
4127 struct got_commit_object *folded_commit = NULL;
4130 err = got_object_id_str(&id_str, hle->commit_id);
4134 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4138 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4139 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4140 got_object_commit_get_logmsg(folded_commit)) == -1) {
4141 err = got_error_from_errno("asprintf");
4146 got_object_commit_close(folded_commit);
4151 static struct got_histedit_list_entry *
4152 get_folded_commits(struct got_histedit_list_entry *hle)
4154 struct got_histedit_list_entry *prev, *folded = NULL;
4156 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4157 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4158 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4159 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4161 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4167 static const struct got_error *
4168 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4169 struct got_repository *repo)
4171 char *logmsg_path = NULL, *id_str = NULL;
4172 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4173 const struct got_error *err = NULL;
4174 struct got_commit_object *commit = NULL;
4176 struct got_histedit_list_entry *folded = NULL;
4178 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4182 folded = get_folded_commits(hle);
4184 while (folded != hle) {
4185 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4186 folded = TAILQ_NEXT(folded, entry);
4189 err = append_folded_commit_msg(&new_msg, folded,
4195 folded = TAILQ_NEXT(folded, entry);
4199 err = got_object_id_str(&id_str, hle->commit_id);
4202 if (asprintf(&new_msg,
4203 "%s\n# original log message of commit %s: %s",
4204 logmsg ? logmsg : "", id_str,
4205 got_object_commit_get_logmsg(commit)) == -1) {
4206 err = got_error_from_errno("asprintf");
4212 err = got_object_id_str(&id_str, hle->commit_id);
4216 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4220 dprintf(fd, logmsg);
4223 err = get_editor(&editor);
4227 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4229 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4232 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4233 if (hle->logmsg == NULL)
4234 err = got_error_from_errno("strdup");
4237 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4238 err = got_error_from_errno2("unlink", logmsg_path);
4243 got_object_commit_close(commit);
4247 static const struct got_error *
4248 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4249 FILE *f, struct got_repository *repo)
4251 const struct got_error *err = NULL;
4252 char *line = NULL, *p, *end;
4256 const struct got_histedit_cmd *cmd;
4257 struct got_object_id *commit_id = NULL;
4258 struct got_histedit_list_entry *hle = NULL;
4261 len = getline(&line, &size, f);
4263 const struct got_error *getline_err;
4266 getline_err = got_error_from_errno("getline");
4267 err = got_ferror(f, getline_err->code);
4272 while (isspace((unsigned char)p[0]))
4274 if (p[0] == '#' || p[0] == '\0') {
4280 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4281 cmd = &got_histedit_cmds[i];
4282 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4283 isspace((unsigned char)p[strlen(cmd->name)])) {
4284 p += strlen(cmd->name);
4287 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4293 err = histedit_syntax_error(lineno);
4296 while (isspace((unsigned char)p[0]))
4298 if (cmd->code == GOT_HISTEDIT_MESG) {
4299 if (hle == NULL || hle->logmsg != NULL) {
4300 err = got_error(GOT_ERR_HISTEDIT_CMD);
4304 err = histedit_edit_logmsg(hle, repo);
4308 hle->logmsg = strdup(p);
4309 if (hle->logmsg == NULL) {
4310 err = got_error_from_errno("strdup");
4319 while (end[0] && !isspace((unsigned char)end[0]))
4323 err = got_object_resolve_id_str(&commit_id, repo, p);
4325 /* override error code */
4326 err = histedit_syntax_error(lineno);
4330 hle = malloc(sizeof(*hle));
4332 err = got_error_from_errno("malloc");
4336 hle->commit_id = commit_id;
4341 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4349 static const struct got_error *
4350 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4351 const char *path, struct got_repository *repo)
4353 const struct got_error *err = NULL;
4357 err = get_editor(&editor);
4361 if (spawn_editor(editor, path) == -1) {
4362 err = got_error_from_errno("failed spawning editor");
4366 f = fopen(path, "r");
4368 err = got_error_from_errno("fopen");
4371 err = histedit_parse_list(histedit_cmds, f, repo);
4373 if (f && fclose(f) != 0 && err == NULL)
4374 err = got_error_from_errno("fclose");
4379 static const struct got_error *
4380 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4381 struct got_object_id_queue *, const char *, struct got_repository *);
4383 static const struct got_error *
4384 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4385 struct got_object_id_queue *commits, struct got_repository *repo)
4387 const struct got_error *err;
4391 err = got_opentemp_named(&path, &f, "got-histedit");
4395 err = write_cmd_list(f);
4399 err = histedit_write_commit_list(commits, f, repo);
4403 if (fclose(f) != 0) {
4404 err = got_error_from_errno("fclose");
4409 err = histedit_run_editor(histedit_cmds, path, repo);
4411 const char *errmsg = err->msg;
4412 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4414 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4415 commits, path, repo);
4418 if (f && fclose(f) != 0 && err == NULL)
4419 err = got_error_from_errno("fclose");
4420 if (path && unlink(path) != 0 && err == NULL)
4421 err = got_error_from_errno2("unlink", path);
4426 static const struct got_error *
4427 histedit_save_list(struct got_histedit_list *histedit_cmds,
4428 struct got_worktree *worktree, struct got_repository *repo)
4430 const struct got_error *err = NULL;
4433 struct got_histedit_list_entry *hle;
4434 struct got_commit_object *commit = NULL;
4436 err = got_worktree_get_histedit_list_path(&path, worktree);
4440 f = fopen(path, "w");
4442 err = got_error_from_errno2("fopen", path);
4445 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4446 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4452 int n = fprintf(f, "%c %s\n",
4453 GOT_HISTEDIT_MESG, hle->logmsg);
4455 err = got_ferror(f, GOT_ERR_IO);
4461 if (f && fclose(f) != 0 && err == NULL)
4462 err = got_error_from_errno("fclose");
4465 got_object_commit_close(commit);
4469 static const struct got_error *
4470 histedit_load_list(struct got_histedit_list *histedit_cmds,
4471 const char *path, struct got_repository *repo)
4473 const struct got_error *err = NULL;
4476 f = fopen(path, "r");
4478 err = got_error_from_errno2("fopen", path);
4482 err = histedit_parse_list(histedit_cmds, f, repo);
4484 if (f && fclose(f) != 0 && err == NULL)
4485 err = got_error_from_errno("fclose");
4489 static const struct got_error *
4490 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4491 const char *errmsg, struct got_object_id_queue *commits,
4492 const char *path, struct got_repository *repo)
4494 const struct got_error *err = NULL;
4497 while (resp != 'c' && resp != 'r' && resp != 'q') {
4498 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4499 "or (a)bort: ", getprogname(), errmsg);
4503 err = histedit_run_editor(histedit_cmds, path, repo);
4506 err = histedit_edit_script(histedit_cmds,
4510 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4513 printf("invalid response '%c'\n", resp);
4521 static const struct got_error *
4522 histedit_complete(struct got_worktree *worktree,
4523 struct got_reference *tmp_branch, struct got_reference *branch,
4524 struct got_repository *repo)
4526 printf("Switching work tree to %s\n",
4527 got_ref_get_symref_target(branch));
4528 return got_worktree_histedit_complete(worktree, tmp_branch, branch,
4532 static const struct got_error *
4533 show_histedit_progress(struct got_commit_object *commit,
4534 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4536 const struct got_error *err;
4537 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4539 err = got_object_id_str(&old_id_str, hle->commit_id);
4544 err = got_object_id_str(&new_id_str, new_id);
4549 old_id_str[12] = '\0';
4551 new_id_str[12] = '\0';
4554 logmsg = strdup(hle->logmsg);
4555 if (logmsg == NULL) {
4556 err = got_error_from_errno("strdup");
4559 trim_logmsg(logmsg, 42);
4561 err = get_short_logmsg(&logmsg, 42, commit);
4566 switch (hle->cmd->code) {
4567 case GOT_HISTEDIT_PICK:
4568 case GOT_HISTEDIT_EDIT:
4569 printf("%s -> %s: %s\n", old_id_str,
4570 new_id_str ? new_id_str : "no-op change", logmsg);
4572 case GOT_HISTEDIT_DROP:
4573 case GOT_HISTEDIT_FOLD:
4574 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4587 static const struct got_error *
4588 histedit_commit(struct got_pathlist_head *merged_paths,
4589 struct got_worktree *worktree, struct got_reference *tmp_branch,
4590 struct got_histedit_list_entry *hle, struct got_repository *repo)
4592 const struct got_error *err;
4593 struct got_commit_object *commit;
4594 struct got_object_id *new_commit_id;
4596 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4597 && hle->logmsg == NULL) {
4598 err = histedit_edit_logmsg(hle, repo);
4603 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4607 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4608 worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
4610 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4612 err = show_histedit_progress(commit, hle, NULL);
4614 err = show_histedit_progress(commit, hle, new_commit_id);
4615 free(new_commit_id);
4618 got_object_commit_close(commit);
4622 static const struct got_error *
4623 histedit_skip_commit(struct got_histedit_list_entry *hle,
4624 struct got_worktree *worktree, struct got_repository *repo)
4626 const struct got_error *error;
4627 struct got_commit_object *commit;
4629 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4634 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4638 error = show_histedit_progress(commit, hle, NULL);
4639 got_object_commit_close(commit);
4643 static const struct got_error *
4644 histedit_check_script(struct got_histedit_list *histedit_cmds,
4645 struct got_object_id_queue *commits, struct got_repository *repo)
4647 const struct got_error *err = NULL;
4648 struct got_object_qid *qid;
4649 struct got_histedit_list_entry *hle;
4650 static char msg[80];
4653 if (TAILQ_EMPTY(histedit_cmds))
4654 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4656 SIMPLEQ_FOREACH(qid, commits, entry) {
4657 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4658 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4662 err = got_object_id_str(&id_str, qid->id);
4665 snprintf(msg, sizeof(msg),
4666 "commit %s missing from histedit script", id_str);
4668 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4672 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4673 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4674 "last commit in histedit script cannot be folded");
4679 static const struct got_error *
4680 cmd_histedit(int argc, char *argv[])
4682 const struct got_error *error = NULL;
4683 struct got_worktree *worktree = NULL;
4684 struct got_repository *repo = NULL;
4686 struct got_reference *branch = NULL;
4687 struct got_reference *tmp_branch = NULL;
4688 struct got_object_id *resume_commit_id = NULL;
4689 struct got_object_id *base_commit_id = NULL;
4690 struct got_object_id *head_commit_id = NULL;
4691 struct got_commit_object *commit = NULL;
4692 int ch, rebase_in_progress = 0;
4693 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4694 const char *edit_script_path = NULL;
4695 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4696 struct got_object_id_queue commits;
4697 struct got_pathlist_head merged_paths;
4698 const struct got_object_id_queue *parent_ids;
4699 struct got_object_qid *pid;
4700 struct got_histedit_list histedit_cmds;
4701 struct got_histedit_list_entry *hle;
4703 SIMPLEQ_INIT(&commits);
4704 TAILQ_INIT(&histedit_cmds);
4705 TAILQ_INIT(&merged_paths);
4707 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4716 edit_script_path = optarg;
4728 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4729 "unveil", NULL) == -1)
4732 if (abort_edit && continue_edit)
4738 * This command cannot apply unveil(2) in all cases because the
4739 * user may choose to run an editor to edit the histedit script
4740 * and to edit individual commit log messages.
4741 * unveil(2) traverses exec(2); if an editor is used we have to
4742 * apply unveil after edit script and log messages have been written.
4743 * XXX TODO: Make use of unveil(2) where possible.
4746 cwd = getcwd(NULL, 0);
4748 error = got_error_from_errno("getcwd");
4751 error = got_worktree_open(&worktree, cwd);
4755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4759 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4762 if (rebase_in_progress) {
4763 error = got_error(GOT_ERR_REBASING);
4767 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4771 if (edit_in_progress && abort_edit) {
4773 error = got_worktree_histedit_continue(&resume_commit_id,
4774 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4777 printf("Switching work tree to %s\n",
4778 got_ref_get_symref_target(branch));
4779 error = got_worktree_histedit_abort(worktree, repo,
4780 branch, base_commit_id, update_progress, &did_something);
4783 printf("Histedit of %s aborted\n",
4784 got_ref_get_symref_target(branch));
4785 goto done; /* nothing else to do */
4786 } else if (abort_edit) {
4787 error = got_error(GOT_ERR_NOT_HISTEDIT);
4791 if (continue_edit) {
4794 if (!edit_in_progress) {
4795 error = got_error(GOT_ERR_NOT_HISTEDIT);
4799 error = got_worktree_get_histedit_list_path(&path, worktree);
4803 error = histedit_load_list(&histedit_cmds, path, repo);
4808 error = got_worktree_histedit_continue(&resume_commit_id,
4809 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4813 error = got_ref_resolve(&head_commit_id, repo, branch);
4817 error = got_object_open_as_commit(&commit, repo,
4821 parent_ids = got_object_commit_get_parent_ids(commit);
4822 pid = SIMPLEQ_FIRST(parent_ids);
4824 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4827 error = collect_commits(&commits, head_commit_id, pid->id,
4828 base_commit_id, got_worktree_get_path_prefix(worktree),
4829 GOT_ERR_HISTEDIT_PATH, repo);
4830 got_object_commit_close(commit);
4835 if (edit_in_progress) {
4836 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4840 error = got_ref_open(&branch, repo,
4841 got_worktree_get_head_ref_name(worktree), 0);
4845 error = got_ref_resolve(&head_commit_id, repo, branch);
4849 error = got_object_open_as_commit(&commit, repo,
4853 parent_ids = got_object_commit_get_parent_ids(commit);
4854 pid = SIMPLEQ_FIRST(parent_ids);
4856 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4859 error = collect_commits(&commits, head_commit_id, pid->id,
4860 got_worktree_get_base_commit_id(worktree),
4861 got_worktree_get_path_prefix(worktree),
4862 GOT_ERR_HISTEDIT_PATH, repo);
4863 got_object_commit_close(commit);
4868 if (edit_script_path) {
4869 error = histedit_load_list(&histedit_cmds,
4870 edit_script_path, repo);
4874 error = histedit_edit_script(&histedit_cmds, &commits,
4881 error = histedit_save_list(&histedit_cmds, worktree,
4886 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4887 &base_commit_id, worktree, repo);
4893 error = histedit_check_script(&histedit_cmds, &commits, repo);
4897 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4898 if (resume_commit_id) {
4899 if (got_object_id_cmp(hle->commit_id,
4900 resume_commit_id) != 0)
4903 resume_commit_id = NULL;
4904 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4905 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4906 error = histedit_skip_commit(hle, worktree,
4909 error = histedit_commit(NULL, worktree,
4910 tmp_branch, hle, repo);
4917 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4918 error = histedit_skip_commit(hle, worktree, repo);
4924 error = got_object_open_as_commit(&commit, repo,
4928 parent_ids = got_object_commit_get_parent_ids(commit);
4929 pid = SIMPLEQ_FIRST(parent_ids);
4931 error = got_worktree_histedit_merge_files(&merged_paths,
4932 worktree, pid->id, hle->commit_id, repo, rebase_progress,
4933 &rebase_status, check_cancelled, NULL);
4936 got_object_commit_close(commit);
4939 if (rebase_status == GOT_STATUS_CONFLICT) {
4940 got_worktree_rebase_pathlist_free(&merged_paths);
4944 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4946 error = got_object_id_str(&id_str, hle->commit_id);
4949 printf("Stopping histedit for amending commit %s\n",
4952 got_worktree_rebase_pathlist_free(&merged_paths);
4953 error = got_worktree_histedit_postpone(worktree);
4957 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4958 error = histedit_skip_commit(hle, worktree, repo);
4964 error = histedit_commit(&merged_paths, worktree, tmp_branch,
4966 got_worktree_rebase_pathlist_free(&merged_paths);
4971 if (rebase_status == GOT_STATUS_CONFLICT) {
4972 error = got_worktree_histedit_postpone(worktree);
4975 error = got_error_msg(GOT_ERR_CONFLICTS,
4976 "conflicts must be resolved before rebasing can continue");
4978 error = histedit_complete(worktree, tmp_branch, branch, repo);
4980 got_object_id_queue_free(&commits);
4981 free(head_commit_id);
4982 free(base_commit_id);
4983 free(resume_commit_id);
4985 got_object_commit_close(commit);
4987 got_ref_close(branch);
4989 got_ref_close(tmp_branch);
4991 got_worktree_close(worktree);
4993 got_repo_close(repo);