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 repository-path\n", getprogname());
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
287 while ((ch = getopt(argc, argv, "")) != -1) {
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
316 error = apply_unveil(repo_path, 0, NULL);
320 error = got_repo_init(repo_path);
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
338 spawn_editor(const char *editor, const char *file)
341 sig_t sighup, sigint, sigquit;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
352 execl(editor, editor, file, (char *)NULL);
356 while (waitpid(pid, &st, 0) == -1)
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
381 int content_changed = 0;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
401 return got_error_from_errno("malloc");
405 fp = fopen(logmsg_path, "r");
407 err = got_error_from_errno("fopen");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
452 dprintf(fd, initial_content);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
457 free(initial_content);
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
490 branch_name = optarg;
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
507 if (optarg[0] == '\0')
509 error = got_pathlist_insert(&pe, &ignores, optarg,
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
552 error = got_ref_open(&branch_ref, repo, refname, 0);
554 if (error->code != GOT_ERR_NOT_REF)
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
567 got_path_strip_trailing_slashes(path_dir);
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
598 error = got_ref_write(branch_ref, repo);
602 error = got_object_id_str(&id_str, new_commit_id);
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
608 if (error->code != GOT_ERR_NOT_REF)
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
616 error = got_ref_write(head_ref, repo);
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
630 got_ref_close(branch_ref);
632 got_ref_close(head_ref);
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
653 while (path[0] == '/')
656 printf("%c %s/%s\n", status, worktree_path, path);
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
681 return got_error(GOT_ERR_ANCESTRY);
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
739 err = got_commit_graph_fetch_commits(graph, 1,
746 if (got_object_id_cmp(id, commit_id) == 0) {
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
761 static const struct got_error *
762 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
770 err = got_ref_resolve(commit_id, repo, ref);
773 if (err->code != GOT_ERR_NOT_REF)
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
795 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
798 branch_name = optarg;
801 commit_id_str = strdup(optarg);
802 if (commit_id_str == NULL)
803 return got_error_from_errno("strdup");
806 path_prefix = optarg;
818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
819 "unveil", NULL) == -1)
823 char *cwd, *base, *dotgit;
824 repo_path = realpath(argv[0], NULL);
825 if (repo_path == NULL)
826 return got_error_from_errno2("realpath", argv[0]);
827 cwd = getcwd(NULL, 0);
829 error = got_error_from_errno("getcwd");
832 if (path_prefix[0]) {
833 base = basename(path_prefix);
835 error = got_error_from_errno2("basename",
840 base = basename(repo_path);
842 error = got_error_from_errno2("basename",
847 dotgit = strstr(base, ".git");
850 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
851 error = got_error_from_errno("asprintf");
856 } else if (argc == 2) {
857 repo_path = realpath(argv[0], NULL);
858 if (repo_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[0]);
862 worktree_path = realpath(argv[1], NULL);
863 if (worktree_path == NULL) {
864 if (errno != ENOENT) {
865 error = got_error_from_errno2("realpath",
869 worktree_path = strdup(argv[1]);
870 if (worktree_path == NULL) {
871 error = got_error_from_errno("strdup");
878 got_path_strip_trailing_slashes(repo_path);
879 got_path_strip_trailing_slashes(worktree_path);
881 error = got_repo_open(&repo, repo_path);
885 /* Pre-create work tree path for unveil(2) */
886 error = got_path_mkdir(worktree_path);
888 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
890 if (!got_path_dir_is_empty(worktree_path)) {
891 error = got_error_path(worktree_path,
892 GOT_ERR_DIR_NOT_EMPTY);
897 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
901 error = got_ref_open(&head_ref, repo, branch_name, 0);
905 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
906 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
909 error = got_worktree_open(&worktree, worktree_path);
913 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
917 if (!same_path_prefix) {
918 error = got_error(GOT_ERR_PATH_PREFIX);
923 struct got_object_id *commit_id;
924 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
927 error = check_linear_ancestry(commit_id,
928 got_worktree_get_base_commit_id(worktree), repo);
933 error = check_same_branch(commit_id, head_ref, repo);
936 error = got_worktree_set_base_commit_id(worktree, repo,
943 error = got_worktree_checkout_files(worktree, "", repo,
944 checkout_progress, worktree_path, check_cancelled, NULL);
948 printf("Now shut up and hack\n");
960 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
965 static const struct got_error *
966 update_progress(void *arg, unsigned char status, const char *path)
968 int *did_something = arg;
970 if (status == GOT_STATUS_EXISTS)
975 /* Base commit bump happens silently. */
976 if (status == GOT_STATUS_BUMP_BASE)
979 while (path[0] == '/')
981 printf("%c %s\n", status, path);
985 static const struct got_error *
986 switch_head_ref(struct got_reference *head_ref,
987 struct got_object_id *commit_id, struct got_worktree *worktree,
988 struct got_repository *repo)
990 const struct got_error *err = NULL;
992 int ref_has_moved = 0;
994 /* Trivial case: switching between two different references. */
995 if (strcmp(got_ref_get_name(head_ref),
996 got_worktree_get_head_ref_name(worktree)) != 0) {
997 printf("Switching work tree from %s to %s\n",
998 got_worktree_get_head_ref_name(worktree),
999 got_ref_get_name(head_ref));
1000 return got_worktree_set_head_ref(worktree, head_ref);
1003 err = check_linear_ancestry(commit_id,
1004 got_worktree_get_base_commit_id(worktree), repo);
1006 if (err->code != GOT_ERR_ANCESTRY)
1013 /* Switching to a rebased branch with the same reference name. */
1014 err = got_object_id_str(&base_id_str,
1015 got_worktree_get_base_commit_id(worktree));
1018 printf("Reference %s now points at a different branch\n",
1019 got_worktree_get_head_ref_name(worktree));
1020 printf("Switching work tree from %s to %s\n", base_id_str,
1021 got_worktree_get_head_ref_name(worktree));
1025 static const struct got_error *
1026 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1028 const struct got_error *err;
1031 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1035 return got_error(GOT_ERR_REBASING);
1037 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1041 return got_error(GOT_ERR_HISTEDIT_BUSY);
1046 static const struct got_error *
1047 cmd_update(int argc, char *argv[])
1049 const struct got_error *error = NULL;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 char *worktree_path = NULL, *path = NULL;
1053 struct got_object_id *commit_id = NULL;
1054 char *commit_id_str = NULL;
1055 const char *branch_name = NULL;
1056 struct got_reference *head_ref = NULL;
1057 int ch, did_something = 0;
1059 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1062 branch_name = optarg;
1065 commit_id_str = strdup(optarg);
1066 if (commit_id_str == NULL)
1067 return got_error_from_errno("strdup");
1079 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1080 "unveil", NULL) == -1)
1083 worktree_path = getcwd(NULL, 0);
1084 if (worktree_path == NULL) {
1085 error = got_error_from_errno("getcwd");
1088 error = got_worktree_open(&worktree, worktree_path);
1092 error = check_rebase_or_histedit_in_progress(worktree);
1099 error = got_error_from_errno("strdup");
1102 } else if (argc == 1) {
1103 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1113 error = apply_unveil(got_repo_get_path(repo), 0,
1114 got_worktree_get_root_path(worktree));
1118 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1119 got_worktree_get_head_ref_name(worktree), 0);
1122 if (commit_id_str == NULL) {
1123 error = got_ref_resolve(&commit_id, repo, head_ref);
1126 error = got_object_id_str(&commit_id_str, commit_id);
1130 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1131 free(commit_id_str);
1132 commit_id_str = NULL;
1135 error = got_object_id_str(&commit_id_str, commit_id);
1141 struct got_object_id *head_commit_id;
1142 if (strlen(path) != 0) {
1143 fprintf(stderr, "%s: switching between branches "
1144 "requires that the entire work tree "
1145 "gets updated, not just '%s'\n",
1146 getprogname(), path);
1147 error = got_error(GOT_ERR_BAD_PATH);
1150 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1153 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1154 free(head_commit_id);
1157 error = check_same_branch(commit_id, head_ref, repo);
1160 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1164 error = check_linear_ancestry(commit_id,
1165 got_worktree_get_base_commit_id(worktree), repo);
1166 if (error != NULL) {
1167 if (error->code == GOT_ERR_ANCESTRY)
1168 error = got_error(GOT_ERR_BRANCH_MOVED);
1171 error = check_same_branch(commit_id, head_ref, repo);
1176 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1178 error = got_worktree_set_base_commit_id(worktree, repo,
1184 error = got_worktree_checkout_files(worktree, path, repo,
1185 update_progress, &did_something, check_cancelled, NULL);
1190 printf("Updated to commit %s\n", commit_id_str);
1192 printf("Already up-to-date\n");
1194 free(worktree_path);
1197 free(commit_id_str);
1201 static const struct got_error *
1202 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1203 int diff_context, struct got_repository *repo)
1205 const struct got_error *err = NULL;
1206 struct got_tree_object *tree1 = NULL, *tree2;
1207 struct got_object_qid *qid;
1208 char *id_str1 = NULL, *id_str2;
1209 struct got_diff_blob_output_unidiff_arg arg;
1211 err = got_object_open_as_tree(&tree2, repo,
1212 got_object_commit_get_tree_id(commit));
1216 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1218 struct got_commit_object *pcommit;
1220 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1224 err = got_object_open_as_tree(&tree1, repo,
1225 got_object_commit_get_tree_id(pcommit));
1226 got_object_commit_close(pcommit);
1230 err = got_object_id_str(&id_str1, qid->id);
1235 err = got_object_id_str(&id_str2, id);
1239 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1240 arg.diff_context = diff_context;
1241 arg.outfile = stdout;
1242 err = got_diff_tree(tree1, tree2, "", "", repo,
1243 got_diff_blob_output_unidiff, &arg);
1246 got_object_tree_close(tree1);
1247 got_object_tree_close(tree2);
1254 get_datestr(time_t *time, char *datebuf)
1256 char *p, *s = ctime_r(time, datebuf);
1257 p = strchr(s, '\n');
1263 static const struct got_error *
1264 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1265 struct got_repository *repo, int show_patch, int diff_context,
1266 struct got_reflist_head *refs)
1268 const struct got_error *err = NULL;
1269 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1271 time_t committer_time;
1272 const char *author, *committer;
1273 char *refs_str = NULL;
1274 struct got_reflist_entry *re;
1276 SIMPLEQ_FOREACH(re, refs, entry) {
1279 if (got_object_id_cmp(re->id, id) != 0)
1281 name = got_ref_get_name(re->ref);
1282 if (strcmp(name, GOT_REF_HEAD) == 0)
1284 if (strncmp(name, "refs/", 5) == 0)
1286 if (strncmp(name, "got/", 4) == 0)
1288 if (strncmp(name, "heads/", 6) == 0)
1290 if (strncmp(name, "remotes/", 8) == 0)
1293 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1295 err = got_error_from_errno("asprintf");
1301 err = got_object_id_str(&id_str, id);
1305 printf("-----------------------------------------------\n");
1306 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1307 refs_str ? refs_str : "", refs_str ? ")" : "");
1312 printf("from: %s\n", got_object_commit_get_author(commit));
1313 committer_time = got_object_commit_get_committer_time(commit);
1314 datestr = get_datestr(&committer_time, datebuf);
1315 printf("date: %s UTC\n", datestr);
1316 author = got_object_commit_get_author(commit);
1317 committer = got_object_commit_get_committer(commit);
1318 if (strcmp(author, committer) != 0)
1319 printf("via: %s\n", committer);
1320 if (got_object_commit_get_nparents(commit) > 1) {
1321 const struct got_object_id_queue *parent_ids;
1322 struct got_object_qid *qid;
1324 parent_ids = got_object_commit_get_parent_ids(commit);
1325 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1326 err = got_object_id_str(&id_str, qid->id);
1329 printf("parent %d: %s\n", n++, id_str);
1334 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1335 if (logmsg0 == NULL)
1336 return got_error_from_errno("strdup");
1340 line = strsep(&logmsg, "\n");
1342 printf(" %s\n", line);
1347 err = print_patch(commit, id, diff_context, repo);
1352 if (fflush(stdout) != 0 && err == NULL)
1353 err = got_error_from_errno("fflush");
1357 static const struct got_error *
1358 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1359 char *path, int show_patch, int diff_context, int limit,
1360 int first_parent_traversal, struct got_reflist_head *refs)
1362 const struct got_error *err;
1363 struct got_commit_graph *graph;
1365 err = got_commit_graph_open(&graph, root_id, path,
1366 first_parent_traversal, repo);
1369 err = got_commit_graph_iter_start(graph, root_id, repo);
1373 struct got_commit_object *commit;
1374 struct got_object_id *id;
1376 if (sigint_received || sigpipe_received)
1379 err = got_commit_graph_iter_next(&id, graph);
1381 if (err->code == GOT_ERR_ITER_COMPLETED) {
1385 if (err->code != GOT_ERR_ITER_NEED_MORE)
1387 err = got_commit_graph_fetch_commits(graph, 1, repo);
1396 err = got_object_open_as_commit(&commit, repo, id);
1399 err = print_commit(commit, id, repo, show_patch, diff_context,
1401 got_object_commit_close(commit);
1402 if (err || (limit && --limit == 0))
1406 got_commit_graph_close(graph);
1413 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1414 "[-r repository-path] [path]\n", getprogname());
1418 static const struct got_error *
1419 cmd_log(int argc, char *argv[])
1421 const struct got_error *error;
1422 struct got_repository *repo = NULL;
1423 struct got_worktree *worktree = NULL;
1424 struct got_commit_object *commit = NULL;
1425 struct got_object_id *id = NULL;
1426 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1427 char *start_commit = NULL;
1428 int diff_context = 3, ch;
1429 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1431 struct got_reflist_head refs;
1433 SIMPLEQ_INIT(&refs);
1436 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1442 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1448 start_commit = optarg;
1451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1454 err(1, "-C option %s", errstr);
1457 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1459 err(1, "-l option %s", errstr);
1462 first_parent_traversal = 1;
1465 repo_path = realpath(optarg, NULL);
1466 if (repo_path == NULL)
1467 err(1, "-r option");
1468 got_path_strip_trailing_slashes(repo_path);
1479 cwd = getcwd(NULL, 0);
1481 error = got_error_from_errno("getcwd");
1485 error = got_worktree_open(&worktree, cwd);
1486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1493 error = got_error_from_errno("strdup");
1496 } else if (argc == 1) {
1498 error = got_worktree_resolve_path(&path, worktree,
1503 path = strdup(argv[0]);
1505 error = got_error_from_errno("strdup");
1512 if (repo_path == NULL) {
1513 repo_path = worktree ?
1514 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1516 if (repo_path == NULL) {
1517 error = got_error_from_errno("strdup");
1521 error = got_repo_open(&repo, repo_path);
1525 error = apply_unveil(got_repo_get_path(repo), 1,
1526 worktree ? got_worktree_get_root_path(worktree) : NULL);
1530 if (start_commit == NULL) {
1531 struct got_reference *head_ref;
1532 error = got_ref_open(&head_ref, repo,
1533 worktree ? got_worktree_get_head_ref_name(worktree)
1537 error = got_ref_resolve(&id, repo, head_ref);
1538 got_ref_close(head_ref);
1541 error = got_object_open_as_commit(&commit, repo, id);
1543 struct got_reference *ref;
1544 error = got_ref_open(&ref, repo, start_commit, 0);
1545 if (error == NULL) {
1547 error = got_ref_resolve(&id, repo, ref);
1551 error = got_object_get_type(&obj_type, repo, id);
1554 if (obj_type == GOT_OBJ_TYPE_TAG) {
1555 struct got_tag_object *tag;
1556 error = got_object_open_as_tag(&tag, repo, id);
1559 if (got_object_tag_get_object_type(tag) !=
1560 GOT_OBJ_TYPE_COMMIT) {
1561 got_object_tag_close(tag);
1562 error = got_error(GOT_ERR_OBJ_TYPE);
1566 id = got_object_id_dup(
1567 got_object_tag_get_object_id(tag));
1569 error = got_error_from_errno(
1570 "got_object_id_dup");
1571 got_object_tag_close(tag);
1574 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1575 error = got_error(GOT_ERR_OBJ_TYPE);
1578 error = got_object_open_as_commit(&commit, repo, id);
1582 if (commit == NULL) {
1583 error = got_repo_match_object_id_prefix(&id,
1584 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1592 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1597 path = in_repo_path;
1600 error = got_ref_list(&refs, repo);
1604 error = print_commits(id, repo, path, show_patch,
1605 diff_context, limit, first_parent_traversal, &refs);
1612 got_worktree_close(worktree);
1614 const struct got_error *repo_error;
1615 repo_error = got_repo_close(repo);
1619 got_ref_list_free(&refs);
1626 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1627 "[object1 object2 | path]\n", getprogname());
1631 struct print_diff_arg {
1632 struct got_repository *repo;
1633 struct got_worktree *worktree;
1639 static const struct got_error *
1640 print_diff(void *arg, unsigned char status, const char *path,
1641 struct got_object_id *blob_id, struct got_object_id *commit_id)
1643 struct print_diff_arg *a = arg;
1644 const struct got_error *err = NULL;
1645 struct got_blob_object *blob1 = NULL;
1647 char *abspath = NULL;
1650 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1651 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1654 if (!a->header_shown) {
1655 printf("diff %s %s\n", a->id_str,
1656 got_worktree_get_root_path(a->worktree));
1657 a->header_shown = 1;
1660 if (status != GOT_STATUS_ADD) {
1661 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1667 if (status != GOT_STATUS_DELETE) {
1668 if (asprintf(&abspath, "%s/%s",
1669 got_worktree_get_root_path(a->worktree), path) == -1) {
1670 err = got_error_from_errno("asprintf");
1674 f2 = fopen(abspath, "r");
1676 err = got_error_from_errno2("fopen", abspath);
1679 if (lstat(abspath, &sb) == -1) {
1680 err = got_error_from_errno2("lstat", abspath);
1686 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1690 got_object_blob_close(blob1);
1691 if (f2 && fclose(f2) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1697 static const struct got_error *
1698 cmd_diff(int argc, char *argv[])
1700 const struct got_error *error;
1701 struct got_repository *repo = NULL;
1702 struct got_worktree *worktree = NULL;
1703 char *cwd = NULL, *repo_path = NULL;
1704 struct got_object_id *id1 = NULL, *id2 = NULL;
1705 const char *id_str1 = NULL, *id_str2 = NULL;
1706 char *label1 = NULL, *label2 = NULL;
1708 int diff_context = 3, ch;
1713 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1718 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1721 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1723 err(1, "-C option %s", errstr);
1726 repo_path = realpath(optarg, NULL);
1727 if (repo_path == NULL)
1728 err(1, "-r option");
1729 got_path_strip_trailing_slashes(repo_path);
1740 cwd = getcwd(NULL, 0);
1742 error = got_error_from_errno("getcwd");
1745 error = got_worktree_open(&worktree, cwd);
1746 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1749 if (worktree == NULL) {
1750 error = got_error(GOT_ERR_NOT_WORKTREE);
1755 "-r option can't be used when diffing a work tree");
1756 repo_path = strdup(got_worktree_get_repo_path(worktree));
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno("strdup");
1762 error = got_worktree_resolve_path(&path, worktree,
1769 error = got_error_from_errno("strdup");
1773 } else if (argc == 2) {
1776 if (worktree && repo_path == NULL) {
1778 strdup(got_worktree_get_repo_path(worktree));
1779 if (repo_path == NULL) {
1780 error = got_error_from_errno("strdup");
1787 if (repo_path == NULL) {
1788 repo_path = getcwd(NULL, 0);
1789 if (repo_path == NULL)
1790 return got_error_from_errno("getcwd");
1793 error = got_repo_open(&repo, repo_path);
1798 error = apply_unveil(got_repo_get_path(repo), 1,
1799 worktree ? got_worktree_get_root_path(worktree) : NULL);
1804 struct print_diff_arg arg;
1805 struct got_pathlist_head paths;
1810 error = got_object_id_str(&id_str,
1811 got_worktree_get_base_commit_id(worktree));
1815 arg.worktree = worktree;
1816 arg.diff_context = diff_context;
1817 arg.id_str = id_str;
1818 arg.header_shown = 0;
1820 error = got_pathlist_append(NULL, &paths, path, NULL);
1824 error = got_worktree_status(worktree, &paths, repo, print_diff,
1825 &arg, check_cancelled, NULL);
1827 got_pathlist_free(&paths);
1831 error = got_repo_match_object_id_prefix(&id1, id_str1,
1832 GOT_OBJ_TYPE_ANY, repo);
1834 struct got_reference *ref;
1835 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1837 error = got_ref_open(&ref, repo, id_str1, 0);
1840 label1 = strdup(got_ref_get_name(ref));
1841 if (label1 == NULL) {
1842 error = got_error_from_errno("strdup");
1845 error = got_ref_resolve(&id1, repo, ref);
1850 error = got_object_id_str(&label1, id1);
1851 if (label1 == NULL) {
1852 error = got_error_from_errno("strdup");
1857 error = got_repo_match_object_id_prefix(&id2, id_str2,
1858 GOT_OBJ_TYPE_ANY, repo);
1860 struct got_reference *ref;
1861 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1863 error = got_ref_open(&ref, repo, id_str2, 0);
1866 label2 = strdup(got_ref_get_name(ref));
1867 if (label2 == NULL) {
1868 error = got_error_from_errno("strdup");
1871 error = got_ref_resolve(&id2, repo, ref);
1876 error = got_object_id_str(&label2, id2);
1877 if (label2 == NULL) {
1878 error = got_error_from_errno("strdup");
1883 error = got_object_get_type(&type1, repo, id1);
1887 error = got_object_get_type(&type2, repo, id2);
1891 if (type1 != type2) {
1892 error = got_error(GOT_ERR_OBJ_TYPE);
1897 case GOT_OBJ_TYPE_BLOB:
1898 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1899 diff_context, repo, stdout);
1901 case GOT_OBJ_TYPE_TREE:
1902 error = got_diff_objects_as_trees(id1, id2, "", "",
1903 diff_context, repo, stdout);
1905 case GOT_OBJ_TYPE_COMMIT:
1906 printf("diff %s %s\n", label1, label2);
1907 error = got_diff_objects_as_commits(id1, id2, diff_context,
1911 error = got_error(GOT_ERR_OBJ_TYPE);
1921 got_worktree_close(worktree);
1923 const struct got_error *repo_error;
1924 repo_error = got_repo_close(repo);
1935 "usage: %s blame [-c commit] [-r repository-path] path\n",
1940 static const struct got_error *
1941 cmd_blame(int argc, char *argv[])
1943 const struct got_error *error;
1944 struct got_repository *repo = NULL;
1945 struct got_worktree *worktree = NULL;
1946 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1947 struct got_object_id *commit_id = NULL;
1948 char *commit_id_str = NULL;
1952 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1957 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1960 commit_id_str = optarg;
1963 repo_path = realpath(optarg, NULL);
1964 if (repo_path == NULL)
1965 err(1, "-r option");
1966 got_path_strip_trailing_slashes(repo_path);
1982 cwd = getcwd(NULL, 0);
1984 error = got_error_from_errno("getcwd");
1987 if (repo_path == NULL) {
1988 error = got_worktree_open(&worktree, cwd);
1989 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1995 strdup(got_worktree_get_repo_path(worktree));
1996 if (repo_path == NULL)
1997 error = got_error_from_errno("strdup");
2001 repo_path = strdup(cwd);
2002 if (repo_path == NULL) {
2003 error = got_error_from_errno("strdup");
2009 error = got_repo_open(&repo, repo_path);
2013 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2018 const char *prefix = got_worktree_get_path_prefix(worktree);
2019 char *p, *worktree_subdir = cwd +
2020 strlen(got_worktree_get_root_path(worktree));
2021 if (asprintf(&p, "%s%s%s%s%s",
2022 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2023 worktree_subdir, worktree_subdir[0] ? "/" : "",
2025 error = got_error_from_errno("asprintf");
2028 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2031 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2036 if (commit_id_str == NULL) {
2037 struct got_reference *head_ref;
2038 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2041 error = got_ref_resolve(&commit_id, repo, head_ref);
2042 got_ref_close(head_ref);
2046 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2051 error = got_blame(in_repo_path, commit_id, repo, stdout);
2058 got_worktree_close(worktree);
2060 const struct got_error *repo_error;
2061 repo_error = got_repo_close(repo);
2072 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2078 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2079 const char *root_path)
2081 int is_root_path = (strcmp(path, root_path) == 0);
2083 path += strlen(root_path);
2084 while (path[0] == '/')
2087 printf("%s%s%s%s%s\n", id ? id : "", path,
2088 is_root_path ? "" : "/", te->name,
2089 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2092 static const struct got_error *
2093 print_tree(const char *path, struct got_object_id *commit_id,
2094 int show_ids, int recurse, const char *root_path,
2095 struct got_repository *repo)
2097 const struct got_error *err = NULL;
2098 struct got_object_id *tree_id = NULL;
2099 struct got_tree_object *tree = NULL;
2100 const struct got_tree_entries *entries;
2101 struct got_tree_entry *te;
2103 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2107 err = got_object_open_as_tree(&tree, repo, tree_id);
2110 entries = got_object_tree_get_entries(tree);
2111 te = SIMPLEQ_FIRST(&entries->head);
2115 if (sigint_received || sigpipe_received)
2120 err = got_object_id_str(&id_str, te->id);
2123 if (asprintf(&id, "%s ", id_str) == -1) {
2124 err = got_error_from_errno("asprintf");
2130 print_entry(te, id, path, root_path);
2133 if (recurse && S_ISDIR(te->mode)) {
2135 if (asprintf(&child_path, "%s%s%s", path,
2136 path[0] == '/' && path[1] == '\0' ? "" : "/",
2138 err = got_error_from_errno("asprintf");
2141 err = print_tree(child_path, commit_id, show_ids, 1,
2148 te = SIMPLEQ_NEXT(te, entry);
2152 got_object_tree_close(tree);
2157 static const struct got_error *
2158 cmd_tree(int argc, char *argv[])
2160 const struct got_error *error;
2161 struct got_repository *repo = NULL;
2162 struct got_worktree *worktree = NULL;
2164 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2165 struct got_object_id *commit_id = NULL;
2166 char *commit_id_str = NULL;
2167 int show_ids = 0, recurse = 0;
2171 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2176 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2179 commit_id_str = optarg;
2182 repo_path = realpath(optarg, NULL);
2183 if (repo_path == NULL)
2184 err(1, "-r option");
2185 got_path_strip_trailing_slashes(repo_path);
2209 cwd = getcwd(NULL, 0);
2211 error = got_error_from_errno("getcwd");
2214 if (repo_path == NULL) {
2215 error = got_worktree_open(&worktree, cwd);
2216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2222 strdup(got_worktree_get_repo_path(worktree));
2223 if (repo_path == NULL)
2224 error = got_error_from_errno("strdup");
2228 repo_path = strdup(cwd);
2229 if (repo_path == NULL) {
2230 error = got_error_from_errno("strdup");
2236 error = got_repo_open(&repo, repo_path);
2240 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2246 char *p, *worktree_subdir = cwd +
2247 strlen(got_worktree_get_root_path(worktree));
2248 if (asprintf(&p, "%s/%s",
2249 got_worktree_get_path_prefix(worktree),
2250 worktree_subdir) == -1) {
2251 error = got_error_from_errno("asprintf");
2254 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2261 if (in_repo_path == NULL) {
2262 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2267 if (commit_id_str == NULL) {
2268 struct got_reference *head_ref;
2269 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2272 error = got_ref_resolve(&commit_id, repo, head_ref);
2273 got_ref_close(head_ref);
2277 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2282 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2283 in_repo_path, repo);
2290 got_worktree_close(worktree);
2292 const struct got_error *repo_error;
2293 repo_error = got_repo_close(repo);
2303 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2307 static const struct got_error *
2308 print_status(void *arg, unsigned char status, const char *path,
2309 struct got_object_id *blob_id, struct got_object_id *commit_id)
2311 printf("%c %s\n", status, path);
2315 static const struct got_error *
2316 cmd_status(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_repository *repo = NULL;
2320 struct got_worktree *worktree = NULL;
2322 struct got_pathlist_head paths;
2327 while ((ch = getopt(argc, argv, "")) != -1) {
2339 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2343 cwd = getcwd(NULL, 0);
2345 error = got_error_from_errno("getcwd");
2349 error = got_worktree_open(&worktree, cwd);
2354 error = got_pathlist_append(NULL, &paths, "", NULL);
2357 } else if (argc >= 1) {
2358 for (i = 0; i < argc; i++) {
2360 error = got_worktree_resolve_path(&path, worktree,
2364 error = got_pathlist_append(NULL, &paths, path, NULL);
2373 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2377 error = apply_unveil(got_repo_get_path(repo), 1,
2378 got_worktree_get_root_path(worktree));
2382 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2383 check_cancelled, NULL);
2386 struct got_pathlist_entry *pe;
2387 TAILQ_FOREACH(pe, &paths, entry)
2388 free((char *)pe->path);
2390 got_pathlist_free(&paths);
2399 "usage: %s ref [-r repository] -l | -d name | name target\n",
2404 static const struct got_error *
2405 list_refs(struct got_repository *repo)
2407 static const struct got_error *err = NULL;
2408 struct got_reflist_head refs;
2409 struct got_reflist_entry *re;
2411 SIMPLEQ_INIT(&refs);
2412 err = got_ref_list(&refs, repo);
2416 SIMPLEQ_FOREACH(re, &refs, entry) {
2418 refstr = got_ref_to_str(re->ref);
2420 return got_error_from_errno("got_ref_to_str");
2421 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2425 got_ref_list_free(&refs);
2429 static const struct got_error *
2430 delete_ref(struct got_repository *repo, const char *refname)
2432 const struct got_error *err = NULL;
2433 struct got_reference *ref;
2435 err = got_ref_open(&ref, repo, refname, 0);
2439 err = got_ref_delete(ref, repo);
2444 static const struct got_error *
2445 add_ref(struct got_repository *repo, const char *refname, const char *target)
2447 const struct got_error *err = NULL;
2448 struct got_object_id *id;
2449 struct got_reference *ref = NULL;
2452 * Don't let the user create a reference named '-'.
2453 * While technically a valid reference name, this case is usually
2454 * an unintended typo.
2456 if (refname[0] == '-' && refname[1] == '\0')
2457 return got_error(GOT_ERR_BAD_REF_NAME);
2459 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2462 struct got_reference *target_ref;
2464 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2466 err = got_ref_open(&target_ref, repo, target, 0);
2469 err = got_ref_resolve(&id, repo, target_ref);
2470 got_ref_close(target_ref);
2475 err = got_ref_alloc(&ref, refname, id);
2479 err = got_ref_write(ref, repo);
2487 static const struct got_error *
2488 cmd_ref(int argc, char *argv[])
2490 const struct got_error *error = NULL;
2491 struct got_repository *repo = NULL;
2492 struct got_worktree *worktree = NULL;
2493 char *cwd = NULL, *repo_path = NULL;
2494 int ch, do_list = 0;
2495 const char *delref = NULL;
2497 /* TODO: Add -s option for adding symbolic references. */
2498 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2504 repo_path = realpath(optarg, NULL);
2505 if (repo_path == NULL)
2506 err(1, "-r option");
2507 got_path_strip_trailing_slashes(repo_path);
2518 if (do_list && delref)
2519 errx(1, "-l and -d options are mutually exclusive\n");
2524 if (do_list || delref) {
2527 } else if (argc != 2)
2532 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2536 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2537 "sendfd unveil", NULL) == -1)
2541 cwd = getcwd(NULL, 0);
2543 error = got_error_from_errno("getcwd");
2547 if (repo_path == NULL) {
2548 error = got_worktree_open(&worktree, cwd);
2549 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2555 strdup(got_worktree_get_repo_path(worktree));
2556 if (repo_path == NULL)
2557 error = got_error_from_errno("strdup");
2561 repo_path = strdup(cwd);
2562 if (repo_path == NULL) {
2563 error = got_error_from_errno("strdup");
2569 error = got_repo_open(&repo, repo_path);
2573 error = apply_unveil(got_repo_get_path(repo), do_list,
2574 worktree ? got_worktree_get_root_path(worktree) : NULL);
2579 error = list_refs(repo);
2581 error = delete_ref(repo, delref);
2583 error = add_ref(repo, argv[0], argv[1]);
2586 got_repo_close(repo);
2588 got_worktree_close(worktree);
2598 "usage: %s branch [-r repository] -l | -d name | "
2599 "name [base-branch]\n", getprogname());
2603 static const struct got_error *
2604 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2606 static const struct got_error *err = NULL;
2607 struct got_reflist_head refs;
2608 struct got_reflist_entry *re;
2610 SIMPLEQ_INIT(&refs);
2612 err = got_ref_list(&refs, repo);
2616 SIMPLEQ_FOREACH(re, &refs, entry) {
2617 const char *refname, *marker = " ";
2619 refname = got_ref_get_name(re->ref);
2620 if (strncmp(refname, "refs/heads/", 11) != 0)
2622 if (worktree && strcmp(refname,
2623 got_worktree_get_head_ref_name(worktree)) == 0) {
2624 struct got_object_id *id = NULL;
2625 err = got_ref_resolve(&id, repo, re->ref);
2628 if (got_object_id_cmp(id,
2629 got_worktree_get_base_commit_id(worktree)) == 0)
2636 refstr = got_ref_to_str(re->ref);
2638 return got_error_from_errno("got_ref_to_str");
2639 printf("%s%s: %s\n", marker, refname, refstr);
2643 got_ref_list_free(&refs);
2647 static const struct got_error *
2648 delete_branch(struct got_repository *repo, const char *branch_name)
2650 const struct got_error *err = NULL;
2651 struct got_reference *ref;
2654 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2655 return got_error_from_errno("asprintf");
2657 err = got_ref_open(&ref, repo, refname, 0);
2661 err = got_ref_delete(ref, repo);
2668 static const struct got_error *
2669 add_branch(struct got_repository *repo, const char *branch_name,
2670 const char *base_branch)
2672 const struct got_error *err = NULL;
2673 struct got_object_id *id = NULL;
2674 struct got_reference *ref = NULL;
2675 char *base_refname = NULL, *refname = NULL;
2676 struct got_reference *base_ref;
2679 * Don't let the user create a branch named '-'.
2680 * While technically a valid reference name, this case is usually
2681 * an unintended typo.
2683 if (branch_name[0] == '-' && branch_name[1] == '\0')
2684 return got_error(GOT_ERR_BAD_REF_NAME);
2686 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2687 base_refname = strdup(GOT_REF_HEAD);
2688 if (base_refname == NULL)
2689 return got_error_from_errno("strdup");
2690 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2691 return got_error_from_errno("asprintf");
2693 err = got_ref_open(&base_ref, repo, base_refname, 0);
2696 err = got_ref_resolve(&id, repo, base_ref);
2697 got_ref_close(base_ref);
2701 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2702 err = got_error_from_errno("asprintf");
2706 err = got_ref_open(&ref, repo, refname, 0);
2708 err = got_error(GOT_ERR_BRANCH_EXISTS);
2710 } else if (err->code != GOT_ERR_NOT_REF)
2713 err = got_ref_alloc(&ref, refname, id);
2717 err = got_ref_write(ref, repo);
2727 static const struct got_error *
2728 cmd_branch(int argc, char *argv[])
2730 const struct got_error *error = NULL;
2731 struct got_repository *repo = NULL;
2732 struct got_worktree *worktree = NULL;
2733 char *cwd = NULL, *repo_path = NULL;
2734 int ch, do_list = 0;
2735 const char *delref = NULL;
2737 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2743 repo_path = realpath(optarg, NULL);
2744 if (repo_path == NULL)
2745 err(1, "-r option");
2746 got_path_strip_trailing_slashes(repo_path);
2757 if (do_list && delref)
2758 errx(1, "-l and -d options are mutually exclusive\n");
2763 if (do_list || delref) {
2766 } else if (argc < 1 || argc > 2)
2771 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2775 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2776 "sendfd unveil", NULL) == -1)
2780 cwd = getcwd(NULL, 0);
2782 error = got_error_from_errno("getcwd");
2786 if (repo_path == NULL) {
2787 error = got_worktree_open(&worktree, cwd);
2788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2794 strdup(got_worktree_get_repo_path(worktree));
2795 if (repo_path == NULL)
2796 error = got_error_from_errno("strdup");
2800 repo_path = strdup(cwd);
2801 if (repo_path == NULL) {
2802 error = got_error_from_errno("strdup");
2808 error = got_repo_open(&repo, repo_path);
2812 error = apply_unveil(got_repo_get_path(repo), do_list,
2813 worktree ? got_worktree_get_root_path(worktree) : NULL);
2818 error = list_branches(repo, worktree);
2820 error = delete_branch(repo, delref);
2822 const char *base_branch;
2824 base_branch = worktree ?
2825 got_worktree_get_head_ref_name(worktree) :
2828 base_branch = argv[1];
2829 error = add_branch(repo, argv[0], base_branch);
2833 got_repo_close(repo);
2835 got_worktree_close(worktree);
2844 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2848 static const struct got_error *
2849 cmd_add(int argc, char *argv[])
2851 const struct got_error *error = NULL;
2852 struct got_repository *repo = NULL;
2853 struct got_worktree *worktree = NULL;
2855 struct got_pathlist_head paths;
2856 struct got_pathlist_entry *pe;
2861 while ((ch = getopt(argc, argv, "")) != -1) {
2873 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2880 cwd = getcwd(NULL, 0);
2882 error = got_error_from_errno("getcwd");
2886 error = got_worktree_open(&worktree, cwd);
2890 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2894 error = apply_unveil(got_repo_get_path(repo), 1,
2895 got_worktree_get_root_path(worktree));
2899 for (x = 0; x < argc; x++) {
2900 char *path = realpath(argv[x], NULL);
2902 error = got_error_from_errno2("realpath", argv[x]);
2906 got_path_strip_trailing_slashes(path);
2907 error = got_pathlist_insert(&pe, &paths, path, NULL);
2913 error = got_worktree_schedule_add(worktree, &paths, print_status,
2917 got_repo_close(repo);
2919 got_worktree_close(worktree);
2920 TAILQ_FOREACH(pe, &paths, entry)
2921 free((char *)pe->path);
2922 got_pathlist_free(&paths);
2930 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2934 static const struct got_error *
2935 cmd_remove(int argc, char *argv[])
2937 const struct got_error *error = NULL;
2938 struct got_worktree *worktree = NULL;
2939 struct got_repository *repo = NULL;
2941 struct got_pathlist_head paths;
2942 struct got_pathlist_entry *pe;
2943 int ch, i, delete_local_mods = 0;
2947 while ((ch = getopt(argc, argv, "f")) != -1) {
2950 delete_local_mods = 1;
2962 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2969 cwd = getcwd(NULL, 0);
2971 error = got_error_from_errno("getcwd");
2974 error = got_worktree_open(&worktree, cwd);
2978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2982 error = apply_unveil(got_repo_get_path(repo), 1,
2983 got_worktree_get_root_path(worktree));
2987 for (i = 0; i < argc; i++) {
2988 char *path = realpath(argv[i], NULL);
2990 error = got_error_from_errno2("realpath", argv[i]);
2994 got_path_strip_trailing_slashes(path);
2995 error = got_pathlist_insert(&pe, &paths, path, NULL);
3001 error = got_worktree_schedule_delete(worktree, &paths,
3002 delete_local_mods, print_status, NULL, repo);
3007 got_repo_close(repo);
3009 got_worktree_close(worktree);
3010 TAILQ_FOREACH(pe, &paths, entry)
3011 free((char *)pe->path);
3012 got_pathlist_free(&paths);
3020 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3024 static const struct got_error *
3025 revert_progress(void *arg, unsigned char status, const char *path)
3027 while (path[0] == '/')
3029 printf("%c %s\n", status, path);
3033 static const struct got_error *
3034 cmd_revert(int argc, char *argv[])
3036 const struct got_error *error = NULL;
3037 struct got_worktree *worktree = NULL;
3038 struct got_repository *repo = NULL;
3039 char *cwd = NULL, *path = NULL;
3040 struct got_pathlist_head paths;
3041 struct got_pathlist_entry *pe;
3046 while ((ch = getopt(argc, argv, "")) != -1) {
3058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3059 "unveil", NULL) == -1)
3065 for (i = 0; i < argc; i++) {
3066 char *path = realpath(argv[i], NULL);
3068 error = got_error_from_errno2("realpath", argv[i]);
3072 got_path_strip_trailing_slashes(path);
3073 error = got_pathlist_insert(&pe, &paths, path, NULL);
3080 cwd = getcwd(NULL, 0);
3082 error = got_error_from_errno("getcwd");
3085 error = got_worktree_open(&worktree, cwd);
3089 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3093 error = apply_unveil(got_repo_get_path(repo), 1,
3094 got_worktree_get_root_path(worktree));
3098 error = got_worktree_revert(worktree, &paths,
3099 revert_progress, NULL, repo);
3104 got_repo_close(repo);
3106 got_worktree_close(worktree);
3115 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3119 struct collect_commit_logmsg_arg {
3120 const char *cmdline_log;
3122 const char *worktree_path;
3123 const char *branch_name;
3124 const char *repo_path;
3129 static const struct got_error *
3130 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3133 char *initial_content = NULL;
3134 struct got_pathlist_entry *pe;
3135 const struct got_error *err = NULL;
3136 char *template = NULL;
3137 struct collect_commit_logmsg_arg *a = arg;
3141 /* if a message was specified on the command line, just use it */
3142 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3143 len = strlen(a->cmdline_log) + 1;
3144 *logmsg = malloc(len + 1);
3145 if (*logmsg == NULL)
3146 return got_error_from_errno("malloc");
3147 strlcpy(*logmsg, a->cmdline_log, len);
3151 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3152 return got_error_from_errno("asprintf");
3154 if (asprintf(&initial_content,
3155 "\n# changes to be committed on branch %s:\n",
3156 a->branch_name) == -1)
3157 return got_error_from_errno("asprintf");
3159 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3163 dprintf(fd, initial_content);
3165 TAILQ_FOREACH(pe, commitable_paths, entry) {
3166 struct got_commitable *ct = pe->data;
3167 dprintf(fd, "# %c %s\n",
3168 got_commitable_get_status(ct),
3169 got_commitable_get_path(ct));
3173 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3175 unlink(a->logmsg_path);
3176 free(a->logmsg_path);
3177 free(initial_content);
3180 /* Editor is done; we can now apply unveil(2) */
3182 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3191 static const struct got_error *
3192 cmd_commit(int argc, char *argv[])
3194 const struct got_error *error = NULL;
3195 struct got_worktree *worktree = NULL;
3196 struct got_repository *repo = NULL;
3197 char *cwd = NULL, *path = NULL, *id_str = NULL;
3198 struct got_object_id *id = NULL;
3199 const char *logmsg = NULL;
3200 const char *got_author = getenv("GOT_AUTHOR");
3201 struct collect_commit_logmsg_arg cl_arg;
3202 char *editor = NULL;
3203 int ch, rebase_in_progress;
3205 while ((ch = getopt(argc, argv, "m:")) != -1) {
3220 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3221 "unveil", NULL) == -1)
3225 path = realpath(argv[0], NULL);
3227 error = got_error_from_errno2("realpath", argv[0]);
3230 got_path_strip_trailing_slashes(path);
3231 } else if (argc != 0)
3234 if (got_author == NULL) {
3235 /* TODO: Look current user up in password database */
3236 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3240 cwd = getcwd(NULL, 0);
3242 error = got_error_from_errno("getcwd");
3245 error = got_worktree_open(&worktree, cwd);
3249 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3252 if (rebase_in_progress) {
3253 error = got_error(GOT_ERR_REBASING);
3257 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3262 * unveil(2) traverses exec(2); if an editor is used we have
3263 * to apply unveil after the log message has been written.
3265 if (logmsg == NULL || strlen(logmsg) == 0)
3266 error = get_editor(&editor);
3268 error = apply_unveil(got_repo_get_path(repo), 0,
3269 got_worktree_get_root_path(worktree));
3273 cl_arg.editor = editor;
3274 cl_arg.cmdline_log = logmsg;
3275 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3276 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3277 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3278 cl_arg.branch_name += 5;
3279 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3280 cl_arg.branch_name += 6;
3281 cl_arg.repo_path = got_repo_get_path(repo);
3282 cl_arg.logmsg_path = NULL;
3283 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3284 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3286 if (cl_arg.logmsg_path)
3287 fprintf(stderr, "%s: log message preserved in %s\n",
3288 getprogname(), cl_arg.logmsg_path);
3292 if (cl_arg.logmsg_path)
3293 unlink(cl_arg.logmsg_path);
3295 error = got_object_id_str(&id_str, id);
3298 printf("Created commit %s\n", id_str);
3301 got_repo_close(repo);
3303 got_worktree_close(worktree);
3312 usage_cherrypick(void)
3314 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3318 static const struct got_error *
3319 cmd_cherrypick(int argc, char *argv[])
3321 const struct got_error *error = NULL;
3322 struct got_worktree *worktree = NULL;
3323 struct got_repository *repo = NULL;
3324 char *cwd = NULL, *commit_id_str = NULL;
3325 struct got_object_id *commit_id = NULL;
3326 struct got_commit_object *commit = NULL;
3327 struct got_object_qid *pid;
3328 struct got_reference *head_ref = NULL;
3329 int ch, did_something = 0;
3331 while ((ch = getopt(argc, argv, "")) != -1) {
3343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3344 "unveil", NULL) == -1)
3350 cwd = getcwd(NULL, 0);
3352 error = got_error_from_errno("getcwd");
3355 error = got_worktree_open(&worktree, cwd);
3359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3363 error = apply_unveil(got_repo_get_path(repo), 0,
3364 got_worktree_get_root_path(worktree));
3368 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3369 GOT_OBJ_TYPE_COMMIT, repo);
3370 if (error != NULL) {
3371 struct got_reference *ref;
3372 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3374 error = got_ref_open(&ref, repo, argv[0], 0);
3377 error = got_ref_resolve(&commit_id, repo, ref);
3382 error = got_object_id_str(&commit_id_str, commit_id);
3386 error = got_ref_open(&head_ref, repo,
3387 got_worktree_get_head_ref_name(worktree), 0);
3391 error = check_same_branch(commit_id, head_ref, repo);
3393 if (error->code != GOT_ERR_ANCESTRY)
3397 error = got_error(GOT_ERR_SAME_BRANCH);
3401 error = got_object_open_as_commit(&commit, repo, commit_id);
3404 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3405 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3406 commit_id, repo, update_progress, &did_something, check_cancelled,
3412 printf("Merged commit %s\n", commit_id_str);
3415 got_object_commit_close(commit);
3416 free(commit_id_str);
3418 got_ref_close(head_ref);
3420 got_worktree_close(worktree);
3422 got_repo_close(repo);
3429 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3433 static const struct got_error *
3434 cmd_backout(int argc, char *argv[])
3436 const struct got_error *error = NULL;
3437 struct got_worktree *worktree = NULL;
3438 struct got_repository *repo = NULL;
3439 char *cwd = NULL, *commit_id_str = NULL;
3440 struct got_object_id *commit_id = NULL;
3441 struct got_commit_object *commit = NULL;
3442 struct got_object_qid *pid;
3443 struct got_reference *head_ref = NULL;
3444 int ch, did_something = 0;
3446 while ((ch = getopt(argc, argv, "")) != -1) {
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3459 "unveil", NULL) == -1)
3465 cwd = getcwd(NULL, 0);
3467 error = got_error_from_errno("getcwd");
3470 error = got_worktree_open(&worktree, cwd);
3474 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3478 error = apply_unveil(got_repo_get_path(repo), 0,
3479 got_worktree_get_root_path(worktree));
3483 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3484 GOT_OBJ_TYPE_COMMIT, repo);
3485 if (error != NULL) {
3486 struct got_reference *ref;
3487 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3489 error = got_ref_open(&ref, repo, argv[0], 0);
3492 error = got_ref_resolve(&commit_id, repo, ref);
3497 error = got_object_id_str(&commit_id_str, commit_id);
3501 error = got_ref_open(&head_ref, repo,
3502 got_worktree_get_head_ref_name(worktree), 0);
3506 error = check_same_branch(commit_id, head_ref, repo);
3510 error = got_object_open_as_commit(&commit, repo, commit_id);
3513 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3515 error = got_error(GOT_ERR_ROOT_COMMIT);
3519 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3520 update_progress, &did_something, check_cancelled, NULL);
3525 printf("Backed out commit %s\n", commit_id_str);
3528 got_object_commit_close(commit);
3529 free(commit_id_str);
3531 got_ref_close(head_ref);
3533 got_worktree_close(worktree);
3535 got_repo_close(repo);
3542 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3548 trim_logmsg(char *logmsg, int limit)
3553 len = strlen(logmsg);
3557 nl = strchr(logmsg, '\n');
3562 static const struct got_error *
3563 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3565 const char *logmsg0 = NULL;
3567 logmsg0 = got_object_commit_get_logmsg(commit);
3569 while (isspace((unsigned char)logmsg0[0]))
3572 *logmsg = strdup(logmsg0);
3573 if (*logmsg == NULL)
3574 return got_error_from_errno("strdup");
3576 trim_logmsg(*logmsg, limit);
3580 static const struct got_error *
3581 show_rebase_progress(struct got_commit_object *commit,
3582 struct got_object_id *old_id, struct got_object_id *new_id)
3584 const struct got_error *err;
3585 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3587 err = got_object_id_str(&old_id_str, old_id);
3592 err = got_object_id_str(&new_id_str, new_id);
3597 old_id_str[12] = '\0';
3599 new_id_str[12] = '\0';
3601 err = get_short_logmsg(&logmsg, 42, commit);
3605 printf("%s -> %s: %s\n", old_id_str,
3606 new_id_str ? new_id_str : "no-op change", logmsg);
3613 static const struct got_error *
3614 rebase_progress(void *arg, unsigned char status, const char *path)
3616 unsigned char *rebase_status = arg;
3618 while (path[0] == '/')
3620 printf("%c %s\n", status, path);
3622 if (*rebase_status == GOT_STATUS_CONFLICT)
3624 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3625 *rebase_status = status;
3629 static const struct got_error *
3630 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3631 struct got_reference *branch, struct got_reference *new_base_branch,
3632 struct got_reference *tmp_branch, struct got_repository *repo)
3634 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3635 return got_worktree_rebase_complete(worktree, fileindex,
3636 new_base_branch, tmp_branch, branch, repo);
3639 static const struct got_error *
3640 rebase_commit(struct got_pathlist_head *merged_paths,
3641 struct got_worktree *worktree, struct got_fileindex *fileindex,
3642 struct got_reference *tmp_branch,
3643 struct got_object_id *commit_id, struct got_repository *repo)
3645 const struct got_error *error;
3646 struct got_commit_object *commit;
3647 struct got_object_id *new_commit_id;
3649 error = got_object_open_as_commit(&commit, repo, commit_id);
3653 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3654 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3656 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3658 error = show_rebase_progress(commit, commit_id, NULL);
3660 error = show_rebase_progress(commit, commit_id, new_commit_id);
3661 free(new_commit_id);
3664 got_object_commit_close(commit);
3668 struct check_path_prefix_arg {
3669 const char *path_prefix;
3674 static const struct got_error *
3675 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3676 struct got_blob_object *blob2, struct got_object_id *id1,
3677 struct got_object_id *id2, const char *path1, const char *path2,
3678 struct got_repository *repo)
3680 struct check_path_prefix_arg *a = arg;
3682 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3683 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3684 return got_error(a->errcode);
3689 static const struct got_error *
3690 check_path_prefix(struct got_object_id *parent_id,
3691 struct got_object_id *commit_id, const char *path_prefix,
3692 int errcode, struct got_repository *repo)
3694 const struct got_error *err;
3695 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3696 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3697 struct check_path_prefix_arg cpp_arg;
3699 if (got_path_is_root_dir(path_prefix))
3702 err = got_object_open_as_commit(&commit, repo, commit_id);
3706 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3710 err = got_object_open_as_tree(&tree1, repo,
3711 got_object_commit_get_tree_id(parent_commit));
3715 err = got_object_open_as_tree(&tree2, repo,
3716 got_object_commit_get_tree_id(commit));
3720 cpp_arg.path_prefix = path_prefix;
3721 while (cpp_arg.path_prefix[0] == '/')
3722 cpp_arg.path_prefix++;
3723 cpp_arg.len = strlen(cpp_arg.path_prefix);
3724 cpp_arg.errcode = errcode;
3725 err = got_diff_tree(tree1, tree2, "", "", repo,
3726 check_path_prefix_in_diff, &cpp_arg);
3729 got_object_tree_close(tree1);
3731 got_object_tree_close(tree2);
3733 got_object_commit_close(commit);
3735 got_object_commit_close(parent_commit);
3739 static const struct got_error *
3740 collect_commits(struct got_object_id_queue *commits,
3741 struct got_object_id *initial_commit_id,
3742 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3743 const char *path_prefix, int path_prefix_errcode,
3744 struct got_repository *repo)
3746 const struct got_error *err = NULL;
3747 struct got_commit_graph *graph = NULL;
3748 struct got_object_id *parent_id = NULL;
3749 struct got_object_qid *qid;
3750 struct got_object_id *commit_id = initial_commit_id;
3752 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3756 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3759 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3760 err = got_commit_graph_iter_next(&parent_id, graph);
3762 if (err->code == GOT_ERR_ITER_COMPLETED) {
3763 err = got_error_msg(GOT_ERR_ANCESTRY,
3764 "ran out of commits to rebase before "
3765 "youngest common ancestor commit has "
3768 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3770 err = got_commit_graph_fetch_commits(graph, 1, repo);
3774 err = check_path_prefix(parent_id, commit_id,
3775 path_prefix, path_prefix_errcode, repo);
3779 err = got_object_qid_alloc(&qid, commit_id);
3782 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3783 commit_id = parent_id;
3787 got_commit_graph_close(graph);
3791 static const struct got_error *
3792 cmd_rebase(int argc, char *argv[])
3794 const struct got_error *error = NULL;
3795 struct got_worktree *worktree = NULL;
3796 struct got_repository *repo = NULL;
3797 struct got_fileindex *fileindex = NULL;
3799 struct got_reference *branch = NULL;
3800 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3801 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3802 struct got_object_id *resume_commit_id = NULL;
3803 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3804 struct got_commit_object *commit = NULL;
3805 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3806 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3807 struct got_object_id_queue commits;
3808 struct got_pathlist_head merged_paths;
3809 const struct got_object_id_queue *parent_ids;
3810 struct got_object_qid *qid, *pid;
3812 SIMPLEQ_INIT(&commits);
3813 TAILQ_INIT(&merged_paths);
3815 while ((ch = getopt(argc, argv, "ac")) != -1) {
3821 continue_rebase = 1;
3833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3834 "unveil", NULL) == -1)
3837 if (abort_rebase && continue_rebase)
3839 else if (abort_rebase || continue_rebase) {
3842 } else if (argc != 1)
3845 cwd = getcwd(NULL, 0);
3847 error = got_error_from_errno("getcwd");
3850 error = got_worktree_open(&worktree, cwd);
3854 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3858 error = apply_unveil(got_repo_get_path(repo), 0,
3859 got_worktree_get_root_path(worktree));
3863 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3869 if (!rebase_in_progress) {
3870 error = got_error(GOT_ERR_NOT_REBASING);
3873 error = got_worktree_rebase_continue(&resume_commit_id,
3874 &new_base_branch, &tmp_branch, &branch, &fileindex,
3878 printf("Switching work tree to %s\n",
3879 got_ref_get_symref_target(new_base_branch));
3880 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3881 new_base_branch, update_progress, &did_something);
3884 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3885 goto done; /* nothing else to do */
3888 if (continue_rebase) {
3889 if (!rebase_in_progress) {
3890 error = got_error(GOT_ERR_NOT_REBASING);
3893 error = got_worktree_rebase_continue(&resume_commit_id,
3894 &new_base_branch, &tmp_branch, &branch, &fileindex,
3899 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3900 resume_commit_id, repo);
3904 yca_id = got_object_id_dup(resume_commit_id);
3905 if (yca_id == NULL) {
3906 error = got_error_from_errno("got_object_id_dup");
3910 error = got_ref_open(&branch, repo, argv[0], 0);
3914 error = check_same_branch(
3915 got_worktree_get_base_commit_id(worktree), branch, repo);
3917 if (error->code != GOT_ERR_ANCESTRY)
3921 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3922 "specified branch resolves to a commit which "
3923 "is already contained in work tree's branch");
3928 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3932 if (!continue_rebase) {
3933 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3934 got_worktree_get_base_commit_id(worktree),
3935 branch_head_commit_id, repo);
3938 if (yca_id == NULL) {
3939 error = got_error_msg(GOT_ERR_ANCESTRY,
3940 "specified branch shares no common ancestry "
3941 "with work tree's branch");
3945 error = got_worktree_rebase_prepare(&new_base_branch,
3946 &tmp_branch, &fileindex, worktree, branch, repo);
3951 commit_id = branch_head_commit_id;
3952 error = got_object_open_as_commit(&commit, repo, commit_id);
3956 parent_ids = got_object_commit_get_parent_ids(commit);
3957 pid = SIMPLEQ_FIRST(parent_ids);
3958 error = collect_commits(&commits, commit_id, pid->id,
3959 yca_id, got_worktree_get_path_prefix(worktree),
3960 GOT_ERR_REBASE_PATH, repo);
3961 got_object_commit_close(commit);
3966 if (SIMPLEQ_EMPTY(&commits)) {
3967 if (continue_rebase)
3968 error = rebase_complete(worktree, fileindex,
3969 branch, new_base_branch, tmp_branch, repo);
3971 error = got_error(GOT_ERR_EMPTY_REBASE);
3976 SIMPLEQ_FOREACH(qid, &commits, entry) {
3977 commit_id = qid->id;
3978 parent_id = pid ? pid->id : yca_id;
3981 error = got_worktree_rebase_merge_files(&merged_paths,
3982 worktree, fileindex, parent_id, commit_id, repo,
3983 rebase_progress, &rebase_status, check_cancelled, NULL);
3987 if (rebase_status == GOT_STATUS_CONFLICT) {
3988 got_worktree_rebase_pathlist_free(&merged_paths);
3992 error = rebase_commit(&merged_paths, worktree, fileindex,
3993 tmp_branch, commit_id, repo);
3994 got_worktree_rebase_pathlist_free(&merged_paths);
3999 if (rebase_status == GOT_STATUS_CONFLICT) {
4000 error = got_worktree_rebase_postpone(worktree, fileindex);
4003 error = got_error_msg(GOT_ERR_CONFLICTS,
4004 "conflicts must be resolved before rebasing can continue");
4006 error = rebase_complete(worktree, fileindex, branch,
4007 new_base_branch, tmp_branch, repo);
4009 got_object_id_queue_free(&commits);
4010 free(branch_head_commit_id);
4011 free(resume_commit_id);
4014 got_object_commit_close(commit);
4016 got_ref_close(branch);
4017 if (new_base_branch)
4018 got_ref_close(new_base_branch);
4020 got_ref_close(tmp_branch);
4022 got_worktree_close(worktree);
4024 got_repo_close(repo);
4029 usage_histedit(void)
4031 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4036 #define GOT_HISTEDIT_PICK 'p'
4037 #define GOT_HISTEDIT_EDIT 'e'
4038 #define GOT_HISTEDIT_FOLD 'f'
4039 #define GOT_HISTEDIT_DROP 'd'
4040 #define GOT_HISTEDIT_MESG 'm'
4042 static struct got_histedit_cmd {
4046 } got_histedit_cmds[] = {
4047 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4048 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4049 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4050 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4051 { GOT_HISTEDIT_MESG, "mesg",
4052 "single-line log message for commit above (open editor if empty)" },
4055 struct got_histedit_list_entry {
4056 TAILQ_ENTRY(got_histedit_list_entry) entry;
4057 struct got_object_id *commit_id;
4058 const struct got_histedit_cmd *cmd;
4061 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4063 static const struct got_error *
4064 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4065 FILE *f, struct got_repository *repo)
4067 const struct got_error *err = NULL;
4068 char *logmsg = NULL, *id_str = NULL;
4069 struct got_commit_object *commit = NULL;
4072 err = got_object_open_as_commit(&commit, repo, commit_id);
4076 err = get_short_logmsg(&logmsg, 34, commit);
4080 err = got_object_id_str(&id_str, commit_id);
4084 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4086 err = got_ferror(f, GOT_ERR_IO);
4089 got_object_commit_close(commit);
4095 static const struct got_error *
4096 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4097 struct got_repository *repo)
4099 const struct got_error *err = NULL;
4100 struct got_object_qid *qid;
4102 if (SIMPLEQ_EMPTY(commits))
4103 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4105 SIMPLEQ_FOREACH(qid, commits, entry) {
4106 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4115 static const struct got_error *
4116 write_cmd_list(FILE *f)
4118 const struct got_error *err = NULL;
4121 n = fprintf(f, "# Available histedit commands:\n");
4123 return got_ferror(f, GOT_ERR_IO);
4125 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4126 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4127 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4130 err = got_ferror(f, GOT_ERR_IO);
4134 n = fprintf(f, "# Commits will be processed in order from top to "
4135 "bottom of this file.\n");
4137 return got_ferror(f, GOT_ERR_IO);
4141 static const struct got_error *
4142 histedit_syntax_error(int lineno)
4144 static char msg[42];
4147 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4149 if (ret == -1 || ret >= sizeof(msg))
4150 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4152 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4155 static const struct got_error *
4156 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4157 char *logmsg, struct got_repository *repo)
4159 const struct got_error *err;
4160 struct got_commit_object *folded_commit = NULL;
4163 err = got_object_id_str(&id_str, hle->commit_id);
4167 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4171 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4172 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4173 got_object_commit_get_logmsg(folded_commit)) == -1) {
4174 err = got_error_from_errno("asprintf");
4179 got_object_commit_close(folded_commit);
4184 static struct got_histedit_list_entry *
4185 get_folded_commits(struct got_histedit_list_entry *hle)
4187 struct got_histedit_list_entry *prev, *folded = NULL;
4189 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4190 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4191 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4192 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4194 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4200 static const struct got_error *
4201 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4202 struct got_repository *repo)
4204 char *logmsg_path = NULL, *id_str = NULL;
4205 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4206 const struct got_error *err = NULL;
4207 struct got_commit_object *commit = NULL;
4209 struct got_histedit_list_entry *folded = NULL;
4211 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4215 folded = get_folded_commits(hle);
4217 while (folded != hle) {
4218 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4219 folded = TAILQ_NEXT(folded, entry);
4222 err = append_folded_commit_msg(&new_msg, folded,
4228 folded = TAILQ_NEXT(folded, entry);
4232 err = got_object_id_str(&id_str, hle->commit_id);
4235 if (asprintf(&new_msg,
4236 "%s\n# original log message of commit %s: %s",
4237 logmsg ? logmsg : "", id_str,
4238 got_object_commit_get_logmsg(commit)) == -1) {
4239 err = got_error_from_errno("asprintf");
4245 err = got_object_id_str(&id_str, hle->commit_id);
4249 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4253 dprintf(fd, logmsg);
4256 err = get_editor(&editor);
4260 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4262 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4265 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4266 if (hle->logmsg == NULL)
4267 err = got_error_from_errno("strdup");
4270 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4271 err = got_error_from_errno2("unlink", logmsg_path);
4276 got_object_commit_close(commit);
4280 static const struct got_error *
4281 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4282 FILE *f, struct got_repository *repo)
4284 const struct got_error *err = NULL;
4285 char *line = NULL, *p, *end;
4289 const struct got_histedit_cmd *cmd;
4290 struct got_object_id *commit_id = NULL;
4291 struct got_histedit_list_entry *hle = NULL;
4294 len = getline(&line, &size, f);
4296 const struct got_error *getline_err;
4299 getline_err = got_error_from_errno("getline");
4300 err = got_ferror(f, getline_err->code);
4305 while (isspace((unsigned char)p[0]))
4307 if (p[0] == '#' || p[0] == '\0') {
4313 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4314 cmd = &got_histedit_cmds[i];
4315 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4316 isspace((unsigned char)p[strlen(cmd->name)])) {
4317 p += strlen(cmd->name);
4320 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4325 if (i == nitems(got_histedit_cmds)) {
4326 err = histedit_syntax_error(lineno);
4329 while (isspace((unsigned char)p[0]))
4331 if (cmd->code == GOT_HISTEDIT_MESG) {
4332 if (hle == NULL || hle->logmsg != NULL) {
4333 err = got_error(GOT_ERR_HISTEDIT_CMD);
4337 err = histedit_edit_logmsg(hle, repo);
4341 hle->logmsg = strdup(p);
4342 if (hle->logmsg == NULL) {
4343 err = got_error_from_errno("strdup");
4352 while (end[0] && !isspace((unsigned char)end[0]))
4356 err = got_object_resolve_id_str(&commit_id, repo, p);
4358 /* override error code */
4359 err = histedit_syntax_error(lineno);
4363 hle = malloc(sizeof(*hle));
4365 err = got_error_from_errno("malloc");
4369 hle->commit_id = commit_id;
4374 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4382 static const struct got_error *
4383 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4384 const char *path, struct got_repository *repo)
4386 const struct got_error *err = NULL;
4390 err = get_editor(&editor);
4394 if (spawn_editor(editor, path) == -1) {
4395 err = got_error_from_errno("failed spawning editor");
4399 f = fopen(path, "r");
4401 err = got_error_from_errno("fopen");
4404 err = histedit_parse_list(histedit_cmds, f, repo);
4406 if (f && fclose(f) != 0 && err == NULL)
4407 err = got_error_from_errno("fclose");
4412 static const struct got_error *
4413 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4414 struct got_object_id_queue *, const char *, struct got_repository *);
4416 static const struct got_error *
4417 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4418 struct got_object_id_queue *commits, struct got_repository *repo)
4420 const struct got_error *err;
4424 err = got_opentemp_named(&path, &f, "got-histedit");
4428 err = write_cmd_list(f);
4432 err = histedit_write_commit_list(commits, f, repo);
4436 if (fclose(f) != 0) {
4437 err = got_error_from_errno("fclose");
4442 err = histedit_run_editor(histedit_cmds, path, repo);
4444 const char *errmsg = err->msg;
4445 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4447 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4448 commits, path, repo);
4451 if (f && fclose(f) != 0 && err == NULL)
4452 err = got_error_from_errno("fclose");
4453 if (path && unlink(path) != 0 && err == NULL)
4454 err = got_error_from_errno2("unlink", path);
4459 static const struct got_error *
4460 histedit_save_list(struct got_histedit_list *histedit_cmds,
4461 struct got_worktree *worktree, struct got_repository *repo)
4463 const struct got_error *err = NULL;
4466 struct got_histedit_list_entry *hle;
4467 struct got_commit_object *commit = NULL;
4469 err = got_worktree_get_histedit_list_path(&path, worktree);
4473 f = fopen(path, "w");
4475 err = got_error_from_errno2("fopen", path);
4478 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4479 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4485 int n = fprintf(f, "%c %s\n",
4486 GOT_HISTEDIT_MESG, hle->logmsg);
4488 err = got_ferror(f, GOT_ERR_IO);
4494 if (f && fclose(f) != 0 && err == NULL)
4495 err = got_error_from_errno("fclose");
4498 got_object_commit_close(commit);
4502 static const struct got_error *
4503 histedit_load_list(struct got_histedit_list *histedit_cmds,
4504 const char *path, struct got_repository *repo)
4506 const struct got_error *err = NULL;
4509 f = fopen(path, "r");
4511 err = got_error_from_errno2("fopen", path);
4515 err = histedit_parse_list(histedit_cmds, f, repo);
4517 if (f && fclose(f) != 0 && err == NULL)
4518 err = got_error_from_errno("fclose");
4522 static const struct got_error *
4523 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4524 const char *errmsg, struct got_object_id_queue *commits,
4525 const char *path, struct got_repository *repo)
4527 const struct got_error *err = NULL;
4530 while (resp != 'c' && resp != 'r' && resp != 'a') {
4531 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4532 "or (a)bort: ", getprogname(), errmsg);
4535 err = histedit_run_editor(histedit_cmds, path, repo);
4537 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4542 } else if (resp == 'r') {
4543 err = histedit_edit_script(histedit_cmds,
4546 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4551 } else if (resp == 'a') {
4552 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4555 printf("invalid response '%c'\n", resp);
4561 static const struct got_error *
4562 histedit_complete(struct got_worktree *worktree,
4563 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4564 struct got_reference *branch, struct got_repository *repo)
4566 printf("Switching work tree to %s\n",
4567 got_ref_get_symref_target(branch));
4568 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4572 static const struct got_error *
4573 show_histedit_progress(struct got_commit_object *commit,
4574 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4576 const struct got_error *err;
4577 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4579 err = got_object_id_str(&old_id_str, hle->commit_id);
4584 err = got_object_id_str(&new_id_str, new_id);
4589 old_id_str[12] = '\0';
4591 new_id_str[12] = '\0';
4594 logmsg = strdup(hle->logmsg);
4595 if (logmsg == NULL) {
4596 err = got_error_from_errno("strdup");
4599 trim_logmsg(logmsg, 42);
4601 err = get_short_logmsg(&logmsg, 42, commit);
4606 switch (hle->cmd->code) {
4607 case GOT_HISTEDIT_PICK:
4608 case GOT_HISTEDIT_EDIT:
4609 printf("%s -> %s: %s\n", old_id_str,
4610 new_id_str ? new_id_str : "no-op change", logmsg);
4612 case GOT_HISTEDIT_DROP:
4613 case GOT_HISTEDIT_FOLD:
4614 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4627 static const struct got_error *
4628 histedit_commit(struct got_pathlist_head *merged_paths,
4629 struct got_worktree *worktree, struct got_fileindex *fileindex,
4630 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4631 struct got_repository *repo)
4633 const struct got_error *err;
4634 struct got_commit_object *commit;
4635 struct got_object_id *new_commit_id;
4637 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4638 && hle->logmsg == NULL) {
4639 err = histedit_edit_logmsg(hle, repo);
4644 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4648 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4649 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4652 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4654 err = show_histedit_progress(commit, hle, NULL);
4656 err = show_histedit_progress(commit, hle, new_commit_id);
4657 free(new_commit_id);
4660 got_object_commit_close(commit);
4664 static const struct got_error *
4665 histedit_skip_commit(struct got_histedit_list_entry *hle,
4666 struct got_worktree *worktree, struct got_repository *repo)
4668 const struct got_error *error;
4669 struct got_commit_object *commit;
4671 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4676 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4680 error = show_histedit_progress(commit, hle, NULL);
4681 got_object_commit_close(commit);
4685 static const struct got_error *
4686 histedit_check_script(struct got_histedit_list *histedit_cmds,
4687 struct got_object_id_queue *commits, struct got_repository *repo)
4689 const struct got_error *err = NULL;
4690 struct got_object_qid *qid;
4691 struct got_histedit_list_entry *hle;
4692 static char msg[80];
4695 if (TAILQ_EMPTY(histedit_cmds))
4696 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4697 "histedit script contains no commands");
4699 SIMPLEQ_FOREACH(qid, commits, entry) {
4700 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4701 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4705 err = got_object_id_str(&id_str, qid->id);
4708 snprintf(msg, sizeof(msg),
4709 "commit %s missing from histedit script", id_str);
4711 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4715 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4716 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4717 "last commit in histedit script cannot be folded");
4722 static const struct got_error *
4723 cmd_histedit(int argc, char *argv[])
4725 const struct got_error *error = NULL;
4726 struct got_worktree *worktree = NULL;
4727 struct got_fileindex *fileindex = NULL;
4728 struct got_repository *repo = NULL;
4730 struct got_reference *branch = NULL;
4731 struct got_reference *tmp_branch = NULL;
4732 struct got_object_id *resume_commit_id = NULL;
4733 struct got_object_id *base_commit_id = NULL;
4734 struct got_object_id *head_commit_id = NULL;
4735 struct got_commit_object *commit = NULL;
4736 int ch, rebase_in_progress = 0;
4737 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4738 const char *edit_script_path = NULL;
4739 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4740 struct got_object_id_queue commits;
4741 struct got_pathlist_head merged_paths;
4742 const struct got_object_id_queue *parent_ids;
4743 struct got_object_qid *pid;
4744 struct got_histedit_list histedit_cmds;
4745 struct got_histedit_list_entry *hle;
4747 SIMPLEQ_INIT(&commits);
4748 TAILQ_INIT(&histedit_cmds);
4749 TAILQ_INIT(&merged_paths);
4751 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4760 edit_script_path = optarg;
4772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4773 "unveil", NULL) == -1)
4776 if (abort_edit && continue_edit)
4782 * This command cannot apply unveil(2) in all cases because the
4783 * user may choose to run an editor to edit the histedit script
4784 * and to edit individual commit log messages.
4785 * unveil(2) traverses exec(2); if an editor is used we have to
4786 * apply unveil after edit script and log messages have been written.
4787 * XXX TODO: Make use of unveil(2) where possible.
4790 cwd = getcwd(NULL, 0);
4792 error = got_error_from_errno("getcwd");
4795 error = got_worktree_open(&worktree, cwd);
4799 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4803 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4806 if (rebase_in_progress) {
4807 error = got_error(GOT_ERR_REBASING);
4811 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4815 if (edit_in_progress && abort_edit) {
4817 error = got_worktree_histedit_continue(&resume_commit_id,
4818 &tmp_branch, &branch, &base_commit_id, &fileindex,
4822 printf("Switching work tree to %s\n",
4823 got_ref_get_symref_target(branch));
4824 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4825 branch, base_commit_id, update_progress, &did_something);
4828 printf("Histedit of %s aborted\n",
4829 got_ref_get_symref_target(branch));
4830 goto done; /* nothing else to do */
4831 } else if (abort_edit) {
4832 error = got_error(GOT_ERR_NOT_HISTEDIT);
4836 if (continue_edit) {
4839 if (!edit_in_progress) {
4840 error = got_error(GOT_ERR_NOT_HISTEDIT);
4844 error = got_worktree_get_histedit_list_path(&path, worktree);
4848 error = histedit_load_list(&histedit_cmds, path, repo);
4853 error = got_worktree_histedit_continue(&resume_commit_id,
4854 &tmp_branch, &branch, &base_commit_id, &fileindex,
4859 error = got_ref_resolve(&head_commit_id, repo, branch);
4863 error = got_object_open_as_commit(&commit, repo,
4867 parent_ids = got_object_commit_get_parent_ids(commit);
4868 pid = SIMPLEQ_FIRST(parent_ids);
4870 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4873 error = collect_commits(&commits, head_commit_id, pid->id,
4874 base_commit_id, got_worktree_get_path_prefix(worktree),
4875 GOT_ERR_HISTEDIT_PATH, repo);
4876 got_object_commit_close(commit);
4881 if (edit_in_progress) {
4882 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4886 error = got_ref_open(&branch, repo,
4887 got_worktree_get_head_ref_name(worktree), 0);
4891 error = got_ref_resolve(&head_commit_id, repo, branch);
4895 error = got_object_open_as_commit(&commit, repo,
4899 parent_ids = got_object_commit_get_parent_ids(commit);
4900 pid = SIMPLEQ_FIRST(parent_ids);
4902 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4905 error = collect_commits(&commits, head_commit_id, pid->id,
4906 got_worktree_get_base_commit_id(worktree),
4907 got_worktree_get_path_prefix(worktree),
4908 GOT_ERR_HISTEDIT_PATH, repo);
4909 got_object_commit_close(commit);
4914 if (edit_script_path) {
4915 error = histedit_load_list(&histedit_cmds,
4916 edit_script_path, repo);
4920 error = histedit_edit_script(&histedit_cmds, &commits,
4927 error = histedit_save_list(&histedit_cmds, worktree,
4932 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4933 &base_commit_id, &fileindex, worktree, repo);
4939 error = histedit_check_script(&histedit_cmds, &commits, repo);
4943 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4944 if (resume_commit_id) {
4945 if (got_object_id_cmp(hle->commit_id,
4946 resume_commit_id) != 0)
4949 resume_commit_id = NULL;
4950 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4951 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4952 error = histedit_skip_commit(hle, worktree,
4955 error = histedit_commit(NULL, worktree,
4956 fileindex, tmp_branch, hle, repo);
4963 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4964 error = histedit_skip_commit(hle, worktree, repo);
4970 error = got_object_open_as_commit(&commit, repo,
4974 parent_ids = got_object_commit_get_parent_ids(commit);
4975 pid = SIMPLEQ_FIRST(parent_ids);
4977 error = got_worktree_histedit_merge_files(&merged_paths,
4978 worktree, fileindex, pid->id, hle->commit_id, repo,
4979 rebase_progress, &rebase_status, check_cancelled, NULL);
4982 got_object_commit_close(commit);
4985 if (rebase_status == GOT_STATUS_CONFLICT) {
4986 got_worktree_rebase_pathlist_free(&merged_paths);
4990 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4992 error = got_object_id_str(&id_str, hle->commit_id);
4995 printf("Stopping histedit for amending commit %s\n",
4998 got_worktree_rebase_pathlist_free(&merged_paths);
4999 error = got_worktree_histedit_postpone(worktree,
5004 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5005 error = histedit_skip_commit(hle, worktree, repo);
5011 error = histedit_commit(&merged_paths, worktree, fileindex,
5012 tmp_branch, hle, repo);
5013 got_worktree_rebase_pathlist_free(&merged_paths);
5018 if (rebase_status == GOT_STATUS_CONFLICT) {
5019 error = got_worktree_histedit_postpone(worktree, fileindex);
5022 error = got_error_msg(GOT_ERR_CONFLICTS,
5023 "conflicts must be resolved before rebasing can continue");
5025 error = histedit_complete(worktree, fileindex, tmp_branch,
5028 got_object_id_queue_free(&commits);
5029 free(head_commit_id);
5030 free(base_commit_id);
5031 free(resume_commit_id);
5033 got_object_commit_close(commit);
5035 got_ref_close(branch);
5037 got_ref_close(tmp_branch);
5039 got_worktree_close(worktree);
5041 got_repo_close(repo);