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 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1048 char *argv[], struct got_worktree *worktree)
1050 const struct got_error *err;
1057 return got_error_from_errno("strdup");
1058 return got_pathlist_append(NULL, paths, path, NULL);
1061 for (i = 0; i < argc; i++) {
1062 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1065 err = got_pathlist_append(NULL, paths, path, NULL);
1075 static const struct got_error *
1076 cmd_update(int argc, char *argv[])
1078 const struct got_error *error = NULL;
1079 struct got_repository *repo = NULL;
1080 struct got_worktree *worktree = NULL;
1081 char *worktree_path = NULL, *path = NULL;
1082 struct got_object_id *commit_id = NULL;
1083 char *commit_id_str = NULL;
1084 const char *branch_name = NULL;
1085 struct got_reference *head_ref = NULL;
1086 int ch, did_something = 0;
1088 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1091 branch_name = optarg;
1094 commit_id_str = strdup(optarg);
1095 if (commit_id_str == NULL)
1096 return got_error_from_errno("strdup");
1108 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1109 "unveil", NULL) == -1)
1112 worktree_path = getcwd(NULL, 0);
1113 if (worktree_path == NULL) {
1114 error = got_error_from_errno("getcwd");
1117 error = got_worktree_open(&worktree, worktree_path);
1121 error = check_rebase_or_histedit_in_progress(worktree);
1128 error = got_error_from_errno("strdup");
1131 } else if (argc == 1) {
1132 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1138 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1142 error = apply_unveil(got_repo_get_path(repo), 0,
1143 got_worktree_get_root_path(worktree));
1147 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1148 got_worktree_get_head_ref_name(worktree), 0);
1151 if (commit_id_str == NULL) {
1152 error = got_ref_resolve(&commit_id, repo, head_ref);
1155 error = got_object_id_str(&commit_id_str, commit_id);
1159 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1160 free(commit_id_str);
1161 commit_id_str = NULL;
1164 error = got_object_id_str(&commit_id_str, commit_id);
1170 struct got_object_id *head_commit_id;
1171 if (strlen(path) != 0) {
1172 fprintf(stderr, "%s: switching between branches "
1173 "requires that the entire work tree "
1174 "gets updated, not just '%s'\n",
1175 getprogname(), path);
1176 error = got_error(GOT_ERR_BAD_PATH);
1179 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1182 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1183 free(head_commit_id);
1186 error = check_same_branch(commit_id, head_ref, repo);
1189 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1193 error = check_linear_ancestry(commit_id,
1194 got_worktree_get_base_commit_id(worktree), repo);
1195 if (error != NULL) {
1196 if (error->code == GOT_ERR_ANCESTRY)
1197 error = got_error(GOT_ERR_BRANCH_MOVED);
1200 error = check_same_branch(commit_id, head_ref, repo);
1205 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1207 error = got_worktree_set_base_commit_id(worktree, repo,
1213 error = got_worktree_checkout_files(worktree, path, repo,
1214 update_progress, &did_something, check_cancelled, NULL);
1219 printf("Updated to commit %s\n", commit_id_str);
1221 printf("Already up-to-date\n");
1223 free(worktree_path);
1226 free(commit_id_str);
1230 static const struct got_error *
1231 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1232 int diff_context, struct got_repository *repo)
1234 const struct got_error *err = NULL;
1235 struct got_tree_object *tree1 = NULL, *tree2;
1236 struct got_object_qid *qid;
1237 char *id_str1 = NULL, *id_str2;
1238 struct got_diff_blob_output_unidiff_arg arg;
1240 err = got_object_open_as_tree(&tree2, repo,
1241 got_object_commit_get_tree_id(commit));
1245 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1247 struct got_commit_object *pcommit;
1249 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1253 err = got_object_open_as_tree(&tree1, repo,
1254 got_object_commit_get_tree_id(pcommit));
1255 got_object_commit_close(pcommit);
1259 err = got_object_id_str(&id_str1, qid->id);
1264 err = got_object_id_str(&id_str2, id);
1268 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1269 arg.diff_context = diff_context;
1270 arg.outfile = stdout;
1271 err = got_diff_tree(tree1, tree2, "", "", repo,
1272 got_diff_blob_output_unidiff, &arg);
1275 got_object_tree_close(tree1);
1276 got_object_tree_close(tree2);
1283 get_datestr(time_t *time, char *datebuf)
1285 char *p, *s = ctime_r(time, datebuf);
1286 p = strchr(s, '\n');
1292 static const struct got_error *
1293 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1294 struct got_repository *repo, int show_patch, int diff_context,
1295 struct got_reflist_head *refs)
1297 const struct got_error *err = NULL;
1298 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1300 time_t committer_time;
1301 const char *author, *committer;
1302 char *refs_str = NULL;
1303 struct got_reflist_entry *re;
1305 SIMPLEQ_FOREACH(re, refs, entry) {
1308 if (got_object_id_cmp(re->id, id) != 0)
1310 name = got_ref_get_name(re->ref);
1311 if (strcmp(name, GOT_REF_HEAD) == 0)
1313 if (strncmp(name, "refs/", 5) == 0)
1315 if (strncmp(name, "got/", 4) == 0)
1317 if (strncmp(name, "heads/", 6) == 0)
1319 if (strncmp(name, "remotes/", 8) == 0)
1322 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1324 err = got_error_from_errno("asprintf");
1330 err = got_object_id_str(&id_str, id);
1334 printf("-----------------------------------------------\n");
1335 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1336 refs_str ? refs_str : "", refs_str ? ")" : "");
1341 printf("from: %s\n", got_object_commit_get_author(commit));
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 datestr = get_datestr(&committer_time, datebuf);
1344 printf("date: %s UTC\n", datestr);
1345 author = got_object_commit_get_author(commit);
1346 committer = got_object_commit_get_committer(commit);
1347 if (strcmp(author, committer) != 0)
1348 printf("via: %s\n", committer);
1349 if (got_object_commit_get_nparents(commit) > 1) {
1350 const struct got_object_id_queue *parent_ids;
1351 struct got_object_qid *qid;
1353 parent_ids = got_object_commit_get_parent_ids(commit);
1354 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1355 err = got_object_id_str(&id_str, qid->id);
1358 printf("parent %d: %s\n", n++, id_str);
1363 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1364 if (logmsg0 == NULL)
1365 return got_error_from_errno("strdup");
1369 line = strsep(&logmsg, "\n");
1371 printf(" %s\n", line);
1376 err = print_patch(commit, id, diff_context, repo);
1381 if (fflush(stdout) != 0 && err == NULL)
1382 err = got_error_from_errno("fflush");
1386 static const struct got_error *
1387 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1388 char *path, int show_patch, int diff_context, int limit,
1389 int first_parent_traversal, struct got_reflist_head *refs)
1391 const struct got_error *err;
1392 struct got_commit_graph *graph;
1394 err = got_commit_graph_open(&graph, root_id, path,
1395 first_parent_traversal, repo);
1398 err = got_commit_graph_iter_start(graph, root_id, repo);
1402 struct got_commit_object *commit;
1403 struct got_object_id *id;
1405 if (sigint_received || sigpipe_received)
1408 err = got_commit_graph_iter_next(&id, graph);
1410 if (err->code == GOT_ERR_ITER_COMPLETED) {
1414 if (err->code != GOT_ERR_ITER_NEED_MORE)
1416 err = got_commit_graph_fetch_commits(graph, 1, repo);
1425 err = got_object_open_as_commit(&commit, repo, id);
1428 err = print_commit(commit, id, repo, show_patch, diff_context,
1430 got_object_commit_close(commit);
1431 if (err || (limit && --limit == 0))
1435 got_commit_graph_close(graph);
1442 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1443 "[-r repository-path] [path]\n", getprogname());
1447 static const struct got_error *
1448 cmd_log(int argc, char *argv[])
1450 const struct got_error *error;
1451 struct got_repository *repo = NULL;
1452 struct got_worktree *worktree = NULL;
1453 struct got_commit_object *commit = NULL;
1454 struct got_object_id *id = NULL;
1455 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1456 char *start_commit = NULL;
1457 int diff_context = 3, ch;
1458 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1460 struct got_reflist_head refs;
1462 SIMPLEQ_INIT(&refs);
1465 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1471 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1477 start_commit = optarg;
1480 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1483 err(1, "-C option %s", errstr);
1486 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1488 err(1, "-l option %s", errstr);
1491 first_parent_traversal = 1;
1494 repo_path = realpath(optarg, NULL);
1495 if (repo_path == NULL)
1496 err(1, "-r option");
1497 got_path_strip_trailing_slashes(repo_path);
1508 cwd = getcwd(NULL, 0);
1510 error = got_error_from_errno("getcwd");
1514 error = got_worktree_open(&worktree, cwd);
1515 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1522 error = got_error_from_errno("strdup");
1525 } else if (argc == 1) {
1527 error = got_worktree_resolve_path(&path, worktree,
1532 path = strdup(argv[0]);
1534 error = got_error_from_errno("strdup");
1541 if (repo_path == NULL) {
1542 repo_path = worktree ?
1543 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1545 if (repo_path == NULL) {
1546 error = got_error_from_errno("strdup");
1550 error = got_repo_open(&repo, repo_path);
1554 error = apply_unveil(got_repo_get_path(repo), 1,
1555 worktree ? got_worktree_get_root_path(worktree) : NULL);
1559 if (start_commit == NULL) {
1560 struct got_reference *head_ref;
1561 error = got_ref_open(&head_ref, repo,
1562 worktree ? got_worktree_get_head_ref_name(worktree)
1566 error = got_ref_resolve(&id, repo, head_ref);
1567 got_ref_close(head_ref);
1570 error = got_object_open_as_commit(&commit, repo, id);
1572 struct got_reference *ref;
1573 error = got_ref_open(&ref, repo, start_commit, 0);
1574 if (error == NULL) {
1576 error = got_ref_resolve(&id, repo, ref);
1580 error = got_object_get_type(&obj_type, repo, id);
1583 if (obj_type == GOT_OBJ_TYPE_TAG) {
1584 struct got_tag_object *tag;
1585 error = got_object_open_as_tag(&tag, repo, id);
1588 if (got_object_tag_get_object_type(tag) !=
1589 GOT_OBJ_TYPE_COMMIT) {
1590 got_object_tag_close(tag);
1591 error = got_error(GOT_ERR_OBJ_TYPE);
1595 id = got_object_id_dup(
1596 got_object_tag_get_object_id(tag));
1598 error = got_error_from_errno(
1599 "got_object_id_dup");
1600 got_object_tag_close(tag);
1603 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1604 error = got_error(GOT_ERR_OBJ_TYPE);
1607 error = got_object_open_as_commit(&commit, repo, id);
1611 if (commit == NULL) {
1612 error = got_repo_match_object_id_prefix(&id,
1613 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1621 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1626 path = in_repo_path;
1629 error = got_ref_list(&refs, repo);
1633 error = print_commits(id, repo, path, show_patch,
1634 diff_context, limit, first_parent_traversal, &refs);
1641 got_worktree_close(worktree);
1643 const struct got_error *repo_error;
1644 repo_error = got_repo_close(repo);
1648 got_ref_list_free(&refs);
1655 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1656 "[object1 object2 | path]\n", getprogname());
1660 struct print_diff_arg {
1661 struct got_repository *repo;
1662 struct got_worktree *worktree;
1668 static const struct got_error *
1669 print_diff(void *arg, unsigned char status, const char *path,
1670 struct got_object_id *blob_id, struct got_object_id *commit_id)
1672 struct print_diff_arg *a = arg;
1673 const struct got_error *err = NULL;
1674 struct got_blob_object *blob1 = NULL;
1676 char *abspath = NULL;
1679 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1680 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1683 if (!a->header_shown) {
1684 printf("diff %s %s\n", a->id_str,
1685 got_worktree_get_root_path(a->worktree));
1686 a->header_shown = 1;
1689 if (status != GOT_STATUS_ADD) {
1690 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1696 if (status != GOT_STATUS_DELETE) {
1697 if (asprintf(&abspath, "%s/%s",
1698 got_worktree_get_root_path(a->worktree), path) == -1) {
1699 err = got_error_from_errno("asprintf");
1703 f2 = fopen(abspath, "r");
1705 err = got_error_from_errno2("fopen", abspath);
1708 if (lstat(abspath, &sb) == -1) {
1709 err = got_error_from_errno2("lstat", abspath);
1715 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1719 got_object_blob_close(blob1);
1720 if (f2 && fclose(f2) != 0 && err == NULL)
1721 err = got_error_from_errno("fclose");
1726 static const struct got_error *
1727 cmd_diff(int argc, char *argv[])
1729 const struct got_error *error;
1730 struct got_repository *repo = NULL;
1731 struct got_worktree *worktree = NULL;
1732 char *cwd = NULL, *repo_path = NULL;
1733 struct got_object_id *id1 = NULL, *id2 = NULL;
1734 const char *id_str1 = NULL, *id_str2 = NULL;
1735 char *label1 = NULL, *label2 = NULL;
1737 int diff_context = 3, ch;
1742 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1747 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1750 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1752 err(1, "-C option %s", errstr);
1755 repo_path = realpath(optarg, NULL);
1756 if (repo_path == NULL)
1757 err(1, "-r option");
1758 got_path_strip_trailing_slashes(repo_path);
1769 cwd = getcwd(NULL, 0);
1771 error = got_error_from_errno("getcwd");
1774 error = got_worktree_open(&worktree, cwd);
1775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1778 if (worktree == NULL) {
1779 error = got_error(GOT_ERR_NOT_WORKTREE);
1784 "-r option can't be used when diffing a work tree");
1785 repo_path = strdup(got_worktree_get_repo_path(worktree));
1786 if (repo_path == NULL) {
1787 error = got_error_from_errno("strdup");
1791 error = got_worktree_resolve_path(&path, worktree,
1798 error = got_error_from_errno("strdup");
1802 } else if (argc == 2) {
1805 if (worktree && repo_path == NULL) {
1807 strdup(got_worktree_get_repo_path(worktree));
1808 if (repo_path == NULL) {
1809 error = got_error_from_errno("strdup");
1816 if (repo_path == NULL) {
1817 repo_path = getcwd(NULL, 0);
1818 if (repo_path == NULL)
1819 return got_error_from_errno("getcwd");
1822 error = got_repo_open(&repo, repo_path);
1827 error = apply_unveil(got_repo_get_path(repo), 1,
1828 worktree ? got_worktree_get_root_path(worktree) : NULL);
1833 struct print_diff_arg arg;
1834 struct got_pathlist_head paths;
1839 error = got_object_id_str(&id_str,
1840 got_worktree_get_base_commit_id(worktree));
1844 arg.worktree = worktree;
1845 arg.diff_context = diff_context;
1846 arg.id_str = id_str;
1847 arg.header_shown = 0;
1849 error = got_pathlist_append(NULL, &paths, path, NULL);
1853 error = got_worktree_status(worktree, &paths, repo, print_diff,
1854 &arg, check_cancelled, NULL);
1856 got_pathlist_free(&paths);
1860 error = got_repo_match_object_id_prefix(&id1, id_str1,
1861 GOT_OBJ_TYPE_ANY, repo);
1863 struct got_reference *ref;
1864 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1866 error = got_ref_open(&ref, repo, id_str1, 0);
1869 label1 = strdup(got_ref_get_name(ref));
1870 if (label1 == NULL) {
1871 error = got_error_from_errno("strdup");
1874 error = got_ref_resolve(&id1, repo, ref);
1879 error = got_object_id_str(&label1, id1);
1880 if (label1 == NULL) {
1881 error = got_error_from_errno("strdup");
1886 error = got_repo_match_object_id_prefix(&id2, id_str2,
1887 GOT_OBJ_TYPE_ANY, repo);
1889 struct got_reference *ref;
1890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1892 error = got_ref_open(&ref, repo, id_str2, 0);
1895 label2 = strdup(got_ref_get_name(ref));
1896 if (label2 == NULL) {
1897 error = got_error_from_errno("strdup");
1900 error = got_ref_resolve(&id2, repo, ref);
1905 error = got_object_id_str(&label2, id2);
1906 if (label2 == NULL) {
1907 error = got_error_from_errno("strdup");
1912 error = got_object_get_type(&type1, repo, id1);
1916 error = got_object_get_type(&type2, repo, id2);
1920 if (type1 != type2) {
1921 error = got_error(GOT_ERR_OBJ_TYPE);
1926 case GOT_OBJ_TYPE_BLOB:
1927 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1928 diff_context, repo, stdout);
1930 case GOT_OBJ_TYPE_TREE:
1931 error = got_diff_objects_as_trees(id1, id2, "", "",
1932 diff_context, repo, stdout);
1934 case GOT_OBJ_TYPE_COMMIT:
1935 printf("diff %s %s\n", label1, label2);
1936 error = got_diff_objects_as_commits(id1, id2, diff_context,
1940 error = got_error(GOT_ERR_OBJ_TYPE);
1950 got_worktree_close(worktree);
1952 const struct got_error *repo_error;
1953 repo_error = got_repo_close(repo);
1964 "usage: %s blame [-c commit] [-r repository-path] path\n",
1969 static const struct got_error *
1970 cmd_blame(int argc, char *argv[])
1972 const struct got_error *error;
1973 struct got_repository *repo = NULL;
1974 struct got_worktree *worktree = NULL;
1975 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1976 struct got_object_id *commit_id = NULL;
1977 char *commit_id_str = NULL;
1981 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1986 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1989 commit_id_str = optarg;
1992 repo_path = realpath(optarg, NULL);
1993 if (repo_path == NULL)
1994 err(1, "-r option");
1995 got_path_strip_trailing_slashes(repo_path);
2011 cwd = getcwd(NULL, 0);
2013 error = got_error_from_errno("getcwd");
2016 if (repo_path == NULL) {
2017 error = got_worktree_open(&worktree, cwd);
2018 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2024 strdup(got_worktree_get_repo_path(worktree));
2025 if (repo_path == NULL)
2026 error = got_error_from_errno("strdup");
2030 repo_path = strdup(cwd);
2031 if (repo_path == NULL) {
2032 error = got_error_from_errno("strdup");
2038 error = got_repo_open(&repo, repo_path);
2042 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2047 const char *prefix = got_worktree_get_path_prefix(worktree);
2048 char *p, *worktree_subdir = cwd +
2049 strlen(got_worktree_get_root_path(worktree));
2050 if (asprintf(&p, "%s%s%s%s%s",
2051 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2052 worktree_subdir, worktree_subdir[0] ? "/" : "",
2054 error = got_error_from_errno("asprintf");
2057 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2060 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2065 if (commit_id_str == NULL) {
2066 struct got_reference *head_ref;
2067 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2070 error = got_ref_resolve(&commit_id, repo, head_ref);
2071 got_ref_close(head_ref);
2075 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2080 error = got_blame(in_repo_path, commit_id, repo, stdout);
2087 got_worktree_close(worktree);
2089 const struct got_error *repo_error;
2090 repo_error = got_repo_close(repo);
2101 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2107 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2108 const char *root_path)
2110 int is_root_path = (strcmp(path, root_path) == 0);
2112 path += strlen(root_path);
2113 while (path[0] == '/')
2116 printf("%s%s%s%s%s\n", id ? id : "", path,
2117 is_root_path ? "" : "/", te->name,
2118 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2121 static const struct got_error *
2122 print_tree(const char *path, struct got_object_id *commit_id,
2123 int show_ids, int recurse, const char *root_path,
2124 struct got_repository *repo)
2126 const struct got_error *err = NULL;
2127 struct got_object_id *tree_id = NULL;
2128 struct got_tree_object *tree = NULL;
2129 const struct got_tree_entries *entries;
2130 struct got_tree_entry *te;
2132 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2136 err = got_object_open_as_tree(&tree, repo, tree_id);
2139 entries = got_object_tree_get_entries(tree);
2140 te = SIMPLEQ_FIRST(&entries->head);
2144 if (sigint_received || sigpipe_received)
2149 err = got_object_id_str(&id_str, te->id);
2152 if (asprintf(&id, "%s ", id_str) == -1) {
2153 err = got_error_from_errno("asprintf");
2159 print_entry(te, id, path, root_path);
2162 if (recurse && S_ISDIR(te->mode)) {
2164 if (asprintf(&child_path, "%s%s%s", path,
2165 path[0] == '/' && path[1] == '\0' ? "" : "/",
2167 err = got_error_from_errno("asprintf");
2170 err = print_tree(child_path, commit_id, show_ids, 1,
2177 te = SIMPLEQ_NEXT(te, entry);
2181 got_object_tree_close(tree);
2186 static const struct got_error *
2187 cmd_tree(int argc, char *argv[])
2189 const struct got_error *error;
2190 struct got_repository *repo = NULL;
2191 struct got_worktree *worktree = NULL;
2193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2194 struct got_object_id *commit_id = NULL;
2195 char *commit_id_str = NULL;
2196 int show_ids = 0, recurse = 0;
2200 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2205 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2208 commit_id_str = optarg;
2211 repo_path = realpath(optarg, NULL);
2212 if (repo_path == NULL)
2213 err(1, "-r option");
2214 got_path_strip_trailing_slashes(repo_path);
2238 cwd = getcwd(NULL, 0);
2240 error = got_error_from_errno("getcwd");
2243 if (repo_path == NULL) {
2244 error = got_worktree_open(&worktree, cwd);
2245 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2251 strdup(got_worktree_get_repo_path(worktree));
2252 if (repo_path == NULL)
2253 error = got_error_from_errno("strdup");
2257 repo_path = strdup(cwd);
2258 if (repo_path == NULL) {
2259 error = got_error_from_errno("strdup");
2265 error = got_repo_open(&repo, repo_path);
2269 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2275 char *p, *worktree_subdir = cwd +
2276 strlen(got_worktree_get_root_path(worktree));
2277 if (asprintf(&p, "%s/%s",
2278 got_worktree_get_path_prefix(worktree),
2279 worktree_subdir) == -1) {
2280 error = got_error_from_errno("asprintf");
2283 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2290 if (in_repo_path == NULL) {
2291 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2296 if (commit_id_str == NULL) {
2297 struct got_reference *head_ref;
2298 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2301 error = got_ref_resolve(&commit_id, repo, head_ref);
2302 got_ref_close(head_ref);
2306 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2311 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2312 in_repo_path, repo);
2319 got_worktree_close(worktree);
2321 const struct got_error *repo_error;
2322 repo_error = got_repo_close(repo);
2332 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2336 static const struct got_error *
2337 print_status(void *arg, unsigned char status, const char *path,
2338 struct got_object_id *blob_id, struct got_object_id *commit_id)
2340 printf("%c %s\n", status, path);
2344 static const struct got_error *
2345 cmd_status(int argc, char *argv[])
2347 const struct got_error *error = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_worktree *worktree = NULL;
2351 struct got_pathlist_head paths;
2352 struct got_pathlist_entry *pe;
2357 while ((ch = getopt(argc, argv, "")) != -1) {
2369 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2373 cwd = getcwd(NULL, 0);
2375 error = got_error_from_errno("getcwd");
2379 error = got_worktree_open(&worktree, cwd);
2383 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2387 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2391 error = apply_unveil(got_repo_get_path(repo), 1,
2392 got_worktree_get_root_path(worktree));
2396 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2397 check_cancelled, NULL);
2399 TAILQ_FOREACH(pe, &paths, entry)
2400 free((char *)pe->path);
2401 got_pathlist_free(&paths);
2410 "usage: %s ref [-r repository] -l | -d name | name target\n",
2415 static const struct got_error *
2416 list_refs(struct got_repository *repo)
2418 static const struct got_error *err = NULL;
2419 struct got_reflist_head refs;
2420 struct got_reflist_entry *re;
2422 SIMPLEQ_INIT(&refs);
2423 err = got_ref_list(&refs, repo);
2427 SIMPLEQ_FOREACH(re, &refs, entry) {
2429 refstr = got_ref_to_str(re->ref);
2431 return got_error_from_errno("got_ref_to_str");
2432 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2436 got_ref_list_free(&refs);
2440 static const struct got_error *
2441 delete_ref(struct got_repository *repo, const char *refname)
2443 const struct got_error *err = NULL;
2444 struct got_reference *ref;
2446 err = got_ref_open(&ref, repo, refname, 0);
2450 err = got_ref_delete(ref, repo);
2455 static const struct got_error *
2456 add_ref(struct got_repository *repo, const char *refname, const char *target)
2458 const struct got_error *err = NULL;
2459 struct got_object_id *id;
2460 struct got_reference *ref = NULL;
2463 * Don't let the user create a reference named '-'.
2464 * While technically a valid reference name, this case is usually
2465 * an unintended typo.
2467 if (refname[0] == '-' && refname[1] == '\0')
2468 return got_error(GOT_ERR_BAD_REF_NAME);
2470 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2473 struct got_reference *target_ref;
2475 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2477 err = got_ref_open(&target_ref, repo, target, 0);
2480 err = got_ref_resolve(&id, repo, target_ref);
2481 got_ref_close(target_ref);
2486 err = got_ref_alloc(&ref, refname, id);
2490 err = got_ref_write(ref, repo);
2498 static const struct got_error *
2499 cmd_ref(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 char *cwd = NULL, *repo_path = NULL;
2505 int ch, do_list = 0;
2506 const char *delref = NULL;
2508 /* TODO: Add -s option for adding symbolic references. */
2509 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2515 repo_path = realpath(optarg, NULL);
2516 if (repo_path == NULL)
2517 err(1, "-r option");
2518 got_path_strip_trailing_slashes(repo_path);
2529 if (do_list && delref)
2530 errx(1, "-l and -d options are mutually exclusive\n");
2535 if (do_list || delref) {
2538 } else if (argc != 2)
2543 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2547 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2548 "sendfd unveil", NULL) == -1)
2552 cwd = getcwd(NULL, 0);
2554 error = got_error_from_errno("getcwd");
2558 if (repo_path == NULL) {
2559 error = got_worktree_open(&worktree, cwd);
2560 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2566 strdup(got_worktree_get_repo_path(worktree));
2567 if (repo_path == NULL)
2568 error = got_error_from_errno("strdup");
2572 repo_path = strdup(cwd);
2573 if (repo_path == NULL) {
2574 error = got_error_from_errno("strdup");
2580 error = got_repo_open(&repo, repo_path);
2584 error = apply_unveil(got_repo_get_path(repo), do_list,
2585 worktree ? got_worktree_get_root_path(worktree) : NULL);
2590 error = list_refs(repo);
2592 error = delete_ref(repo, delref);
2594 error = add_ref(repo, argv[0], argv[1]);
2597 got_repo_close(repo);
2599 got_worktree_close(worktree);
2609 "usage: %s branch [-r repository] -l | -d name | "
2610 "name [base-branch]\n", getprogname());
2614 static const struct got_error *
2615 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2617 static const struct got_error *err = NULL;
2618 struct got_reflist_head refs;
2619 struct got_reflist_entry *re;
2621 SIMPLEQ_INIT(&refs);
2623 err = got_ref_list(&refs, repo);
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 const char *refname, *marker = " ";
2630 refname = got_ref_get_name(re->ref);
2631 if (strncmp(refname, "refs/heads/", 11) != 0)
2633 if (worktree && strcmp(refname,
2634 got_worktree_get_head_ref_name(worktree)) == 0) {
2635 struct got_object_id *id = NULL;
2636 err = got_ref_resolve(&id, repo, re->ref);
2639 if (got_object_id_cmp(id,
2640 got_worktree_get_base_commit_id(worktree)) == 0)
2647 refstr = got_ref_to_str(re->ref);
2649 return got_error_from_errno("got_ref_to_str");
2650 printf("%s%s: %s\n", marker, refname, refstr);
2654 got_ref_list_free(&refs);
2658 static const struct got_error *
2659 delete_branch(struct got_repository *repo, const char *branch_name)
2661 const struct got_error *err = NULL;
2662 struct got_reference *ref;
2665 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2666 return got_error_from_errno("asprintf");
2668 err = got_ref_open(&ref, repo, refname, 0);
2672 err = got_ref_delete(ref, repo);
2679 static const struct got_error *
2680 add_branch(struct got_repository *repo, const char *branch_name,
2681 const char *base_branch)
2683 const struct got_error *err = NULL;
2684 struct got_object_id *id = NULL;
2685 struct got_reference *ref = NULL;
2686 char *base_refname = NULL, *refname = NULL;
2687 struct got_reference *base_ref;
2690 * Don't let the user create a branch named '-'.
2691 * While technically a valid reference name, this case is usually
2692 * an unintended typo.
2694 if (branch_name[0] == '-' && branch_name[1] == '\0')
2695 return got_error(GOT_ERR_BAD_REF_NAME);
2697 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2698 base_refname = strdup(GOT_REF_HEAD);
2699 if (base_refname == NULL)
2700 return got_error_from_errno("strdup");
2701 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2702 return got_error_from_errno("asprintf");
2704 err = got_ref_open(&base_ref, repo, base_refname, 0);
2707 err = got_ref_resolve(&id, repo, base_ref);
2708 got_ref_close(base_ref);
2712 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2713 err = got_error_from_errno("asprintf");
2717 err = got_ref_open(&ref, repo, refname, 0);
2719 err = got_error(GOT_ERR_BRANCH_EXISTS);
2721 } else if (err->code != GOT_ERR_NOT_REF)
2724 err = got_ref_alloc(&ref, refname, id);
2728 err = got_ref_write(ref, repo);
2738 static const struct got_error *
2739 cmd_branch(int argc, char *argv[])
2741 const struct got_error *error = NULL;
2742 struct got_repository *repo = NULL;
2743 struct got_worktree *worktree = NULL;
2744 char *cwd = NULL, *repo_path = NULL;
2745 int ch, do_list = 0;
2746 const char *delref = NULL;
2748 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2754 repo_path = realpath(optarg, NULL);
2755 if (repo_path == NULL)
2756 err(1, "-r option");
2757 got_path_strip_trailing_slashes(repo_path);
2768 if (do_list && delref)
2769 errx(1, "-l and -d options are mutually exclusive\n");
2774 if (do_list || delref) {
2777 } else if (argc < 1 || argc > 2)
2782 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2786 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2787 "sendfd unveil", NULL) == -1)
2791 cwd = getcwd(NULL, 0);
2793 error = got_error_from_errno("getcwd");
2797 if (repo_path == NULL) {
2798 error = got_worktree_open(&worktree, cwd);
2799 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2805 strdup(got_worktree_get_repo_path(worktree));
2806 if (repo_path == NULL)
2807 error = got_error_from_errno("strdup");
2811 repo_path = strdup(cwd);
2812 if (repo_path == NULL) {
2813 error = got_error_from_errno("strdup");
2819 error = got_repo_open(&repo, repo_path);
2823 error = apply_unveil(got_repo_get_path(repo), do_list,
2824 worktree ? got_worktree_get_root_path(worktree) : NULL);
2829 error = list_branches(repo, worktree);
2831 error = delete_branch(repo, delref);
2833 const char *base_branch;
2835 base_branch = worktree ?
2836 got_worktree_get_head_ref_name(worktree) :
2839 base_branch = argv[1];
2840 error = add_branch(repo, argv[0], base_branch);
2844 got_repo_close(repo);
2846 got_worktree_close(worktree);
2855 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2859 static const struct got_error *
2860 cmd_add(int argc, char *argv[])
2862 const struct got_error *error = NULL;
2863 struct got_repository *repo = NULL;
2864 struct got_worktree *worktree = NULL;
2866 struct got_pathlist_head paths;
2867 struct got_pathlist_entry *pe;
2872 while ((ch = getopt(argc, argv, "")) != -1) {
2884 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2891 cwd = getcwd(NULL, 0);
2893 error = got_error_from_errno("getcwd");
2897 error = got_worktree_open(&worktree, cwd);
2901 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2905 error = apply_unveil(got_repo_get_path(repo), 1,
2906 got_worktree_get_root_path(worktree));
2910 for (x = 0; x < argc; x++) {
2911 char *path = realpath(argv[x], NULL);
2913 error = got_error_from_errno2("realpath", argv[x]);
2917 got_path_strip_trailing_slashes(path);
2918 error = got_pathlist_insert(&pe, &paths, path, NULL);
2924 error = got_worktree_schedule_add(worktree, &paths, print_status,
2928 got_repo_close(repo);
2930 got_worktree_close(worktree);
2931 TAILQ_FOREACH(pe, &paths, entry)
2932 free((char *)pe->path);
2933 got_pathlist_free(&paths);
2941 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2945 static const struct got_error *
2946 cmd_remove(int argc, char *argv[])
2948 const struct got_error *error = NULL;
2949 struct got_worktree *worktree = NULL;
2950 struct got_repository *repo = NULL;
2952 struct got_pathlist_head paths;
2953 struct got_pathlist_entry *pe;
2954 int ch, i, delete_local_mods = 0;
2958 while ((ch = getopt(argc, argv, "f")) != -1) {
2961 delete_local_mods = 1;
2973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2980 cwd = getcwd(NULL, 0);
2982 error = got_error_from_errno("getcwd");
2985 error = got_worktree_open(&worktree, cwd);
2989 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2993 error = apply_unveil(got_repo_get_path(repo), 1,
2994 got_worktree_get_root_path(worktree));
2998 for (i = 0; i < argc; i++) {
2999 char *path = realpath(argv[i], NULL);
3001 error = got_error_from_errno2("realpath", argv[i]);
3005 got_path_strip_trailing_slashes(path);
3006 error = got_pathlist_insert(&pe, &paths, path, NULL);
3012 error = got_worktree_schedule_delete(worktree, &paths,
3013 delete_local_mods, print_status, NULL, repo);
3018 got_repo_close(repo);
3020 got_worktree_close(worktree);
3021 TAILQ_FOREACH(pe, &paths, entry)
3022 free((char *)pe->path);
3023 got_pathlist_free(&paths);
3031 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3035 static const struct got_error *
3036 revert_progress(void *arg, unsigned char status, const char *path)
3038 while (path[0] == '/')
3040 printf("%c %s\n", status, path);
3044 static const struct got_error *
3045 cmd_revert(int argc, char *argv[])
3047 const struct got_error *error = NULL;
3048 struct got_worktree *worktree = NULL;
3049 struct got_repository *repo = NULL;
3050 char *cwd = NULL, *path = NULL;
3051 struct got_pathlist_head paths;
3052 struct got_pathlist_entry *pe;
3057 while ((ch = getopt(argc, argv, "")) != -1) {
3069 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3070 "unveil", NULL) == -1)
3076 for (i = 0; i < argc; i++) {
3077 char *path = realpath(argv[i], NULL);
3079 error = got_error_from_errno2("realpath", argv[i]);
3083 got_path_strip_trailing_slashes(path);
3084 error = got_pathlist_insert(&pe, &paths, path, NULL);
3091 cwd = getcwd(NULL, 0);
3093 error = got_error_from_errno("getcwd");
3096 error = got_worktree_open(&worktree, cwd);
3100 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3104 error = apply_unveil(got_repo_get_path(repo), 1,
3105 got_worktree_get_root_path(worktree));
3109 error = got_worktree_revert(worktree, &paths,
3110 revert_progress, NULL, repo);
3115 got_repo_close(repo);
3117 got_worktree_close(worktree);
3126 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3130 struct collect_commit_logmsg_arg {
3131 const char *cmdline_log;
3133 const char *worktree_path;
3134 const char *branch_name;
3135 const char *repo_path;
3140 static const struct got_error *
3141 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3144 char *initial_content = NULL;
3145 struct got_pathlist_entry *pe;
3146 const struct got_error *err = NULL;
3147 char *template = NULL;
3148 struct collect_commit_logmsg_arg *a = arg;
3152 /* if a message was specified on the command line, just use it */
3153 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3154 len = strlen(a->cmdline_log) + 1;
3155 *logmsg = malloc(len + 1);
3156 if (*logmsg == NULL)
3157 return got_error_from_errno("malloc");
3158 strlcpy(*logmsg, a->cmdline_log, len);
3162 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3163 return got_error_from_errno("asprintf");
3165 if (asprintf(&initial_content,
3166 "\n# changes to be committed on branch %s:\n",
3167 a->branch_name) == -1)
3168 return got_error_from_errno("asprintf");
3170 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3174 dprintf(fd, initial_content);
3176 TAILQ_FOREACH(pe, commitable_paths, entry) {
3177 struct got_commitable *ct = pe->data;
3178 dprintf(fd, "# %c %s\n",
3179 got_commitable_get_status(ct),
3180 got_commitable_get_path(ct));
3184 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3186 unlink(a->logmsg_path);
3187 free(a->logmsg_path);
3188 free(initial_content);
3191 /* Editor is done; we can now apply unveil(2) */
3193 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3202 static const struct got_error *
3203 cmd_commit(int argc, char *argv[])
3205 const struct got_error *error = NULL;
3206 struct got_worktree *worktree = NULL;
3207 struct got_repository *repo = NULL;
3208 char *cwd = NULL, *path = NULL, *id_str = NULL;
3209 struct got_object_id *id = NULL;
3210 const char *logmsg = NULL;
3211 const char *got_author = getenv("GOT_AUTHOR");
3212 struct collect_commit_logmsg_arg cl_arg;
3213 char *editor = NULL;
3214 int ch, rebase_in_progress;
3216 while ((ch = getopt(argc, argv, "m:")) != -1) {
3231 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3232 "unveil", NULL) == -1)
3236 path = realpath(argv[0], NULL);
3238 error = got_error_from_errno2("realpath", argv[0]);
3241 got_path_strip_trailing_slashes(path);
3242 } else if (argc != 0)
3245 if (got_author == NULL) {
3246 /* TODO: Look current user up in password database */
3247 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3251 cwd = getcwd(NULL, 0);
3253 error = got_error_from_errno("getcwd");
3256 error = got_worktree_open(&worktree, cwd);
3260 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3263 if (rebase_in_progress) {
3264 error = got_error(GOT_ERR_REBASING);
3268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3273 * unveil(2) traverses exec(2); if an editor is used we have
3274 * to apply unveil after the log message has been written.
3276 if (logmsg == NULL || strlen(logmsg) == 0)
3277 error = get_editor(&editor);
3279 error = apply_unveil(got_repo_get_path(repo), 0,
3280 got_worktree_get_root_path(worktree));
3284 cl_arg.editor = editor;
3285 cl_arg.cmdline_log = logmsg;
3286 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3287 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3288 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3289 cl_arg.branch_name += 5;
3290 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3291 cl_arg.branch_name += 6;
3292 cl_arg.repo_path = got_repo_get_path(repo);
3293 cl_arg.logmsg_path = NULL;
3294 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3295 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3297 if (cl_arg.logmsg_path)
3298 fprintf(stderr, "%s: log message preserved in %s\n",
3299 getprogname(), cl_arg.logmsg_path);
3303 if (cl_arg.logmsg_path)
3304 unlink(cl_arg.logmsg_path);
3306 error = got_object_id_str(&id_str, id);
3309 printf("Created commit %s\n", id_str);
3312 got_repo_close(repo);
3314 got_worktree_close(worktree);
3323 usage_cherrypick(void)
3325 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3329 static const struct got_error *
3330 cmd_cherrypick(int argc, char *argv[])
3332 const struct got_error *error = NULL;
3333 struct got_worktree *worktree = NULL;
3334 struct got_repository *repo = NULL;
3335 char *cwd = NULL, *commit_id_str = NULL;
3336 struct got_object_id *commit_id = NULL;
3337 struct got_commit_object *commit = NULL;
3338 struct got_object_qid *pid;
3339 struct got_reference *head_ref = NULL;
3340 int ch, did_something = 0;
3342 while ((ch = getopt(argc, argv, "")) != -1) {
3354 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3355 "unveil", NULL) == -1)
3361 cwd = getcwd(NULL, 0);
3363 error = got_error_from_errno("getcwd");
3366 error = got_worktree_open(&worktree, cwd);
3370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3374 error = apply_unveil(got_repo_get_path(repo), 0,
3375 got_worktree_get_root_path(worktree));
3379 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3380 GOT_OBJ_TYPE_COMMIT, repo);
3381 if (error != NULL) {
3382 struct got_reference *ref;
3383 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3385 error = got_ref_open(&ref, repo, argv[0], 0);
3388 error = got_ref_resolve(&commit_id, repo, ref);
3393 error = got_object_id_str(&commit_id_str, commit_id);
3397 error = got_ref_open(&head_ref, repo,
3398 got_worktree_get_head_ref_name(worktree), 0);
3402 error = check_same_branch(commit_id, head_ref, repo);
3404 if (error->code != GOT_ERR_ANCESTRY)
3408 error = got_error(GOT_ERR_SAME_BRANCH);
3412 error = got_object_open_as_commit(&commit, repo, commit_id);
3415 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3416 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3417 commit_id, repo, update_progress, &did_something, check_cancelled,
3423 printf("Merged commit %s\n", commit_id_str);
3426 got_object_commit_close(commit);
3427 free(commit_id_str);
3429 got_ref_close(head_ref);
3431 got_worktree_close(worktree);
3433 got_repo_close(repo);
3440 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3444 static const struct got_error *
3445 cmd_backout(int argc, char *argv[])
3447 const struct got_error *error = NULL;
3448 struct got_worktree *worktree = NULL;
3449 struct got_repository *repo = NULL;
3450 char *cwd = NULL, *commit_id_str = NULL;
3451 struct got_object_id *commit_id = NULL;
3452 struct got_commit_object *commit = NULL;
3453 struct got_object_qid *pid;
3454 struct got_reference *head_ref = NULL;
3455 int ch, did_something = 0;
3457 while ((ch = getopt(argc, argv, "")) != -1) {
3469 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3470 "unveil", NULL) == -1)
3476 cwd = getcwd(NULL, 0);
3478 error = got_error_from_errno("getcwd");
3481 error = got_worktree_open(&worktree, cwd);
3485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3489 error = apply_unveil(got_repo_get_path(repo), 0,
3490 got_worktree_get_root_path(worktree));
3494 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3495 GOT_OBJ_TYPE_COMMIT, repo);
3496 if (error != NULL) {
3497 struct got_reference *ref;
3498 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3500 error = got_ref_open(&ref, repo, argv[0], 0);
3503 error = got_ref_resolve(&commit_id, repo, ref);
3508 error = got_object_id_str(&commit_id_str, commit_id);
3512 error = got_ref_open(&head_ref, repo,
3513 got_worktree_get_head_ref_name(worktree), 0);
3517 error = check_same_branch(commit_id, head_ref, repo);
3521 error = got_object_open_as_commit(&commit, repo, commit_id);
3524 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3526 error = got_error(GOT_ERR_ROOT_COMMIT);
3530 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3531 update_progress, &did_something, check_cancelled, NULL);
3536 printf("Backed out commit %s\n", commit_id_str);
3539 got_object_commit_close(commit);
3540 free(commit_id_str);
3542 got_ref_close(head_ref);
3544 got_worktree_close(worktree);
3546 got_repo_close(repo);
3553 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3559 trim_logmsg(char *logmsg, int limit)
3564 len = strlen(logmsg);
3568 nl = strchr(logmsg, '\n');
3573 static const struct got_error *
3574 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3576 const char *logmsg0 = NULL;
3578 logmsg0 = got_object_commit_get_logmsg(commit);
3580 while (isspace((unsigned char)logmsg0[0]))
3583 *logmsg = strdup(logmsg0);
3584 if (*logmsg == NULL)
3585 return got_error_from_errno("strdup");
3587 trim_logmsg(*logmsg, limit);
3591 static const struct got_error *
3592 show_rebase_progress(struct got_commit_object *commit,
3593 struct got_object_id *old_id, struct got_object_id *new_id)
3595 const struct got_error *err;
3596 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3598 err = got_object_id_str(&old_id_str, old_id);
3603 err = got_object_id_str(&new_id_str, new_id);
3608 old_id_str[12] = '\0';
3610 new_id_str[12] = '\0';
3612 err = get_short_logmsg(&logmsg, 42, commit);
3616 printf("%s -> %s: %s\n", old_id_str,
3617 new_id_str ? new_id_str : "no-op change", logmsg);
3624 static const struct got_error *
3625 rebase_progress(void *arg, unsigned char status, const char *path)
3627 unsigned char *rebase_status = arg;
3629 while (path[0] == '/')
3631 printf("%c %s\n", status, path);
3633 if (*rebase_status == GOT_STATUS_CONFLICT)
3635 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3636 *rebase_status = status;
3640 static const struct got_error *
3641 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3642 struct got_reference *branch, struct got_reference *new_base_branch,
3643 struct got_reference *tmp_branch, struct got_repository *repo)
3645 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3646 return got_worktree_rebase_complete(worktree, fileindex,
3647 new_base_branch, tmp_branch, branch, repo);
3650 static const struct got_error *
3651 rebase_commit(struct got_pathlist_head *merged_paths,
3652 struct got_worktree *worktree, struct got_fileindex *fileindex,
3653 struct got_reference *tmp_branch,
3654 struct got_object_id *commit_id, struct got_repository *repo)
3656 const struct got_error *error;
3657 struct got_commit_object *commit;
3658 struct got_object_id *new_commit_id;
3660 error = got_object_open_as_commit(&commit, repo, commit_id);
3664 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3665 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3667 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3669 error = show_rebase_progress(commit, commit_id, NULL);
3671 error = show_rebase_progress(commit, commit_id, new_commit_id);
3672 free(new_commit_id);
3675 got_object_commit_close(commit);
3679 struct check_path_prefix_arg {
3680 const char *path_prefix;
3685 static const struct got_error *
3686 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3687 struct got_blob_object *blob2, struct got_object_id *id1,
3688 struct got_object_id *id2, const char *path1, const char *path2,
3689 struct got_repository *repo)
3691 struct check_path_prefix_arg *a = arg;
3693 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3694 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3695 return got_error(a->errcode);
3700 static const struct got_error *
3701 check_path_prefix(struct got_object_id *parent_id,
3702 struct got_object_id *commit_id, const char *path_prefix,
3703 int errcode, struct got_repository *repo)
3705 const struct got_error *err;
3706 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3707 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3708 struct check_path_prefix_arg cpp_arg;
3710 if (got_path_is_root_dir(path_prefix))
3713 err = got_object_open_as_commit(&commit, repo, commit_id);
3717 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3721 err = got_object_open_as_tree(&tree1, repo,
3722 got_object_commit_get_tree_id(parent_commit));
3726 err = got_object_open_as_tree(&tree2, repo,
3727 got_object_commit_get_tree_id(commit));
3731 cpp_arg.path_prefix = path_prefix;
3732 while (cpp_arg.path_prefix[0] == '/')
3733 cpp_arg.path_prefix++;
3734 cpp_arg.len = strlen(cpp_arg.path_prefix);
3735 cpp_arg.errcode = errcode;
3736 err = got_diff_tree(tree1, tree2, "", "", repo,
3737 check_path_prefix_in_diff, &cpp_arg);
3740 got_object_tree_close(tree1);
3742 got_object_tree_close(tree2);
3744 got_object_commit_close(commit);
3746 got_object_commit_close(parent_commit);
3750 static const struct got_error *
3751 collect_commits(struct got_object_id_queue *commits,
3752 struct got_object_id *initial_commit_id,
3753 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3754 const char *path_prefix, int path_prefix_errcode,
3755 struct got_repository *repo)
3757 const struct got_error *err = NULL;
3758 struct got_commit_graph *graph = NULL;
3759 struct got_object_id *parent_id = NULL;
3760 struct got_object_qid *qid;
3761 struct got_object_id *commit_id = initial_commit_id;
3763 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3767 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3770 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3771 err = got_commit_graph_iter_next(&parent_id, graph);
3773 if (err->code == GOT_ERR_ITER_COMPLETED) {
3774 err = got_error_msg(GOT_ERR_ANCESTRY,
3775 "ran out of commits to rebase before "
3776 "youngest common ancestor commit has "
3779 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3781 err = got_commit_graph_fetch_commits(graph, 1, repo);
3785 err = check_path_prefix(parent_id, commit_id,
3786 path_prefix, path_prefix_errcode, repo);
3790 err = got_object_qid_alloc(&qid, commit_id);
3793 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3794 commit_id = parent_id;
3798 got_commit_graph_close(graph);
3802 static const struct got_error *
3803 cmd_rebase(int argc, char *argv[])
3805 const struct got_error *error = NULL;
3806 struct got_worktree *worktree = NULL;
3807 struct got_repository *repo = NULL;
3808 struct got_fileindex *fileindex = NULL;
3810 struct got_reference *branch = NULL;
3811 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3812 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3813 struct got_object_id *resume_commit_id = NULL;
3814 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3815 struct got_commit_object *commit = NULL;
3816 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3817 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3818 struct got_object_id_queue commits;
3819 struct got_pathlist_head merged_paths;
3820 const struct got_object_id_queue *parent_ids;
3821 struct got_object_qid *qid, *pid;
3823 SIMPLEQ_INIT(&commits);
3824 TAILQ_INIT(&merged_paths);
3826 while ((ch = getopt(argc, argv, "ac")) != -1) {
3832 continue_rebase = 1;
3844 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3845 "unveil", NULL) == -1)
3848 if (abort_rebase && continue_rebase)
3850 else if (abort_rebase || continue_rebase) {
3853 } else if (argc != 1)
3856 cwd = getcwd(NULL, 0);
3858 error = got_error_from_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3869 error = apply_unveil(got_repo_get_path(repo), 0,
3870 got_worktree_get_root_path(worktree));
3874 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3880 if (!rebase_in_progress) {
3881 error = got_error(GOT_ERR_NOT_REBASING);
3884 error = got_worktree_rebase_continue(&resume_commit_id,
3885 &new_base_branch, &tmp_branch, &branch, &fileindex,
3889 printf("Switching work tree to %s\n",
3890 got_ref_get_symref_target(new_base_branch));
3891 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3892 new_base_branch, update_progress, &did_something);
3895 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3896 goto done; /* nothing else to do */
3899 if (continue_rebase) {
3900 if (!rebase_in_progress) {
3901 error = got_error(GOT_ERR_NOT_REBASING);
3904 error = got_worktree_rebase_continue(&resume_commit_id,
3905 &new_base_branch, &tmp_branch, &branch, &fileindex,
3910 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3911 resume_commit_id, repo);
3915 yca_id = got_object_id_dup(resume_commit_id);
3916 if (yca_id == NULL) {
3917 error = got_error_from_errno("got_object_id_dup");
3921 error = got_ref_open(&branch, repo, argv[0], 0);
3925 error = check_same_branch(
3926 got_worktree_get_base_commit_id(worktree), branch, repo);
3928 if (error->code != GOT_ERR_ANCESTRY)
3932 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3933 "specified branch resolves to a commit which "
3934 "is already contained in work tree's branch");
3939 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3943 if (!continue_rebase) {
3944 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3945 got_worktree_get_base_commit_id(worktree),
3946 branch_head_commit_id, repo);
3949 if (yca_id == NULL) {
3950 error = got_error_msg(GOT_ERR_ANCESTRY,
3951 "specified branch shares no common ancestry "
3952 "with work tree's branch");
3956 error = got_worktree_rebase_prepare(&new_base_branch,
3957 &tmp_branch, &fileindex, worktree, branch, repo);
3962 commit_id = branch_head_commit_id;
3963 error = got_object_open_as_commit(&commit, repo, commit_id);
3967 parent_ids = got_object_commit_get_parent_ids(commit);
3968 pid = SIMPLEQ_FIRST(parent_ids);
3969 error = collect_commits(&commits, commit_id, pid->id,
3970 yca_id, got_worktree_get_path_prefix(worktree),
3971 GOT_ERR_REBASE_PATH, repo);
3972 got_object_commit_close(commit);
3977 if (SIMPLEQ_EMPTY(&commits)) {
3978 if (continue_rebase)
3979 error = rebase_complete(worktree, fileindex,
3980 branch, new_base_branch, tmp_branch, repo);
3982 error = got_error(GOT_ERR_EMPTY_REBASE);
3987 SIMPLEQ_FOREACH(qid, &commits, entry) {
3988 commit_id = qid->id;
3989 parent_id = pid ? pid->id : yca_id;
3992 error = got_worktree_rebase_merge_files(&merged_paths,
3993 worktree, fileindex, parent_id, commit_id, repo,
3994 rebase_progress, &rebase_status, check_cancelled, NULL);
3998 if (rebase_status == GOT_STATUS_CONFLICT) {
3999 got_worktree_rebase_pathlist_free(&merged_paths);
4003 error = rebase_commit(&merged_paths, worktree, fileindex,
4004 tmp_branch, commit_id, repo);
4005 got_worktree_rebase_pathlist_free(&merged_paths);
4010 if (rebase_status == GOT_STATUS_CONFLICT) {
4011 error = got_worktree_rebase_postpone(worktree, fileindex);
4014 error = got_error_msg(GOT_ERR_CONFLICTS,
4015 "conflicts must be resolved before rebasing can continue");
4017 error = rebase_complete(worktree, fileindex, branch,
4018 new_base_branch, tmp_branch, repo);
4020 got_object_id_queue_free(&commits);
4021 free(branch_head_commit_id);
4022 free(resume_commit_id);
4025 got_object_commit_close(commit);
4027 got_ref_close(branch);
4028 if (new_base_branch)
4029 got_ref_close(new_base_branch);
4031 got_ref_close(tmp_branch);
4033 got_worktree_close(worktree);
4035 got_repo_close(repo);
4040 usage_histedit(void)
4042 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4047 #define GOT_HISTEDIT_PICK 'p'
4048 #define GOT_HISTEDIT_EDIT 'e'
4049 #define GOT_HISTEDIT_FOLD 'f'
4050 #define GOT_HISTEDIT_DROP 'd'
4051 #define GOT_HISTEDIT_MESG 'm'
4053 static struct got_histedit_cmd {
4057 } got_histedit_cmds[] = {
4058 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4059 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4060 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4061 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4062 { GOT_HISTEDIT_MESG, "mesg",
4063 "single-line log message for commit above (open editor if empty)" },
4066 struct got_histedit_list_entry {
4067 TAILQ_ENTRY(got_histedit_list_entry) entry;
4068 struct got_object_id *commit_id;
4069 const struct got_histedit_cmd *cmd;
4072 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4074 static const struct got_error *
4075 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4076 FILE *f, struct got_repository *repo)
4078 const struct got_error *err = NULL;
4079 char *logmsg = NULL, *id_str = NULL;
4080 struct got_commit_object *commit = NULL;
4083 err = got_object_open_as_commit(&commit, repo, commit_id);
4087 err = get_short_logmsg(&logmsg, 34, commit);
4091 err = got_object_id_str(&id_str, commit_id);
4095 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4097 err = got_ferror(f, GOT_ERR_IO);
4100 got_object_commit_close(commit);
4106 static const struct got_error *
4107 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4108 struct got_repository *repo)
4110 const struct got_error *err = NULL;
4111 struct got_object_qid *qid;
4113 if (SIMPLEQ_EMPTY(commits))
4114 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4116 SIMPLEQ_FOREACH(qid, commits, entry) {
4117 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4126 static const struct got_error *
4127 write_cmd_list(FILE *f)
4129 const struct got_error *err = NULL;
4132 n = fprintf(f, "# Available histedit commands:\n");
4134 return got_ferror(f, GOT_ERR_IO);
4136 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4137 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4138 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4141 err = got_ferror(f, GOT_ERR_IO);
4145 n = fprintf(f, "# Commits will be processed in order from top to "
4146 "bottom of this file.\n");
4148 return got_ferror(f, GOT_ERR_IO);
4152 static const struct got_error *
4153 histedit_syntax_error(int lineno)
4155 static char msg[42];
4158 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4160 if (ret == -1 || ret >= sizeof(msg))
4161 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4163 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4166 static const struct got_error *
4167 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4168 char *logmsg, struct got_repository *repo)
4170 const struct got_error *err;
4171 struct got_commit_object *folded_commit = NULL;
4174 err = got_object_id_str(&id_str, hle->commit_id);
4178 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4182 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4183 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4184 got_object_commit_get_logmsg(folded_commit)) == -1) {
4185 err = got_error_from_errno("asprintf");
4190 got_object_commit_close(folded_commit);
4195 static struct got_histedit_list_entry *
4196 get_folded_commits(struct got_histedit_list_entry *hle)
4198 struct got_histedit_list_entry *prev, *folded = NULL;
4200 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4201 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4202 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4203 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4205 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4211 static const struct got_error *
4212 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4213 struct got_repository *repo)
4215 char *logmsg_path = NULL, *id_str = NULL;
4216 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4217 const struct got_error *err = NULL;
4218 struct got_commit_object *commit = NULL;
4220 struct got_histedit_list_entry *folded = NULL;
4222 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4226 folded = get_folded_commits(hle);
4228 while (folded != hle) {
4229 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4230 folded = TAILQ_NEXT(folded, entry);
4233 err = append_folded_commit_msg(&new_msg, folded,
4239 folded = TAILQ_NEXT(folded, entry);
4243 err = got_object_id_str(&id_str, hle->commit_id);
4246 if (asprintf(&new_msg,
4247 "%s\n# original log message of commit %s: %s",
4248 logmsg ? logmsg : "", id_str,
4249 got_object_commit_get_logmsg(commit)) == -1) {
4250 err = got_error_from_errno("asprintf");
4256 err = got_object_id_str(&id_str, hle->commit_id);
4260 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4264 dprintf(fd, logmsg);
4267 err = get_editor(&editor);
4271 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4273 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4276 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4277 if (hle->logmsg == NULL)
4278 err = got_error_from_errno("strdup");
4281 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4282 err = got_error_from_errno2("unlink", logmsg_path);
4287 got_object_commit_close(commit);
4291 static const struct got_error *
4292 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4293 FILE *f, struct got_repository *repo)
4295 const struct got_error *err = NULL;
4296 char *line = NULL, *p, *end;
4300 const struct got_histedit_cmd *cmd;
4301 struct got_object_id *commit_id = NULL;
4302 struct got_histedit_list_entry *hle = NULL;
4305 len = getline(&line, &size, f);
4307 const struct got_error *getline_err;
4310 getline_err = got_error_from_errno("getline");
4311 err = got_ferror(f, getline_err->code);
4316 while (isspace((unsigned char)p[0]))
4318 if (p[0] == '#' || p[0] == '\0') {
4324 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4325 cmd = &got_histedit_cmds[i];
4326 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4327 isspace((unsigned char)p[strlen(cmd->name)])) {
4328 p += strlen(cmd->name);
4331 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4336 if (i == nitems(got_histedit_cmds)) {
4337 err = histedit_syntax_error(lineno);
4340 while (isspace((unsigned char)p[0]))
4342 if (cmd->code == GOT_HISTEDIT_MESG) {
4343 if (hle == NULL || hle->logmsg != NULL) {
4344 err = got_error(GOT_ERR_HISTEDIT_CMD);
4348 err = histedit_edit_logmsg(hle, repo);
4352 hle->logmsg = strdup(p);
4353 if (hle->logmsg == NULL) {
4354 err = got_error_from_errno("strdup");
4363 while (end[0] && !isspace((unsigned char)end[0]))
4367 err = got_object_resolve_id_str(&commit_id, repo, p);
4369 /* override error code */
4370 err = histedit_syntax_error(lineno);
4374 hle = malloc(sizeof(*hle));
4376 err = got_error_from_errno("malloc");
4380 hle->commit_id = commit_id;
4385 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4393 static const struct got_error *
4394 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4395 const char *path, struct got_repository *repo)
4397 const struct got_error *err = NULL;
4401 err = get_editor(&editor);
4405 if (spawn_editor(editor, path) == -1) {
4406 err = got_error_from_errno("failed spawning editor");
4410 f = fopen(path, "r");
4412 err = got_error_from_errno("fopen");
4415 err = histedit_parse_list(histedit_cmds, f, repo);
4417 if (f && fclose(f) != 0 && err == NULL)
4418 err = got_error_from_errno("fclose");
4423 static const struct got_error *
4424 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4425 struct got_object_id_queue *, const char *, struct got_repository *);
4427 static const struct got_error *
4428 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4429 struct got_object_id_queue *commits, struct got_repository *repo)
4431 const struct got_error *err;
4435 err = got_opentemp_named(&path, &f, "got-histedit");
4439 err = write_cmd_list(f);
4443 err = histedit_write_commit_list(commits, f, repo);
4447 if (fclose(f) != 0) {
4448 err = got_error_from_errno("fclose");
4453 err = histedit_run_editor(histedit_cmds, path, repo);
4455 const char *errmsg = err->msg;
4456 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4458 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4459 commits, path, repo);
4462 if (f && fclose(f) != 0 && err == NULL)
4463 err = got_error_from_errno("fclose");
4464 if (path && unlink(path) != 0 && err == NULL)
4465 err = got_error_from_errno2("unlink", path);
4470 static const struct got_error *
4471 histedit_save_list(struct got_histedit_list *histedit_cmds,
4472 struct got_worktree *worktree, struct got_repository *repo)
4474 const struct got_error *err = NULL;
4477 struct got_histedit_list_entry *hle;
4478 struct got_commit_object *commit = NULL;
4480 err = got_worktree_get_histedit_list_path(&path, worktree);
4484 f = fopen(path, "w");
4486 err = got_error_from_errno2("fopen", path);
4489 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4490 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4496 int n = fprintf(f, "%c %s\n",
4497 GOT_HISTEDIT_MESG, hle->logmsg);
4499 err = got_ferror(f, GOT_ERR_IO);
4505 if (f && fclose(f) != 0 && err == NULL)
4506 err = got_error_from_errno("fclose");
4509 got_object_commit_close(commit);
4513 static const struct got_error *
4514 histedit_load_list(struct got_histedit_list *histedit_cmds,
4515 const char *path, struct got_repository *repo)
4517 const struct got_error *err = NULL;
4520 f = fopen(path, "r");
4522 err = got_error_from_errno2("fopen", path);
4526 err = histedit_parse_list(histedit_cmds, f, repo);
4528 if (f && fclose(f) != 0 && err == NULL)
4529 err = got_error_from_errno("fclose");
4533 static const struct got_error *
4534 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4535 const char *errmsg, struct got_object_id_queue *commits,
4536 const char *path, struct got_repository *repo)
4538 const struct got_error *err = NULL;
4541 while (resp != 'c' && resp != 'r' && resp != 'a') {
4542 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4543 "or (a)bort: ", getprogname(), errmsg);
4546 err = histedit_run_editor(histedit_cmds, path, repo);
4548 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4553 } else if (resp == 'r') {
4554 err = histedit_edit_script(histedit_cmds,
4557 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4562 } else if (resp == 'a') {
4563 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4566 printf("invalid response '%c'\n", resp);
4572 static const struct got_error *
4573 histedit_complete(struct got_worktree *worktree,
4574 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4575 struct got_reference *branch, struct got_repository *repo)
4577 printf("Switching work tree to %s\n",
4578 got_ref_get_symref_target(branch));
4579 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4583 static const struct got_error *
4584 show_histedit_progress(struct got_commit_object *commit,
4585 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4587 const struct got_error *err;
4588 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4590 err = got_object_id_str(&old_id_str, hle->commit_id);
4595 err = got_object_id_str(&new_id_str, new_id);
4600 old_id_str[12] = '\0';
4602 new_id_str[12] = '\0';
4605 logmsg = strdup(hle->logmsg);
4606 if (logmsg == NULL) {
4607 err = got_error_from_errno("strdup");
4610 trim_logmsg(logmsg, 42);
4612 err = get_short_logmsg(&logmsg, 42, commit);
4617 switch (hle->cmd->code) {
4618 case GOT_HISTEDIT_PICK:
4619 case GOT_HISTEDIT_EDIT:
4620 printf("%s -> %s: %s\n", old_id_str,
4621 new_id_str ? new_id_str : "no-op change", logmsg);
4623 case GOT_HISTEDIT_DROP:
4624 case GOT_HISTEDIT_FOLD:
4625 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4638 static const struct got_error *
4639 histedit_commit(struct got_pathlist_head *merged_paths,
4640 struct got_worktree *worktree, struct got_fileindex *fileindex,
4641 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4642 struct got_repository *repo)
4644 const struct got_error *err;
4645 struct got_commit_object *commit;
4646 struct got_object_id *new_commit_id;
4648 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4649 && hle->logmsg == NULL) {
4650 err = histedit_edit_logmsg(hle, repo);
4655 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4659 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4660 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4663 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4665 err = show_histedit_progress(commit, hle, NULL);
4667 err = show_histedit_progress(commit, hle, new_commit_id);
4668 free(new_commit_id);
4671 got_object_commit_close(commit);
4675 static const struct got_error *
4676 histedit_skip_commit(struct got_histedit_list_entry *hle,
4677 struct got_worktree *worktree, struct got_repository *repo)
4679 const struct got_error *error;
4680 struct got_commit_object *commit;
4682 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4687 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4691 error = show_histedit_progress(commit, hle, NULL);
4692 got_object_commit_close(commit);
4696 static const struct got_error *
4697 histedit_check_script(struct got_histedit_list *histedit_cmds,
4698 struct got_object_id_queue *commits, struct got_repository *repo)
4700 const struct got_error *err = NULL;
4701 struct got_object_qid *qid;
4702 struct got_histedit_list_entry *hle;
4703 static char msg[80];
4706 if (TAILQ_EMPTY(histedit_cmds))
4707 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4708 "histedit script contains no commands");
4710 SIMPLEQ_FOREACH(qid, commits, entry) {
4711 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4712 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4716 err = got_object_id_str(&id_str, qid->id);
4719 snprintf(msg, sizeof(msg),
4720 "commit %s missing from histedit script", id_str);
4722 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4726 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4727 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4728 "last commit in histedit script cannot be folded");
4733 static const struct got_error *
4734 cmd_histedit(int argc, char *argv[])
4736 const struct got_error *error = NULL;
4737 struct got_worktree *worktree = NULL;
4738 struct got_fileindex *fileindex = NULL;
4739 struct got_repository *repo = NULL;
4741 struct got_reference *branch = NULL;
4742 struct got_reference *tmp_branch = NULL;
4743 struct got_object_id *resume_commit_id = NULL;
4744 struct got_object_id *base_commit_id = NULL;
4745 struct got_object_id *head_commit_id = NULL;
4746 struct got_commit_object *commit = NULL;
4747 int ch, rebase_in_progress = 0;
4748 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4749 const char *edit_script_path = NULL;
4750 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4751 struct got_object_id_queue commits;
4752 struct got_pathlist_head merged_paths;
4753 const struct got_object_id_queue *parent_ids;
4754 struct got_object_qid *pid;
4755 struct got_histedit_list histedit_cmds;
4756 struct got_histedit_list_entry *hle;
4758 SIMPLEQ_INIT(&commits);
4759 TAILQ_INIT(&histedit_cmds);
4760 TAILQ_INIT(&merged_paths);
4762 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4771 edit_script_path = optarg;
4783 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4784 "unveil", NULL) == -1)
4787 if (abort_edit && continue_edit)
4793 * This command cannot apply unveil(2) in all cases because the
4794 * user may choose to run an editor to edit the histedit script
4795 * and to edit individual commit log messages.
4796 * unveil(2) traverses exec(2); if an editor is used we have to
4797 * apply unveil after edit script and log messages have been written.
4798 * XXX TODO: Make use of unveil(2) where possible.
4801 cwd = getcwd(NULL, 0);
4803 error = got_error_from_errno("getcwd");
4806 error = got_worktree_open(&worktree, cwd);
4810 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4814 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4817 if (rebase_in_progress) {
4818 error = got_error(GOT_ERR_REBASING);
4822 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4826 if (edit_in_progress && abort_edit) {
4828 error = got_worktree_histedit_continue(&resume_commit_id,
4829 &tmp_branch, &branch, &base_commit_id, &fileindex,
4833 printf("Switching work tree to %s\n",
4834 got_ref_get_symref_target(branch));
4835 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4836 branch, base_commit_id, update_progress, &did_something);
4839 printf("Histedit of %s aborted\n",
4840 got_ref_get_symref_target(branch));
4841 goto done; /* nothing else to do */
4842 } else if (abort_edit) {
4843 error = got_error(GOT_ERR_NOT_HISTEDIT);
4847 if (continue_edit) {
4850 if (!edit_in_progress) {
4851 error = got_error(GOT_ERR_NOT_HISTEDIT);
4855 error = got_worktree_get_histedit_list_path(&path, worktree);
4859 error = histedit_load_list(&histedit_cmds, path, repo);
4864 error = got_worktree_histedit_continue(&resume_commit_id,
4865 &tmp_branch, &branch, &base_commit_id, &fileindex,
4870 error = got_ref_resolve(&head_commit_id, repo, branch);
4874 error = got_object_open_as_commit(&commit, repo,
4878 parent_ids = got_object_commit_get_parent_ids(commit);
4879 pid = SIMPLEQ_FIRST(parent_ids);
4881 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4884 error = collect_commits(&commits, head_commit_id, pid->id,
4885 base_commit_id, got_worktree_get_path_prefix(worktree),
4886 GOT_ERR_HISTEDIT_PATH, repo);
4887 got_object_commit_close(commit);
4892 if (edit_in_progress) {
4893 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4897 error = got_ref_open(&branch, repo,
4898 got_worktree_get_head_ref_name(worktree), 0);
4902 error = got_ref_resolve(&head_commit_id, repo, branch);
4906 error = got_object_open_as_commit(&commit, repo,
4910 parent_ids = got_object_commit_get_parent_ids(commit);
4911 pid = SIMPLEQ_FIRST(parent_ids);
4913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4916 error = collect_commits(&commits, head_commit_id, pid->id,
4917 got_worktree_get_base_commit_id(worktree),
4918 got_worktree_get_path_prefix(worktree),
4919 GOT_ERR_HISTEDIT_PATH, repo);
4920 got_object_commit_close(commit);
4925 if (edit_script_path) {
4926 error = histedit_load_list(&histedit_cmds,
4927 edit_script_path, repo);
4931 error = histedit_edit_script(&histedit_cmds, &commits,
4938 error = histedit_save_list(&histedit_cmds, worktree,
4943 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4944 &base_commit_id, &fileindex, worktree, repo);
4950 error = histedit_check_script(&histedit_cmds, &commits, repo);
4954 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4955 if (resume_commit_id) {
4956 if (got_object_id_cmp(hle->commit_id,
4957 resume_commit_id) != 0)
4960 resume_commit_id = NULL;
4961 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4962 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4963 error = histedit_skip_commit(hle, worktree,
4966 error = histedit_commit(NULL, worktree,
4967 fileindex, tmp_branch, hle, repo);
4974 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4975 error = histedit_skip_commit(hle, worktree, repo);
4981 error = got_object_open_as_commit(&commit, repo,
4985 parent_ids = got_object_commit_get_parent_ids(commit);
4986 pid = SIMPLEQ_FIRST(parent_ids);
4988 error = got_worktree_histedit_merge_files(&merged_paths,
4989 worktree, fileindex, pid->id, hle->commit_id, repo,
4990 rebase_progress, &rebase_status, check_cancelled, NULL);
4993 got_object_commit_close(commit);
4996 if (rebase_status == GOT_STATUS_CONFLICT) {
4997 got_worktree_rebase_pathlist_free(&merged_paths);
5001 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5003 error = got_object_id_str(&id_str, hle->commit_id);
5006 printf("Stopping histedit for amending commit %s\n",
5009 got_worktree_rebase_pathlist_free(&merged_paths);
5010 error = got_worktree_histedit_postpone(worktree,
5015 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5016 error = histedit_skip_commit(hle, worktree, repo);
5022 error = histedit_commit(&merged_paths, worktree, fileindex,
5023 tmp_branch, hle, repo);
5024 got_worktree_rebase_pathlist_free(&merged_paths);
5029 if (rebase_status == GOT_STATUS_CONFLICT) {
5030 error = got_worktree_histedit_postpone(worktree, fileindex);
5033 error = got_error_msg(GOT_ERR_CONFLICTS,
5034 "conflicts must be resolved before rebasing can continue");
5036 error = histedit_complete(worktree, fileindex, tmp_branch,
5039 got_object_id_queue_free(&commits);
5040 free(head_commit_id);
5041 free(base_commit_id);
5042 free(resume_commit_id);
5044 got_object_commit_close(commit);
5046 got_ref_close(branch);
5048 got_ref_close(tmp_branch);
5050 got_worktree_close(worktree);
5052 got_repo_close(repo);