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;
794 struct got_pathlist_head paths;
798 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
801 branch_name = optarg;
804 commit_id_str = strdup(optarg);
805 if (commit_id_str == NULL)
806 return got_error_from_errno("strdup");
809 path_prefix = optarg;
821 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
822 "unveil", NULL) == -1)
826 char *cwd, *base, *dotgit;
827 repo_path = realpath(argv[0], NULL);
828 if (repo_path == NULL)
829 return got_error_from_errno2("realpath", argv[0]);
830 cwd = getcwd(NULL, 0);
832 error = got_error_from_errno("getcwd");
835 if (path_prefix[0]) {
836 base = basename(path_prefix);
838 error = got_error_from_errno2("basename",
843 base = basename(repo_path);
845 error = got_error_from_errno2("basename",
850 dotgit = strstr(base, ".git");
853 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
854 error = got_error_from_errno("asprintf");
859 } else if (argc == 2) {
860 repo_path = realpath(argv[0], NULL);
861 if (repo_path == NULL) {
862 error = got_error_from_errno2("realpath", argv[0]);
865 worktree_path = realpath(argv[1], NULL);
866 if (worktree_path == NULL) {
867 if (errno != ENOENT) {
868 error = got_error_from_errno2("realpath",
872 worktree_path = strdup(argv[1]);
873 if (worktree_path == NULL) {
874 error = got_error_from_errno("strdup");
881 got_path_strip_trailing_slashes(repo_path);
882 got_path_strip_trailing_slashes(worktree_path);
884 error = got_repo_open(&repo, repo_path);
888 /* Pre-create work tree path for unveil(2) */
889 error = got_path_mkdir(worktree_path);
891 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
893 if (!got_path_dir_is_empty(worktree_path)) {
894 error = got_error_path(worktree_path,
895 GOT_ERR_DIR_NOT_EMPTY);
900 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
904 error = got_ref_open(&head_ref, repo, branch_name, 0);
908 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
909 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
912 error = got_worktree_open(&worktree, worktree_path);
916 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
920 if (!same_path_prefix) {
921 error = got_error(GOT_ERR_PATH_PREFIX);
926 struct got_object_id *commit_id;
927 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
930 error = check_linear_ancestry(commit_id,
931 got_worktree_get_base_commit_id(worktree), repo);
936 error = check_same_branch(commit_id, head_ref, repo);
939 error = got_worktree_set_base_commit_id(worktree, repo,
946 error = got_pathlist_append(NULL, &paths, "", NULL);
949 error = got_worktree_checkout_files(worktree, &paths, repo,
950 checkout_progress, worktree_path, check_cancelled, NULL);
954 printf("Now shut up and hack\n");
957 got_pathlist_free(&paths);
967 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
972 static const struct got_error *
973 update_progress(void *arg, unsigned char status, const char *path)
975 int *did_something = arg;
977 if (status == GOT_STATUS_EXISTS)
982 /* Base commit bump happens silently. */
983 if (status == GOT_STATUS_BUMP_BASE)
986 while (path[0] == '/')
988 printf("%c %s\n", status, path);
992 static const struct got_error *
993 switch_head_ref(struct got_reference *head_ref,
994 struct got_object_id *commit_id, struct got_worktree *worktree,
995 struct got_repository *repo)
997 const struct got_error *err = NULL;
999 int ref_has_moved = 0;
1001 /* Trivial case: switching between two different references. */
1002 if (strcmp(got_ref_get_name(head_ref),
1003 got_worktree_get_head_ref_name(worktree)) != 0) {
1004 printf("Switching work tree from %s to %s\n",
1005 got_worktree_get_head_ref_name(worktree),
1006 got_ref_get_name(head_ref));
1007 return got_worktree_set_head_ref(worktree, head_ref);
1010 err = check_linear_ancestry(commit_id,
1011 got_worktree_get_base_commit_id(worktree), repo);
1013 if (err->code != GOT_ERR_ANCESTRY)
1020 /* Switching to a rebased branch with the same reference name. */
1021 err = got_object_id_str(&base_id_str,
1022 got_worktree_get_base_commit_id(worktree));
1025 printf("Reference %s now points at a different branch\n",
1026 got_worktree_get_head_ref_name(worktree));
1027 printf("Switching work tree from %s to %s\n", base_id_str,
1028 got_worktree_get_head_ref_name(worktree));
1032 static const struct got_error *
1033 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1035 const struct got_error *err;
1038 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1042 return got_error(GOT_ERR_REBASING);
1044 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1048 return got_error(GOT_ERR_HISTEDIT_BUSY);
1053 static const struct got_error *
1054 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1055 char *argv[], struct got_worktree *worktree)
1057 const struct got_error *err;
1064 return got_error_from_errno("strdup");
1065 return got_pathlist_append(NULL, paths, path, NULL);
1068 for (i = 0; i < argc; i++) {
1069 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1072 err = got_pathlist_append(NULL, paths, path, NULL);
1082 static const struct got_error *
1083 cmd_update(int argc, char *argv[])
1085 const struct got_error *error = NULL;
1086 struct got_repository *repo = NULL;
1087 struct got_worktree *worktree = NULL;
1088 char *worktree_path = NULL;
1089 struct got_object_id *commit_id = NULL;
1090 char *commit_id_str = NULL;
1091 const char *branch_name = NULL;
1092 struct got_reference *head_ref = NULL;
1093 struct got_pathlist_head paths;
1094 struct got_pathlist_entry *pe;
1095 int ch, did_something = 0;
1099 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1102 branch_name = optarg;
1105 commit_id_str = strdup(optarg);
1106 if (commit_id_str == NULL)
1107 return got_error_from_errno("strdup");
1119 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1120 "unveil", NULL) == -1)
1123 worktree_path = getcwd(NULL, 0);
1124 if (worktree_path == NULL) {
1125 error = got_error_from_errno("getcwd");
1128 error = got_worktree_open(&worktree, worktree_path);
1132 error = check_rebase_or_histedit_in_progress(worktree);
1136 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1140 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1144 error = apply_unveil(got_repo_get_path(repo), 0,
1145 got_worktree_get_root_path(worktree));
1149 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1150 got_worktree_get_head_ref_name(worktree), 0);
1153 if (commit_id_str == NULL) {
1154 error = got_ref_resolve(&commit_id, repo, head_ref);
1157 error = got_object_id_str(&commit_id_str, commit_id);
1161 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1162 free(commit_id_str);
1163 commit_id_str = NULL;
1166 error = got_object_id_str(&commit_id_str, commit_id);
1172 struct got_object_id *head_commit_id;
1173 TAILQ_FOREACH(pe, &paths, entry) {
1174 if (strlen(pe->path) == 0)
1176 error = got_error_msg(GOT_ERR_BAD_PATH,
1177 "switching between branches requires that "
1178 "the entire work tree gets updated");
1181 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1184 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1185 free(head_commit_id);
1188 error = check_same_branch(commit_id, head_ref, repo);
1191 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1195 error = check_linear_ancestry(commit_id,
1196 got_worktree_get_base_commit_id(worktree), repo);
1197 if (error != NULL) {
1198 if (error->code == GOT_ERR_ANCESTRY)
1199 error = got_error(GOT_ERR_BRANCH_MOVED);
1202 error = check_same_branch(commit_id, head_ref, repo);
1207 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1209 error = got_worktree_set_base_commit_id(worktree, repo,
1215 error = got_worktree_checkout_files(worktree, &paths, repo,
1216 update_progress, &did_something, check_cancelled, NULL);
1221 printf("Updated to commit %s\n", commit_id_str);
1223 printf("Already up-to-date\n");
1225 free(worktree_path);
1226 TAILQ_FOREACH(pe, &paths, entry)
1227 free((char *)pe->path);
1228 got_pathlist_free(&paths);
1230 free(commit_id_str);
1234 static const struct got_error *
1235 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1236 int diff_context, struct got_repository *repo)
1238 const struct got_error *err = NULL;
1239 struct got_tree_object *tree1 = NULL, *tree2;
1240 struct got_object_qid *qid;
1241 char *id_str1 = NULL, *id_str2;
1242 struct got_diff_blob_output_unidiff_arg arg;
1244 err = got_object_open_as_tree(&tree2, repo,
1245 got_object_commit_get_tree_id(commit));
1249 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1251 struct got_commit_object *pcommit;
1253 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1257 err = got_object_open_as_tree(&tree1, repo,
1258 got_object_commit_get_tree_id(pcommit));
1259 got_object_commit_close(pcommit);
1263 err = got_object_id_str(&id_str1, qid->id);
1268 err = got_object_id_str(&id_str2, id);
1272 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1273 arg.diff_context = diff_context;
1274 arg.outfile = stdout;
1275 err = got_diff_tree(tree1, tree2, "", "", repo,
1276 got_diff_blob_output_unidiff, &arg);
1279 got_object_tree_close(tree1);
1280 got_object_tree_close(tree2);
1287 get_datestr(time_t *time, char *datebuf)
1289 char *p, *s = ctime_r(time, datebuf);
1290 p = strchr(s, '\n');
1296 static const struct got_error *
1297 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1298 struct got_repository *repo, int show_patch, int diff_context,
1299 struct got_reflist_head *refs)
1301 const struct got_error *err = NULL;
1302 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1304 time_t committer_time;
1305 const char *author, *committer;
1306 char *refs_str = NULL;
1307 struct got_reflist_entry *re;
1309 SIMPLEQ_FOREACH(re, refs, entry) {
1312 if (got_object_id_cmp(re->id, id) != 0)
1314 name = got_ref_get_name(re->ref);
1315 if (strcmp(name, GOT_REF_HEAD) == 0)
1317 if (strncmp(name, "refs/", 5) == 0)
1319 if (strncmp(name, "got/", 4) == 0)
1321 if (strncmp(name, "heads/", 6) == 0)
1323 if (strncmp(name, "remotes/", 8) == 0)
1326 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1328 err = got_error_from_errno("asprintf");
1334 err = got_object_id_str(&id_str, id);
1338 printf("-----------------------------------------------\n");
1339 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1340 refs_str ? refs_str : "", refs_str ? ")" : "");
1345 printf("from: %s\n", got_object_commit_get_author(commit));
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 datestr = get_datestr(&committer_time, datebuf);
1348 printf("date: %s UTC\n", datestr);
1349 author = got_object_commit_get_author(commit);
1350 committer = got_object_commit_get_committer(commit);
1351 if (strcmp(author, committer) != 0)
1352 printf("via: %s\n", committer);
1353 if (got_object_commit_get_nparents(commit) > 1) {
1354 const struct got_object_id_queue *parent_ids;
1355 struct got_object_qid *qid;
1357 parent_ids = got_object_commit_get_parent_ids(commit);
1358 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1359 err = got_object_id_str(&id_str, qid->id);
1362 printf("parent %d: %s\n", n++, id_str);
1367 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1368 if (logmsg0 == NULL)
1369 return got_error_from_errno("strdup");
1373 line = strsep(&logmsg, "\n");
1375 printf(" %s\n", line);
1380 err = print_patch(commit, id, diff_context, repo);
1385 if (fflush(stdout) != 0 && err == NULL)
1386 err = got_error_from_errno("fflush");
1390 static const struct got_error *
1391 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1392 char *path, int show_patch, int diff_context, int limit,
1393 int first_parent_traversal, struct got_reflist_head *refs)
1395 const struct got_error *err;
1396 struct got_commit_graph *graph;
1398 err = got_commit_graph_open(&graph, root_id, path,
1399 first_parent_traversal, repo);
1402 err = got_commit_graph_iter_start(graph, root_id, repo);
1406 struct got_commit_object *commit;
1407 struct got_object_id *id;
1409 if (sigint_received || sigpipe_received)
1412 err = got_commit_graph_iter_next(&id, graph);
1414 if (err->code == GOT_ERR_ITER_COMPLETED) {
1418 if (err->code != GOT_ERR_ITER_NEED_MORE)
1420 err = got_commit_graph_fetch_commits(graph, 1, repo);
1429 err = got_object_open_as_commit(&commit, repo, id);
1432 err = print_commit(commit, id, repo, show_patch, diff_context,
1434 got_object_commit_close(commit);
1435 if (err || (limit && --limit == 0))
1439 got_commit_graph_close(graph);
1446 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1447 "[-r repository-path] [path]\n", getprogname());
1451 static const struct got_error *
1452 cmd_log(int argc, char *argv[])
1454 const struct got_error *error;
1455 struct got_repository *repo = NULL;
1456 struct got_worktree *worktree = NULL;
1457 struct got_commit_object *commit = NULL;
1458 struct got_object_id *id = NULL;
1459 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1460 char *start_commit = NULL;
1461 int diff_context = 3, ch;
1462 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1464 struct got_reflist_head refs;
1466 SIMPLEQ_INIT(&refs);
1469 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1475 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1481 start_commit = optarg;
1484 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1487 err(1, "-C option %s", errstr);
1490 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1492 err(1, "-l option %s", errstr);
1495 first_parent_traversal = 1;
1498 repo_path = realpath(optarg, NULL);
1499 if (repo_path == NULL)
1500 err(1, "-r option");
1501 got_path_strip_trailing_slashes(repo_path);
1512 cwd = getcwd(NULL, 0);
1514 error = got_error_from_errno("getcwd");
1518 error = got_worktree_open(&worktree, cwd);
1519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1526 error = got_error_from_errno("strdup");
1529 } else if (argc == 1) {
1531 error = got_worktree_resolve_path(&path, worktree,
1536 path = strdup(argv[0]);
1538 error = got_error_from_errno("strdup");
1545 if (repo_path == NULL) {
1546 repo_path = worktree ?
1547 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1549 if (repo_path == NULL) {
1550 error = got_error_from_errno("strdup");
1554 error = got_repo_open(&repo, repo_path);
1558 error = apply_unveil(got_repo_get_path(repo), 1,
1559 worktree ? got_worktree_get_root_path(worktree) : NULL);
1563 if (start_commit == NULL) {
1564 struct got_reference *head_ref;
1565 error = got_ref_open(&head_ref, repo,
1566 worktree ? got_worktree_get_head_ref_name(worktree)
1570 error = got_ref_resolve(&id, repo, head_ref);
1571 got_ref_close(head_ref);
1574 error = got_object_open_as_commit(&commit, repo, id);
1576 struct got_reference *ref;
1577 error = got_ref_open(&ref, repo, start_commit, 0);
1578 if (error == NULL) {
1580 error = got_ref_resolve(&id, repo, ref);
1584 error = got_object_get_type(&obj_type, repo, id);
1587 if (obj_type == GOT_OBJ_TYPE_TAG) {
1588 struct got_tag_object *tag;
1589 error = got_object_open_as_tag(&tag, repo, id);
1592 if (got_object_tag_get_object_type(tag) !=
1593 GOT_OBJ_TYPE_COMMIT) {
1594 got_object_tag_close(tag);
1595 error = got_error(GOT_ERR_OBJ_TYPE);
1599 id = got_object_id_dup(
1600 got_object_tag_get_object_id(tag));
1602 error = got_error_from_errno(
1603 "got_object_id_dup");
1604 got_object_tag_close(tag);
1607 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1608 error = got_error(GOT_ERR_OBJ_TYPE);
1611 error = got_object_open_as_commit(&commit, repo, id);
1615 if (commit == NULL) {
1616 error = got_repo_match_object_id_prefix(&id,
1617 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1625 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1630 path = in_repo_path;
1633 error = got_ref_list(&refs, repo);
1637 error = print_commits(id, repo, path, show_patch,
1638 diff_context, limit, first_parent_traversal, &refs);
1645 got_worktree_close(worktree);
1647 const struct got_error *repo_error;
1648 repo_error = got_repo_close(repo);
1652 got_ref_list_free(&refs);
1659 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1660 "[object1 object2 | path]\n", getprogname());
1664 struct print_diff_arg {
1665 struct got_repository *repo;
1666 struct got_worktree *worktree;
1672 static const struct got_error *
1673 print_diff(void *arg, unsigned char status, const char *path,
1674 struct got_object_id *blob_id, struct got_object_id *commit_id)
1676 struct print_diff_arg *a = arg;
1677 const struct got_error *err = NULL;
1678 struct got_blob_object *blob1 = NULL;
1680 char *abspath = NULL;
1683 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1684 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1687 if (!a->header_shown) {
1688 printf("diff %s %s\n", a->id_str,
1689 got_worktree_get_root_path(a->worktree));
1690 a->header_shown = 1;
1693 if (status != GOT_STATUS_ADD) {
1694 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1700 if (status != GOT_STATUS_DELETE) {
1701 if (asprintf(&abspath, "%s/%s",
1702 got_worktree_get_root_path(a->worktree), path) == -1) {
1703 err = got_error_from_errno("asprintf");
1707 f2 = fopen(abspath, "r");
1709 err = got_error_from_errno2("fopen", abspath);
1712 if (lstat(abspath, &sb) == -1) {
1713 err = got_error_from_errno2("lstat", abspath);
1719 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1723 got_object_blob_close(blob1);
1724 if (f2 && fclose(f2) != 0 && err == NULL)
1725 err = got_error_from_errno("fclose");
1730 static const struct got_error *
1731 cmd_diff(int argc, char *argv[])
1733 const struct got_error *error;
1734 struct got_repository *repo = NULL;
1735 struct got_worktree *worktree = NULL;
1736 char *cwd = NULL, *repo_path = NULL;
1737 struct got_object_id *id1 = NULL, *id2 = NULL;
1738 const char *id_str1 = NULL, *id_str2 = NULL;
1739 char *label1 = NULL, *label2 = NULL;
1741 int diff_context = 3, ch;
1746 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1751 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1754 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1756 err(1, "-C option %s", errstr);
1759 repo_path = realpath(optarg, NULL);
1760 if (repo_path == NULL)
1761 err(1, "-r option");
1762 got_path_strip_trailing_slashes(repo_path);
1773 cwd = getcwd(NULL, 0);
1775 error = got_error_from_errno("getcwd");
1778 error = got_worktree_open(&worktree, cwd);
1779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1782 if (worktree == NULL) {
1783 error = got_error(GOT_ERR_NOT_WORKTREE);
1788 "-r option can't be used when diffing a work tree");
1789 repo_path = strdup(got_worktree_get_repo_path(worktree));
1790 if (repo_path == NULL) {
1791 error = got_error_from_errno("strdup");
1795 error = got_worktree_resolve_path(&path, worktree,
1802 error = got_error_from_errno("strdup");
1806 } else if (argc == 2) {
1809 if (worktree && repo_path == NULL) {
1811 strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL) {
1813 error = got_error_from_errno("strdup");
1820 if (repo_path == NULL) {
1821 repo_path = getcwd(NULL, 0);
1822 if (repo_path == NULL)
1823 return got_error_from_errno("getcwd");
1826 error = got_repo_open(&repo, repo_path);
1831 error = apply_unveil(got_repo_get_path(repo), 1,
1832 worktree ? got_worktree_get_root_path(worktree) : NULL);
1837 struct print_diff_arg arg;
1838 struct got_pathlist_head paths;
1843 error = got_object_id_str(&id_str,
1844 got_worktree_get_base_commit_id(worktree));
1848 arg.worktree = worktree;
1849 arg.diff_context = diff_context;
1850 arg.id_str = id_str;
1851 arg.header_shown = 0;
1853 error = got_pathlist_append(NULL, &paths, path, NULL);
1857 error = got_worktree_status(worktree, &paths, repo, print_diff,
1858 &arg, check_cancelled, NULL);
1860 got_pathlist_free(&paths);
1864 error = got_repo_match_object_id_prefix(&id1, id_str1,
1865 GOT_OBJ_TYPE_ANY, repo);
1867 struct got_reference *ref;
1868 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1870 error = got_ref_open(&ref, repo, id_str1, 0);
1873 label1 = strdup(got_ref_get_name(ref));
1874 if (label1 == NULL) {
1875 error = got_error_from_errno("strdup");
1878 error = got_ref_resolve(&id1, repo, ref);
1883 error = got_object_id_str(&label1, id1);
1884 if (label1 == NULL) {
1885 error = got_error_from_errno("strdup");
1890 error = got_repo_match_object_id_prefix(&id2, id_str2,
1891 GOT_OBJ_TYPE_ANY, repo);
1893 struct got_reference *ref;
1894 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1896 error = got_ref_open(&ref, repo, id_str2, 0);
1899 label2 = strdup(got_ref_get_name(ref));
1900 if (label2 == NULL) {
1901 error = got_error_from_errno("strdup");
1904 error = got_ref_resolve(&id2, repo, ref);
1909 error = got_object_id_str(&label2, id2);
1910 if (label2 == NULL) {
1911 error = got_error_from_errno("strdup");
1916 error = got_object_get_type(&type1, repo, id1);
1920 error = got_object_get_type(&type2, repo, id2);
1924 if (type1 != type2) {
1925 error = got_error(GOT_ERR_OBJ_TYPE);
1930 case GOT_OBJ_TYPE_BLOB:
1931 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1932 diff_context, repo, stdout);
1934 case GOT_OBJ_TYPE_TREE:
1935 error = got_diff_objects_as_trees(id1, id2, "", "",
1936 diff_context, repo, stdout);
1938 case GOT_OBJ_TYPE_COMMIT:
1939 printf("diff %s %s\n", label1, label2);
1940 error = got_diff_objects_as_commits(id1, id2, diff_context,
1944 error = got_error(GOT_ERR_OBJ_TYPE);
1954 got_worktree_close(worktree);
1956 const struct got_error *repo_error;
1957 repo_error = got_repo_close(repo);
1968 "usage: %s blame [-c commit] [-r repository-path] path\n",
1973 static const struct got_error *
1974 cmd_blame(int argc, char *argv[])
1976 const struct got_error *error;
1977 struct got_repository *repo = NULL;
1978 struct got_worktree *worktree = NULL;
1979 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1980 struct got_object_id *commit_id = NULL;
1981 char *commit_id_str = NULL;
1985 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1990 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1993 commit_id_str = optarg;
1996 repo_path = realpath(optarg, NULL);
1997 if (repo_path == NULL)
1998 err(1, "-r option");
1999 got_path_strip_trailing_slashes(repo_path);
2015 cwd = getcwd(NULL, 0);
2017 error = got_error_from_errno("getcwd");
2020 if (repo_path == NULL) {
2021 error = got_worktree_open(&worktree, cwd);
2022 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2028 strdup(got_worktree_get_repo_path(worktree));
2029 if (repo_path == NULL)
2030 error = got_error_from_errno("strdup");
2034 repo_path = strdup(cwd);
2035 if (repo_path == NULL) {
2036 error = got_error_from_errno("strdup");
2042 error = got_repo_open(&repo, repo_path);
2046 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2051 const char *prefix = got_worktree_get_path_prefix(worktree);
2052 char *p, *worktree_subdir = cwd +
2053 strlen(got_worktree_get_root_path(worktree));
2054 if (asprintf(&p, "%s%s%s%s%s",
2055 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2056 worktree_subdir, worktree_subdir[0] ? "/" : "",
2058 error = got_error_from_errno("asprintf");
2061 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2064 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2069 if (commit_id_str == NULL) {
2070 struct got_reference *head_ref;
2071 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2074 error = got_ref_resolve(&commit_id, repo, head_ref);
2075 got_ref_close(head_ref);
2079 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2084 error = got_blame(in_repo_path, commit_id, repo, stdout);
2091 got_worktree_close(worktree);
2093 const struct got_error *repo_error;
2094 repo_error = got_repo_close(repo);
2105 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2111 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2112 const char *root_path)
2114 int is_root_path = (strcmp(path, root_path) == 0);
2116 path += strlen(root_path);
2117 while (path[0] == '/')
2120 printf("%s%s%s%s%s\n", id ? id : "", path,
2121 is_root_path ? "" : "/", te->name,
2122 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2125 static const struct got_error *
2126 print_tree(const char *path, struct got_object_id *commit_id,
2127 int show_ids, int recurse, const char *root_path,
2128 struct got_repository *repo)
2130 const struct got_error *err = NULL;
2131 struct got_object_id *tree_id = NULL;
2132 struct got_tree_object *tree = NULL;
2133 const struct got_tree_entries *entries;
2134 struct got_tree_entry *te;
2136 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2140 err = got_object_open_as_tree(&tree, repo, tree_id);
2143 entries = got_object_tree_get_entries(tree);
2144 te = SIMPLEQ_FIRST(&entries->head);
2148 if (sigint_received || sigpipe_received)
2153 err = got_object_id_str(&id_str, te->id);
2156 if (asprintf(&id, "%s ", id_str) == -1) {
2157 err = got_error_from_errno("asprintf");
2163 print_entry(te, id, path, root_path);
2166 if (recurse && S_ISDIR(te->mode)) {
2168 if (asprintf(&child_path, "%s%s%s", path,
2169 path[0] == '/' && path[1] == '\0' ? "" : "/",
2171 err = got_error_from_errno("asprintf");
2174 err = print_tree(child_path, commit_id, show_ids, 1,
2181 te = SIMPLEQ_NEXT(te, entry);
2185 got_object_tree_close(tree);
2190 static const struct got_error *
2191 cmd_tree(int argc, char *argv[])
2193 const struct got_error *error;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2197 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2198 struct got_object_id *commit_id = NULL;
2199 char *commit_id_str = NULL;
2200 int show_ids = 0, recurse = 0;
2204 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2209 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2212 commit_id_str = optarg;
2215 repo_path = realpath(optarg, NULL);
2216 if (repo_path == NULL)
2217 err(1, "-r option");
2218 got_path_strip_trailing_slashes(repo_path);
2242 cwd = getcwd(NULL, 0);
2244 error = got_error_from_errno("getcwd");
2247 if (repo_path == NULL) {
2248 error = got_worktree_open(&worktree, cwd);
2249 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2255 strdup(got_worktree_get_repo_path(worktree));
2256 if (repo_path == NULL)
2257 error = got_error_from_errno("strdup");
2261 repo_path = strdup(cwd);
2262 if (repo_path == NULL) {
2263 error = got_error_from_errno("strdup");
2269 error = got_repo_open(&repo, repo_path);
2273 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2279 char *p, *worktree_subdir = cwd +
2280 strlen(got_worktree_get_root_path(worktree));
2281 if (asprintf(&p, "%s/%s",
2282 got_worktree_get_path_prefix(worktree),
2283 worktree_subdir) == -1) {
2284 error = got_error_from_errno("asprintf");
2287 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2294 if (in_repo_path == NULL) {
2295 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2300 if (commit_id_str == NULL) {
2301 struct got_reference *head_ref;
2302 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2305 error = got_ref_resolve(&commit_id, repo, head_ref);
2306 got_ref_close(head_ref);
2310 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2315 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2316 in_repo_path, repo);
2323 got_worktree_close(worktree);
2325 const struct got_error *repo_error;
2326 repo_error = got_repo_close(repo);
2336 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2340 static const struct got_error *
2341 print_status(void *arg, unsigned char status, const char *path,
2342 struct got_object_id *blob_id, struct got_object_id *commit_id)
2344 printf("%c %s\n", status, path);
2348 static const struct got_error *
2349 cmd_status(int argc, char *argv[])
2351 const struct got_error *error = NULL;
2352 struct got_repository *repo = NULL;
2353 struct got_worktree *worktree = NULL;
2355 struct got_pathlist_head paths;
2356 struct got_pathlist_entry *pe;
2361 while ((ch = getopt(argc, argv, "")) != -1) {
2373 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2377 cwd = getcwd(NULL, 0);
2379 error = got_error_from_errno("getcwd");
2383 error = got_worktree_open(&worktree, cwd);
2387 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2391 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2395 error = apply_unveil(got_repo_get_path(repo), 1,
2396 got_worktree_get_root_path(worktree));
2400 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2401 check_cancelled, NULL);
2403 TAILQ_FOREACH(pe, &paths, entry)
2404 free((char *)pe->path);
2405 got_pathlist_free(&paths);
2414 "usage: %s ref [-r repository] -l | -d name | name target\n",
2419 static const struct got_error *
2420 list_refs(struct got_repository *repo)
2422 static const struct got_error *err = NULL;
2423 struct got_reflist_head refs;
2424 struct got_reflist_entry *re;
2426 SIMPLEQ_INIT(&refs);
2427 err = got_ref_list(&refs, repo);
2431 SIMPLEQ_FOREACH(re, &refs, entry) {
2433 refstr = got_ref_to_str(re->ref);
2435 return got_error_from_errno("got_ref_to_str");
2436 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2440 got_ref_list_free(&refs);
2444 static const struct got_error *
2445 delete_ref(struct got_repository *repo, const char *refname)
2447 const struct got_error *err = NULL;
2448 struct got_reference *ref;
2450 err = got_ref_open(&ref, repo, refname, 0);
2454 err = got_ref_delete(ref, repo);
2459 static const struct got_error *
2460 add_ref(struct got_repository *repo, const char *refname, const char *target)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id;
2464 struct got_reference *ref = NULL;
2467 * Don't let the user create a reference named '-'.
2468 * While technically a valid reference name, this case is usually
2469 * an unintended typo.
2471 if (refname[0] == '-' && refname[1] == '\0')
2472 return got_error(GOT_ERR_BAD_REF_NAME);
2474 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2477 struct got_reference *target_ref;
2479 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2481 err = got_ref_open(&target_ref, repo, target, 0);
2484 err = got_ref_resolve(&id, repo, target_ref);
2485 got_ref_close(target_ref);
2490 err = got_ref_alloc(&ref, refname, id);
2494 err = got_ref_write(ref, repo);
2502 static const struct got_error *
2503 cmd_ref(int argc, char *argv[])
2505 const struct got_error *error = NULL;
2506 struct got_repository *repo = NULL;
2507 struct got_worktree *worktree = NULL;
2508 char *cwd = NULL, *repo_path = NULL;
2509 int ch, do_list = 0;
2510 const char *delref = NULL;
2512 /* TODO: Add -s option for adding symbolic references. */
2513 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2519 repo_path = realpath(optarg, NULL);
2520 if (repo_path == NULL)
2521 err(1, "-r option");
2522 got_path_strip_trailing_slashes(repo_path);
2533 if (do_list && delref)
2534 errx(1, "-l and -d options are mutually exclusive\n");
2539 if (do_list || delref) {
2542 } else if (argc != 2)
2547 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2551 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2552 "sendfd unveil", NULL) == -1)
2556 cwd = getcwd(NULL, 0);
2558 error = got_error_from_errno("getcwd");
2562 if (repo_path == NULL) {
2563 error = got_worktree_open(&worktree, cwd);
2564 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2570 strdup(got_worktree_get_repo_path(worktree));
2571 if (repo_path == NULL)
2572 error = got_error_from_errno("strdup");
2576 repo_path = strdup(cwd);
2577 if (repo_path == NULL) {
2578 error = got_error_from_errno("strdup");
2584 error = got_repo_open(&repo, repo_path);
2588 error = apply_unveil(got_repo_get_path(repo), do_list,
2589 worktree ? got_worktree_get_root_path(worktree) : NULL);
2594 error = list_refs(repo);
2596 error = delete_ref(repo, delref);
2598 error = add_ref(repo, argv[0], argv[1]);
2601 got_repo_close(repo);
2603 got_worktree_close(worktree);
2613 "usage: %s branch [-r repository] -l | -d name | "
2614 "name [base-branch]\n", getprogname());
2618 static const struct got_error *
2619 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2621 static const struct got_error *err = NULL;
2622 struct got_reflist_head refs;
2623 struct got_reflist_entry *re;
2625 SIMPLEQ_INIT(&refs);
2627 err = got_ref_list(&refs, repo);
2631 SIMPLEQ_FOREACH(re, &refs, entry) {
2632 const char *refname, *marker = " ";
2634 refname = got_ref_get_name(re->ref);
2635 if (strncmp(refname, "refs/heads/", 11) != 0)
2637 if (worktree && strcmp(refname,
2638 got_worktree_get_head_ref_name(worktree)) == 0) {
2639 struct got_object_id *id = NULL;
2640 err = got_ref_resolve(&id, repo, re->ref);
2643 if (got_object_id_cmp(id,
2644 got_worktree_get_base_commit_id(worktree)) == 0)
2651 refstr = got_ref_to_str(re->ref);
2653 return got_error_from_errno("got_ref_to_str");
2654 printf("%s%s: %s\n", marker, refname, refstr);
2658 got_ref_list_free(&refs);
2662 static const struct got_error *
2663 delete_branch(struct got_repository *repo, const char *branch_name)
2665 const struct got_error *err = NULL;
2666 struct got_reference *ref;
2669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2670 return got_error_from_errno("asprintf");
2672 err = got_ref_open(&ref, repo, refname, 0);
2676 err = got_ref_delete(ref, repo);
2683 static const struct got_error *
2684 add_branch(struct got_repository *repo, const char *branch_name,
2685 const char *base_branch)
2687 const struct got_error *err = NULL;
2688 struct got_object_id *id = NULL;
2689 struct got_reference *ref = NULL;
2690 char *base_refname = NULL, *refname = NULL;
2691 struct got_reference *base_ref;
2694 * Don't let the user create a branch named '-'.
2695 * While technically a valid reference name, this case is usually
2696 * an unintended typo.
2698 if (branch_name[0] == '-' && branch_name[1] == '\0')
2699 return got_error(GOT_ERR_BAD_REF_NAME);
2701 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2702 base_refname = strdup(GOT_REF_HEAD);
2703 if (base_refname == NULL)
2704 return got_error_from_errno("strdup");
2705 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2706 return got_error_from_errno("asprintf");
2708 err = got_ref_open(&base_ref, repo, base_refname, 0);
2711 err = got_ref_resolve(&id, repo, base_ref);
2712 got_ref_close(base_ref);
2716 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2717 err = got_error_from_errno("asprintf");
2721 err = got_ref_open(&ref, repo, refname, 0);
2723 err = got_error(GOT_ERR_BRANCH_EXISTS);
2725 } else if (err->code != GOT_ERR_NOT_REF)
2728 err = got_ref_alloc(&ref, refname, id);
2732 err = got_ref_write(ref, repo);
2742 static const struct got_error *
2743 cmd_branch(int argc, char *argv[])
2745 const struct got_error *error = NULL;
2746 struct got_repository *repo = NULL;
2747 struct got_worktree *worktree = NULL;
2748 char *cwd = NULL, *repo_path = NULL;
2749 int ch, do_list = 0;
2750 const char *delref = NULL;
2752 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2758 repo_path = realpath(optarg, NULL);
2759 if (repo_path == NULL)
2760 err(1, "-r option");
2761 got_path_strip_trailing_slashes(repo_path);
2772 if (do_list && delref)
2773 errx(1, "-l and -d options are mutually exclusive\n");
2778 if (do_list || delref) {
2781 } else if (argc < 1 || argc > 2)
2786 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2790 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2791 "sendfd unveil", NULL) == -1)
2795 cwd = getcwd(NULL, 0);
2797 error = got_error_from_errno("getcwd");
2801 if (repo_path == NULL) {
2802 error = got_worktree_open(&worktree, cwd);
2803 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2809 strdup(got_worktree_get_repo_path(worktree));
2810 if (repo_path == NULL)
2811 error = got_error_from_errno("strdup");
2815 repo_path = strdup(cwd);
2816 if (repo_path == NULL) {
2817 error = got_error_from_errno("strdup");
2823 error = got_repo_open(&repo, repo_path);
2827 error = apply_unveil(got_repo_get_path(repo), do_list,
2828 worktree ? got_worktree_get_root_path(worktree) : NULL);
2833 error = list_branches(repo, worktree);
2835 error = delete_branch(repo, delref);
2837 const char *base_branch;
2839 base_branch = worktree ?
2840 got_worktree_get_head_ref_name(worktree) :
2843 base_branch = argv[1];
2844 error = add_branch(repo, argv[0], base_branch);
2848 got_repo_close(repo);
2850 got_worktree_close(worktree);
2859 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2863 static const struct got_error *
2864 cmd_add(int argc, char *argv[])
2866 const struct got_error *error = NULL;
2867 struct got_repository *repo = NULL;
2868 struct got_worktree *worktree = NULL;
2870 struct got_pathlist_head paths;
2871 struct got_pathlist_entry *pe;
2876 while ((ch = getopt(argc, argv, "")) != -1) {
2888 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2895 cwd = getcwd(NULL, 0);
2897 error = got_error_from_errno("getcwd");
2901 error = got_worktree_open(&worktree, cwd);
2905 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2909 error = apply_unveil(got_repo_get_path(repo), 1,
2910 got_worktree_get_root_path(worktree));
2914 for (x = 0; x < argc; x++) {
2915 char *path = realpath(argv[x], NULL);
2917 error = got_error_from_errno2("realpath", argv[x]);
2921 got_path_strip_trailing_slashes(path);
2922 error = got_pathlist_insert(&pe, &paths, path, NULL);
2928 error = got_worktree_schedule_add(worktree, &paths, print_status,
2932 got_repo_close(repo);
2934 got_worktree_close(worktree);
2935 TAILQ_FOREACH(pe, &paths, entry)
2936 free((char *)pe->path);
2937 got_pathlist_free(&paths);
2945 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2949 static const struct got_error *
2950 cmd_remove(int argc, char *argv[])
2952 const struct got_error *error = NULL;
2953 struct got_worktree *worktree = NULL;
2954 struct got_repository *repo = NULL;
2956 struct got_pathlist_head paths;
2957 struct got_pathlist_entry *pe;
2958 int ch, i, delete_local_mods = 0;
2962 while ((ch = getopt(argc, argv, "f")) != -1) {
2965 delete_local_mods = 1;
2977 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2984 cwd = getcwd(NULL, 0);
2986 error = got_error_from_errno("getcwd");
2989 error = got_worktree_open(&worktree, cwd);
2993 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2997 error = apply_unveil(got_repo_get_path(repo), 1,
2998 got_worktree_get_root_path(worktree));
3002 for (i = 0; i < argc; i++) {
3003 char *path = realpath(argv[i], NULL);
3005 error = got_error_from_errno2("realpath", argv[i]);
3009 got_path_strip_trailing_slashes(path);
3010 error = got_pathlist_insert(&pe, &paths, path, NULL);
3016 error = got_worktree_schedule_delete(worktree, &paths,
3017 delete_local_mods, print_status, NULL, repo);
3022 got_repo_close(repo);
3024 got_worktree_close(worktree);
3025 TAILQ_FOREACH(pe, &paths, entry)
3026 free((char *)pe->path);
3027 got_pathlist_free(&paths);
3035 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3039 static const struct got_error *
3040 revert_progress(void *arg, unsigned char status, const char *path)
3042 while (path[0] == '/')
3044 printf("%c %s\n", status, path);
3048 static const struct got_error *
3049 cmd_revert(int argc, char *argv[])
3051 const struct got_error *error = NULL;
3052 struct got_worktree *worktree = NULL;
3053 struct got_repository *repo = NULL;
3054 char *cwd = NULL, *path = NULL;
3055 struct got_pathlist_head paths;
3056 struct got_pathlist_entry *pe;
3061 while ((ch = getopt(argc, argv, "")) != -1) {
3073 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3074 "unveil", NULL) == -1)
3080 for (i = 0; i < argc; i++) {
3081 char *path = realpath(argv[i], NULL);
3083 error = got_error_from_errno2("realpath", argv[i]);
3087 got_path_strip_trailing_slashes(path);
3088 error = got_pathlist_insert(&pe, &paths, path, NULL);
3095 cwd = getcwd(NULL, 0);
3097 error = got_error_from_errno("getcwd");
3100 error = got_worktree_open(&worktree, cwd);
3104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3108 error = apply_unveil(got_repo_get_path(repo), 1,
3109 got_worktree_get_root_path(worktree));
3113 error = got_worktree_revert(worktree, &paths,
3114 revert_progress, NULL, repo);
3119 got_repo_close(repo);
3121 got_worktree_close(worktree);
3130 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3134 struct collect_commit_logmsg_arg {
3135 const char *cmdline_log;
3137 const char *worktree_path;
3138 const char *branch_name;
3139 const char *repo_path;
3144 static const struct got_error *
3145 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3148 char *initial_content = NULL;
3149 struct got_pathlist_entry *pe;
3150 const struct got_error *err = NULL;
3151 char *template = NULL;
3152 struct collect_commit_logmsg_arg *a = arg;
3156 /* if a message was specified on the command line, just use it */
3157 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3158 len = strlen(a->cmdline_log) + 1;
3159 *logmsg = malloc(len + 1);
3160 if (*logmsg == NULL)
3161 return got_error_from_errno("malloc");
3162 strlcpy(*logmsg, a->cmdline_log, len);
3166 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3167 return got_error_from_errno("asprintf");
3169 if (asprintf(&initial_content,
3170 "\n# changes to be committed on branch %s:\n",
3171 a->branch_name) == -1)
3172 return got_error_from_errno("asprintf");
3174 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3178 dprintf(fd, initial_content);
3180 TAILQ_FOREACH(pe, commitable_paths, entry) {
3181 struct got_commitable *ct = pe->data;
3182 dprintf(fd, "# %c %s\n",
3183 got_commitable_get_status(ct),
3184 got_commitable_get_path(ct));
3188 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3190 unlink(a->logmsg_path);
3191 free(a->logmsg_path);
3192 free(initial_content);
3195 /* Editor is done; we can now apply unveil(2) */
3197 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3206 static const struct got_error *
3207 cmd_commit(int argc, char *argv[])
3209 const struct got_error *error = NULL;
3210 struct got_worktree *worktree = NULL;
3211 struct got_repository *repo = NULL;
3212 char *cwd = NULL, *path = NULL, *id_str = NULL;
3213 struct got_object_id *id = NULL;
3214 const char *logmsg = NULL;
3215 const char *got_author = getenv("GOT_AUTHOR");
3216 struct collect_commit_logmsg_arg cl_arg;
3217 char *editor = NULL;
3218 int ch, rebase_in_progress;
3220 while ((ch = getopt(argc, argv, "m:")) != -1) {
3235 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3236 "unveil", NULL) == -1)
3240 path = realpath(argv[0], NULL);
3242 error = got_error_from_errno2("realpath", argv[0]);
3245 got_path_strip_trailing_slashes(path);
3246 } else if (argc != 0)
3249 if (got_author == NULL) {
3250 /* TODO: Look current user up in password database */
3251 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3255 cwd = getcwd(NULL, 0);
3257 error = got_error_from_errno("getcwd");
3260 error = got_worktree_open(&worktree, cwd);
3264 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3267 if (rebase_in_progress) {
3268 error = got_error(GOT_ERR_REBASING);
3272 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3277 * unveil(2) traverses exec(2); if an editor is used we have
3278 * to apply unveil after the log message has been written.
3280 if (logmsg == NULL || strlen(logmsg) == 0)
3281 error = get_editor(&editor);
3283 error = apply_unveil(got_repo_get_path(repo), 0,
3284 got_worktree_get_root_path(worktree));
3288 cl_arg.editor = editor;
3289 cl_arg.cmdline_log = logmsg;
3290 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3291 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3292 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3293 cl_arg.branch_name += 5;
3294 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3295 cl_arg.branch_name += 6;
3296 cl_arg.repo_path = got_repo_get_path(repo);
3297 cl_arg.logmsg_path = NULL;
3298 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3299 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3301 if (cl_arg.logmsg_path)
3302 fprintf(stderr, "%s: log message preserved in %s\n",
3303 getprogname(), cl_arg.logmsg_path);
3307 if (cl_arg.logmsg_path)
3308 unlink(cl_arg.logmsg_path);
3310 error = got_object_id_str(&id_str, id);
3313 printf("Created commit %s\n", id_str);
3316 got_repo_close(repo);
3318 got_worktree_close(worktree);
3327 usage_cherrypick(void)
3329 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3333 static const struct got_error *
3334 cmd_cherrypick(int argc, char *argv[])
3336 const struct got_error *error = NULL;
3337 struct got_worktree *worktree = NULL;
3338 struct got_repository *repo = NULL;
3339 char *cwd = NULL, *commit_id_str = NULL;
3340 struct got_object_id *commit_id = NULL;
3341 struct got_commit_object *commit = NULL;
3342 struct got_object_qid *pid;
3343 struct got_reference *head_ref = NULL;
3344 int ch, did_something = 0;
3346 while ((ch = getopt(argc, argv, "")) != -1) {
3358 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3359 "unveil", NULL) == -1)
3365 cwd = getcwd(NULL, 0);
3367 error = got_error_from_errno("getcwd");
3370 error = got_worktree_open(&worktree, cwd);
3374 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3378 error = apply_unveil(got_repo_get_path(repo), 0,
3379 got_worktree_get_root_path(worktree));
3383 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3384 GOT_OBJ_TYPE_COMMIT, repo);
3385 if (error != NULL) {
3386 struct got_reference *ref;
3387 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3389 error = got_ref_open(&ref, repo, argv[0], 0);
3392 error = got_ref_resolve(&commit_id, repo, ref);
3397 error = got_object_id_str(&commit_id_str, commit_id);
3401 error = got_ref_open(&head_ref, repo,
3402 got_worktree_get_head_ref_name(worktree), 0);
3406 error = check_same_branch(commit_id, head_ref, repo);
3408 if (error->code != GOT_ERR_ANCESTRY)
3412 error = got_error(GOT_ERR_SAME_BRANCH);
3416 error = got_object_open_as_commit(&commit, repo, commit_id);
3419 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3420 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3421 commit_id, repo, update_progress, &did_something, check_cancelled,
3427 printf("Merged commit %s\n", commit_id_str);
3430 got_object_commit_close(commit);
3431 free(commit_id_str);
3433 got_ref_close(head_ref);
3435 got_worktree_close(worktree);
3437 got_repo_close(repo);
3444 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3448 static const struct got_error *
3449 cmd_backout(int argc, char *argv[])
3451 const struct got_error *error = NULL;
3452 struct got_worktree *worktree = NULL;
3453 struct got_repository *repo = NULL;
3454 char *cwd = NULL, *commit_id_str = NULL;
3455 struct got_object_id *commit_id = NULL;
3456 struct got_commit_object *commit = NULL;
3457 struct got_object_qid *pid;
3458 struct got_reference *head_ref = NULL;
3459 int ch, did_something = 0;
3461 while ((ch = getopt(argc, argv, "")) != -1) {
3473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3474 "unveil", NULL) == -1)
3480 cwd = getcwd(NULL, 0);
3482 error = got_error_from_errno("getcwd");
3485 error = got_worktree_open(&worktree, cwd);
3489 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3493 error = apply_unveil(got_repo_get_path(repo), 0,
3494 got_worktree_get_root_path(worktree));
3498 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3499 GOT_OBJ_TYPE_COMMIT, repo);
3500 if (error != NULL) {
3501 struct got_reference *ref;
3502 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3504 error = got_ref_open(&ref, repo, argv[0], 0);
3507 error = got_ref_resolve(&commit_id, repo, ref);
3512 error = got_object_id_str(&commit_id_str, commit_id);
3516 error = got_ref_open(&head_ref, repo,
3517 got_worktree_get_head_ref_name(worktree), 0);
3521 error = check_same_branch(commit_id, head_ref, repo);
3525 error = got_object_open_as_commit(&commit, repo, commit_id);
3528 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3530 error = got_error(GOT_ERR_ROOT_COMMIT);
3534 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3535 update_progress, &did_something, check_cancelled, NULL);
3540 printf("Backed out commit %s\n", commit_id_str);
3543 got_object_commit_close(commit);
3544 free(commit_id_str);
3546 got_ref_close(head_ref);
3548 got_worktree_close(worktree);
3550 got_repo_close(repo);
3557 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3563 trim_logmsg(char *logmsg, int limit)
3568 len = strlen(logmsg);
3572 nl = strchr(logmsg, '\n');
3577 static const struct got_error *
3578 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3580 const char *logmsg0 = NULL;
3582 logmsg0 = got_object_commit_get_logmsg(commit);
3584 while (isspace((unsigned char)logmsg0[0]))
3587 *logmsg = strdup(logmsg0);
3588 if (*logmsg == NULL)
3589 return got_error_from_errno("strdup");
3591 trim_logmsg(*logmsg, limit);
3595 static const struct got_error *
3596 show_rebase_progress(struct got_commit_object *commit,
3597 struct got_object_id *old_id, struct got_object_id *new_id)
3599 const struct got_error *err;
3600 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3602 err = got_object_id_str(&old_id_str, old_id);
3607 err = got_object_id_str(&new_id_str, new_id);
3612 old_id_str[12] = '\0';
3614 new_id_str[12] = '\0';
3616 err = get_short_logmsg(&logmsg, 42, commit);
3620 printf("%s -> %s: %s\n", old_id_str,
3621 new_id_str ? new_id_str : "no-op change", logmsg);
3628 static const struct got_error *
3629 rebase_progress(void *arg, unsigned char status, const char *path)
3631 unsigned char *rebase_status = arg;
3633 while (path[0] == '/')
3635 printf("%c %s\n", status, path);
3637 if (*rebase_status == GOT_STATUS_CONFLICT)
3639 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3640 *rebase_status = status;
3644 static const struct got_error *
3645 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3646 struct got_reference *branch, struct got_reference *new_base_branch,
3647 struct got_reference *tmp_branch, struct got_repository *repo)
3649 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3650 return got_worktree_rebase_complete(worktree, fileindex,
3651 new_base_branch, tmp_branch, branch, repo);
3654 static const struct got_error *
3655 rebase_commit(struct got_pathlist_head *merged_paths,
3656 struct got_worktree *worktree, struct got_fileindex *fileindex,
3657 struct got_reference *tmp_branch,
3658 struct got_object_id *commit_id, struct got_repository *repo)
3660 const struct got_error *error;
3661 struct got_commit_object *commit;
3662 struct got_object_id *new_commit_id;
3664 error = got_object_open_as_commit(&commit, repo, commit_id);
3668 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3669 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3671 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3673 error = show_rebase_progress(commit, commit_id, NULL);
3675 error = show_rebase_progress(commit, commit_id, new_commit_id);
3676 free(new_commit_id);
3679 got_object_commit_close(commit);
3683 struct check_path_prefix_arg {
3684 const char *path_prefix;
3689 static const struct got_error *
3690 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3691 struct got_blob_object *blob2, struct got_object_id *id1,
3692 struct got_object_id *id2, const char *path1, const char *path2,
3693 struct got_repository *repo)
3695 struct check_path_prefix_arg *a = arg;
3697 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3698 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3699 return got_error(a->errcode);
3704 static const struct got_error *
3705 check_path_prefix(struct got_object_id *parent_id,
3706 struct got_object_id *commit_id, const char *path_prefix,
3707 int errcode, struct got_repository *repo)
3709 const struct got_error *err;
3710 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3711 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3712 struct check_path_prefix_arg cpp_arg;
3714 if (got_path_is_root_dir(path_prefix))
3717 err = got_object_open_as_commit(&commit, repo, commit_id);
3721 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3725 err = got_object_open_as_tree(&tree1, repo,
3726 got_object_commit_get_tree_id(parent_commit));
3730 err = got_object_open_as_tree(&tree2, repo,
3731 got_object_commit_get_tree_id(commit));
3735 cpp_arg.path_prefix = path_prefix;
3736 while (cpp_arg.path_prefix[0] == '/')
3737 cpp_arg.path_prefix++;
3738 cpp_arg.len = strlen(cpp_arg.path_prefix);
3739 cpp_arg.errcode = errcode;
3740 err = got_diff_tree(tree1, tree2, "", "", repo,
3741 check_path_prefix_in_diff, &cpp_arg);
3744 got_object_tree_close(tree1);
3746 got_object_tree_close(tree2);
3748 got_object_commit_close(commit);
3750 got_object_commit_close(parent_commit);
3754 static const struct got_error *
3755 collect_commits(struct got_object_id_queue *commits,
3756 struct got_object_id *initial_commit_id,
3757 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3758 const char *path_prefix, int path_prefix_errcode,
3759 struct got_repository *repo)
3761 const struct got_error *err = NULL;
3762 struct got_commit_graph *graph = NULL;
3763 struct got_object_id *parent_id = NULL;
3764 struct got_object_qid *qid;
3765 struct got_object_id *commit_id = initial_commit_id;
3767 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3771 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3774 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3775 err = got_commit_graph_iter_next(&parent_id, graph);
3777 if (err->code == GOT_ERR_ITER_COMPLETED) {
3778 err = got_error_msg(GOT_ERR_ANCESTRY,
3779 "ran out of commits to rebase before "
3780 "youngest common ancestor commit has "
3783 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3785 err = got_commit_graph_fetch_commits(graph, 1, repo);
3789 err = check_path_prefix(parent_id, commit_id,
3790 path_prefix, path_prefix_errcode, repo);
3794 err = got_object_qid_alloc(&qid, commit_id);
3797 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3798 commit_id = parent_id;
3802 got_commit_graph_close(graph);
3806 static const struct got_error *
3807 cmd_rebase(int argc, char *argv[])
3809 const struct got_error *error = NULL;
3810 struct got_worktree *worktree = NULL;
3811 struct got_repository *repo = NULL;
3812 struct got_fileindex *fileindex = NULL;
3814 struct got_reference *branch = NULL;
3815 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3816 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3817 struct got_object_id *resume_commit_id = NULL;
3818 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3819 struct got_commit_object *commit = NULL;
3820 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3821 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3822 struct got_object_id_queue commits;
3823 struct got_pathlist_head merged_paths;
3824 const struct got_object_id_queue *parent_ids;
3825 struct got_object_qid *qid, *pid;
3827 SIMPLEQ_INIT(&commits);
3828 TAILQ_INIT(&merged_paths);
3830 while ((ch = getopt(argc, argv, "ac")) != -1) {
3836 continue_rebase = 1;
3848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3849 "unveil", NULL) == -1)
3852 if (abort_rebase && continue_rebase)
3854 else if (abort_rebase || continue_rebase) {
3857 } else if (argc != 1)
3860 cwd = getcwd(NULL, 0);
3862 error = got_error_from_errno("getcwd");
3865 error = got_worktree_open(&worktree, cwd);
3869 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3873 error = apply_unveil(got_repo_get_path(repo), 0,
3874 got_worktree_get_root_path(worktree));
3878 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3884 if (!rebase_in_progress) {
3885 error = got_error(GOT_ERR_NOT_REBASING);
3888 error = got_worktree_rebase_continue(&resume_commit_id,
3889 &new_base_branch, &tmp_branch, &branch, &fileindex,
3893 printf("Switching work tree to %s\n",
3894 got_ref_get_symref_target(new_base_branch));
3895 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3896 new_base_branch, update_progress, &did_something);
3899 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3900 goto done; /* nothing else to do */
3903 if (continue_rebase) {
3904 if (!rebase_in_progress) {
3905 error = got_error(GOT_ERR_NOT_REBASING);
3908 error = got_worktree_rebase_continue(&resume_commit_id,
3909 &new_base_branch, &tmp_branch, &branch, &fileindex,
3914 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3915 resume_commit_id, repo);
3919 yca_id = got_object_id_dup(resume_commit_id);
3920 if (yca_id == NULL) {
3921 error = got_error_from_errno("got_object_id_dup");
3925 error = got_ref_open(&branch, repo, argv[0], 0);
3929 error = check_same_branch(
3930 got_worktree_get_base_commit_id(worktree), branch, repo);
3932 if (error->code != GOT_ERR_ANCESTRY)
3936 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3937 "specified branch resolves to a commit which "
3938 "is already contained in work tree's branch");
3943 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3947 if (!continue_rebase) {
3948 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3949 got_worktree_get_base_commit_id(worktree),
3950 branch_head_commit_id, repo);
3953 if (yca_id == NULL) {
3954 error = got_error_msg(GOT_ERR_ANCESTRY,
3955 "specified branch shares no common ancestry "
3956 "with work tree's branch");
3960 error = got_worktree_rebase_prepare(&new_base_branch,
3961 &tmp_branch, &fileindex, worktree, branch, repo);
3966 commit_id = branch_head_commit_id;
3967 error = got_object_open_as_commit(&commit, repo, commit_id);
3971 parent_ids = got_object_commit_get_parent_ids(commit);
3972 pid = SIMPLEQ_FIRST(parent_ids);
3973 error = collect_commits(&commits, commit_id, pid->id,
3974 yca_id, got_worktree_get_path_prefix(worktree),
3975 GOT_ERR_REBASE_PATH, repo);
3976 got_object_commit_close(commit);
3981 if (SIMPLEQ_EMPTY(&commits)) {
3982 if (continue_rebase)
3983 error = rebase_complete(worktree, fileindex,
3984 branch, new_base_branch, tmp_branch, repo);
3986 error = got_error(GOT_ERR_EMPTY_REBASE);
3991 SIMPLEQ_FOREACH(qid, &commits, entry) {
3992 commit_id = qid->id;
3993 parent_id = pid ? pid->id : yca_id;
3996 error = got_worktree_rebase_merge_files(&merged_paths,
3997 worktree, fileindex, parent_id, commit_id, repo,
3998 rebase_progress, &rebase_status, check_cancelled, NULL);
4002 if (rebase_status == GOT_STATUS_CONFLICT) {
4003 got_worktree_rebase_pathlist_free(&merged_paths);
4007 error = rebase_commit(&merged_paths, worktree, fileindex,
4008 tmp_branch, commit_id, repo);
4009 got_worktree_rebase_pathlist_free(&merged_paths);
4014 if (rebase_status == GOT_STATUS_CONFLICT) {
4015 error = got_worktree_rebase_postpone(worktree, fileindex);
4018 error = got_error_msg(GOT_ERR_CONFLICTS,
4019 "conflicts must be resolved before rebasing can continue");
4021 error = rebase_complete(worktree, fileindex, branch,
4022 new_base_branch, tmp_branch, repo);
4024 got_object_id_queue_free(&commits);
4025 free(branch_head_commit_id);
4026 free(resume_commit_id);
4029 got_object_commit_close(commit);
4031 got_ref_close(branch);
4032 if (new_base_branch)
4033 got_ref_close(new_base_branch);
4035 got_ref_close(tmp_branch);
4037 got_worktree_close(worktree);
4039 got_repo_close(repo);
4044 usage_histedit(void)
4046 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4051 #define GOT_HISTEDIT_PICK 'p'
4052 #define GOT_HISTEDIT_EDIT 'e'
4053 #define GOT_HISTEDIT_FOLD 'f'
4054 #define GOT_HISTEDIT_DROP 'd'
4055 #define GOT_HISTEDIT_MESG 'm'
4057 static struct got_histedit_cmd {
4061 } got_histedit_cmds[] = {
4062 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4063 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4064 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4065 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4066 { GOT_HISTEDIT_MESG, "mesg",
4067 "single-line log message for commit above (open editor if empty)" },
4070 struct got_histedit_list_entry {
4071 TAILQ_ENTRY(got_histedit_list_entry) entry;
4072 struct got_object_id *commit_id;
4073 const struct got_histedit_cmd *cmd;
4076 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4078 static const struct got_error *
4079 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4080 FILE *f, struct got_repository *repo)
4082 const struct got_error *err = NULL;
4083 char *logmsg = NULL, *id_str = NULL;
4084 struct got_commit_object *commit = NULL;
4087 err = got_object_open_as_commit(&commit, repo, commit_id);
4091 err = get_short_logmsg(&logmsg, 34, commit);
4095 err = got_object_id_str(&id_str, commit_id);
4099 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4101 err = got_ferror(f, GOT_ERR_IO);
4104 got_object_commit_close(commit);
4110 static const struct got_error *
4111 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4112 struct got_repository *repo)
4114 const struct got_error *err = NULL;
4115 struct got_object_qid *qid;
4117 if (SIMPLEQ_EMPTY(commits))
4118 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4120 SIMPLEQ_FOREACH(qid, commits, entry) {
4121 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4130 static const struct got_error *
4131 write_cmd_list(FILE *f)
4133 const struct got_error *err = NULL;
4136 n = fprintf(f, "# Available histedit commands:\n");
4138 return got_ferror(f, GOT_ERR_IO);
4140 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4141 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4142 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4145 err = got_ferror(f, GOT_ERR_IO);
4149 n = fprintf(f, "# Commits will be processed in order from top to "
4150 "bottom of this file.\n");
4152 return got_ferror(f, GOT_ERR_IO);
4156 static const struct got_error *
4157 histedit_syntax_error(int lineno)
4159 static char msg[42];
4162 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4164 if (ret == -1 || ret >= sizeof(msg))
4165 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4167 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4170 static const struct got_error *
4171 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4172 char *logmsg, struct got_repository *repo)
4174 const struct got_error *err;
4175 struct got_commit_object *folded_commit = NULL;
4178 err = got_object_id_str(&id_str, hle->commit_id);
4182 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4186 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4187 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4188 got_object_commit_get_logmsg(folded_commit)) == -1) {
4189 err = got_error_from_errno("asprintf");
4194 got_object_commit_close(folded_commit);
4199 static struct got_histedit_list_entry *
4200 get_folded_commits(struct got_histedit_list_entry *hle)
4202 struct got_histedit_list_entry *prev, *folded = NULL;
4204 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4205 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4206 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4207 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4209 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4215 static const struct got_error *
4216 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4217 struct got_repository *repo)
4219 char *logmsg_path = NULL, *id_str = NULL;
4220 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4221 const struct got_error *err = NULL;
4222 struct got_commit_object *commit = NULL;
4224 struct got_histedit_list_entry *folded = NULL;
4226 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4230 folded = get_folded_commits(hle);
4232 while (folded != hle) {
4233 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4234 folded = TAILQ_NEXT(folded, entry);
4237 err = append_folded_commit_msg(&new_msg, folded,
4243 folded = TAILQ_NEXT(folded, entry);
4247 err = got_object_id_str(&id_str, hle->commit_id);
4250 if (asprintf(&new_msg,
4251 "%s\n# original log message of commit %s: %s",
4252 logmsg ? logmsg : "", id_str,
4253 got_object_commit_get_logmsg(commit)) == -1) {
4254 err = got_error_from_errno("asprintf");
4260 err = got_object_id_str(&id_str, hle->commit_id);
4264 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4268 dprintf(fd, logmsg);
4271 err = get_editor(&editor);
4275 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4277 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4280 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4281 if (hle->logmsg == NULL)
4282 err = got_error_from_errno("strdup");
4285 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4286 err = got_error_from_errno2("unlink", logmsg_path);
4291 got_object_commit_close(commit);
4295 static const struct got_error *
4296 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4297 FILE *f, struct got_repository *repo)
4299 const struct got_error *err = NULL;
4300 char *line = NULL, *p, *end;
4304 const struct got_histedit_cmd *cmd;
4305 struct got_object_id *commit_id = NULL;
4306 struct got_histedit_list_entry *hle = NULL;
4309 len = getline(&line, &size, f);
4311 const struct got_error *getline_err;
4314 getline_err = got_error_from_errno("getline");
4315 err = got_ferror(f, getline_err->code);
4320 while (isspace((unsigned char)p[0]))
4322 if (p[0] == '#' || p[0] == '\0') {
4328 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4329 cmd = &got_histedit_cmds[i];
4330 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4331 isspace((unsigned char)p[strlen(cmd->name)])) {
4332 p += strlen(cmd->name);
4335 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4340 if (i == nitems(got_histedit_cmds)) {
4341 err = histedit_syntax_error(lineno);
4344 while (isspace((unsigned char)p[0]))
4346 if (cmd->code == GOT_HISTEDIT_MESG) {
4347 if (hle == NULL || hle->logmsg != NULL) {
4348 err = got_error(GOT_ERR_HISTEDIT_CMD);
4352 err = histedit_edit_logmsg(hle, repo);
4356 hle->logmsg = strdup(p);
4357 if (hle->logmsg == NULL) {
4358 err = got_error_from_errno("strdup");
4367 while (end[0] && !isspace((unsigned char)end[0]))
4371 err = got_object_resolve_id_str(&commit_id, repo, p);
4373 /* override error code */
4374 err = histedit_syntax_error(lineno);
4378 hle = malloc(sizeof(*hle));
4380 err = got_error_from_errno("malloc");
4384 hle->commit_id = commit_id;
4389 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4397 static const struct got_error *
4398 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4399 const char *path, struct got_repository *repo)
4401 const struct got_error *err = NULL;
4405 err = get_editor(&editor);
4409 if (spawn_editor(editor, path) == -1) {
4410 err = got_error_from_errno("failed spawning editor");
4414 f = fopen(path, "r");
4416 err = got_error_from_errno("fopen");
4419 err = histedit_parse_list(histedit_cmds, f, repo);
4421 if (f && fclose(f) != 0 && err == NULL)
4422 err = got_error_from_errno("fclose");
4427 static const struct got_error *
4428 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4429 struct got_object_id_queue *, const char *, struct got_repository *);
4431 static const struct got_error *
4432 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4433 struct got_object_id_queue *commits, struct got_repository *repo)
4435 const struct got_error *err;
4439 err = got_opentemp_named(&path, &f, "got-histedit");
4443 err = write_cmd_list(f);
4447 err = histedit_write_commit_list(commits, f, repo);
4451 if (fclose(f) != 0) {
4452 err = got_error_from_errno("fclose");
4457 err = histedit_run_editor(histedit_cmds, path, repo);
4459 const char *errmsg = err->msg;
4460 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4462 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4463 commits, path, repo);
4466 if (f && fclose(f) != 0 && err == NULL)
4467 err = got_error_from_errno("fclose");
4468 if (path && unlink(path) != 0 && err == NULL)
4469 err = got_error_from_errno2("unlink", path);
4474 static const struct got_error *
4475 histedit_save_list(struct got_histedit_list *histedit_cmds,
4476 struct got_worktree *worktree, struct got_repository *repo)
4478 const struct got_error *err = NULL;
4481 struct got_histedit_list_entry *hle;
4482 struct got_commit_object *commit = NULL;
4484 err = got_worktree_get_histedit_list_path(&path, worktree);
4488 f = fopen(path, "w");
4490 err = got_error_from_errno2("fopen", path);
4493 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4494 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4500 int n = fprintf(f, "%c %s\n",
4501 GOT_HISTEDIT_MESG, hle->logmsg);
4503 err = got_ferror(f, GOT_ERR_IO);
4509 if (f && fclose(f) != 0 && err == NULL)
4510 err = got_error_from_errno("fclose");
4513 got_object_commit_close(commit);
4517 static const struct got_error *
4518 histedit_load_list(struct got_histedit_list *histedit_cmds,
4519 const char *path, struct got_repository *repo)
4521 const struct got_error *err = NULL;
4524 f = fopen(path, "r");
4526 err = got_error_from_errno2("fopen", path);
4530 err = histedit_parse_list(histedit_cmds, f, repo);
4532 if (f && fclose(f) != 0 && err == NULL)
4533 err = got_error_from_errno("fclose");
4537 static const struct got_error *
4538 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4539 const char *errmsg, struct got_object_id_queue *commits,
4540 const char *path, struct got_repository *repo)
4542 const struct got_error *err = NULL;
4545 while (resp != 'c' && resp != 'r' && resp != 'a') {
4546 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4547 "or (a)bort: ", getprogname(), errmsg);
4550 err = histedit_run_editor(histedit_cmds, path, repo);
4552 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4557 } else if (resp == 'r') {
4558 err = histedit_edit_script(histedit_cmds,
4561 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4566 } else if (resp == 'a') {
4567 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4570 printf("invalid response '%c'\n", resp);
4576 static const struct got_error *
4577 histedit_complete(struct got_worktree *worktree,
4578 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4579 struct got_reference *branch, struct got_repository *repo)
4581 printf("Switching work tree to %s\n",
4582 got_ref_get_symref_target(branch));
4583 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4587 static const struct got_error *
4588 show_histedit_progress(struct got_commit_object *commit,
4589 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4591 const struct got_error *err;
4592 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4594 err = got_object_id_str(&old_id_str, hle->commit_id);
4599 err = got_object_id_str(&new_id_str, new_id);
4604 old_id_str[12] = '\0';
4606 new_id_str[12] = '\0';
4609 logmsg = strdup(hle->logmsg);
4610 if (logmsg == NULL) {
4611 err = got_error_from_errno("strdup");
4614 trim_logmsg(logmsg, 42);
4616 err = get_short_logmsg(&logmsg, 42, commit);
4621 switch (hle->cmd->code) {
4622 case GOT_HISTEDIT_PICK:
4623 case GOT_HISTEDIT_EDIT:
4624 printf("%s -> %s: %s\n", old_id_str,
4625 new_id_str ? new_id_str : "no-op change", logmsg);
4627 case GOT_HISTEDIT_DROP:
4628 case GOT_HISTEDIT_FOLD:
4629 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4642 static const struct got_error *
4643 histedit_commit(struct got_pathlist_head *merged_paths,
4644 struct got_worktree *worktree, struct got_fileindex *fileindex,
4645 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4646 struct got_repository *repo)
4648 const struct got_error *err;
4649 struct got_commit_object *commit;
4650 struct got_object_id *new_commit_id;
4652 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4653 && hle->logmsg == NULL) {
4654 err = histedit_edit_logmsg(hle, repo);
4659 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4663 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4664 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4667 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4669 err = show_histedit_progress(commit, hle, NULL);
4671 err = show_histedit_progress(commit, hle, new_commit_id);
4672 free(new_commit_id);
4675 got_object_commit_close(commit);
4679 static const struct got_error *
4680 histedit_skip_commit(struct got_histedit_list_entry *hle,
4681 struct got_worktree *worktree, struct got_repository *repo)
4683 const struct got_error *error;
4684 struct got_commit_object *commit;
4686 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4691 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4695 error = show_histedit_progress(commit, hle, NULL);
4696 got_object_commit_close(commit);
4700 static const struct got_error *
4701 histedit_check_script(struct got_histedit_list *histedit_cmds,
4702 struct got_object_id_queue *commits, struct got_repository *repo)
4704 const struct got_error *err = NULL;
4705 struct got_object_qid *qid;
4706 struct got_histedit_list_entry *hle;
4707 static char msg[80];
4710 if (TAILQ_EMPTY(histedit_cmds))
4711 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4712 "histedit script contains no commands");
4714 SIMPLEQ_FOREACH(qid, commits, entry) {
4715 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4716 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4720 err = got_object_id_str(&id_str, qid->id);
4723 snprintf(msg, sizeof(msg),
4724 "commit %s missing from histedit script", id_str);
4726 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4730 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4731 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4732 "last commit in histedit script cannot be folded");
4737 static const struct got_error *
4738 cmd_histedit(int argc, char *argv[])
4740 const struct got_error *error = NULL;
4741 struct got_worktree *worktree = NULL;
4742 struct got_fileindex *fileindex = NULL;
4743 struct got_repository *repo = NULL;
4745 struct got_reference *branch = NULL;
4746 struct got_reference *tmp_branch = NULL;
4747 struct got_object_id *resume_commit_id = NULL;
4748 struct got_object_id *base_commit_id = NULL;
4749 struct got_object_id *head_commit_id = NULL;
4750 struct got_commit_object *commit = NULL;
4751 int ch, rebase_in_progress = 0;
4752 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4753 const char *edit_script_path = NULL;
4754 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4755 struct got_object_id_queue commits;
4756 struct got_pathlist_head merged_paths;
4757 const struct got_object_id_queue *parent_ids;
4758 struct got_object_qid *pid;
4759 struct got_histedit_list histedit_cmds;
4760 struct got_histedit_list_entry *hle;
4762 SIMPLEQ_INIT(&commits);
4763 TAILQ_INIT(&histedit_cmds);
4764 TAILQ_INIT(&merged_paths);
4766 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4775 edit_script_path = optarg;
4787 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4788 "unveil", NULL) == -1)
4791 if (abort_edit && continue_edit)
4797 * This command cannot apply unveil(2) in all cases because the
4798 * user may choose to run an editor to edit the histedit script
4799 * and to edit individual commit log messages.
4800 * unveil(2) traverses exec(2); if an editor is used we have to
4801 * apply unveil after edit script and log messages have been written.
4802 * XXX TODO: Make use of unveil(2) where possible.
4805 cwd = getcwd(NULL, 0);
4807 error = got_error_from_errno("getcwd");
4810 error = got_worktree_open(&worktree, cwd);
4814 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4818 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4821 if (rebase_in_progress) {
4822 error = got_error(GOT_ERR_REBASING);
4826 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4830 if (edit_in_progress && abort_edit) {
4832 error = got_worktree_histedit_continue(&resume_commit_id,
4833 &tmp_branch, &branch, &base_commit_id, &fileindex,
4837 printf("Switching work tree to %s\n",
4838 got_ref_get_symref_target(branch));
4839 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4840 branch, base_commit_id, update_progress, &did_something);
4843 printf("Histedit of %s aborted\n",
4844 got_ref_get_symref_target(branch));
4845 goto done; /* nothing else to do */
4846 } else if (abort_edit) {
4847 error = got_error(GOT_ERR_NOT_HISTEDIT);
4851 if (continue_edit) {
4854 if (!edit_in_progress) {
4855 error = got_error(GOT_ERR_NOT_HISTEDIT);
4859 error = got_worktree_get_histedit_list_path(&path, worktree);
4863 error = histedit_load_list(&histedit_cmds, path, repo);
4868 error = got_worktree_histedit_continue(&resume_commit_id,
4869 &tmp_branch, &branch, &base_commit_id, &fileindex,
4874 error = got_ref_resolve(&head_commit_id, repo, branch);
4878 error = got_object_open_as_commit(&commit, repo,
4882 parent_ids = got_object_commit_get_parent_ids(commit);
4883 pid = SIMPLEQ_FIRST(parent_ids);
4885 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4888 error = collect_commits(&commits, head_commit_id, pid->id,
4889 base_commit_id, got_worktree_get_path_prefix(worktree),
4890 GOT_ERR_HISTEDIT_PATH, repo);
4891 got_object_commit_close(commit);
4896 if (edit_in_progress) {
4897 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4901 error = got_ref_open(&branch, repo,
4902 got_worktree_get_head_ref_name(worktree), 0);
4906 error = got_ref_resolve(&head_commit_id, repo, branch);
4910 error = got_object_open_as_commit(&commit, repo,
4914 parent_ids = got_object_commit_get_parent_ids(commit);
4915 pid = SIMPLEQ_FIRST(parent_ids);
4917 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4920 error = collect_commits(&commits, head_commit_id, pid->id,
4921 got_worktree_get_base_commit_id(worktree),
4922 got_worktree_get_path_prefix(worktree),
4923 GOT_ERR_HISTEDIT_PATH, repo);
4924 got_object_commit_close(commit);
4929 if (edit_script_path) {
4930 error = histedit_load_list(&histedit_cmds,
4931 edit_script_path, repo);
4935 error = histedit_edit_script(&histedit_cmds, &commits,
4942 error = histedit_save_list(&histedit_cmds, worktree,
4947 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4948 &base_commit_id, &fileindex, worktree, repo);
4954 error = histedit_check_script(&histedit_cmds, &commits, repo);
4958 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4959 if (resume_commit_id) {
4960 if (got_object_id_cmp(hle->commit_id,
4961 resume_commit_id) != 0)
4964 resume_commit_id = NULL;
4965 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4966 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4967 error = histedit_skip_commit(hle, worktree,
4970 error = histedit_commit(NULL, worktree,
4971 fileindex, tmp_branch, hle, repo);
4978 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4979 error = histedit_skip_commit(hle, worktree, repo);
4985 error = got_object_open_as_commit(&commit, repo,
4989 parent_ids = got_object_commit_get_parent_ids(commit);
4990 pid = SIMPLEQ_FIRST(parent_ids);
4992 error = got_worktree_histedit_merge_files(&merged_paths,
4993 worktree, fileindex, pid->id, hle->commit_id, repo,
4994 rebase_progress, &rebase_status, check_cancelled, NULL);
4997 got_object_commit_close(commit);
5000 if (rebase_status == GOT_STATUS_CONFLICT) {
5001 got_worktree_rebase_pathlist_free(&merged_paths);
5005 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5007 error = got_object_id_str(&id_str, hle->commit_id);
5010 printf("Stopping histedit for amending commit %s\n",
5013 got_worktree_rebase_pathlist_free(&merged_paths);
5014 error = got_worktree_histedit_postpone(worktree,
5019 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5020 error = histedit_skip_commit(hle, worktree, repo);
5026 error = histedit_commit(&merged_paths, worktree, fileindex,
5027 tmp_branch, hle, repo);
5028 got_worktree_rebase_pathlist_free(&merged_paths);
5033 if (rebase_status == GOT_STATUS_CONFLICT) {
5034 error = got_worktree_histedit_postpone(worktree, fileindex);
5037 error = got_error_msg(GOT_ERR_CONFLICTS,
5038 "conflicts must be resolved before rebasing can continue");
5040 error = histedit_complete(worktree, fileindex, tmp_branch,
5043 got_object_id_queue_free(&commits);
5044 free(head_commit_id);
5045 free(base_commit_id);
5046 free(resume_commit_id);
5048 got_object_commit_close(commit);
5050 got_ref_close(branch);
5052 got_ref_close(tmp_branch);
5054 got_worktree_close(worktree);
5056 got_repo_close(repo);